Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

VBrowserDetails.java 18KB

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