Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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