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

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