Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

VBrowserDetails.java 16KB

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