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

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