| 1 | // Copyright 2009 The Closure Library Authors. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | /** |
| 16 | * @fileoverview Functions for understanding the version of the browser. |
| 17 | * This is pulled out of product.js to ensure that only builds that need |
| 18 | * this functionality actually get it, without having to rely on the compiler |
| 19 | * to strip out unneeded pieces. |
| 20 | * |
| 21 | * TODO(nnaze): Move to more appropriate filename/namespace. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | goog.provide('goog.userAgent.product.isVersion'); |
| 27 | |
| 28 | |
| 29 | goog.require('goog.labs.userAgent.platform'); |
| 30 | goog.require('goog.string'); |
| 31 | goog.require('goog.userAgent'); |
| 32 | goog.require('goog.userAgent.product'); |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * @return {string} The string that describes the version number of the user |
| 37 | * agent product. This is a string rather than a number because it may |
| 38 | * contain 'b', 'a', and so on. |
| 39 | * @private |
| 40 | */ |
| 41 | goog.userAgent.product.determineVersion_ = function() { |
| 42 | // All browsers have different ways to detect the version and they all have |
| 43 | // different naming schemes. |
| 44 | |
| 45 | if (goog.userAgent.product.FIREFOX) { |
| 46 | // Firefox/2.0.0.1 or Firefox/3.5.3 |
| 47 | return goog.userAgent.product.getFirstRegExpGroup_(/Firefox\/([0-9.]+)/); |
| 48 | } |
| 49 | |
| 50 | if (goog.userAgent.product.IE || goog.userAgent.product.OPERA) { |
| 51 | return goog.userAgent.VERSION; |
| 52 | } |
| 53 | |
| 54 | if (goog.userAgent.product.CHROME) { |
| 55 | // Chrome/4.0.223.1 |
| 56 | return goog.userAgent.product.getFirstRegExpGroup_(/Chrome\/([0-9.]+)/); |
| 57 | } |
| 58 | |
| 59 | // This replicates legacy logic, which considered Safari and iOS to be |
| 60 | // different products. |
| 61 | if (goog.userAgent.product.SAFARI && !goog.labs.userAgent.platform.isIos()) { |
| 62 | // Version/5.0.3 |
| 63 | // |
| 64 | // NOTE: Before version 3, Safari did not report a product version number. |
| 65 | // The product version number for these browsers will be the empty string. |
| 66 | // They may be differentiated by WebKit version number in goog.userAgent. |
| 67 | return goog.userAgent.product.getFirstRegExpGroup_(/Version\/([0-9.]+)/); |
| 68 | } |
| 69 | |
| 70 | if (goog.userAgent.product.IPHONE || goog.userAgent.product.IPAD) { |
| 71 | // Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 |
| 72 | // (KHTML, like Gecko) Version/3.0 Mobile/3A100a Safari/419.3 |
| 73 | // Version is the browser version, Mobile is the build number. We combine |
| 74 | // the version string with the build number: 3.0.3A100a for the example. |
| 75 | var arr = goog.userAgent.product.execRegExp_( |
| 76 | /Version\/(\S+).*Mobile\/(\S+)/); |
| 77 | if (arr) { |
| 78 | return arr[1] + '.' + arr[2]; |
| 79 | } |
| 80 | } else if (goog.userAgent.product.ANDROID) { |
| 81 | // Mozilla/5.0 (Linux; U; Android 0.5; en-us) AppleWebKit/522+ |
| 82 | // (KHTML, like Gecko) Safari/419.3 |
| 83 | // |
| 84 | // Mozilla/5.0 (Linux; U; Android 1.0; en-us; dream) AppleWebKit/525.10+ |
| 85 | // (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2 |
| 86 | // |
| 87 | // Prefer Version number if present, else make do with the OS number |
| 88 | var version = goog.userAgent.product.getFirstRegExpGroup_( |
| 89 | /Android\s+([0-9.]+)/); |
| 90 | if (version) { |
| 91 | return version; |
| 92 | } |
| 93 | |
| 94 | return goog.userAgent.product.getFirstRegExpGroup_(/Version\/([0-9.]+)/); |
| 95 | } |
| 96 | |
| 97 | return ''; |
| 98 | }; |
| 99 | |
| 100 | |
| 101 | /** |
| 102 | * Return the first group of the given regex. |
| 103 | * @param {!RegExp} re Regular expression with at least one group. |
| 104 | * @return {string} Contents of the first group or an empty string if no match. |
| 105 | * @private |
| 106 | */ |
| 107 | goog.userAgent.product.getFirstRegExpGroup_ = function(re) { |
| 108 | var arr = goog.userAgent.product.execRegExp_(re); |
| 109 | return arr ? arr[1] : ''; |
| 110 | }; |
| 111 | |
| 112 | |
| 113 | /** |
| 114 | * Run regexp's exec() on the userAgent string. |
| 115 | * @param {!RegExp} re Regular expression. |
| 116 | * @return {Array<?>} A result array, or null for no match. |
| 117 | * @private |
| 118 | */ |
| 119 | goog.userAgent.product.execRegExp_ = function(re) { |
| 120 | return re.exec(goog.userAgent.getUserAgentString()); |
| 121 | }; |
| 122 | |
| 123 | |
| 124 | /** |
| 125 | * The version of the user agent. This is a string because it might contain |
| 126 | * 'b' (as in beta) as well as multiple dots. |
| 127 | * @type {string} |
| 128 | */ |
| 129 | goog.userAgent.product.VERSION = goog.userAgent.product.determineVersion_(); |
| 130 | |
| 131 | |
| 132 | /** |
| 133 | * Whether the user agent product version is higher or the same as the given |
| 134 | * version. |
| 135 | * |
| 136 | * @param {string|number} version The version to check. |
| 137 | * @return {boolean} Whether the user agent product version is higher or the |
| 138 | * same as the given version. |
| 139 | */ |
| 140 | goog.userAgent.product.isVersion = function(version) { |
| 141 | return goog.string.compareVersions( |
| 142 | goog.userAgent.product.VERSION, version) >= 0; |
| 143 | }; |