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

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