Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ApplicationConfiguration.java 30KB

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