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

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