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

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