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.

BrowserInfo.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright 2011 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.terminal.gwt.client;
  17. import com.google.gwt.user.client.ui.RootPanel;
  18. import com.vaadin.shared.VBrowserDetails;
  19. /**
  20. * Class used to query information about web browser.
  21. *
  22. * Browser details are detected only once and those are stored in this singleton
  23. * class.
  24. *
  25. */
  26. public class BrowserInfo {
  27. private static final String BROWSER_OPERA = "op";
  28. private static final String BROWSER_IE = "ie";
  29. private static final String BROWSER_FIREFOX = "ff";
  30. private static final String BROWSER_SAFARI = "sa";
  31. public static final String ENGINE_GECKO = "gecko";
  32. public static final String ENGINE_WEBKIT = "webkit";
  33. public static final String ENGINE_PRESTO = "presto";
  34. public static final String ENGINE_TRIDENT = "trident";
  35. private static final String OS_WINDOWS = "win";
  36. private static final String OS_LINUX = "lin";
  37. private static final String OS_MACOSX = "mac";
  38. private static final String OS_ANDROID = "android";
  39. private static final String OS_IOS = "ios";
  40. // Common CSS class for all touch devices
  41. private static final String UI_TOUCH = "touch";
  42. private static BrowserInfo instance;
  43. private static String cssClass = null;
  44. static {
  45. // Add browser dependent v-* classnames to body to help css hacks
  46. String browserClassnames = get().getCSSClass();
  47. RootPanel.get().addStyleName(browserClassnames);
  48. }
  49. /**
  50. * Singleton method to get BrowserInfo object.
  51. *
  52. * @return instance of BrowserInfo object
  53. */
  54. public static BrowserInfo get() {
  55. if (instance == null) {
  56. instance = new BrowserInfo();
  57. }
  58. return instance;
  59. }
  60. private VBrowserDetails browserDetails;
  61. private boolean touchDevice;
  62. private BrowserInfo() {
  63. browserDetails = new VBrowserDetails(getBrowserString());
  64. if (browserDetails.isIE()) {
  65. // Use document mode instead user agent to accurately detect how we
  66. // are rendering
  67. int documentMode = getIEDocumentMode();
  68. if (documentMode != -1) {
  69. browserDetails.setIEMode(documentMode);
  70. }
  71. }
  72. if (browserDetails.isChrome()) {
  73. touchDevice = detectChromeTouchDevice();
  74. } else {
  75. touchDevice = detectTouchDevice();
  76. }
  77. }
  78. private native boolean detectTouchDevice()
  79. /*-{
  80. try { document.createEvent("TouchEvent");return true;} catch(e){return false;};
  81. }-*/;
  82. private native boolean detectChromeTouchDevice()
  83. /*-{
  84. return ("ontouchstart" in window);
  85. }-*/;
  86. private native int getIEDocumentMode()
  87. /*-{
  88. var mode = $wnd.document.documentMode;
  89. if (!mode)
  90. return -1;
  91. return mode;
  92. }-*/;
  93. /**
  94. * Returns a string representing the browser in use, for use in CSS
  95. * classnames. The classnames will be space separated abbreviations,
  96. * optionally with a version appended.
  97. *
  98. * Abbreviations: Firefox: ff Internet Explorer: ie Safari: sa Opera: op
  99. *
  100. * Browsers that CSS-wise behave like each other will get the same
  101. * abbreviation (this usually depends on the rendering engine).
  102. *
  103. * This is quite simple at the moment, more heuristics will be added when
  104. * needed.
  105. *
  106. * Examples: Internet Explorer 6: ".v-ie .v-ie6 .v-ie60", Firefox 3.0.4:
  107. * ".v-ff .v-ff3 .v-ff30", Opera 9.60: ".v-op .v-op9 .v-op960", Opera 10.10:
  108. * ".v-op .v-op10 .v-op1010"
  109. *
  110. * @return
  111. */
  112. public String getCSSClass() {
  113. String prefix = "v-";
  114. if (cssClass == null) {
  115. String browserIdentifier = "";
  116. String majorVersionClass = "";
  117. String minorVersionClass = "";
  118. String browserEngineClass = "";
  119. if (browserDetails.isFirefox()) {
  120. browserIdentifier = BROWSER_FIREFOX;
  121. majorVersionClass = browserIdentifier
  122. + browserDetails.getBrowserMajorVersion();
  123. minorVersionClass = majorVersionClass
  124. + browserDetails.getBrowserMinorVersion();
  125. browserEngineClass = ENGINE_GECKO;
  126. } else if (browserDetails.isChrome()) {
  127. // TODO update when Chrome is more stable
  128. browserIdentifier = BROWSER_SAFARI;
  129. majorVersionClass = "ch";
  130. browserEngineClass = ENGINE_WEBKIT;
  131. } else if (browserDetails.isSafari()) {
  132. browserIdentifier = BROWSER_SAFARI;
  133. majorVersionClass = browserIdentifier
  134. + browserDetails.getBrowserMajorVersion();
  135. minorVersionClass = majorVersionClass
  136. + browserDetails.getBrowserMinorVersion();
  137. browserEngineClass = ENGINE_WEBKIT;
  138. } else if (browserDetails.isIE()) {
  139. browserIdentifier = BROWSER_IE;
  140. majorVersionClass = browserIdentifier
  141. + browserDetails.getBrowserMajorVersion();
  142. minorVersionClass = majorVersionClass
  143. + browserDetails.getBrowserMinorVersion();
  144. browserEngineClass = ENGINE_TRIDENT;
  145. } else if (browserDetails.isOpera()) {
  146. browserIdentifier = BROWSER_OPERA;
  147. majorVersionClass = browserIdentifier
  148. + browserDetails.getBrowserMajorVersion();
  149. minorVersionClass = majorVersionClass
  150. + browserDetails.getBrowserMinorVersion();
  151. browserEngineClass = ENGINE_PRESTO;
  152. }
  153. cssClass = prefix + browserIdentifier;
  154. if (!"".equals(majorVersionClass)) {
  155. cssClass = cssClass + " " + prefix + majorVersionClass;
  156. }
  157. if (!"".equals(minorVersionClass)) {
  158. cssClass = cssClass + " " + prefix + minorVersionClass;
  159. }
  160. if (!"".equals(browserEngineClass)) {
  161. cssClass = cssClass + " " + prefix + browserEngineClass;
  162. }
  163. String osClass = getOperatingSystemClass();
  164. if (osClass != null) {
  165. cssClass = cssClass + " " + prefix + osClass;
  166. }
  167. if (isTouchDevice()) {
  168. cssClass = cssClass + " " + prefix + UI_TOUCH;
  169. }
  170. }
  171. return cssClass;
  172. }
  173. private String getOperatingSystemClass() {
  174. if (browserDetails.isAndroid()) {
  175. return OS_ANDROID;
  176. } else if (browserDetails.isIOS()) {
  177. return OS_IOS;
  178. } else if (browserDetails.isWindows()) {
  179. return OS_WINDOWS;
  180. } else if (browserDetails.isLinux()) {
  181. return OS_LINUX;
  182. } else if (browserDetails.isMacOSX()) {
  183. return OS_MACOSX;
  184. }
  185. // Unknown OS
  186. return null;
  187. }
  188. public boolean isIE() {
  189. return browserDetails.isIE();
  190. }
  191. public boolean isFirefox() {
  192. return browserDetails.isFirefox();
  193. }
  194. public boolean isSafari() {
  195. return browserDetails.isSafari();
  196. }
  197. public boolean isIE8() {
  198. return isIE() && browserDetails.getBrowserMajorVersion() == 8;
  199. }
  200. public boolean isIE9() {
  201. return isIE() && browserDetails.getBrowserMajorVersion() == 9;
  202. }
  203. public boolean isChrome() {
  204. return browserDetails.isChrome();
  205. }
  206. public boolean isGecko() {
  207. return browserDetails.isGecko();
  208. }
  209. public boolean isWebkit() {
  210. return browserDetails.isWebKit();
  211. }
  212. /**
  213. * Returns the Gecko version if the browser is Gecko based. The Gecko
  214. * version for Firefox 2 is 1.8 and 1.9 for Firefox 3.
  215. *
  216. * @return The Gecko version or -1 if the browser is not Gecko based
  217. */
  218. public float getGeckoVersion() {
  219. if (!browserDetails.isGecko()) {
  220. return -1;
  221. }
  222. return browserDetails.getBrowserEngineVersion();
  223. }
  224. /**
  225. * Returns the WebKit version if the browser is WebKit based. The WebKit
  226. * version returned is the major version e.g., 523.
  227. *
  228. * @return The WebKit version or -1 if the browser is not WebKit based
  229. */
  230. public float getWebkitVersion() {
  231. if (!browserDetails.isWebKit()) {
  232. return -1;
  233. }
  234. return browserDetails.getBrowserEngineVersion();
  235. }
  236. public float getIEVersion() {
  237. if (!browserDetails.isIE()) {
  238. return -1;
  239. }
  240. return browserDetails.getBrowserMajorVersion();
  241. }
  242. public float getOperaVersion() {
  243. if (!browserDetails.isOpera()) {
  244. return -1;
  245. }
  246. return browserDetails.getBrowserMajorVersion();
  247. }
  248. public boolean isOpera() {
  249. return browserDetails.isOpera();
  250. }
  251. public boolean isOpera10() {
  252. return browserDetails.isOpera()
  253. && browserDetails.getBrowserMajorVersion() == 10;
  254. }
  255. public boolean isOpera11() {
  256. return browserDetails.isOpera()
  257. && browserDetails.getBrowserMajorVersion() == 11;
  258. }
  259. public native static String getBrowserString()
  260. /*-{
  261. return $wnd.navigator.userAgent;
  262. }-*/;
  263. public native int getScreenWidth()
  264. /*-{
  265. return $wnd.screen.width;
  266. }-*/;
  267. public native int getScreenHeight()
  268. /*-{
  269. return $wnd.screen.height;
  270. }-*/;
  271. /**
  272. * @return true if the browser runs on a touch based device.
  273. */
  274. public boolean isTouchDevice() {
  275. return touchDevice;
  276. }
  277. /**
  278. * Indicates whether the browser might require juggling to properly update
  279. * sizes inside elements with overflow: auto.
  280. *
  281. * @return <code>true</code> if the browser requires the workaround,
  282. * otherwise <code>false</code>
  283. */
  284. public boolean requiresOverflowAutoFix() {
  285. return (getWebkitVersion() > 0 || getOperaVersion() >= 11)
  286. && Util.getNativeScrollbarSize() > 0;
  287. }
  288. /**
  289. * Checks if the browser is run on iOS
  290. *
  291. * @return true if the browser is run on iOS, false otherwise
  292. */
  293. public boolean isIOS() {
  294. return browserDetails.isIOS();
  295. }
  296. /**
  297. * Checks if the browser is run on Android
  298. *
  299. * @return true if the browser is run on Android, false otherwise
  300. */
  301. public boolean isAndroid() {
  302. return browserDetails.isAndroid();
  303. }
  304. /**
  305. * Checks if the browser is capable of handling scrolling natively or if a
  306. * touch scroll helper is needed for scrolling.
  307. *
  308. * @return true if browser needs a touch scroll helper, false if the browser
  309. * can handle scrolling natively
  310. */
  311. public boolean requiresTouchScrollDelegate() {
  312. if (!isTouchDevice()) {
  313. return false;
  314. }
  315. if (isAndroid() && isWebkit() && getWebkitVersion() >= 534) {
  316. return false;
  317. }
  318. // Cannot enable native touch scrolling on iOS 5 until #8792 is resolved
  319. // if (isIOS() && isWebkit() && getWebkitVersion() >= 534) {
  320. // return false;
  321. // }
  322. return true;
  323. }
  324. /**
  325. * Tests if this is an Android devices with a broken scrollTop
  326. * implementation
  327. *
  328. * @return true if scrollTop cannot be trusted on this device, false
  329. * otherwise
  330. */
  331. public boolean isAndroidWithBrokenScrollTop() {
  332. return isAndroid()
  333. && (getOperatingSystemMajorVersion() == 3 || getOperatingSystemMajorVersion() == 4);
  334. }
  335. private int getOperatingSystemMajorVersion() {
  336. return browserDetails.getOperatingSystemMajorVersion();
  337. }
  338. }