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.

VBrowserDetails.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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.shared;
  17. import java.io.Serializable;
  18. /**
  19. * Class that parses the user agent string from the browser and provides
  20. * information about the browser. Used internally by
  21. * {@link com.vaadin.client.BrowserInfo} and
  22. * {@link com.vaadin.server.WebBrowser}. Should not be used directly.
  23. *
  24. * @author Vaadin Ltd.
  25. * @since 6.3
  26. */
  27. public class VBrowserDetails implements Serializable {
  28. private boolean isGecko = false;
  29. private boolean isWebKit = false;
  30. private boolean isPresto = false;
  31. private boolean isTrident = false;
  32. private boolean isChromeFrameCapable = false;
  33. private boolean isChromeFrame = false;
  34. private boolean isSafari = false;
  35. private boolean isChrome = false;
  36. private boolean isFirefox = false;
  37. private boolean isOpera = false;
  38. private boolean isIE = false;
  39. private boolean isWindowsPhone;
  40. private boolean isIPad;
  41. private boolean isIPhone;
  42. private OperatingSystem os = OperatingSystem.UNKNOWN;
  43. public enum OperatingSystem {
  44. UNKNOWN, WINDOWS, MACOSX, LINUX, IOS, ANDROID;
  45. }
  46. private float browserEngineVersion = -1;
  47. private int browserMajorVersion = -1;
  48. private int browserMinorVersion = -1;
  49. private int osMajorVersion = -1;
  50. private int osMinorVersion = -1;
  51. /**
  52. * Create an instance based on the given user agent.
  53. *
  54. * @param userAgent
  55. * User agent as provided by the browser.
  56. */
  57. public VBrowserDetails(String userAgent) {
  58. userAgent = userAgent.toLowerCase();
  59. // browser engine name
  60. isGecko = userAgent.indexOf("gecko") != -1
  61. && userAgent.indexOf("webkit") == -1
  62. && userAgent.indexOf("trident/") == -1;
  63. isPresto = userAgent.indexOf(" presto/") != -1;
  64. isTrident = userAgent.indexOf("trident/") != -1;
  65. isWebKit = !isTrident && userAgent.indexOf("applewebkit") != -1;
  66. // browser name
  67. isChrome = userAgent.indexOf(" chrome/") != -1;
  68. isOpera = userAgent.indexOf("opera") != -1;
  69. isIE = userAgent.indexOf("msie") != -1 && !isOpera
  70. && (userAgent.indexOf("webtv") == -1);
  71. // IE 11 no longer contains MSIE in the user agent
  72. isIE = isIE || isTrident;
  73. isSafari = !isChrome && !isIE && userAgent.indexOf("safari") != -1;
  74. isFirefox = userAgent.indexOf(" firefox/") != -1;
  75. // chromeframe
  76. isChromeFrameCapable = userAgent.indexOf("chromeframe") != -1;
  77. isChromeFrame = isChromeFrameCapable && !isChrome;
  78. // Rendering engine version
  79. try {
  80. if (isGecko) {
  81. int rvPos = userAgent.indexOf("rv:");
  82. if (rvPos >= 0) {
  83. String tmp = userAgent.substring(rvPos + 3);
  84. tmp = tmp.replaceFirst("(\\.[0-9]+).+", "$1");
  85. browserEngineVersion = Float.parseFloat(tmp);
  86. }
  87. } else if (isWebKit) {
  88. String tmp = userAgent
  89. .substring(userAgent.indexOf("webkit/") + 7);
  90. tmp = tmp.replaceFirst("([0-9]+)[^0-9].+", "$1");
  91. browserEngineVersion = Float.parseFloat(tmp);
  92. } else if (isIE) {
  93. int tridentPos = userAgent.indexOf("trident/");
  94. if (tridentPos >= 0) {
  95. String tmp = userAgent.substring(tridentPos
  96. + "Trident/".length());
  97. tmp = tmp.replaceFirst("([0-9]+\\.[0-9]+).*", "$1");
  98. browserEngineVersion = Float.parseFloat(tmp);
  99. }
  100. }
  101. } catch (Exception e) {
  102. // Browser engine version parsing failed
  103. System.err.println("Browser engine version parsing failed for: "
  104. + userAgent);
  105. }
  106. // Browser version
  107. try {
  108. if (isIE) {
  109. if (userAgent.indexOf("msie") == -1) {
  110. // IE 11+
  111. int rvPos = userAgent.indexOf("rv:");
  112. if (rvPos >= 0) {
  113. String tmp = userAgent.substring(rvPos + 3);
  114. tmp = tmp.replaceFirst("(\\.[0-9]+).+", "$1");
  115. parseVersionString(tmp);
  116. }
  117. } else {
  118. String ieVersionString = userAgent.substring(userAgent
  119. .indexOf("msie ") + 5);
  120. ieVersionString = safeSubstring(ieVersionString, 0,
  121. ieVersionString.indexOf(";"));
  122. parseVersionString(ieVersionString);
  123. }
  124. } else if (isFirefox) {
  125. int i = userAgent.indexOf(" firefox/") + 9;
  126. parseVersionString(safeSubstring(userAgent, i, i + 5));
  127. } else if (isChrome) {
  128. int i = userAgent.indexOf(" chrome/") + 8;
  129. parseVersionString(safeSubstring(userAgent, i, i + 5));
  130. } else if (isSafari) {
  131. int i = userAgent.indexOf(" version/") + 9;
  132. parseVersionString(safeSubstring(userAgent, i, i + 5));
  133. } else if (isOpera) {
  134. int i = userAgent.indexOf(" version/");
  135. if (i != -1) {
  136. // Version present in Opera 10 and newer
  137. i += 9; // " version/".length
  138. } else {
  139. i = userAgent.indexOf("opera/") + 6;
  140. }
  141. parseVersionString(safeSubstring(userAgent, i, i + 5));
  142. }
  143. } catch (Exception e) {
  144. // Browser version parsing failed
  145. System.err.println("Browser version parsing failed for: "
  146. + userAgent);
  147. }
  148. // Operating system
  149. if (userAgent.contains("windows ")) {
  150. os = OperatingSystem.WINDOWS;
  151. isWindowsPhone = userAgent.contains("windows phone");
  152. } else if (userAgent.contains("android")) {
  153. os = OperatingSystem.ANDROID;
  154. parseAndroidVersion(userAgent);
  155. } else if (userAgent.contains("linux")) {
  156. os = OperatingSystem.LINUX;
  157. } else if (userAgent.contains("macintosh")
  158. || userAgent.contains("mac osx")
  159. || userAgent.contains("mac os x")) {
  160. isIPad = userAgent.contains("ipad");
  161. isIPhone = userAgent.contains("iphone");
  162. if (isIPad || userAgent.contains("ipod") || isIPhone) {
  163. os = OperatingSystem.IOS;
  164. parseIOSVersion(userAgent);
  165. } else {
  166. os = OperatingSystem.MACOSX;
  167. }
  168. }
  169. }
  170. private void parseAndroidVersion(String userAgent) {
  171. // Android 5.1;
  172. if (!userAgent.contains("android")) {
  173. return;
  174. }
  175. String osVersionString = safeSubstring(userAgent,
  176. userAgent.indexOf("android ") + "android ".length(),
  177. userAgent.length());
  178. osVersionString = safeSubstring(osVersionString, 0,
  179. osVersionString.indexOf(";"));
  180. String[] parts = osVersionString.split("\\.");
  181. parseOsVersion(parts);
  182. }
  183. private void parseIOSVersion(String userAgent) {
  184. // OS 5_1 like Mac OS X
  185. if (!userAgent.contains("os ") || !userAgent.contains(" like mac")) {
  186. return;
  187. }
  188. String osVersionString = safeSubstring(userAgent,
  189. userAgent.indexOf("os ") + 3, userAgent.indexOf(" like mac"));
  190. String[] parts = osVersionString.split("_");
  191. parseOsVersion(parts);
  192. }
  193. private void parseOsVersion(String[] parts) {
  194. osMajorVersion = -1;
  195. osMinorVersion = -1;
  196. if (parts.length >= 1) {
  197. try {
  198. osMajorVersion = Integer.parseInt(parts[0]);
  199. } catch (Exception e) {
  200. }
  201. }
  202. if (parts.length >= 2) {
  203. try {
  204. osMinorVersion = Integer.parseInt(parts[1]);
  205. } catch (Exception e) {
  206. }
  207. // Some Androids report version numbers as "2.1-update1"
  208. if (osMinorVersion == -1 && parts[1].contains("-")) {
  209. try {
  210. osMinorVersion = Integer.parseInt(parts[1].substring(0,
  211. parts[1].indexOf('-')));
  212. } catch (Exception ee) {
  213. }
  214. }
  215. }
  216. }
  217. private void parseVersionString(String versionString) {
  218. int idx = versionString.indexOf('.');
  219. if (idx < 0) {
  220. idx = versionString.length();
  221. }
  222. browserMajorVersion = Integer.parseInt(safeSubstring(versionString, 0,
  223. idx));
  224. int idx2 = versionString.indexOf('.', idx + 1);
  225. if (idx2 < 0) {
  226. idx2 = versionString.length();
  227. }
  228. try {
  229. browserMinorVersion = Integer.parseInt(safeSubstring(versionString,
  230. idx + 1, idx2).replaceAll("[^0-9].*", ""));
  231. } catch (NumberFormatException e) {
  232. // leave the minor version unmodified (-1 = unknown)
  233. }
  234. }
  235. private String safeSubstring(String string, int beginIndex, int endIndex) {
  236. if (beginIndex < 0) {
  237. beginIndex = 0;
  238. }
  239. if (endIndex < 0 || endIndex > string.length()) {
  240. endIndex = string.length();
  241. }
  242. return string.substring(beginIndex, endIndex);
  243. }
  244. /**
  245. * Tests if the browser is Firefox.
  246. *
  247. * @return true if it is Firefox, false otherwise
  248. */
  249. public boolean isFirefox() {
  250. return isFirefox;
  251. }
  252. /**
  253. * Tests if the browser is using the Gecko engine
  254. *
  255. * @return true if it is Gecko, false otherwise
  256. */
  257. public boolean isGecko() {
  258. return isGecko;
  259. }
  260. /**
  261. * Tests if the browser is using the WebKit engine
  262. *
  263. * @return true if it is WebKit, false otherwise
  264. */
  265. public boolean isWebKit() {
  266. return isWebKit;
  267. }
  268. /**
  269. * Tests if the browser is using the Presto engine
  270. *
  271. * @return true if it is Presto, false otherwise
  272. */
  273. public boolean isPresto() {
  274. return isPresto;
  275. }
  276. /**
  277. * Tests if the browser is using the Trident engine
  278. *
  279. * @since 7.1.7
  280. * @return true if it is Trident, false otherwise
  281. */
  282. public boolean isTrident() {
  283. return isTrident;
  284. }
  285. /**
  286. * Tests if the browser is Safari.
  287. *
  288. * @return true if it is Safari, false otherwise
  289. */
  290. public boolean isSafari() {
  291. return isSafari;
  292. }
  293. /**
  294. * Tests if the browser is Chrome.
  295. *
  296. * @return true if it is Chrome, false otherwise
  297. */
  298. public boolean isChrome() {
  299. return isChrome;
  300. }
  301. /**
  302. * Tests if the browser is capable of running ChromeFrame.
  303. *
  304. * @return true if it has ChromeFrame, false otherwise
  305. */
  306. public boolean isChromeFrameCapable() {
  307. return isChromeFrameCapable;
  308. }
  309. /**
  310. * Tests if the browser is running ChromeFrame.
  311. *
  312. * @return true if it is ChromeFrame, false otherwise
  313. */
  314. public boolean isChromeFrame() {
  315. return isChromeFrame;
  316. }
  317. /**
  318. * Tests if the browser is Opera.
  319. *
  320. * @return true if it is Opera, false otherwise
  321. */
  322. public boolean isOpera() {
  323. return isOpera;
  324. }
  325. /**
  326. * Tests if the browser is Internet Explorer.
  327. *
  328. * @return true if it is Internet Explorer, false otherwise
  329. */
  330. public boolean isIE() {
  331. return isIE;
  332. }
  333. /**
  334. * Returns the version of the browser engine. For WebKit this is an integer
  335. * e.g., 532.0. For gecko it is a float e.g., 1.8 or 1.9.
  336. *
  337. * @return The version of the browser engine
  338. */
  339. public float getBrowserEngineVersion() {
  340. return browserEngineVersion;
  341. }
  342. /**
  343. * Returns the browser major version e.g., 3 for Firefox 3.5, 4 for Chrome
  344. * 4, 8 for Internet Explorer 8.
  345. * <p>
  346. * Note that Internet Explorer 8 and newer will return the document mode so
  347. * IE8 rendering as IE7 will return 7.
  348. * </p>
  349. *
  350. * @return The major version of the browser.
  351. */
  352. public final int getBrowserMajorVersion() {
  353. return browserMajorVersion;
  354. }
  355. /**
  356. * Returns the browser minor version e.g., 5 for Firefox 3.5.
  357. *
  358. * @see #getBrowserMajorVersion()
  359. *
  360. * @return The minor version of the browser, or -1 if not known/parsed.
  361. */
  362. public final int getBrowserMinorVersion() {
  363. return browserMinorVersion;
  364. }
  365. /**
  366. * Sets the version for IE based on the documentMode. This is used to return
  367. * the correct the correct IE version when the version from the user agent
  368. * string and the value of the documentMode property do not match.
  369. *
  370. * @param documentMode
  371. * The current document mode
  372. */
  373. public void setIEMode(int documentMode) {
  374. browserMajorVersion = documentMode;
  375. browserMinorVersion = 0;
  376. }
  377. /**
  378. * Tests if the browser is run on Windows.
  379. *
  380. * @return true if run on Windows, false otherwise
  381. */
  382. public boolean isWindows() {
  383. return os == OperatingSystem.WINDOWS;
  384. }
  385. /**
  386. * Tests if the browser is run on Windows Phone.
  387. *
  388. * @return true if run on Windows Phone, false otherwise
  389. * @since 7.3.2
  390. */
  391. public boolean isWindowsPhone() {
  392. return isWindowsPhone;
  393. }
  394. /**
  395. * Tests if the browser is run on Mac OSX.
  396. *
  397. * @return true if run on Mac OSX, false otherwise
  398. */
  399. public boolean isMacOSX() {
  400. return os == OperatingSystem.MACOSX;
  401. }
  402. /**
  403. * Tests if the browser is run on Linux.
  404. *
  405. * @return true if run on Linux, false otherwise
  406. */
  407. public boolean isLinux() {
  408. return os == OperatingSystem.LINUX;
  409. }
  410. /**
  411. * Tests if the browser is run on Android.
  412. *
  413. * @return true if run on Android, false otherwise
  414. */
  415. public boolean isAndroid() {
  416. return os == OperatingSystem.ANDROID;
  417. }
  418. /**
  419. * Tests if the browser is run in iOS.
  420. *
  421. * @return true if run in iOS, false otherwise
  422. */
  423. public boolean isIOS() {
  424. return os == OperatingSystem.IOS;
  425. }
  426. /**
  427. * Tests if the browser is run on iPhone.
  428. *
  429. * @return true if run on iPhone, false otherwise
  430. * @since 7.3.3
  431. */
  432. public boolean isIPhone() {
  433. return isIPhone;
  434. }
  435. /**
  436. * Tests if the browser is run on iPad.
  437. *
  438. * @return true if run on iPad, false otherwise
  439. * @since 7.3.3
  440. */
  441. public boolean isIPad() {
  442. return isIPad;
  443. }
  444. /**
  445. * Returns the major version of the operating system. Currently only
  446. * supported for mobile devices (iOS/Android)
  447. *
  448. * @return The major version or -1 if unknown
  449. */
  450. public int getOperatingSystemMajorVersion() {
  451. return osMajorVersion;
  452. }
  453. /**
  454. * Returns the minor version of the operating system. Currently only
  455. * supported for mobile devices (iOS/Android)
  456. *
  457. * @return The minor version or -1 if unknown
  458. */
  459. public int getOperatingSystemMinorVersion() {
  460. return osMinorVersion;
  461. }
  462. /**
  463. * Checks if the browser is so old that it simply won't work with a Vaadin
  464. * application. NOTE that the browser might still be capable of running
  465. * Crome Frame, so you might still want to check
  466. * {@link #isChromeFrameCapable()} if this returns true.
  467. *
  468. * @return true if the browser won't work, false if not the browser is
  469. * supported or might work
  470. */
  471. public boolean isTooOldToFunctionProperly() {
  472. // Check Trident version to detect compatibility mode
  473. if (isIE() && getBrowserMajorVersion() < 8
  474. && getBrowserEngineVersion() < 4) {
  475. return true;
  476. }
  477. // Webkit 533 in Safari 4.1+, Android 2.2+, iOS 4+
  478. if (isSafari() && getBrowserEngineVersion() < 533) {
  479. return true;
  480. }
  481. if (isFirefox() && getBrowserMajorVersion() < 4) {
  482. return true;
  483. }
  484. if (isOpera() && getBrowserMajorVersion() < 11) {
  485. return true;
  486. }
  487. return false;
  488. }
  489. }