Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

VBrowserDetails.java 21KB

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