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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * Copyright 2000-2016 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 (!"".equals(majorVersionClass)) {
  179. cssClass = cssClass + " " + prefix + majorVersionClass;
  180. }
  181. if (!"".equals(minorVersionClass)) {
  182. cssClass = cssClass + " " + prefix + minorVersionClass;
  183. }
  184. if (!"".equals(browserEngineClass)) {
  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. public boolean isIE8() {
  227. return isIE() && getBrowserMajorVersion() == 8;
  228. }
  229. public boolean isIE9() {
  230. return isIE() && getBrowserMajorVersion() == 9;
  231. }
  232. public boolean isIE10() {
  233. return isIE() && getBrowserMajorVersion() == 10;
  234. }
  235. public boolean isIE11() {
  236. return isIE() && getBrowserMajorVersion() == 11;
  237. }
  238. public boolean isChrome() {
  239. return browserDetails.isChrome();
  240. }
  241. public boolean isGecko() {
  242. return browserDetails.isGecko();
  243. }
  244. public boolean isWebkit() {
  245. return browserDetails.isWebKit();
  246. }
  247. /**
  248. * Returns the Gecko version if the browser is Gecko based. The Gecko
  249. * version for Firefox 2 is 1.8 and 1.9 for Firefox 3.
  250. *
  251. * @return The Gecko version or -1 if the browser is not Gecko based
  252. */
  253. public float getGeckoVersion() {
  254. if (!browserDetails.isGecko()) {
  255. return -1;
  256. }
  257. return browserDetails.getBrowserEngineVersion();
  258. }
  259. /**
  260. * Returns the WebKit version if the browser is WebKit based. The WebKit
  261. * version returned is the major version e.g., 523.
  262. *
  263. * @return The WebKit version or -1 if the browser is not WebKit based
  264. */
  265. public float getWebkitVersion() {
  266. if (!browserDetails.isWebKit()) {
  267. return -1;
  268. }
  269. return browserDetails.getBrowserEngineVersion();
  270. }
  271. public float getIEVersion() {
  272. if (!browserDetails.isIE()) {
  273. return -1;
  274. }
  275. return getBrowserMajorVersion();
  276. }
  277. public float getOperaVersion() {
  278. if (!browserDetails.isOpera()) {
  279. return -1;
  280. }
  281. return getBrowserMajorVersion();
  282. }
  283. public boolean isOpera() {
  284. return browserDetails.isOpera();
  285. }
  286. public boolean isOpera10() {
  287. return browserDetails.isOpera() && getBrowserMajorVersion() == 10;
  288. }
  289. public boolean isOpera11() {
  290. return browserDetails.isOpera() && getBrowserMajorVersion() == 11;
  291. }
  292. public native static String getBrowserString()
  293. /*-{
  294. return $wnd.navigator.userAgent;
  295. }-*/;
  296. public native int getScreenWidth()
  297. /*-{
  298. return $wnd.screen.width;
  299. }-*/;
  300. public native int getScreenHeight()
  301. /*-{
  302. return $wnd.screen.height;
  303. }-*/;
  304. /**
  305. * @return true if the browser runs on a touch based device.
  306. */
  307. public boolean isTouchDevice() {
  308. return touchDevice;
  309. }
  310. /**
  311. * Indicates whether the browser might require juggling to properly update
  312. * sizes inside elements with overflow: auto.
  313. *
  314. * @return <code>true</code> if the browser requires the workaround,
  315. * otherwise <code>false</code>
  316. */
  317. public boolean requiresOverflowAutoFix() {
  318. return (getWebkitVersion() > 0 || getOperaVersion() >= 11
  319. || getIEVersion() >= 10 || isFirefox())
  320. && WidgetUtil.getNativeScrollbarSize() > 0;
  321. }
  322. /**
  323. * Indicates whether the browser might require juggling to properly update
  324. * sizes inside elements with overflow: auto when adjusting absolutely
  325. * positioned elements.
  326. * <p>
  327. * See https://bugs.webkit.org/show_bug.cgi?id=123958 and
  328. * http://code.google.com/p/chromium/issues/detail?id=316549
  329. *
  330. * @since 7.1.8
  331. * @return <code>true</code> if the browser requires the workaround,
  332. * otherwise <code>false</code>
  333. */
  334. public boolean requiresPositionAbsoluteOverflowAutoFix() {
  335. return (getWebkitVersion() > 0)
  336. && WidgetUtil.getNativeScrollbarSize() > 0;
  337. }
  338. /**
  339. * Checks if the browser is run on iOS
  340. *
  341. * @return true if the browser is run on iOS, false otherwise
  342. */
  343. public boolean isIOS() {
  344. return browserDetails.isIOS();
  345. }
  346. /**
  347. * Checks if the browser is run on iOS 6.
  348. *
  349. * @since 7.1.1
  350. * @return true if the browser is run on iOS 6, false otherwise
  351. */
  352. public boolean isIOS6() {
  353. return isIOS() && getOperatingSystemMajorVersion() == 6;
  354. }
  355. /**
  356. * Checks if the browser is run on Android
  357. *
  358. * @return true if the browser is run on Android, false otherwise
  359. */
  360. public boolean isAndroid() {
  361. return browserDetails.isAndroid();
  362. }
  363. /**
  364. * Checks if the browser is capable of handling scrolling natively or if a
  365. * touch scroll helper is needed for scrolling.
  366. *
  367. * @return true if browser needs a touch scroll helper, false if the browser
  368. * can handle scrolling natively
  369. */
  370. public boolean requiresTouchScrollDelegate() {
  371. if (!isTouchDevice()) {
  372. return false;
  373. }
  374. // TODO Should test other Android browsers, especially Chrome
  375. if (isAndroid() && isWebkit() && getWebkitVersion() >= 534) {
  376. return false;
  377. }
  378. // iOS 6+ Safari supports native scrolling; iOS 5 suffers from #8792
  379. // TODO Should test other iOS browsers
  380. if (isIOS() && isWebkit() && getOperatingSystemMajorVersion() >= 6) {
  381. return false;
  382. }
  383. if (isIE()) {
  384. return false;
  385. }
  386. return true;
  387. }
  388. /**
  389. * Tests if this is an Android devices with a broken scrollTop
  390. * implementation
  391. *
  392. * @return true if scrollTop cannot be trusted on this device, false
  393. * otherwise
  394. */
  395. public boolean isAndroidWithBrokenScrollTop() {
  396. return isAndroid() && (getOperatingSystemMajorVersion() == 3
  397. || getOperatingSystemMajorVersion() == 4);
  398. }
  399. public boolean isAndroid23() {
  400. return isAndroid() && getOperatingSystemMajorVersion() == 2
  401. && getOperatingSystemMinorVersion() == 3;
  402. }
  403. private int getOperatingSystemMajorVersion() {
  404. return browserDetails.getOperatingSystemMajorVersion();
  405. }
  406. private int getOperatingSystemMinorVersion() {
  407. return browserDetails.getOperatingSystemMinorVersion();
  408. }
  409. /**
  410. * Returns the browser major version e.g., 3 for Firefox 3.5, 4 for Chrome
  411. * 4, 8 for Internet Explorer 8.
  412. * <p>
  413. * Note that Internet Explorer 8 and newer will return the document mode so
  414. * IE8 rendering as IE7 will return 7.
  415. * </p>
  416. *
  417. * @return The major version of the browser.
  418. */
  419. public int getBrowserMajorVersion() {
  420. return browserDetails.getBrowserMajorVersion();
  421. }
  422. /**
  423. * Returns the browser minor version e.g., 5 for Firefox 3.5.
  424. *
  425. * @see #getBrowserMajorVersion()
  426. *
  427. * @return The minor version of the browser, or -1 if not known/parsed.
  428. */
  429. public int getBrowserMinorVersion() {
  430. return browserDetails.getBrowserMinorVersion();
  431. }
  432. /**
  433. * Checks if the browser version is newer or equal to the given major+minor
  434. * version.
  435. *
  436. * @param majorVersion
  437. * The major version to check for
  438. * @param minorVersion
  439. * The minor version to check for
  440. * @return true if the browser version is newer or equal to the given
  441. * version
  442. */
  443. public boolean isBrowserVersionNewerOrEqual(int majorVersion,
  444. int minorVersion) {
  445. if (getBrowserMajorVersion() == majorVersion) {
  446. // Same major
  447. return (getBrowserMinorVersion() >= minorVersion);
  448. }
  449. // Older or newer major
  450. return (getBrowserMajorVersion() > majorVersion);
  451. }
  452. }