Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

VBrowserDetails.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 boolean isWindows = false;
  28. private boolean isMacOSX = false;
  29. private boolean isLinux = false;
  30. private float browserEngineVersion = -1;
  31. private int browserMajorVersion = -1;
  32. private int browserMinorVersion = -1;
  33. /**
  34. * Create an instance based on the given user agent.
  35. *
  36. * @param userAgent
  37. * User agent as provided by the browser.
  38. */
  39. public VBrowserDetails(String userAgent) {
  40. userAgent = userAgent.toLowerCase();
  41. // browser engine name
  42. isGecko = userAgent.indexOf("gecko") != -1
  43. && userAgent.indexOf("webkit") == -1;
  44. isWebKit = userAgent.indexOf("applewebkit") != -1;
  45. isPresto = userAgent.indexOf(" presto/") != -1;
  46. // browser name
  47. isChrome = userAgent.indexOf(" chrome/") != -1;
  48. isSafari = !isChrome && userAgent.indexOf("safari") != -1;
  49. isOpera = userAgent.indexOf("opera") != -1;
  50. isIE = userAgent.indexOf("msie") != -1 && !isOpera
  51. && (userAgent.indexOf("webtv") == -1);
  52. isFirefox = userAgent.indexOf(" firefox/") != -1;
  53. // chromeframe
  54. isChromeFrameCapable = userAgent.indexOf("chromeframe") != -1;
  55. isChromeFrame = isChromeFrameCapable && !isIE;
  56. // Rendering engine version
  57. try {
  58. if (isGecko) {
  59. int rvPos = userAgent.indexOf("rv:");
  60. if (rvPos >= 0) {
  61. String tmp = userAgent.substring(rvPos + 3);
  62. tmp = tmp.replaceFirst("(\\.[0-9]+).+", "$1");
  63. browserEngineVersion = Float.parseFloat(tmp);
  64. }
  65. } else if (isWebKit) {
  66. String tmp = userAgent
  67. .substring(userAgent.indexOf("webkit/") + 7);
  68. tmp = tmp.replaceFirst("([0-9]+)[^0-9].+", "$1");
  69. browserEngineVersion = Float.parseFloat(tmp);
  70. }
  71. } catch (Exception e) {
  72. // Browser engine version parsing failed
  73. System.err.println("Browser engine version parsing failed for: "
  74. + userAgent);
  75. }
  76. // Browser version
  77. try {
  78. if (isIE) {
  79. String ieVersionString = userAgent.substring(userAgent
  80. .indexOf("msie ") + 5);
  81. ieVersionString = safeSubstring(ieVersionString, 0,
  82. ieVersionString.indexOf(";"));
  83. parseVersionString(ieVersionString);
  84. } else if (isFirefox) {
  85. int i = userAgent.indexOf(" firefox/") + 9;
  86. parseVersionString(safeSubstring(userAgent, i, i + 5));
  87. } else if (isChrome) {
  88. int i = userAgent.indexOf(" chrome/") + 8;
  89. parseVersionString(safeSubstring(userAgent, i, i + 5));
  90. } else if (isSafari) {
  91. int i = userAgent.indexOf(" version/") + 9;
  92. parseVersionString(safeSubstring(userAgent, i, i + 5));
  93. } else if (isOpera) {
  94. int i = userAgent.indexOf(" version/");
  95. if (i != -1) {
  96. // Version present in Opera 10 and newer
  97. i += 9; // " version/".length
  98. } else {
  99. i = userAgent.indexOf("opera/") + 6;
  100. }
  101. parseVersionString(safeSubstring(userAgent, i, i + 5));
  102. }
  103. } catch (Exception e) {
  104. // Browser version parsing failed
  105. System.err.println("Browser version parsing failed for: "
  106. + userAgent);
  107. }
  108. // Operating system
  109. if (userAgent.contains("windows ")) {
  110. isWindows = true;
  111. } else if (userAgent.contains("linux")) {
  112. isLinux = true;
  113. } else if (userAgent.contains("macintosh")
  114. || userAgent.contains("mac osx")
  115. || userAgent.contains("mac os x")) {
  116. isMacOSX = true;
  117. }
  118. }
  119. private void parseVersionString(String versionString) {
  120. int idx = versionString.indexOf('.');
  121. if (idx < 0) {
  122. idx = versionString.length();
  123. }
  124. browserMajorVersion = Integer.parseInt(safeSubstring(versionString, 0,
  125. idx));
  126. int idx2 = versionString.indexOf('.', idx + 1);
  127. if (idx2 < 0) {
  128. idx2 = versionString.length();
  129. }
  130. try {
  131. browserMinorVersion = Integer.parseInt(safeSubstring(versionString,
  132. idx + 1, idx2).replaceAll("[^0-9].*", ""));
  133. } catch (NumberFormatException e) {
  134. // leave the minor version unmodified (-1 = unknown)
  135. }
  136. }
  137. private String safeSubstring(String string, int beginIndex, int endIndex) {
  138. if (beginIndex < 0) {
  139. beginIndex = 0;
  140. }
  141. if (endIndex < 0 || endIndex > string.length()) {
  142. endIndex = string.length();
  143. }
  144. return string.substring(beginIndex, endIndex);
  145. }
  146. /**
  147. * Tests if the browser is Firefox.
  148. *
  149. * @return true if it is Firefox, false otherwise
  150. */
  151. public boolean isFirefox() {
  152. return isFirefox;
  153. }
  154. /**
  155. * Tests if the browser is using the Gecko engine
  156. *
  157. * @return true if it is Gecko, false otherwise
  158. */
  159. public boolean isGecko() {
  160. return isGecko;
  161. }
  162. /**
  163. * Tests if the browser is using the WebKit engine
  164. *
  165. * @return true if it is WebKit, false otherwise
  166. */
  167. public boolean isWebKit() {
  168. return isWebKit;
  169. }
  170. /**
  171. * Tests if the browser is using the Presto engine
  172. *
  173. * @return true if it is Presto, false otherwise
  174. */
  175. public boolean isPresto() {
  176. return isPresto;
  177. }
  178. /**
  179. * Tests if the browser is Safari.
  180. *
  181. * @return true if it is Safari, false otherwise
  182. */
  183. public boolean isSafari() {
  184. return isSafari;
  185. }
  186. /**
  187. * Tests if the browser is Chrome.
  188. *
  189. * @return true if it is Chrome, false otherwise
  190. */
  191. public boolean isChrome() {
  192. return isChrome;
  193. }
  194. /**
  195. * Tests if the browser is capable of running ChromeFrame.
  196. *
  197. * @return true if it has ChromeFrame, false otherwise
  198. */
  199. public boolean isChromeFrameCapable() {
  200. return isChromeFrameCapable;
  201. }
  202. /**
  203. * Tests if the browser is running ChromeFrame.
  204. *
  205. * @return true if it is ChromeFrame, false otherwise
  206. */
  207. public boolean isChromeFrame() {
  208. return isChromeFrame;
  209. }
  210. /**
  211. * Tests if the browser is Opera.
  212. *
  213. * @return true if it is Opera, false otherwise
  214. */
  215. public boolean isOpera() {
  216. return isOpera;
  217. }
  218. /**
  219. * Tests if the browser is Internet Explorer.
  220. *
  221. * @return true if it is Internet Explorer, false otherwise
  222. */
  223. public boolean isIE() {
  224. return isIE;
  225. }
  226. /**
  227. * Returns the version of the browser engine. For WebKit this is an integer
  228. * e.g., 532.0. For gecko it is a float e.g., 1.8 or 1.9.
  229. *
  230. * @return The version of the browser engine
  231. */
  232. public float getBrowserEngineVersion() {
  233. return browserEngineVersion;
  234. }
  235. /**
  236. * Returns the browser major version e.g., 3 for Firefox 3.5, 4 for Chrome
  237. * 4, 8 for Internet Explorer 8.
  238. * <p>
  239. * Note that Internet Explorer 8 and newer will return the document mode so
  240. * IE8 rendering as IE7 will return 7.
  241. * </p>
  242. *
  243. * @return The major version of the browser.
  244. */
  245. public final int getBrowserMajorVersion() {
  246. return browserMajorVersion;
  247. }
  248. /**
  249. * Returns the browser minor version e.g., 5 for Firefox 3.5.
  250. *
  251. * @see #getBrowserMajorVersion()
  252. *
  253. * @return The minor version of the browser, or -1 if not known/parsed.
  254. */
  255. public final int getBrowserMinorVersion() {
  256. return browserMinorVersion;
  257. }
  258. /**
  259. * Sets the version for IE based on the documentMode. This is used to return
  260. * the correct the correct IE version when the version from the user agent
  261. * string and the value of the documentMode property do not match.
  262. *
  263. * @param documentMode
  264. * The current document mode
  265. */
  266. public void setIEMode(int documentMode) {
  267. browserMajorVersion = documentMode;
  268. browserMinorVersion = 0;
  269. }
  270. /**
  271. * Tests if the browser is run on Windows.
  272. *
  273. * @return true if run on Windows, false otherwise
  274. */
  275. public boolean isWindows() {
  276. return isWindows;
  277. }
  278. /**
  279. * Tests if the browser is run on Mac OSX.
  280. *
  281. * @return true if run on Mac OSX, false otherwise
  282. */
  283. public boolean isMacOSX() {
  284. return isMacOSX;
  285. }
  286. /**
  287. * Tests if the browser is run on Linux.
  288. *
  289. * @return true if run on Linux, false otherwise
  290. */
  291. public boolean isLinux() {
  292. return isLinux;
  293. }
  294. /**
  295. * Checks if the browser is so old that it simply won't work with a Vaadin
  296. * application. NOTE that the browser might still be capable of running
  297. * Crome Frame, so you might still want to check
  298. * {@link #isChromeFrameCapable()} if this returns true.
  299. *
  300. * @return true if the browser won't work, false if not the browser is
  301. * supported or might work
  302. */
  303. public boolean isTooOldToFunctionProperly() {
  304. if (isIE() && getBrowserMajorVersion() < 8) {
  305. return true;
  306. }
  307. if (isSafari() && getBrowserMajorVersion() < 5) {
  308. return true;
  309. }
  310. if (isFirefox() && getBrowserMajorVersion() < 4) {
  311. return true;
  312. }
  313. if (isOpera() && getBrowserMajorVersion() < 11) {
  314. return true;
  315. }
  316. return false;
  317. }
  318. }