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

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