You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ApplicationConfiguration.java 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client;
  17. import java.util.ArrayList;
  18. import java.util.HashMap;
  19. import java.util.LinkedList;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.logging.Handler;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25. import com.google.gwt.core.client.EntryPoint;
  26. import com.google.gwt.core.client.GWT;
  27. import com.google.gwt.core.client.JavaScriptObject;
  28. import com.google.gwt.core.client.JsArrayString;
  29. import com.google.gwt.core.client.RunAsyncCallback;
  30. import com.google.gwt.core.client.Scheduler;
  31. import com.google.gwt.core.client.ScriptInjector;
  32. import com.google.gwt.dom.client.Element;
  33. import com.google.gwt.logging.client.LogConfiguration;
  34. import com.google.gwt.user.client.Command;
  35. import com.google.gwt.user.client.Window;
  36. import com.vaadin.client.debug.internal.ErrorNotificationHandler;
  37. import com.vaadin.client.debug.internal.HierarchySection;
  38. import com.vaadin.client.debug.internal.InfoSection;
  39. import com.vaadin.client.debug.internal.LogSection;
  40. import com.vaadin.client.debug.internal.NetworkSection;
  41. import com.vaadin.client.debug.internal.ProfilerSection;
  42. import com.vaadin.client.debug.internal.Section;
  43. import com.vaadin.client.debug.internal.TestBenchSection;
  44. import com.vaadin.client.debug.internal.VDebugWindow;
  45. import com.vaadin.client.debug.internal.theme.DebugWindowStyles;
  46. import com.vaadin.client.event.PointerEventSupport;
  47. import com.vaadin.client.metadata.NoDataException;
  48. import com.vaadin.client.metadata.TypeData;
  49. import com.vaadin.client.ui.UnknownComponentConnector;
  50. import com.vaadin.client.ui.UnknownExtensionConnector;
  51. import com.vaadin.client.ui.ui.UIConnector;
  52. import com.vaadin.shared.ApplicationConstants;
  53. import com.vaadin.shared.ui.ui.UIConstants;
  54. public class ApplicationConfiguration implements EntryPoint {
  55. /**
  56. * Helper class for reading configuration options from the bootstap
  57. * javascript
  58. *
  59. * @since 7.0
  60. */
  61. private static class JsoConfiguration extends JavaScriptObject {
  62. protected JsoConfiguration() {
  63. // JSO Constructor
  64. }
  65. /**
  66. * Reads a configuration parameter as a string. Please note that the
  67. * javascript value of the parameter should also be a string, or else an
  68. * undefined exception may be thrown.
  69. *
  70. * @param name
  71. * name of the configuration parameter
  72. * @return value of the configuration parameter, or <code>null</code> if
  73. * not defined
  74. */
  75. private native String getConfigString(String name)
  76. /*-{
  77. var value = this.getConfig(name);
  78. if (value === null || value === undefined) {
  79. return null;
  80. } else {
  81. return value +"";
  82. }
  83. }-*/;
  84. /**
  85. * Reads a configuration parameter as a boolean object. Please note that
  86. * the javascript value of the parameter should also be a boolean, or
  87. * else an undefined exception may be thrown.
  88. *
  89. * @param name
  90. * name of the configuration parameter
  91. * @return boolean value of the configuration parameter, or
  92. * <code>null</code> if no value is defined
  93. */
  94. private native Boolean getConfigBoolean(String name)
  95. /*-{
  96. var value = this.getConfig(name);
  97. if (value === null || value === undefined) {
  98. return null;
  99. } else {
  100. // $entry not needed as function is not exported
  101. return @java.lang.Boolean::valueOf(Z)(value);
  102. }
  103. }-*/;
  104. /**
  105. * Reads a configuration parameter as an integer object. Please note
  106. * that the javascript value of the parameter should also be an integer,
  107. * or else an undefined exception may be thrown.
  108. *
  109. * @param name
  110. * name of the configuration parameter
  111. * @return integer value of the configuration parameter, or
  112. * <code>null</code> if no value is defined
  113. */
  114. private native Integer getConfigInteger(String name)
  115. /*-{
  116. var value = this.getConfig(name);
  117. if (value === null || value === undefined) {
  118. return null;
  119. } else {
  120. // $entry not needed as function is not exported
  121. return @java.lang.Integer::valueOf(I)(value);
  122. }
  123. }-*/;
  124. /**
  125. * Reads a configuration parameter as an {@link ErrorMessage} object.
  126. * Please note that the javascript value of the parameter should also be
  127. * an object with appropriate fields, or else an undefined exception may
  128. * be thrown when calling this method or when calling methods on the
  129. * returned object.
  130. *
  131. * @param name
  132. * name of the configuration parameter
  133. * @return error message with the given name, or <code>null</code> if no
  134. * value is defined
  135. */
  136. private native ErrorMessage getConfigError(String name)
  137. /*-{
  138. return this.getConfig(name);
  139. }-*/;
  140. /**
  141. * Returns a native javascript object containing version information
  142. * from the server.
  143. *
  144. * @return a javascript object with the version information
  145. */
  146. private native JavaScriptObject getVersionInfoJSObject()
  147. /*-{
  148. return this.getConfig("versionInfo");
  149. }-*/;
  150. /**
  151. * Gets the version of the Vaadin framework used on the server.
  152. *
  153. * @return a string with the version
  154. *
  155. * @see com.vaadin.server.VaadinServlet#VERSION
  156. */
  157. private native String getVaadinVersion()
  158. /*-{
  159. return this.getConfig("versionInfo").vaadinVersion;
  160. }-*/;
  161. /**
  162. * Gets the version of the Atmosphere framework.
  163. *
  164. * @return a string with the version
  165. *
  166. * @see org.atmosphere.util#getRawVersion()
  167. */
  168. private native String getAtmosphereVersion()
  169. /*-{
  170. return this.getConfig("versionInfo").atmosphereVersion;
  171. }-*/;
  172. /**
  173. * Gets the JS version used in the Atmosphere framework.
  174. *
  175. * @return a string with the version
  176. */
  177. private native String getAtmosphereJSVersion()
  178. /*-{
  179. if ($wnd.vaadinPush && $wnd.vaadinPush.atmosphere) {
  180. return $wnd.vaadinPush.atmosphere.version;
  181. } else {
  182. return null;
  183. }
  184. }-*/;
  185. private native String getUIDL()
  186. /*-{
  187. return this.getConfig("uidl");
  188. }-*/;
  189. }
  190. /**
  191. * Wraps a native javascript object containing fields for an error message.
  192. *
  193. * @since 7.0
  194. */
  195. public static final class ErrorMessage extends JavaScriptObject {
  196. protected ErrorMessage() {
  197. // JSO constructor
  198. }
  199. public final native String getCaption()
  200. /*-{
  201. return this.caption;
  202. }-*/;
  203. public final native String getMessage()
  204. /*-{
  205. return this.message;
  206. }-*/;
  207. public final native String getUrl()
  208. /*-{
  209. return this.url;
  210. }-*/;
  211. }
  212. private static WidgetSet widgetSet = GWT.create(WidgetSet.class);
  213. private String id;
  214. /**
  215. * The URL to the VAADIN directory containing themes and widgetsets. Should
  216. * always end with a slash (/).
  217. */
  218. private String vaadinDirUrl;
  219. private String frontendUrl;
  220. private String serviceUrl;
  221. private String contextRootUrl;
  222. private int uiId;
  223. private boolean standalone;
  224. private ErrorMessage communicationError;
  225. private ErrorMessage authorizationError;
  226. private ErrorMessage sessionExpiredError;
  227. private int heartbeatInterval;
  228. private Map<Integer, String> unknownComponents;
  229. private Map<Integer, Class<? extends ServerConnector>> classes = new HashMap<>();
  230. private boolean widgetsetVersionSent = false;
  231. private static boolean moduleLoaded = false;
  232. static// TODO consider to make this hashmap per application
  233. List<Command> callbacks = new LinkedList<>();
  234. private static int dependenciesLoading;
  235. private static List<ApplicationConnection> runningApplications = new ArrayList<>();
  236. private Map<Integer, Integer> componentInheritanceMap = new HashMap<>();
  237. private Map<Integer, String> tagToServerSideClassName = new HashMap<>();
  238. /**
  239. * Checks whether path info in requests to the server-side service should be
  240. * in a request parameter (named <code>v-resourcePath</code>) or appended to
  241. * the end of the service URL.
  242. *
  243. * @see #getServiceUrl()
  244. *
  245. * @return <code>true</code> if path info should be a request parameter;
  246. * <code>false</code> if the path info goes after the service URL
  247. */
  248. public boolean useServiceUrlPathParam() {
  249. return getServiceUrlParameterName() != null;
  250. }
  251. /**
  252. * Return the name of the parameter used to to send data to the service url.
  253. * This method should only be called if {@link #useServiceUrlPathParam()} is
  254. * true.
  255. *
  256. * @since 7.1.6
  257. * @return The parameter name, by default <code>v-resourcePath</code>
  258. */
  259. public String getServiceUrlParameterName() {
  260. return getJsoConfiguration(id).getConfigString(
  261. ApplicationConstants.SERVICE_URL_PARAMETER_NAME);
  262. }
  263. public String getRootPanelId() {
  264. return id;
  265. }
  266. /**
  267. * Gets the URL to the server-side VaadinService. If
  268. * {@link #useServiceUrlPathParam()} return <code>true</code>, the requested
  269. * path info should be in the <code>v-resourcePath</code> query parameter;
  270. * else the path info should be appended to the end of the URL.
  271. *
  272. * @see #useServiceUrlPathParam()
  273. *
  274. * @return the URL to the server-side service as a string
  275. */
  276. public String getServiceUrl() {
  277. return serviceUrl;
  278. }
  279. /**
  280. * Gets the URL to the context root of the web application.
  281. *
  282. * @return the URL to the server-side context root as a string
  283. *
  284. * @since 8.0.3
  285. */
  286. public String getContextRootUrl() {
  287. return contextRootUrl;
  288. }
  289. /**
  290. * @return the theme name used when initializing the application
  291. * @deprecated as of 7.3. Use {@link UIConnector#getActiveTheme()} to get
  292. * the theme currently in use
  293. */
  294. @Deprecated
  295. public String getThemeName() {
  296. return getJsoConfiguration(id).getConfigString("theme");
  297. }
  298. /**
  299. * Gets the URL of the VAADIN directory on the server.
  300. *
  301. * @return the URL of the VAADIN directory
  302. */
  303. public String getVaadinDirUrl() {
  304. return vaadinDirUrl;
  305. }
  306. /**
  307. * Gets the URL of the that the {@literal frontend://} protocol should
  308. * resolve to.
  309. *
  310. * @return the URL of the frontend protocol
  311. * @since 8.1
  312. */
  313. public String getFrontendUrl() {
  314. return frontendUrl;
  315. }
  316. public void setAppId(String appId) {
  317. id = appId;
  318. }
  319. /**
  320. * Gets the initial UIDL from the DOM, if it was provided during the init
  321. * process.
  322. *
  323. * @return
  324. */
  325. public String getUIDL() {
  326. return getJsoConfiguration(id).getUIDL();
  327. }
  328. /**
  329. * @return true if the application is served by std. Vaadin servlet and is
  330. * considered to be the only or main content of the host page.
  331. */
  332. public boolean isStandalone() {
  333. return standalone;
  334. }
  335. /**
  336. * Gets the UI id of the server-side UI associated with this client-side
  337. * instance. The UI id should be included in every request originating from
  338. * this instance in order to associate the request with the right UI
  339. * instance on the server.
  340. *
  341. * @return the UI id
  342. */
  343. public int getUIId() {
  344. return uiId;
  345. }
  346. /**
  347. * @return The interval in seconds between heartbeat requests, or a
  348. * non-positive number if heartbeat is disabled.
  349. */
  350. public int getHeartbeatInterval() {
  351. return heartbeatInterval;
  352. }
  353. public JavaScriptObject getVersionInfoJSObject() {
  354. return getJsoConfiguration(id).getVersionInfoJSObject();
  355. }
  356. public ErrorMessage getCommunicationError() {
  357. return communicationError;
  358. }
  359. public ErrorMessage getAuthorizationError() {
  360. return authorizationError;
  361. }
  362. public ErrorMessage getSessionExpiredError() {
  363. return sessionExpiredError;
  364. }
  365. /**
  366. * Reads the configuration values defined by the bootstrap javascript.
  367. */
  368. private void loadFromDOM() {
  369. JsoConfiguration jsoConfiguration = getJsoConfiguration(id);
  370. serviceUrl = jsoConfiguration
  371. .getConfigString(ApplicationConstants.SERVICE_URL);
  372. if (serviceUrl == null || serviceUrl.isEmpty()) {
  373. /*
  374. * Use the current url without query parameters and fragment as the
  375. * default value.
  376. */
  377. serviceUrl = Window.Location.getHref().replaceFirst("[?#].*", "");
  378. } else {
  379. /*
  380. * Resolve potentially relative URLs to ensure they point to the
  381. * desired locations even if the base URL of the page changes later
  382. * (e.g. with pushState)
  383. */
  384. serviceUrl = WidgetUtil.getAbsoluteUrl(serviceUrl);
  385. }
  386. // Ensure there's an ending slash (to make appending e.g. UIDL work)
  387. if (!useServiceUrlPathParam() && !serviceUrl.endsWith("/")) {
  388. serviceUrl += '/';
  389. }
  390. contextRootUrl = jsoConfiguration
  391. .getConfigString(ApplicationConstants.CONTEXT_ROOT_URL);
  392. vaadinDirUrl = WidgetUtil.getAbsoluteUrl(jsoConfiguration
  393. .getConfigString(ApplicationConstants.VAADIN_DIR_URL));
  394. frontendUrl = WidgetUtil.getAbsoluteUrl(jsoConfiguration
  395. .getConfigString(ApplicationConstants.FRONTEND_URL));
  396. uiId = jsoConfiguration.getConfigInteger(UIConstants.UI_ID_PARAMETER)
  397. .intValue();
  398. // null -> false
  399. standalone = jsoConfiguration
  400. .getConfigBoolean("standalone") == Boolean.TRUE;
  401. heartbeatInterval = jsoConfiguration
  402. .getConfigInteger("heartbeatInterval");
  403. communicationError = jsoConfiguration.getConfigError("comErrMsg");
  404. authorizationError = jsoConfiguration.getConfigError("authErrMsg");
  405. sessionExpiredError = jsoConfiguration.getConfigError("sessExpMsg");
  406. }
  407. /**
  408. * Starts the application with a given id by reading the configuration
  409. * options stored by the bootstrap javascript.
  410. *
  411. * @param applicationId
  412. * id of the application to load, this is also the id of the html
  413. * element into which the application should be rendered.
  414. */
  415. public static void startApplication(final String applicationId) {
  416. Scheduler.get().scheduleDeferred(() -> {
  417. Profiler.enter("ApplicationConfiguration.startApplication");
  418. ApplicationConfiguration appConf = getConfigFromDOM(applicationId);
  419. ApplicationConnection a = GWT.create(ApplicationConnection.class);
  420. a.init(widgetSet, appConf);
  421. runningApplications.add(a);
  422. Profiler.leave("ApplicationConfiguration.startApplication");
  423. a.start();
  424. });
  425. }
  426. public static List<ApplicationConnection> getRunningApplications() {
  427. return runningApplications;
  428. }
  429. /**
  430. * Gets the configuration object for a specific application from the
  431. * bootstrap javascript.
  432. *
  433. * @param appId
  434. * the id of the application to get configuration data for
  435. * @return a native javascript object containing the configuration data
  436. */
  437. private static native JsoConfiguration getJsoConfiguration(String appId)
  438. /*-{
  439. return $wnd.vaadin.getApp(appId);
  440. }-*/;
  441. public static ApplicationConfiguration getConfigFromDOM(String appId) {
  442. ApplicationConfiguration conf = new ApplicationConfiguration();
  443. conf.setAppId(appId);
  444. conf.loadFromDOM();
  445. return conf;
  446. }
  447. public String getServletVersion() {
  448. return getJsoConfiguration(id).getVaadinVersion();
  449. }
  450. /**
  451. * Return Atmosphere version.
  452. *
  453. * @since 7.4
  454. *
  455. * @return Atmosphere version.
  456. */
  457. public String getAtmosphereVersion() {
  458. return getJsoConfiguration(id).getAtmosphereVersion();
  459. }
  460. /**
  461. * Return Atmosphere JS version.
  462. *
  463. * @since 7.4
  464. *
  465. * @return Atmosphere JS version.
  466. */
  467. public String getAtmosphereJSVersion() {
  468. return getJsoConfiguration(id).getAtmosphereJSVersion();
  469. }
  470. public Class<? extends ServerConnector> getConnectorClassByEncodedTag(
  471. int tag) {
  472. Class<? extends ServerConnector> type = classes.get(tag);
  473. if (type == null && !classes.containsKey(tag)) {
  474. // Initialize if not already loaded
  475. Integer currentTag = Integer.valueOf(tag);
  476. while (type == null && currentTag != null) {
  477. String serverSideClassNameForTag = getServerSideClassNameForTag(
  478. currentTag);
  479. if (TypeData.hasIdentifier(serverSideClassNameForTag)) {
  480. try {
  481. type = (Class<? extends ServerConnector>) TypeData
  482. .getClass(serverSideClassNameForTag);
  483. } catch (NoDataException e) {
  484. throw new RuntimeException(e);
  485. }
  486. }
  487. currentTag = getParentTag(currentTag.intValue());
  488. }
  489. if (type == null) {
  490. if (isExtensionType(tag)) {
  491. type = UnknownExtensionConnector.class;
  492. } else {
  493. type = UnknownComponentConnector.class;
  494. }
  495. if (unknownComponents == null) {
  496. unknownComponents = new HashMap<>();
  497. }
  498. unknownComponents.put(tag, getServerSideClassNameForTag(tag));
  499. }
  500. classes.put(tag, type);
  501. }
  502. return type;
  503. }
  504. private boolean isExtensionType(int tag) {
  505. Integer currentTag = Integer.valueOf(tag);
  506. while (currentTag != null) {
  507. String serverSideClassNameForTag = getServerSideClassNameForTag(
  508. currentTag);
  509. if ("com.vaadin.server.AbstractExtension"
  510. .equals(serverSideClassNameForTag)) {
  511. return true;
  512. }
  513. currentTag = getParentTag(currentTag.intValue());
  514. }
  515. return false;
  516. }
  517. public void addComponentInheritanceInfo(ValueMap valueMap) {
  518. JsArrayString keyArray = valueMap.getKeyArray();
  519. for (int i = 0; i < keyArray.length(); i++) {
  520. String key = keyArray.get(i);
  521. int value = valueMap.getInt(key);
  522. componentInheritanceMap.put(Integer.parseInt(key), value);
  523. }
  524. }
  525. public void addComponentMappings(ValueMap valueMap, WidgetSet widgetSet) {
  526. JsArrayString keyArray = valueMap.getKeyArray();
  527. for (int i = 0; i < keyArray.length(); i++) {
  528. String key = keyArray.get(i).intern();
  529. int value = valueMap.getInt(key);
  530. tagToServerSideClassName.put(value, key);
  531. }
  532. for (int i = 0; i < keyArray.length(); i++) {
  533. String key = keyArray.get(i).intern();
  534. int value = valueMap.getInt(key);
  535. widgetSet.ensureConnectorLoaded(value, this);
  536. }
  537. }
  538. /**
  539. * Returns all tags for given class. Tags are used in
  540. * {@link ApplicationConfiguration} to keep track of different classes and
  541. * their hierarchy
  542. *
  543. * @since 7.2
  544. * @param classname
  545. * name of class which tags we want
  546. * @return Integer array of tags pointing to this classname
  547. */
  548. public Integer[] getTagsForServerSideClassName(String classname) {
  549. List<Integer> tags = new ArrayList<>();
  550. for (Map.Entry<Integer, String> entry : tagToServerSideClassName
  551. .entrySet()) {
  552. if (classname.equals(entry.getValue())) {
  553. tags.add(entry.getKey());
  554. }
  555. }
  556. Integer[] out = new Integer[tags.size()];
  557. return tags.toArray(out);
  558. }
  559. public Integer getParentTag(int tag) {
  560. return componentInheritanceMap.get(tag);
  561. }
  562. public String getServerSideClassNameForTag(Integer tag) {
  563. return tagToServerSideClassName.get(tag);
  564. }
  565. String getUnknownServerClassNameByTag(int tag) {
  566. if (unknownComponents != null) {
  567. String className = unknownComponents.get(tag);
  568. if (className == null) {
  569. className = "unknown class with id " + tag;
  570. }
  571. return className;
  572. }
  573. return null;
  574. }
  575. /**
  576. * Runs the given command when all pending dependencies have been loaded, or
  577. * immediately if no dependencies are being loaded.
  578. *
  579. * @since 7.6
  580. * @param command
  581. * the command to run
  582. */
  583. public static void runWhenDependenciesLoaded(Command command) {
  584. if (dependenciesLoading == 0) {
  585. command.execute();
  586. } else {
  587. callbacks.add(command);
  588. }
  589. }
  590. static void startDependencyLoading() {
  591. dependenciesLoading++;
  592. }
  593. static void endDependencyLoading() {
  594. dependenciesLoading--;
  595. if (dependenciesLoading == 0 && !callbacks.isEmpty()) {
  596. for (Command cmd : callbacks) {
  597. cmd.execute();
  598. }
  599. callbacks.clear();
  600. }
  601. }
  602. private boolean vaadinBootstrapLoaded() {
  603. Element window = ScriptInjector.TOP_WINDOW.cast();
  604. return window.getPropertyJSO("vaadin") != null;
  605. }
  606. @Override
  607. public void onModuleLoad() {
  608. // Don't run twice if the module has been inherited several times,
  609. // and don't continue if vaadinBootstrap was not executed.
  610. if (moduleLoaded || !vaadinBootstrapLoaded()) {
  611. getLogger().log(Level.WARNING,
  612. "vaadinBootstrap.js was not loaded, skipping vaadin application configuration.");
  613. return;
  614. }
  615. moduleLoaded = true;
  616. Profiler.initialize();
  617. Profiler.enter("ApplicationConfiguration.onModuleLoad");
  618. BrowserInfo browserInfo = BrowserInfo.get();
  619. // Enable iOS6 cast fix (see #10460)
  620. if (browserInfo.isIOS6() && browserInfo.isWebkit()) {
  621. enableIOS6castFix();
  622. }
  623. // Enable IE prompt fix (#13367)
  624. if (browserInfo.isIE() && browserInfo.getBrowserMajorVersion() >= 10) {
  625. enableIEPromptFix();
  626. }
  627. // Register pointer events (must be done before any events are used)
  628. PointerEventSupport.init();
  629. if (LogConfiguration.loggingIsEnabled()) {
  630. GWT.setUncaughtExceptionHandler(throwable -> {
  631. /*
  632. * If the debug window is not enabled (?debug), this will not
  633. * show anything to normal users. "a1 is not an object" style
  634. * errors helps nobody, especially end user. It does not work
  635. * tells just as much.
  636. */
  637. getLogger().log(Level.SEVERE, throwable.getMessage(),
  638. throwable);
  639. });
  640. if (isProductionMode()) {
  641. // Disable all logging if in production mode
  642. Logger.getLogger("").setLevel(Level.OFF);
  643. }
  644. }
  645. Profiler.leave("ApplicationConfiguration.onModuleLoad");
  646. if (SuperDevMode.enableBasedOnParameter()) {
  647. // Do not start any application as super dev mode will refresh the
  648. // page once done compiling
  649. return;
  650. }
  651. if (isDebugMode()) {
  652. // Load debug window bundle and continue the bootstrap sequence once
  653. // it's loaded
  654. GWT.runAsync(VDebugWindow.class, new RunAsyncCallback() {
  655. @Override
  656. public void onSuccess() {
  657. initDebugWindow();
  658. registerCallback(GWT.getModuleName());
  659. }
  660. @Override
  661. public void onFailure(Throwable reason) {
  662. Window.alert("Failed to load Vaadin debug window");
  663. registerCallback(GWT.getModuleName());
  664. }
  665. });
  666. } else {
  667. // Continue the bootstrap sequence right away
  668. registerCallback(GWT.getModuleName());
  669. }
  670. }
  671. private static void initDebugWindow() {
  672. /*
  673. * XXX Lots of implementation details here right now. This should be
  674. * cleared up when an API for extending the debug window is implemented.
  675. */
  676. VDebugWindow window = VDebugWindow.get();
  677. if (LogConfiguration.loggingIsEnabled()) {
  678. window.addSection((Section) GWT.create(LogSection.class));
  679. }
  680. window.addSection((Section) GWT.create(InfoSection.class));
  681. window.addSection((Section) GWT.create(HierarchySection.class));
  682. window.addSection((Section) GWT.create(NetworkSection.class));
  683. window.addSection((Section) GWT.create(TestBenchSection.class));
  684. if (Profiler.isEnabled()) {
  685. window.addSection((Section) GWT.create(ProfilerSection.class));
  686. }
  687. if (isQuietDebugMode()) {
  688. window.close();
  689. } else {
  690. DebugWindowStyles dws = GWT.create(DebugWindowStyles.class);
  691. dws.css().ensureInjected();
  692. window.init();
  693. }
  694. // Connect to the legacy API
  695. VConsole.setImplementation(window);
  696. Handler errorNotificationHandler = GWT
  697. .create(ErrorNotificationHandler.class);
  698. Logger.getLogger("").addHandler(errorNotificationHandler);
  699. }
  700. /**
  701. * Fix to iOS6 failing when comparing with 0 directly after the kind of
  702. * comparison done by GWT when a double or float is cast to an int. Forcing
  703. * another trivial operation (other than a compare to 0) after the dangerous
  704. * comparison makes the issue go away. See #10460.
  705. */
  706. private static native void enableIOS6castFix()
  707. /*-{
  708. Math.max = function(a,b) {return (a > b === 1 < 2)? a : b}
  709. Math.min = function(a,b) {return (a < b === 1 < 2)? a : b}
  710. }-*/;
  711. /**
  712. * Make Metro versions of IE suggest switching to the desktop when
  713. * window.prompt is called.
  714. */
  715. private static native void enableIEPromptFix()
  716. /*-{
  717. var prompt = $wnd.prompt;
  718. $wnd.prompt = function () {
  719. var result = prompt.apply($wnd, Array.prototype.slice.call(arguments));
  720. if (result === undefined) {
  721. // force the browser to suggest desktop mode
  722. showModalDialog();
  723. return null;
  724. } else {
  725. return result;
  726. }
  727. };
  728. }-*/;
  729. /**
  730. * Registers that callback that the bootstrap javascript uses to start
  731. * applications once the widgetset is loaded and all required information is
  732. * available.
  733. *
  734. * @param widgetsetName
  735. * the name of this widgetset
  736. */
  737. public static native void registerCallback(String widgetsetName)
  738. /*-{
  739. var callbackHandler = $entry(@com.vaadin.client.ApplicationConfiguration::startApplication(Ljava/lang/String;));
  740. $wnd.vaadin.registerWidgetset(widgetsetName, callbackHandler);
  741. }-*/;
  742. /**
  743. * Checks if client side is in debug mode. Practically this is invoked by
  744. * adding ?debug parameter to URI. Please note that debug mode is always
  745. * disabled if production mode is enabled, but disabling production mode
  746. * does not automatically enable debug mode.
  747. *
  748. * @see #isProductionMode()
  749. *
  750. * @return true if client side is currently been debugged
  751. */
  752. public static boolean isDebugMode() {
  753. return isDebugAvailable()
  754. && Window.Location.getParameter("debug") != null;
  755. }
  756. /**
  757. * Checks if production mode is enabled. When production mode is enabled,
  758. * client-side logging is disabled. There may also be other performance
  759. * optimizations.
  760. *
  761. * @since 7.1.2
  762. * @return <code>true</code> if production mode is enabled; otherwise
  763. * <code>false</code>.
  764. */
  765. public static boolean isProductionMode() {
  766. return !isDebugAvailable();
  767. }
  768. private static native boolean isDebugAvailable()
  769. /*-{
  770. return $wnd.vaadin.debug;
  771. }-*/;
  772. /**
  773. * Checks whether debug logging should be quiet.
  774. *
  775. * @return <code>true</code> if debug logging should be quiet
  776. */
  777. public static boolean isQuietDebugMode() {
  778. String debugParameter = Window.Location.getParameter("debug");
  779. return isDebugAvailable() && debugParameter != null
  780. && debugParameter.startsWith("q");
  781. }
  782. /**
  783. * Checks whether the widget set version has been sent to the server. It is
  784. * sent in the first UIDL request.
  785. *
  786. * @return <code>true</code> if browser information has already been sent
  787. */
  788. public boolean isWidgetsetVersionSent() {
  789. return widgetsetVersionSent;
  790. }
  791. /**
  792. * Registers that the widget set version has been sent to the server.
  793. */
  794. public void setWidgetsetVersionSent() {
  795. widgetsetVersionSent = true;
  796. }
  797. private static final Logger getLogger() {
  798. return Logger.getLogger(ApplicationConfiguration.class.getName());
  799. }
  800. }