lib/goog/labs/useragent/browser.js

1// Copyright 2013 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 Closure user agent detection (Browser).
17 * @see <a href="http://www.useragentstring.com/">User agent strings</a>
18 * For more information on rendering engine, platform, or device see the other
19 * sub-namespaces in goog.labs.userAgent, goog.labs.userAgent.platform,
20 * goog.labs.userAgent.device respectively.)
21 *
22 */
23
24goog.provide('goog.labs.userAgent.browser');
25
26goog.require('goog.array');
27goog.require('goog.labs.userAgent.util');
28goog.require('goog.object');
29goog.require('goog.string');
30
31
32/**
33 * @return {boolean} Whether the user's browser is Opera.
34 * @private
35 */
36goog.labs.userAgent.browser.matchOpera_ = function() {
37 return goog.labs.userAgent.util.matchUserAgent('Opera') ||
38 goog.labs.userAgent.util.matchUserAgent('OPR');
39};
40
41
42/**
43 * @return {boolean} Whether the user's browser is IE.
44 * @private
45 */
46goog.labs.userAgent.browser.matchIE_ = function() {
47 return goog.labs.userAgent.util.matchUserAgent('Trident') ||
48 goog.labs.userAgent.util.matchUserAgent('MSIE');
49};
50
51
52/**
53 * @return {boolean} Whether the user's browser is Firefox.
54 * @private
55 */
56goog.labs.userAgent.browser.matchFirefox_ = function() {
57 return goog.labs.userAgent.util.matchUserAgent('Firefox');
58};
59
60
61/**
62 * @return {boolean} Whether the user's browser is Safari.
63 * @private
64 */
65goog.labs.userAgent.browser.matchSafari_ = function() {
66 return goog.labs.userAgent.util.matchUserAgent('Safari') &&
67 !goog.labs.userAgent.util.matchUserAgent('Chrome') &&
68 !goog.labs.userAgent.util.matchUserAgent('CriOS') &&
69 !goog.labs.userAgent.util.matchUserAgent('Android');
70};
71
72
73/**
74 * @return {boolean} Whether the user's browser is Chrome.
75 * @private
76 */
77goog.labs.userAgent.browser.matchChrome_ = function() {
78 return goog.labs.userAgent.util.matchUserAgent('Chrome') ||
79 goog.labs.userAgent.util.matchUserAgent('CriOS');
80};
81
82
83/**
84 * @return {boolean} Whether the user's browser is the Android browser.
85 * @private
86 */
87goog.labs.userAgent.browser.matchAndroidBrowser_ = function() {
88 // Android can appear in the user agent string for Chrome on Android.
89 // This is not the Android standalone browser if it does.
90 return !goog.labs.userAgent.browser.isChrome() &&
91 goog.labs.userAgent.util.matchUserAgent('Android');
92
93};
94
95
96/**
97 * @return {boolean} Whether the user's browser is Opera.
98 */
99goog.labs.userAgent.browser.isOpera = goog.labs.userAgent.browser.matchOpera_;
100
101
102/**
103 * @return {boolean} Whether the user's browser is IE.
104 */
105goog.labs.userAgent.browser.isIE = goog.labs.userAgent.browser.matchIE_;
106
107
108/**
109 * @return {boolean} Whether the user's browser is Firefox.
110 */
111goog.labs.userAgent.browser.isFirefox =
112 goog.labs.userAgent.browser.matchFirefox_;
113
114
115/**
116 * @return {boolean} Whether the user's browser is Safari.
117 */
118goog.labs.userAgent.browser.isSafari =
119 goog.labs.userAgent.browser.matchSafari_;
120
121
122/**
123 * @return {boolean} Whether the user's browser is Chrome.
124 */
125goog.labs.userAgent.browser.isChrome =
126 goog.labs.userAgent.browser.matchChrome_;
127
128
129/**
130 * @return {boolean} Whether the user's browser is the Android browser.
131 */
132goog.labs.userAgent.browser.isAndroidBrowser =
133 goog.labs.userAgent.browser.matchAndroidBrowser_;
134
135
136/**
137 * For more information, see:
138 * http://docs.aws.amazon.com/silk/latest/developerguide/user-agent.html
139 * @return {boolean} Whether the user's browser is Silk.
140 */
141goog.labs.userAgent.browser.isSilk = function() {
142 return goog.labs.userAgent.util.matchUserAgent('Silk');
143};
144
145
146/**
147 * @return {string} The browser version or empty string if version cannot be
148 * determined. Note that for Internet Explorer, this returns the version of
149 * the browser, not the version of the rendering engine. (IE 8 in
150 * compatibility mode will return 8.0 rather than 7.0. To determine the
151 * rendering engine version, look at document.documentMode instead. See
152 * http://msdn.microsoft.com/en-us/library/cc196988(v=vs.85).aspx for more
153 * details.)
154 */
155goog.labs.userAgent.browser.getVersion = function() {
156 var userAgentString = goog.labs.userAgent.util.getUserAgent();
157 // Special case IE since IE's version is inside the parenthesis and
158 // without the '/'.
159 if (goog.labs.userAgent.browser.isIE()) {
160 return goog.labs.userAgent.browser.getIEVersion_(userAgentString);
161 }
162
163 var versionTuples = goog.labs.userAgent.util.extractVersionTuples(
164 userAgentString);
165
166 // Construct a map for easy lookup.
167 var versionMap = {};
168 goog.array.forEach(versionTuples, function(tuple) {
169 // Note that the tuple is of length three, but we only care about the
170 // first two.
171 var key = tuple[0];
172 var value = tuple[1];
173 versionMap[key] = value;
174 });
175
176 var versionMapHasKey = goog.partial(goog.object.containsKey, versionMap);
177
178 // Gives the value with the first key it finds, otherwise empty string.
179 function lookUpValueWithKeys(keys) {
180 var key = goog.array.find(keys, versionMapHasKey);
181 return versionMap[key] || '';
182 }
183
184 // Check Opera before Chrome since Opera 15+ has "Chrome" in the string.
185 // See
186 // http://my.opera.com/ODIN/blog/2013/07/15/opera-user-agent-strings-opera-15-and-beyond
187 if (goog.labs.userAgent.browser.isOpera()) {
188 // Opera 10 has Version/10.0 but Opera/9.8, so look for "Version" first.
189 // Opera uses 'OPR' for more recent UAs.
190 return lookUpValueWithKeys(['Version', 'Opera', 'OPR']);
191 }
192
193 if (goog.labs.userAgent.browser.isChrome()) {
194 return lookUpValueWithKeys(['Chrome', 'CriOS']);
195 }
196
197 // Usually products browser versions are in the third tuple after "Mozilla"
198 // and the engine.
199 var tuple = versionTuples[2];
200 return tuple && tuple[1] || '';
201};
202
203
204/**
205 * @param {string|number} version The version to check.
206 * @return {boolean} Whether the browser version is higher or the same as the
207 * given version.
208 */
209goog.labs.userAgent.browser.isVersionOrHigher = function(version) {
210 return goog.string.compareVersions(goog.labs.userAgent.browser.getVersion(),
211 version) >= 0;
212};
213
214
215/**
216 * Determines IE version. More information:
217 * http://msdn.microsoft.com/en-us/library/ie/bg182625(v=vs.85).aspx#uaString
218 * http://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx
219 * http://blogs.msdn.com/b/ie/archive/2010/03/23/introducing-ie9-s-user-agent-string.aspx
220 * http://blogs.msdn.com/b/ie/archive/2009/01/09/the-internet-explorer-8-user-agent-string-updated-edition.aspx
221 *
222 * @param {string} userAgent the User-Agent.
223 * @return {string}
224 * @private
225 */
226goog.labs.userAgent.browser.getIEVersion_ = function(userAgent) {
227 // IE11 may identify itself as MSIE 9.0 or MSIE 10.0 due to an IE 11 upgrade
228 // bug. Example UA:
229 // Mozilla/5.0 (MSIE 9.0; Windows NT 6.1; WOW64; Trident/7.0; rv:11.0)
230 // like Gecko.
231 // See http://www.whatismybrowser.com/developers/unknown-user-agent-fragments.
232 var rv = /rv: *([\d\.]*)/.exec(userAgent);
233 if (rv && rv[1]) {
234 return rv[1];
235 }
236
237 var version = '';
238 var msie = /MSIE +([\d\.]+)/.exec(userAgent);
239 if (msie && msie[1]) {
240 // IE in compatibility mode usually identifies itself as MSIE 7.0; in this
241 // case, use the Trident version to determine the version of IE. For more
242 // details, see the links above.
243 var tridentVersion = /Trident\/(\d.\d)/.exec(userAgent);
244 if (msie[1] == '7.0') {
245 if (tridentVersion && tridentVersion[1]) {
246 switch (tridentVersion[1]) {
247 case '4.0':
248 version = '8.0';
249 break;
250 case '5.0':
251 version = '9.0';
252 break;
253 case '6.0':
254 version = '10.0';
255 break;
256 case '7.0':
257 version = '11.0';
258 break;
259 }
260 } else {
261 version = '7.0';
262 }
263 } else {
264 version = msie[1];
265 }
266 }
267 return version;
268};