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