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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 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 if (browserDetails.isIE()) {
  75. touchDevice = detectIETouchDevice();
  76. } else {
  77. touchDevice = detectTouchDevice();
  78. }
  79. }
  80. private native boolean detectTouchDevice()
  81. /*-{
  82. try { document.createEvent("TouchEvent");return true;} catch(e){return false;};
  83. }-*/;
  84. private native boolean detectChromeTouchDevice()
  85. /*-{
  86. return ("ontouchstart" in window);
  87. }-*/;
  88. private native boolean detectIETouchDevice()
  89. /*-{
  90. return !!navigator.msMaxTouchPoints;
  91. }-*/;
  92. private native int getIEDocumentMode()
  93. /*-{
  94. var mode = $wnd.document.documentMode;
  95. if (!mode)
  96. return -1;
  97. return mode;
  98. }-*/;
  99. /**
  100. * Returns a string representing the browser in use, for use in CSS
  101. * classnames. The classnames will be space separated abbreviations,
  102. * optionally with a version appended.
  103. *
  104. * Abbreviations: Firefox: ff Internet Explorer: ie Safari: sa Opera: op
  105. *
  106. * Browsers that CSS-wise behave like each other will get the same
  107. * abbreviation (this usually depends on the rendering engine).
  108. *
  109. * This is quite simple at the moment, more heuristics will be added when
  110. * needed.
  111. *
  112. * Examples: Internet Explorer 6: ".v-ie .v-ie6 .v-ie60", Firefox 3.0.4:
  113. * ".v-ff .v-ff3 .v-ff30", Opera 9.60: ".v-op .v-op9 .v-op960", Opera 10.10:
  114. * ".v-op .v-op10 .v-op1010"
  115. *
  116. * @return
  117. */
  118. public String getCSSClass() {
  119. String prefix = "v-";
  120. if (cssClass == null) {
  121. String browserIdentifier = "";
  122. String majorVersionClass = "";
  123. String minorVersionClass = "";
  124. String browserEngineClass = "";
  125. if (browserDetails.isFirefox()) {
  126. browserIdentifier = BROWSER_FIREFOX;
  127. majorVersionClass = browserIdentifier
  128. + getBrowserMajorVersion();
  129. minorVersionClass = majorVersionClass
  130. + browserDetails.getBrowserMinorVersion();
  131. browserEngineClass = ENGINE_GECKO;
  132. } else if (browserDetails.isChrome()) {
  133. // TODO update when Chrome is more stable
  134. browserIdentifier = BROWSER_SAFARI;
  135. majorVersionClass = "ch";
  136. browserEngineClass = ENGINE_WEBKIT;
  137. } else if (browserDetails.isSafari()) {
  138. browserIdentifier = BROWSER_SAFARI;
  139. majorVersionClass = browserIdentifier
  140. + getBrowserMajorVersion();
  141. minorVersionClass = majorVersionClass
  142. + browserDetails.getBrowserMinorVersion();
  143. browserEngineClass = ENGINE_WEBKIT;
  144. } else if (browserDetails.isIE()) {
  145. browserIdentifier = BROWSER_IE;
  146. majorVersionClass = browserIdentifier
  147. + getBrowserMajorVersion();
  148. minorVersionClass = majorVersionClass
  149. + browserDetails.getBrowserMinorVersion();
  150. browserEngineClass = ENGINE_TRIDENT;
  151. } else if (browserDetails.isOpera()) {
  152. browserIdentifier = BROWSER_OPERA;
  153. majorVersionClass = browserIdentifier
  154. + getBrowserMajorVersion();
  155. minorVersionClass = majorVersionClass
  156. + browserDetails.getBrowserMinorVersion();
  157. browserEngineClass = ENGINE_PRESTO;
  158. }
  159. cssClass = prefix + browserIdentifier;
  160. if (!"".equals(majorVersionClass)) {
  161. cssClass = cssClass + " " + prefix + majorVersionClass;
  162. }
  163. if (!"".equals(minorVersionClass)) {
  164. cssClass = cssClass + " " + prefix + minorVersionClass;
  165. }
  166. if (!"".equals(browserEngineClass)) {
  167. cssClass = cssClass + " " + prefix + browserEngineClass;
  168. }
  169. String osClass = getOperatingSystemClass();
  170. if (osClass != null) {
  171. cssClass = cssClass + " " + osClass;
  172. }
  173. if (isTouchDevice()) {
  174. cssClass = cssClass + " " + prefix + UI_TOUCH;
  175. }
  176. }
  177. return cssClass;
  178. }
  179. private String getOperatingSystemClass() {
  180. String prefix = "v-";
  181. if (browserDetails.isAndroid()) {
  182. return prefix + OS_ANDROID;
  183. } else if (browserDetails.isIOS()) {
  184. String iosClass = prefix + OS_IOS;
  185. return iosClass + " " + iosClass + getOperatingSystemMajorVersion();
  186. } else if (browserDetails.isWindows()) {
  187. return prefix + OS_WINDOWS;
  188. } else if (browserDetails.isLinux()) {
  189. return prefix + OS_LINUX;
  190. } else if (browserDetails.isMacOSX()) {
  191. return prefix + OS_MACOSX;
  192. }
  193. // Unknown OS
  194. return null;
  195. }
  196. public boolean isIE() {
  197. return browserDetails.isIE();
  198. }
  199. public boolean isFirefox() {
  200. return browserDetails.isFirefox();
  201. }
  202. public boolean isSafari() {
  203. return browserDetails.isSafari();
  204. }
  205. public boolean isIE8() {
  206. return isIE() && getBrowserMajorVersion() == 8;
  207. }
  208. public boolean isIE9() {
  209. return isIE() && getBrowserMajorVersion() == 9;
  210. }
  211. public boolean isIE10() {
  212. return isIE() && getBrowserMajorVersion() == 10;
  213. }
  214. public boolean isChrome() {
  215. return browserDetails.isChrome();
  216. }
  217. public boolean isGecko() {
  218. return browserDetails.isGecko();
  219. }
  220. public boolean isWebkit() {
  221. return browserDetails.isWebKit();
  222. }
  223. /**
  224. * Returns the Gecko version if the browser is Gecko based. The Gecko
  225. * version for Firefox 2 is 1.8 and 1.9 for Firefox 3.
  226. *
  227. * @return The Gecko version or -1 if the browser is not Gecko based
  228. */
  229. public float getGeckoVersion() {
  230. if (!browserDetails.isGecko()) {
  231. return -1;
  232. }
  233. return browserDetails.getBrowserEngineVersion();
  234. }
  235. /**
  236. * Returns the WebKit version if the browser is WebKit based. The WebKit
  237. * version returned is the major version e.g., 523.
  238. *
  239. * @return The WebKit version or -1 if the browser is not WebKit based
  240. */
  241. public float getWebkitVersion() {
  242. if (!browserDetails.isWebKit()) {
  243. return -1;
  244. }
  245. return browserDetails.getBrowserEngineVersion();
  246. }
  247. public float getIEVersion() {
  248. if (!browserDetails.isIE()) {
  249. return -1;
  250. }
  251. return getBrowserMajorVersion();
  252. }
  253. public float getOperaVersion() {
  254. if (!browserDetails.isOpera()) {
  255. return -1;
  256. }
  257. return getBrowserMajorVersion();
  258. }
  259. public boolean isOpera() {
  260. return browserDetails.isOpera();
  261. }
  262. public boolean isOpera10() {
  263. return browserDetails.isOpera() && getBrowserMajorVersion() == 10;
  264. }
  265. public boolean isOpera11() {
  266. return browserDetails.isOpera() && getBrowserMajorVersion() == 11;
  267. }
  268. public native static String getBrowserString()
  269. /*-{
  270. return $wnd.navigator.userAgent;
  271. }-*/;
  272. public native int getScreenWidth()
  273. /*-{
  274. return $wnd.screen.width;
  275. }-*/;
  276. public native int getScreenHeight()
  277. /*-{
  278. return $wnd.screen.height;
  279. }-*/;
  280. /**
  281. * @return true if the browser runs on a touch based device.
  282. */
  283. public boolean isTouchDevice() {
  284. return touchDevice;
  285. }
  286. /**
  287. * Indicates whether the browser might require juggling to properly update
  288. * sizes inside elements with overflow: auto.
  289. *
  290. * @return <code>true</code> if the browser requires the workaround,
  291. * otherwise <code>false</code>
  292. */
  293. public boolean requiresOverflowAutoFix() {
  294. return (getWebkitVersion() > 0 || getOperaVersion() >= 11
  295. || getIEVersion() >= 10 || isFirefox())
  296. && Util.getNativeScrollbarSize() > 0;
  297. }
  298. /**
  299. * Indicates whether the browser might require juggling to properly update
  300. * sizes inside elements with overflow: auto when adjusting absolutely
  301. * positioned elements.
  302. * <p>
  303. * See https://bugs.webkit.org/show_bug.cgi?id=123958 and
  304. * http://code.google.com/p/chromium/issues/detail?id=316549
  305. *
  306. * @since 7.1.8
  307. * @return <code>true</code> if the browser requires the workaround,
  308. * otherwise <code>false</code>
  309. */
  310. public boolean requiresPositionAbsoluteOverflowAutoFix() {
  311. return (getWebkitVersion() > 0) && Util.getNativeScrollbarSize() > 0;
  312. }
  313. /**
  314. * Checks if the browser is run on iOS
  315. *
  316. * @return true if the browser is run on iOS, false otherwise
  317. */
  318. public boolean isIOS() {
  319. return browserDetails.isIOS();
  320. }
  321. /**
  322. * Checks if the browser is run on iOS 6.
  323. *
  324. * @since 7.1.1
  325. * @return true if the browser is run on iOS 6, false otherwise
  326. */
  327. public boolean isIOS6() {
  328. return isIOS() && getOperatingSystemMajorVersion() == 6;
  329. }
  330. /**
  331. * Checks if the browser is run on Android
  332. *
  333. * @return true if the browser is run on Android, false otherwise
  334. */
  335. public boolean isAndroid() {
  336. return browserDetails.isAndroid();
  337. }
  338. /**
  339. * Checks if the browser is capable of handling scrolling natively or if a
  340. * touch scroll helper is needed for scrolling.
  341. *
  342. * @return true if browser needs a touch scroll helper, false if the browser
  343. * can handle scrolling natively
  344. */
  345. public boolean requiresTouchScrollDelegate() {
  346. if (!isTouchDevice()) {
  347. return false;
  348. }
  349. // TODO Should test other Android browsers, especially Chrome
  350. if (isAndroid() && isWebkit() && getWebkitVersion() >= 534) {
  351. return false;
  352. }
  353. // iOS 6+ Safari supports native scrolling; iOS 5 suffers from #8792
  354. // TODO Should test other iOS browsers
  355. if (isIOS() && isWebkit() && getOperatingSystemMajorVersion() >= 6) {
  356. return false;
  357. }
  358. return true;
  359. }
  360. /**
  361. * Tests if this is an Android devices with a broken scrollTop
  362. * implementation
  363. *
  364. * @return true if scrollTop cannot be trusted on this device, false
  365. * otherwise
  366. */
  367. public boolean isAndroidWithBrokenScrollTop() {
  368. return isAndroid()
  369. && (getOperatingSystemMajorVersion() == 3 || getOperatingSystemMajorVersion() == 4);
  370. }
  371. public boolean isAndroid23() {
  372. return isAndroid() && getOperatingSystemMajorVersion() == 2
  373. && getOperatingSystemMinorVersion() == 3;
  374. }
  375. private int getOperatingSystemMajorVersion() {
  376. return browserDetails.getOperatingSystemMajorVersion();
  377. }
  378. private int getOperatingSystemMinorVersion() {
  379. return browserDetails.getOperatingSystemMinorVersion();
  380. }
  381. /**
  382. * Returns the browser major version e.g., 3 for Firefox 3.5, 4 for Chrome
  383. * 4, 8 for Internet Explorer 8.
  384. * <p>
  385. * Note that Internet Explorer 8 and newer will return the document mode so
  386. * IE8 rendering as IE7 will return 7.
  387. * </p>
  388. *
  389. * @return The major version of the browser.
  390. */
  391. public int getBrowserMajorVersion() {
  392. return browserDetails.getBrowserMajorVersion();
  393. }
  394. /**
  395. * Returns the browser minor version e.g., 5 for Firefox 3.5.
  396. *
  397. * @see #getBrowserMajorVersion()
  398. *
  399. * @return The minor version of the browser, or -1 if not known/parsed.
  400. */
  401. public int getBrowserMinorVersion() {
  402. return browserDetails.getBrowserMinorVersion();
  403. }
  404. /**
  405. * Checks if the browser version is newer or equal to the given major+minor
  406. * version.
  407. *
  408. * @param majorVersion
  409. * The major version to check for
  410. * @param minorVersion
  411. * The minor version to check for
  412. * @return true if the browser version is newer or equal to the given
  413. * version
  414. */
  415. public boolean isBrowserVersionNewerOrEqual(int majorVersion,
  416. int minorVersion) {
  417. if (getBrowserMajorVersion() == majorVersion) {
  418. // Same major
  419. return (getBrowserMinorVersion() >= minorVersion);
  420. }
  421. // Older or newer major
  422. return (getBrowserMajorVersion() > majorVersion);
  423. }
  424. }