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

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