diff options
4 files changed, 107 insertions, 14 deletions
diff --git a/client/src/main/java/com/vaadin/client/BrowserInfo.java b/client/src/main/java/com/vaadin/client/BrowserInfo.java index 60be48a7c4..2799ecec07 100644 --- a/client/src/main/java/com/vaadin/client/BrowserInfo.java +++ b/client/src/main/java/com/vaadin/client/BrowserInfo.java @@ -510,6 +510,19 @@ public class BrowserInfo { } /** + * Gets the complete browser version in form of a string. The version is + * given by the browser through the user agent string and usually consists + * of dot-separated numbers. Note that the string may contain characters + * other than dots and digits. + * + * @return the complete browser version or {@code null} if unknown + * @since + */ + public String getBrowserVersion() { + return browserDetails.getBrowserVersion(); + } + + /** * Checks if the browser version is newer or equal to the given major+minor * version. * diff --git a/client/src/test/java/com/vaadin/client/VBrowserDetailsUserAgentParserTest.java b/client/src/test/java/com/vaadin/client/VBrowserDetailsUserAgentParserTest.java index 7c3167110a..6acefadc61 100644 --- a/client/src/test/java/com/vaadin/client/VBrowserDetailsUserAgentParserTest.java +++ b/client/src/test/java/com/vaadin/client/VBrowserDetailsUserAgentParserTest.java @@ -70,6 +70,7 @@ public class VBrowserDetailsUserAgentParserTest { assertSafari(bd); assertBrowserMajorVersion(bd, 3); assertBrowserMinorVersion(bd, 2); + assertBrowserVersion(bd, "3.2.3"); assertEngineVersion(bd, 525.0f); assertWindows(bd); } @@ -81,6 +82,7 @@ public class VBrowserDetailsUserAgentParserTest { assertSafari(bd); assertBrowserMajorVersion(bd, 4); assertBrowserMinorVersion(bd, 0); + assertBrowserVersion(bd, "4.0.5"); assertEngineVersion(bd, 531f); assertMacOSX(bd); } @@ -106,6 +108,7 @@ public class VBrowserDetailsUserAgentParserTest { assertSafari(bd); assertBrowserMajorVersion(bd, 5); assertBrowserMinorVersion(bd, 1); + assertBrowserVersion(bd, "5.1"); assertEngineVersion(bd, 534f); assertIOS(bd, 5, 1); assertIPhone(bd); @@ -118,6 +121,7 @@ public class VBrowserDetailsUserAgentParserTest { assertSafari(bd); assertBrowserMajorVersion(bd, 4); assertBrowserMinorVersion(bd, 0); + assertBrowserVersion(bd, "4.0.5"); assertEngineVersion(bd, 532f); assertIOS(bd, 4, 0); assertIPhone(bd); @@ -142,6 +146,7 @@ public class VBrowserDetailsUserAgentParserTest { assertSafari(bd); assertBrowserMajorVersion(bd, 4); assertBrowserMinorVersion(bd, 0); + assertBrowserVersion(bd, "4.0"); assertEngineVersion(bd, 530f); assertAndroid(bd, 2, 1); @@ -154,6 +159,7 @@ public class VBrowserDetailsUserAgentParserTest { assertSafari(bd); assertBrowserMajorVersion(bd, 4); assertBrowserMinorVersion(bd, 0); + assertBrowserVersion(bd, "4.0"); assertEngineVersion(bd, 533f); assertAndroid(bd, 2, 2); } @@ -165,6 +171,7 @@ public class VBrowserDetailsUserAgentParserTest { assertSafari(bd); assertBrowserMajorVersion(bd, 4); assertBrowserMinorVersion(bd, 0); + assertBrowserVersion(bd, "4.0"); assertEngineVersion(bd, 534f); assertAndroid(bd, 3, 0); } @@ -177,6 +184,7 @@ public class VBrowserDetailsUserAgentParserTest { assertChrome(bd); assertBrowserMajorVersion(bd, 18); assertBrowserMinorVersion(bd, 0); + assertBrowserVersion(bd, "18.0.1025.133"); assertEngineVersion(bd, 535f); assertAndroid(bd, 4, 0); } @@ -196,6 +204,7 @@ public class VBrowserDetailsUserAgentParserTest { assertChrome(bd); assertBrowserMajorVersion(bd, 3); assertBrowserMinorVersion(bd, 0); + assertBrowserVersion(bd, "3.0.198"); assertEngineVersion(bd, 532.0f); assertMacOSX(bd); } @@ -207,6 +216,7 @@ public class VBrowserDetailsUserAgentParserTest { assertChrome(bd); assertBrowserMajorVersion(bd, 4); assertBrowserMinorVersion(bd, 0); + assertBrowserVersion(bd, "4.0.249.89"); assertEngineVersion(bd, 532f); assertWindows(bd); } @@ -218,6 +228,7 @@ public class VBrowserDetailsUserAgentParserTest { assertChrome(bd); assertBrowserMajorVersion(bd, 40); assertBrowserMinorVersion(bd, 0); + assertBrowserVersion(bd, "40.0.2214.38"); assertEngineVersion(bd, 537f); assertChromeOS(bd); } @@ -229,6 +240,7 @@ public class VBrowserDetailsUserAgentParserTest { assertChrome(bd); assertBrowserMajorVersion(bd, 57); assertBrowserMinorVersion(bd, 0); + assertBrowserVersion(bd, "57.0.2987.137"); assertEngineVersion(bd, 602f); assertIOS(bd, 10, 3); } @@ -240,6 +252,7 @@ public class VBrowserDetailsUserAgentParserTest { assertFirefox(bd); assertBrowserMajorVersion(bd, 3); assertBrowserMinorVersion(bd, 0); + assertBrowserVersion(bd, "3.0.6"); assertEngineVersion(bd, 1.9f); assertWindows(bd); @@ -248,6 +261,7 @@ public class VBrowserDetailsUserAgentParserTest { assertFirefox(bd); assertBrowserMajorVersion(bd, 3); assertBrowserMinorVersion(bd, 0); + assertBrowserVersion(bd, "3.0.12"); assertEngineVersion(bd, 1.9f); assertLinux(bd); } @@ -259,6 +273,7 @@ public class VBrowserDetailsUserAgentParserTest { assertFirefox(bd); assertBrowserMajorVersion(bd, 33); assertBrowserMinorVersion(bd, 0); + assertBrowserVersion(bd, "33.0"); assertAndroid(bd, -1, -1); } @@ -279,6 +294,7 @@ public class VBrowserDetailsUserAgentParserTest { assertFirefox(bd); assertBrowserMajorVersion(bd, 3); assertBrowserMinorVersion(bd, 5); + assertBrowserVersion(bd, "3.5.8"); assertEngineVersion(bd, 1.9f); assertWindows(bd); } @@ -290,6 +306,7 @@ public class VBrowserDetailsUserAgentParserTest { assertFirefox(bd); assertBrowserMajorVersion(bd, 3); assertBrowserMinorVersion(bd, 6); + assertBrowserVersion(bd, "3.6"); assertEngineVersion(bd, 1.9f); assertWindows(bd); } @@ -301,6 +318,7 @@ public class VBrowserDetailsUserAgentParserTest { assertFirefox(bd); assertBrowserMajorVersion(bd, 3); assertBrowserMinorVersion(bd, 0); + assertBrowserVersion(bd, "3.0b5"); assertEngineVersion(bd, 1.9f); assertMacOSX(bd); } @@ -312,6 +330,7 @@ public class VBrowserDetailsUserAgentParserTest { assertFirefox(bd); assertBrowserMajorVersion(bd, 4); assertBrowserMinorVersion(bd, 0); + assertBrowserVersion(bd, "4.0b11"); assertEngineVersion(bd, 2.0f); assertWindows(bd); } @@ -323,6 +342,7 @@ public class VBrowserDetailsUserAgentParserTest { assertFirefox(bd); assertBrowserMajorVersion(bd, 4); assertBrowserMinorVersion(bd, 0); + assertBrowserVersion(bd, "4.0b7"); assertEngineVersion(bd, 2.0f); assertWindows(bd); } @@ -341,6 +361,7 @@ public class VBrowserDetailsUserAgentParserTest { assertFirefox(bd); assertBrowserMajorVersion(bd, 3); assertBrowserMinorVersion(bd, 6); + assertBrowserVersion(bd, "3.6"); assertEngineVersion(bd, 1.9f); assertMacOSX(bd); } @@ -352,6 +373,7 @@ public class VBrowserDetailsUserAgentParserTest { assertOpera(bd); assertBrowserMajorVersion(bd, 9); assertBrowserMinorVersion(bd, 64); + assertBrowserVersion(bd, "9.64"); assertWindows(bd); } @@ -362,6 +384,7 @@ public class VBrowserDetailsUserAgentParserTest { assertOpera(bd); assertBrowserMajorVersion(bd, 10); assertBrowserMinorVersion(bd, 10); + assertBrowserVersion(bd, "10.10"); assertWindows(bd); } @@ -372,6 +395,7 @@ public class VBrowserDetailsUserAgentParserTest { assertOpera(bd); assertBrowserMajorVersion(bd, 10); assertBrowserMinorVersion(bd, 50); + assertBrowserVersion(bd, "10.50"); assertWindows(bd); } @@ -383,6 +407,7 @@ public class VBrowserDetailsUserAgentParserTest { assertIE(bd); assertBrowserMajorVersion(bd, 11); assertBrowserMinorVersion(bd, 0); + assertBrowserVersion(bd, "11.0"); assertWindows(bd); } @@ -395,6 +420,7 @@ public class VBrowserDetailsUserAgentParserTest { assertIE(bd); assertBrowserMajorVersion(bd, 11); assertBrowserMinorVersion(bd, 0); + assertBrowserVersion(bd, "11.0"); assertWindows(bd); } @@ -407,6 +433,7 @@ public class VBrowserDetailsUserAgentParserTest { assertIE(bd); assertBrowserMajorVersion(bd, 11); assertBrowserMinorVersion(bd, 0); + assertBrowserVersion(bd, "11.0"); assertWindows(bd); } @@ -419,6 +446,7 @@ public class VBrowserDetailsUserAgentParserTest { assertIE(bd); assertBrowserMajorVersion(bd, 11); assertBrowserMinorVersion(bd, 0); + assertBrowserVersion(bd, "11.0"); assertWindows(bd); } @@ -430,6 +458,7 @@ public class VBrowserDetailsUserAgentParserTest { assertIE(bd); assertBrowserMajorVersion(bd, 11); assertBrowserMinorVersion(bd, 0); + assertBrowserVersion(bd, "11.0"); assertWindows(bd, true); } @@ -439,6 +468,7 @@ public class VBrowserDetailsUserAgentParserTest { assertEdge(bd); assertBrowserMajorVersion(bd, 12); assertBrowserMinorVersion(bd, 10240); + assertBrowserVersion(bd, "12.10240"); assertWindows(bd, false); } @@ -449,6 +479,7 @@ public class VBrowserDetailsUserAgentParserTest { assertWebKit(bd); assertBrowserMajorVersion(bd, 2); assertBrowserMinorVersion(bd, 1); + assertBrowserVersion(bd, "2.1.1"); assertMacOSX(bd); } @@ -467,6 +498,7 @@ public class VBrowserDetailsUserAgentParserTest { assertFirefox(details); assertBrowserMajorVersion(details, 8); assertBrowserMinorVersion(details, 3); + assertBrowserVersion(details, "8.3b5826"); assertWebKit(details); assertEngineVersion(details, 601); } @@ -477,6 +509,7 @@ public class VBrowserDetailsUserAgentParserTest { assertSafari(details); assertBrowserMajorVersion(details, 9); assertBrowserMinorVersion(details, 0); + assertBrowserVersion(details, "9.0"); assertWebKit(details); assertEngineVersion(details, 601); } @@ -487,6 +520,7 @@ public class VBrowserDetailsUserAgentParserTest { assertChrome(details); assertBrowserMajorVersion(details, 63); assertBrowserMinorVersion(details, 0); + assertBrowserVersion(details, "63.0.3239.73"); assertWebKit(details); assertEngineVersion(details, 601); } @@ -513,6 +547,11 @@ public class VBrowserDetailsUserAgentParserTest { } + private void assertBrowserVersion(VBrowserDetails browserDetails, + String version) { + assertEquals(version, browserDetails.getBrowserVersion()); + } + private void assertGecko(VBrowserDetails browserDetails) { // Engine assertTrue(browserDetails.isGecko()); diff --git a/server/src/main/java/com/vaadin/server/WebBrowser.java b/server/src/main/java/com/vaadin/server/WebBrowser.java index 0a5e6f2dc4..6622a0e90f 100644 --- a/server/src/main/java/com/vaadin/server/WebBrowser.java +++ b/server/src/main/java/com/vaadin/server/WebBrowser.java @@ -247,6 +247,20 @@ public class WebBrowser implements Serializable { } /** + * Gets the complete browser version as string. The version is given by the + * browser through the user agent string and usually consists of + * dot-separated numbers. Note that the string may contain characters other + * than dots and digits. + * + * @return the complete browser version or {@code null} if unknown + * @since + */ + public String getBrowserVersion() { + return browserDetails != null ? browserDetails.getBrowserVersion() + : null; + } + + /** * Tests whether the user is using Linux. * * @return true if the user is using Linux, false if the user is not using diff --git a/shared/src/main/java/com/vaadin/shared/VBrowserDetails.java b/shared/src/main/java/com/vaadin/shared/VBrowserDetails.java index b33e82c1a3..20e3071d0e 100644 --- a/shared/src/main/java/com/vaadin/shared/VBrowserDetails.java +++ b/shared/src/main/java/com/vaadin/shared/VBrowserDetails.java @@ -18,6 +18,8 @@ package com.vaadin.shared; import java.io.Serializable; import java.util.Locale; +import com.google.gwt.regexp.shared.RegExp; + /** * Class that parses the user agent string from the browser and provides * information about the browser. Used internally by @@ -59,6 +61,7 @@ public class VBrowserDetails implements Serializable { private float browserEngineVersion = -1; private int browserMajorVersion = -1; private int browserMinorVersion = -1; + private String browserVersion; private int osMajorVersion = -1; private int osMinorVersion = -1; @@ -143,25 +146,27 @@ public class VBrowserDetails implements Serializable { // Browser version try { + RegExp versionRegExp = RegExp.compile("[\\w\\.]*", "g"); if (isIE) { if (userAgent.indexOf("msie") == -1) { // IE 11+ int rvPos = userAgent.indexOf("rv:"); if (rvPos >= 0) { - String tmp = userAgent.substring(rvPos + 3); - tmp = tmp.replaceFirst("(\\.[0-9]+).+", "$1"); - parseVersionString(tmp); + int i = rvPos + "rv:".length(); + versionRegExp.setLastIndex(i); + browserVersion = versionRegExp.exec(userAgent) + .getGroup(0); + parseVersionString(browserVersion); } } else if (isTrident) { // See // https://msdn.microsoft.com/en-us/library/ms537503(v=vs.85).aspx#TriToken setIEMode((int) browserEngineVersion + 4); } else { - String ieVersionString = userAgent - .substring(userAgent.indexOf("msie ") + 5); - ieVersionString = safeSubstring(ieVersionString, 0, - ieVersionString.indexOf(";")); - parseVersionString(ieVersionString); + int i = userAgent.indexOf("msie ") + 5; + versionRegExp.setLastIndex(i); + browserVersion = versionRegExp.exec(userAgent).getGroup(0); + parseVersionString(browserVersion); } } else if (isFirefox) { int i = userAgent.indexOf(" firefox/"); @@ -170,7 +175,9 @@ public class VBrowserDetails implements Serializable { } else { i = userAgent.indexOf(" fxios/") + " fxios/".length(); } - parseVersionString(safeSubstring(userAgent, i, i + 5)); + versionRegExp.setLastIndex(i); + browserVersion = versionRegExp.exec(userAgent).getGroup(0); + parseVersionString(browserVersion); } else if (isChrome) { int i = userAgent.indexOf(" chrome/"); if (i != -1) { @@ -178,10 +185,14 @@ public class VBrowserDetails implements Serializable { } else { i = userAgent.indexOf(" crios/") + " crios/".length(); } - parseVersionString(safeSubstring(userAgent, i, i + 5)); + versionRegExp.setLastIndex(i); + browserVersion = versionRegExp.exec(userAgent).getGroup(0); + parseVersionString(browserVersion); } else if (isSafari) { int i = userAgent.indexOf(" version/") + 9; - parseVersionString(safeSubstring(userAgent, i, i + 5)); + versionRegExp.setLastIndex(i); + browserVersion = versionRegExp.exec(userAgent).getGroup(0); + parseVersionString(browserVersion); } else if (isOpera) { int i = userAgent.indexOf(" version/"); if (i != -1) { @@ -190,14 +201,20 @@ public class VBrowserDetails implements Serializable { } else { i = userAgent.indexOf("opera/") + 6; } - parseVersionString(safeSubstring(userAgent, i, i + 5)); + versionRegExp.setLastIndex(i); + browserVersion = versionRegExp.exec(userAgent).getGroup(0); + parseVersionString(browserVersion); } else if (isEdge) { int i = userAgent.indexOf(" edge/") + 6; - parseVersionString(safeSubstring(userAgent, i, i + 8)); + versionRegExp.setLastIndex(i); + browserVersion = versionRegExp.exec(userAgent).getGroup(0); + parseVersionString(browserVersion); } else if (isPhantomJS) { String prefix = " phantomjs/"; int i = userAgent.indexOf(prefix) + prefix.length(); - parseVersionString(safeSubstring(userAgent, i, i + 5)); + versionRegExp.setLastIndex(i); + browserVersion = versionRegExp.exec(userAgent).getGroup(0); + parseVersionString(browserVersion); } } catch (Exception e) { // Browser version parsing failed @@ -521,6 +538,15 @@ public class VBrowserDetails implements Serializable { } /** + * Gets the complete browser version as string. + * + * @return the complete browser version or {@code null} if unknown + */ + public final String getBrowserVersion() { + return browserVersion; + } + + /** * Sets the version for IE based on the documentMode. This is used to return * the correct the correct IE version when the version from the user agent * string and the value of the documentMode property do not match. @@ -531,6 +557,7 @@ public class VBrowserDetails implements Serializable { public void setIEMode(int documentMode) { browserMajorVersion = documentMode; browserMinorVersion = 0; + browserVersion = browserMajorVersion + "." + browserMinorVersion; } /** |