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

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