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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * Copyright 2000-2018 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_EDGE = "edge";
  30. private static final String BROWSER_FIREFOX = "ff";
  31. private static final String BROWSER_SAFARI = "sa";
  32. public static final String ENGINE_GECKO = "gecko";
  33. public static final String ENGINE_WEBKIT = "webkit";
  34. public static final String ENGINE_PRESTO = "presto";
  35. public static final String ENGINE_TRIDENT = "trident";
  36. private static final String OS_WINDOWS = "win";
  37. private static final String OS_LINUX = "lin";
  38. private static final String OS_MACOSX = "mac";
  39. private static final String OS_ANDROID = "android";
  40. private static final String OS_IOS = "ios";
  41. // Common CSS class for all touch devices
  42. private static final String UI_TOUCH = "touch";
  43. private static BrowserInfo instance;
  44. private static String cssClass = null;
  45. static {
  46. // Add browser dependent v-* classnames to body to help css hacks
  47. String browserClassnames = get().getCSSClass();
  48. RootPanel.get().addStyleName(browserClassnames);
  49. }
  50. /**
  51. * Singleton method to get BrowserInfo object.
  52. *
  53. * @return instance of BrowserInfo object
  54. */
  55. public static BrowserInfo get() {
  56. if (instance == null) {
  57. instance = new BrowserInfo();
  58. }
  59. return instance;
  60. }
  61. private VBrowserDetails browserDetails;
  62. private boolean touchDevice;
  63. private BrowserInfo() {
  64. browserDetails = new VBrowserDetails(getBrowserString());
  65. if (browserDetails.isIE()) {
  66. // Use document mode instead user agent to accurately detect how we
  67. // are rendering
  68. int documentMode = getIEDocumentMode();
  69. if (documentMode != -1) {
  70. browserDetails.setIEMode(documentMode);
  71. }
  72. }
  73. if (browserDetails.isChrome()) {
  74. touchDevice = detectChromeTouchDevice();
  75. } else if (browserDetails.isIE()) {
  76. touchDevice = detectIETouchDevice();
  77. } else {
  78. // PhantomJS pretends to be a touch device which breaks some UI
  79. // tests
  80. touchDevice = !browserDetails.isPhantomJS() && detectTouchDevice();
  81. }
  82. }
  83. private native boolean detectTouchDevice()
  84. /*-{
  85. try { document.createEvent("TouchEvent");return true;} catch(e) {return false;};
  86. }-*/;
  87. private native boolean detectChromeTouchDevice()
  88. /*-{
  89. return ("ontouchstart" in window);
  90. }-*/;
  91. private native boolean detectIETouchDevice()
  92. /*-{
  93. return !!navigator.msMaxTouchPoints;
  94. }-*/;
  95. private native int getIEDocumentMode()
  96. /*-{
  97. var mode = $wnd.document.documentMode;
  98. if (!mode)
  99. return -1;
  100. return mode;
  101. }-*/;
  102. /**
  103. * Returns a string representing the browser in use, for use in CSS
  104. * classnames. The classnames will be space separated abbreviations,
  105. * optionally with a version appended.
  106. *
  107. * Abbreviations: Firefox: ff Internet Explorer: ie Safari: sa Opera: op
  108. *
  109. * Browsers that CSS-wise behave like each other will get the same
  110. * abbreviation (this usually depends on the rendering engine).
  111. *
  112. * This is quite simple at the moment, more heuristics will be added when
  113. * needed.
  114. *
  115. * Examples: Internet Explorer 6: ".v-ie .v-ie6 .v-ie60", Firefox 3.0.4:
  116. * ".v-ff .v-ff3 .v-ff30", Opera 9.60: ".v-op .v-op9 .v-op960", Opera 10.10:
  117. * ".v-op .v-op10 .v-op1010"
  118. *
  119. * @return
  120. */
  121. public String getCSSClass() {
  122. String prefix = "v-";
  123. if (cssClass == null) {
  124. String browserIdentifier = "";
  125. String majorVersionClass = "";
  126. String minorVersionClass = "";
  127. String browserEngineClass = "";
  128. if (browserDetails.isFirefox()) {
  129. browserIdentifier = BROWSER_FIREFOX;
  130. majorVersionClass = browserIdentifier
  131. + getBrowserMajorVersion();
  132. minorVersionClass = majorVersionClass
  133. + browserDetails.getBrowserMinorVersion();
  134. browserEngineClass = ENGINE_GECKO;
  135. } else if (browserDetails.isChrome()) {
  136. // TODO update when Chrome is more stable
  137. browserIdentifier = BROWSER_SAFARI;
  138. majorVersionClass = "ch";
  139. browserEngineClass = ENGINE_WEBKIT;
  140. } else if (browserDetails.isSafari()) {
  141. browserIdentifier = BROWSER_SAFARI;
  142. majorVersionClass = browserIdentifier
  143. + getBrowserMajorVersion();
  144. minorVersionClass = majorVersionClass
  145. + browserDetails.getBrowserMinorVersion();
  146. browserEngineClass = ENGINE_WEBKIT;
  147. } else if (browserDetails.isPhantomJS()) {
  148. // Safari needed for theme
  149. browserIdentifier = BROWSER_SAFARI;
  150. majorVersionClass = browserIdentifier
  151. + getBrowserMajorVersion();
  152. minorVersionClass = majorVersionClass
  153. + browserDetails.getBrowserMinorVersion();
  154. browserEngineClass = ENGINE_WEBKIT;
  155. } else if (browserDetails.isIE()) {
  156. browserIdentifier = BROWSER_IE;
  157. majorVersionClass = browserIdentifier
  158. + getBrowserMajorVersion();
  159. minorVersionClass = majorVersionClass
  160. + browserDetails.getBrowserMinorVersion();
  161. browserEngineClass = ENGINE_TRIDENT;
  162. } else if (browserDetails.isEdge()) {
  163. browserIdentifier = BROWSER_EDGE;
  164. majorVersionClass = browserIdentifier
  165. + getBrowserMajorVersion();
  166. minorVersionClass = majorVersionClass
  167. + browserDetails.getBrowserMinorVersion();
  168. browserEngineClass = "";
  169. } else if (browserDetails.isOpera()) {
  170. browserIdentifier = BROWSER_OPERA;
  171. majorVersionClass = browserIdentifier
  172. + getBrowserMajorVersion();
  173. minorVersionClass = majorVersionClass
  174. + browserDetails.getBrowserMinorVersion();
  175. browserEngineClass = ENGINE_PRESTO;
  176. }
  177. cssClass = prefix + browserIdentifier;
  178. if (!majorVersionClass.isEmpty()) {
  179. cssClass = cssClass + " " + prefix + majorVersionClass;
  180. }
  181. if (!minorVersionClass.isEmpty()) {
  182. cssClass = cssClass + " " + prefix + minorVersionClass;
  183. }
  184. if (!browserEngineClass.isEmpty()) {
  185. cssClass = cssClass + " " + prefix + browserEngineClass;
  186. }
  187. String osClass = getOperatingSystemClass();
  188. if (osClass != null) {
  189. cssClass = cssClass + " " + osClass;
  190. }
  191. if (isTouchDevice()) {
  192. cssClass = cssClass + " " + prefix + UI_TOUCH;
  193. }
  194. }
  195. return cssClass;
  196. }
  197. private String getOperatingSystemClass() {
  198. String prefix = "v-";
  199. if (browserDetails.isAndroid()) {
  200. return prefix + OS_ANDROID;
  201. } else if (browserDetails.isIOS()) {
  202. String iosClass = prefix + OS_IOS;
  203. return iosClass + " " + iosClass + getOperatingSystemMajorVersion();
  204. } else if (browserDetails.isWindows()) {
  205. return prefix + OS_WINDOWS;
  206. } else if (browserDetails.isLinux()) {
  207. return prefix + OS_LINUX;
  208. } else if (browserDetails.isMacOSX()) {
  209. return prefix + OS_MACOSX;
  210. }
  211. // Unknown OS
  212. return null;
  213. }
  214. public boolean isIE() {
  215. return browserDetails.isIE();
  216. }
  217. public boolean isEdge() {
  218. return browserDetails.isEdge();
  219. }
  220. public boolean isFirefox() {
  221. return browserDetails.isFirefox();
  222. }
  223. public boolean isSafari() {
  224. return browserDetails.isSafari();
  225. }
  226. /**
  227. * Returns true if the browser is Safari or is a browser that is running on
  228. * iOS and using the Safari rendering engine.
  229. *
  230. * @return true if the browser is using the Safari rendering engine
  231. * @since 8.1
  232. */
  233. public boolean isSafariOrIOS() {
  234. return browserDetails.isSafariOrIOS();
  235. }
  236. @Deprecated
  237. public boolean isIE8() {
  238. return isIE() && getBrowserMajorVersion() == 8;
  239. }
  240. @Deprecated
  241. public boolean isIE9() {
  242. return isIE() && getBrowserMajorVersion() == 9;
  243. }
  244. @Deprecated
  245. public boolean isIE10() {
  246. return isIE() && getBrowserMajorVersion() == 10;
  247. }
  248. public boolean isIE11() {
  249. return isIE() && getBrowserMajorVersion() == 11;
  250. }
  251. public boolean isChrome() {
  252. return browserDetails.isChrome();
  253. }
  254. public boolean isGecko() {
  255. return browserDetails.isGecko();
  256. }
  257. public boolean isWebkit() {
  258. return browserDetails.isWebKit();
  259. }
  260. /**
  261. * Returns the Gecko version if the browser is Gecko based. The Gecko
  262. * version for Firefox 2 is 1.8 and 1.9 for Firefox 3.
  263. *
  264. * @return The Gecko version or -1 if the browser is not Gecko based
  265. */
  266. public float getGeckoVersion() {
  267. if (!browserDetails.isGecko()) {
  268. return -1;
  269. }
  270. return browserDetails.getBrowserEngineVersion();
  271. }
  272. /**
  273. * Returns the WebKit version if the browser is WebKit based. The WebKit
  274. * version returned is the major version e.g., 523.
  275. *
  276. * @return The WebKit version or -1 if the browser is not WebKit based
  277. */
  278. public float getWebkitVersion() {
  279. if (!browserDetails.isWebKit()) {
  280. return -1;
  281. }
  282. return browserDetails.getBrowserEngineVersion();
  283. }
  284. public float getIEVersion() {
  285. if (!browserDetails.isIE()) {
  286. return -1;
  287. }
  288. return getBrowserMajorVersion();
  289. }
  290. public float getOperaVersion() {
  291. if (!browserDetails.isOpera()) {
  292. return -1;
  293. }
  294. return getBrowserMajorVersion();
  295. }
  296. public boolean isOpera() {
  297. return browserDetails.isOpera();
  298. }
  299. public boolean isOpera10() {
  300. return browserDetails.isOpera() && getBrowserMajorVersion() == 10;
  301. }
  302. public boolean isOpera11() {
  303. return browserDetails.isOpera() && getBrowserMajorVersion() == 11;
  304. }
  305. public static native String getBrowserString()
  306. /*-{
  307. return $wnd.navigator.userAgent;
  308. }-*/;
  309. public native int getScreenWidth()
  310. /*-{
  311. return $wnd.screen.width;
  312. }-*/;
  313. public native int getScreenHeight()
  314. /*-{
  315. return $wnd.screen.height;
  316. }-*/;
  317. /**
  318. * @return true if the browser runs on a touch based device.
  319. */
  320. public boolean isTouchDevice() {
  321. return touchDevice;
  322. }
  323. /**
  324. * Indicates whether the browser might require juggling to properly update
  325. * sizes inside elements with overflow: auto.
  326. *
  327. * @return <code>true</code> if the browser requires the workaround,
  328. * otherwise <code>false</code>
  329. */
  330. public boolean requiresOverflowAutoFix() {
  331. return (getWebkitVersion() > 0 || getOperaVersion() >= 11
  332. || getIEVersion() >= 10 || isFirefox())
  333. && WidgetUtil.getNativeScrollbarSize() > 0;
  334. }
  335. /**
  336. * Indicates whether the browser might require juggling to properly update
  337. * sizes inside elements with overflow: auto when adjusting absolutely
  338. * positioned elements.
  339. * <p>
  340. * See https://bugs.webkit.org/show_bug.cgi?id=123958 and
  341. * http://code.google.com/p/chromium/issues/detail?id=316549
  342. *
  343. * @since 7.1.8
  344. * @return <code>true</code> if the browser requires the workaround,
  345. * otherwise <code>false</code>
  346. */
  347. public boolean requiresPositionAbsoluteOverflowAutoFix() {
  348. return (getWebkitVersion() > 0)
  349. && WidgetUtil.getNativeScrollbarSize() > 0;
  350. }
  351. /**
  352. * Checks if the browser is run on iOS.
  353. *
  354. * @return true if the browser is run on iOS, false otherwise
  355. */
  356. public boolean isIOS() {
  357. return browserDetails.isIOS();
  358. }
  359. /**
  360. * Checks if the browser is run on iOS 6.
  361. *
  362. * @since 7.1.1
  363. * @return true if the browser is run on iOS 6, false otherwise
  364. */
  365. public boolean isIOS6() {
  366. return isIOS() && getOperatingSystemMajorVersion() == 6;
  367. }
  368. /**
  369. * Checks if the browser is run on Android.
  370. *
  371. * @return true if the browser is run on Android, false otherwise
  372. */
  373. public boolean isAndroid() {
  374. return browserDetails.isAndroid();
  375. }
  376. /**
  377. * Checks if the browser is capable of handling scrolling natively or if a
  378. * touch scroll helper is needed for scrolling.
  379. *
  380. * @return true if browser needs a touch scroll helper, false if the browser
  381. * can handle scrolling natively
  382. */
  383. public boolean requiresTouchScrollDelegate() {
  384. if (!isTouchDevice()) {
  385. return false;
  386. }
  387. // TODO Should test other Android browsers, especially Chrome
  388. if (isAndroid() && isWebkit() && getWebkitVersion() >= 534) {
  389. return false;
  390. }
  391. // iOS 6+ Safari supports native scrolling; iOS 5 suffers from #8792
  392. // TODO Should test other iOS browsers
  393. if (isIOS() && isWebkit() && getOperatingSystemMajorVersion() >= 6) {
  394. return false;
  395. }
  396. if (isIE()) {
  397. return false;
  398. }
  399. return true;
  400. }
  401. /**
  402. * Tests if this is an Android devices with a broken scrollTop
  403. * implementation.
  404. *
  405. * @return true if scrollTop cannot be trusted on this device, false
  406. * otherwise
  407. */
  408. public boolean isAndroidWithBrokenScrollTop() {
  409. return isAndroid() && (getOperatingSystemMajorVersion() == 3
  410. || getOperatingSystemMajorVersion() == 4);
  411. }
  412. public boolean isAndroid23() {
  413. return isAndroid() && getOperatingSystemMajorVersion() == 2
  414. && getOperatingSystemMinorVersion() == 3;
  415. }
  416. private int getOperatingSystemMajorVersion() {
  417. return browserDetails.getOperatingSystemMajorVersion();
  418. }
  419. private int getOperatingSystemMinorVersion() {
  420. return browserDetails.getOperatingSystemMinorVersion();
  421. }
  422. /**
  423. * Returns the browser major version e.g., 3 for Firefox 3.5, 4 for Chrome
  424. * 4, 8 for Internet Explorer 8.
  425. * <p>
  426. * Note that Internet Explorer 8 and newer will return the document mode so
  427. * IE8 rendering as IE7 will return 7.
  428. * </p>
  429. *
  430. * @return The major version of the browser.
  431. */
  432. public int getBrowserMajorVersion() {
  433. return browserDetails.getBrowserMajorVersion();
  434. }
  435. /**
  436. * Returns the browser minor version e.g., 5 for Firefox 3.5.
  437. *
  438. * @see #getBrowserMajorVersion()
  439. *
  440. * @return The minor version of the browser, or -1 if not known/parsed.
  441. */
  442. public int getBrowserMinorVersion() {
  443. return browserDetails.getBrowserMinorVersion();
  444. }
  445. /**
  446. * Gets the complete browser version in form of a string. The version is
  447. * given by the browser through the user agent string and usually consists
  448. * of dot-separated numbers. Note that the string may contain characters
  449. * other than dots and digits.
  450. *
  451. * @return the complete browser version or {@code null} if unknown
  452. * @since
  453. */
  454. public String getBrowserVersion() {
  455. return browserDetails.getBrowserVersion();
  456. }
  457. /**
  458. * Checks if the browser version is newer or equal to the given major+minor
  459. * version.
  460. *
  461. * @param majorVersion
  462. * The major version to check for
  463. * @param minorVersion
  464. * The minor version to check for
  465. * @return true if the browser version is newer or equal to the given
  466. * version
  467. */
  468. public boolean isBrowserVersionNewerOrEqual(int majorVersion,
  469. int minorVersion) {
  470. if (getBrowserMajorVersion() == majorVersion) {
  471. // Same major
  472. return (getBrowserMinorVersion() >= minorVersion);
  473. }
  474. // Older or newer major
  475. return (getBrowserMajorVersion() > majorVersion);
  476. }
  477. }