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