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

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