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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Copyright 2000-2013 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 + " " + prefix + osClass;
  172. }
  173. if (isTouchDevice()) {
  174. cssClass = cssClass + " " + prefix + UI_TOUCH;
  175. }
  176. }
  177. return cssClass;
  178. }
  179. private String getOperatingSystemClass() {
  180. if (browserDetails.isAndroid()) {
  181. return OS_ANDROID;
  182. } else if (browserDetails.isIOS()) {
  183. return OS_IOS;
  184. } else if (browserDetails.isWindows()) {
  185. return OS_WINDOWS;
  186. } else if (browserDetails.isLinux()) {
  187. return OS_LINUX;
  188. } else if (browserDetails.isMacOSX()) {
  189. return OS_MACOSX;
  190. }
  191. // Unknown OS
  192. return null;
  193. }
  194. public boolean isIE() {
  195. return browserDetails.isIE();
  196. }
  197. public boolean isFirefox() {
  198. return browserDetails.isFirefox();
  199. }
  200. public boolean isSafari() {
  201. return browserDetails.isSafari();
  202. }
  203. public boolean isIE8() {
  204. return isIE() && getBrowserMajorVersion() == 8;
  205. }
  206. public boolean isIE9() {
  207. return isIE() && getBrowserMajorVersion() == 9;
  208. }
  209. public boolean isIE10() {
  210. return isIE() && getBrowserMajorVersion() == 10;
  211. }
  212. public boolean isChrome() {
  213. return browserDetails.isChrome();
  214. }
  215. public boolean isGecko() {
  216. return browserDetails.isGecko();
  217. }
  218. public boolean isWebkit() {
  219. return browserDetails.isWebKit();
  220. }
  221. /**
  222. * Returns the Gecko version if the browser is Gecko based. The Gecko
  223. * version for Firefox 2 is 1.8 and 1.9 for Firefox 3.
  224. *
  225. * @return The Gecko version or -1 if the browser is not Gecko based
  226. */
  227. public float getGeckoVersion() {
  228. if (!browserDetails.isGecko()) {
  229. return -1;
  230. }
  231. return browserDetails.getBrowserEngineVersion();
  232. }
  233. /**
  234. * Returns the WebKit version if the browser is WebKit based. The WebKit
  235. * version returned is the major version e.g., 523.
  236. *
  237. * @return The WebKit version or -1 if the browser is not WebKit based
  238. */
  239. public float getWebkitVersion() {
  240. if (!browserDetails.isWebKit()) {
  241. return -1;
  242. }
  243. return browserDetails.getBrowserEngineVersion();
  244. }
  245. public float getIEVersion() {
  246. if (!browserDetails.isIE()) {
  247. return -1;
  248. }
  249. return getBrowserMajorVersion();
  250. }
  251. public float getOperaVersion() {
  252. if (!browserDetails.isOpera()) {
  253. return -1;
  254. }
  255. return getBrowserMajorVersion();
  256. }
  257. public boolean isOpera() {
  258. return browserDetails.isOpera();
  259. }
  260. public boolean isOpera10() {
  261. return browserDetails.isOpera() && getBrowserMajorVersion() == 10;
  262. }
  263. public boolean isOpera11() {
  264. return browserDetails.isOpera() && getBrowserMajorVersion() == 11;
  265. }
  266. public native static String getBrowserString()
  267. /*-{
  268. return $wnd.navigator.userAgent;
  269. }-*/;
  270. public native int getScreenWidth()
  271. /*-{
  272. return $wnd.screen.width;
  273. }-*/;
  274. public native int getScreenHeight()
  275. /*-{
  276. return $wnd.screen.height;
  277. }-*/;
  278. /**
  279. * @return true if the browser runs on a touch based device.
  280. */
  281. public boolean isTouchDevice() {
  282. return touchDevice;
  283. }
  284. /**
  285. * Indicates whether the browser might require juggling to properly update
  286. * sizes inside elements with overflow: auto.
  287. *
  288. * @return <code>true</code> if the browser requires the workaround,
  289. * otherwise <code>false</code>
  290. */
  291. public boolean requiresOverflowAutoFix() {
  292. return (getWebkitVersion() > 0 || getOperaVersion() >= 11
  293. || getIEVersion() >= 10 || isFirefox())
  294. && Util.getNativeScrollbarSize() > 0;
  295. }
  296. /**
  297. * Checks if the browser is run on iOS
  298. *
  299. * @return true if the browser is run on iOS, false otherwise
  300. */
  301. public boolean isIOS() {
  302. return browserDetails.isIOS();
  303. }
  304. /**
  305. * Checks if the browser is run on Android
  306. *
  307. * @return true if the browser is run on Android, false otherwise
  308. */
  309. public boolean isAndroid() {
  310. return browserDetails.isAndroid();
  311. }
  312. /**
  313. * Checks if the browser is capable of handling scrolling natively or if a
  314. * touch scroll helper is needed for scrolling.
  315. *
  316. * @return true if browser needs a touch scroll helper, false if the browser
  317. * can handle scrolling natively
  318. */
  319. public boolean requiresTouchScrollDelegate() {
  320. if (!isTouchDevice()) {
  321. return false;
  322. }
  323. // TODO Should test other Android browsers, especially Chrome
  324. if (isAndroid() && isWebkit() && getWebkitVersion() >= 534) {
  325. return false;
  326. }
  327. // iOS 6 Safari supports native scrolling; iOS 5 suffers from #8792
  328. // TODO Should test other iOS browsers
  329. if (isIOS() && isSafari() && getBrowserMajorVersion() >= 6) {
  330. return false;
  331. }
  332. return true;
  333. }
  334. /**
  335. * Tests if this is an Android devices with a broken scrollTop
  336. * implementation
  337. *
  338. * @return true if scrollTop cannot be trusted on this device, false
  339. * otherwise
  340. */
  341. public boolean isAndroidWithBrokenScrollTop() {
  342. return isAndroid()
  343. && (getOperatingSystemMajorVersion() == 3 || getOperatingSystemMajorVersion() == 4);
  344. }
  345. private int getOperatingSystemMajorVersion() {
  346. return browserDetails.getOperatingSystemMajorVersion();
  347. }
  348. /**
  349. * Returns the browser major version e.g., 3 for Firefox 3.5, 4 for Chrome
  350. * 4, 8 for Internet Explorer 8.
  351. * <p>
  352. * Note that Internet Explorer 8 and newer will return the document mode so
  353. * IE8 rendering as IE7 will return 7.
  354. * </p>
  355. *
  356. * @return The major version of the browser.
  357. */
  358. public int getBrowserMajorVersion() {
  359. return browserDetails.getBrowserMajorVersion();
  360. }
  361. /**
  362. * Returns the browser minor version e.g., 5 for Firefox 3.5.
  363. *
  364. * @see #getBrowserMajorVersion()
  365. *
  366. * @return The minor version of the browser, or -1 if not known/parsed.
  367. */
  368. public int getBrowserMinorVersion() {
  369. return browserDetails.getBrowserMinorVersion();
  370. }
  371. /**
  372. * Checks if the browser version is newer or equal to the given major+minor
  373. * version.
  374. *
  375. * @param majorVersion
  376. * The major version to check for
  377. * @param minorVersion
  378. * The minor version to check for
  379. * @return true if the browser version is newer or equal to the given
  380. * version
  381. */
  382. public boolean isBrowserVersionNewerOrEqual(int majorVersion,
  383. int minorVersion) {
  384. if (getBrowserMajorVersion() == majorVersion) {
  385. // Same major
  386. return (getBrowserMinorVersion() >= minorVersion);
  387. }
  388. // Older or newer major
  389. return (getBrowserMajorVersion() > majorVersion);
  390. }
  391. }