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