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

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