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

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