Debian Codegraph @

closure-library/closure/goog/labs/useragent/browser.js

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