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.asserts');
28goog.require('goog.labs.userAgent.util');
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 return goog.labs.userAgent.util.matchUserAgent('Android') &&
89 !goog.labs.userAgent.util.matchUserAgent('Chrome') &&
90 !goog.labs.userAgent.util.matchUserAgent('CriOS');
91};
92
93
94/**
95 * @return {boolean} Whether the user's browser is Opera.
96 */
97goog.labs.userAgent.browser.isOpera = goog.labs.userAgent.browser.matchOpera_;
98
99
100/**
101 * @return {boolean} Whether the user's browser is IE.
102 */
103goog.labs.userAgent.browser.isIE = goog.labs.userAgent.browser.matchIE_;
104
105
106/**
107 * @return {boolean} Whether the user's browser is Firefox.
108 */
109goog.labs.userAgent.browser.isFirefox =
110 goog.labs.userAgent.browser.matchFirefox_;
111
112
113/**
114 * @return {boolean} Whether the user's browser is Safari.
115 */
116goog.labs.userAgent.browser.isSafari =
117 goog.labs.userAgent.browser.matchSafari_;
118
119
120/**
121 * @return {boolean} Whether the user's browser is Chrome.
122 */
123goog.labs.userAgent.browser.isChrome =
124 goog.labs.userAgent.browser.matchChrome_;
125
126
127/**
128 * @return {boolean} Whether the user's browser is the Android browser.
129 */
130goog.labs.userAgent.browser.isAndroidBrowser =
131 goog.labs.userAgent.browser.matchAndroidBrowser_;
132
133
134/**
135 * For more information, see:
136 * http://docs.aws.amazon.com/silk/latest/developerguide/user-agent.html
137 * @return {boolean} Whether the user's browser is Silk.
138 */
139goog.labs.userAgent.browser.isSilk = function() {
140 return goog.labs.userAgent.util.matchUserAgent('Silk');
141};
142
143
144/**
145 * @return {string} The browser version or empty string if version cannot be
146 * determined. Note that for Internet Explorer, this returns the version of
147 * the browser, not the version of the rendering engine. (IE 8 in
148 * compatibility mode will return 8.0 rather than 7.0. To determine the
149 * rendering engine version, look at document.documentMode instead. See
150 * http://msdn.microsoft.com/en-us/library/cc196988(v=vs.85).aspx for more
151 * details.)
152 */
153goog.labs.userAgent.browser.getVersion = function() {
154 var userAgentString = goog.labs.userAgent.util.getUserAgent();
155 // Special case IE since IE's version is inside the parenthesis and
156 // without the '/'.
157 if (goog.labs.userAgent.browser.isIE()) {
158 return goog.labs.userAgent.browser.getIEVersion_(userAgentString);
159 }
160
161 if (goog.labs.userAgent.browser.isOpera()) {
162 return goog.labs.userAgent.browser.getOperaVersion_(userAgentString);
163 }
164
165 var versionTuples =
166 goog.labs.userAgent.util.extractVersionTuples(userAgentString);
167 return goog.labs.userAgent.browser.getVersionFromTuples_(versionTuples);
168};
169
170
171/**
172 * @param {string|number} version The version to check.
173 * @return {boolean} Whether the browser version is higher or the same as the
174 * given version.
175 */
176goog.labs.userAgent.browser.isVersionOrHigher = function(version) {
177 return goog.string.compareVersions(goog.labs.userAgent.browser.getVersion(),
178 version) >= 0;
179};
180
181
182/**
183 * Determines IE version. More information:
184 * http://msdn.microsoft.com/en-us/library/ie/bg182625(v=vs.85).aspx#uaString
185 * http://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx
186 * http://blogs.msdn.com/b/ie/archive/2010/03/23/introducing-ie9-s-user-agent-string.aspx
187 * http://blogs.msdn.com/b/ie/archive/2009/01/09/the-internet-explorer-8-user-agent-string-updated-edition.aspx
188 *
189 * @param {string} userAgent the User-Agent.
190 * @return {string}
191 * @private
192 */
193goog.labs.userAgent.browser.getIEVersion_ = function(userAgent) {
194 // IE11 may identify itself as MSIE 9.0 or MSIE 10.0 due to an IE 11 upgrade
195 // bug. Example UA:
196 // Mozilla/5.0 (MSIE 9.0; Windows NT 6.1; WOW64; Trident/7.0; rv:11.0)
197 // like Gecko.
198 // See http://www.whatismybrowser.com/developers/unknown-user-agent-fragments.
199 var rv = /rv: *([\d\.]*)/.exec(userAgent);
200 if (rv && rv[1]) {
201 return rv[1];
202 }
203
204 var version = '';
205 var msie = /MSIE +([\d\.]+)/.exec(userAgent);
206 if (msie && msie[1]) {
207 // IE in compatibility mode usually identifies itself as MSIE 7.0; in this
208 // case, use the Trident version to determine the version of IE. For more
209 // details, see the links above.
210 var tridentVersion = /Trident\/(\d.\d)/.exec(userAgent);
211 if (msie[1] == '7.0') {
212 if (tridentVersion && tridentVersion[1]) {
213 switch (tridentVersion[1]) {
214 case '4.0':
215 version = '8.0';
216 break;
217 case '5.0':
218 version = '9.0';
219 break;
220 case '6.0':
221 version = '10.0';
222 break;
223 case '7.0':
224 version = '11.0';
225 break;
226 }
227 } else {
228 version = '7.0';
229 }
230 } else {
231 version = msie[1];
232 }
233 }
234 return version;
235};
236
237
238/**
239 * Determines Opera version. More information:
240 * http://my.opera.com/ODIN/blog/2013/07/15/opera-user-agent-strings-opera-15-and-beyond
241 *
242 * @param {string} userAgent The User-Agent.
243 * @return {string}
244 * @private
245 */
246goog.labs.userAgent.browser.getOperaVersion_ = function(userAgent) {
247 var versionTuples =
248 goog.labs.userAgent.util.extractVersionTuples(userAgent);
249 var lastTuple = goog.array.peek(versionTuples);
250 if (lastTuple[0] == 'OPR' && lastTuple[1]) {
251 return lastTuple[1];
252 }
253
254 return goog.labs.userAgent.browser.getVersionFromTuples_(versionTuples);
255};
256
257
258/**
259 * Nearly all User-Agents start with Mozilla/N.0. This looks at the second tuple
260 * for the actual browser version number.
261 * @param {!Array.<!Array.<string>>} versionTuples
262 * @return {string} The version or empty string if it cannot be determined.
263 * @private
264 */
265goog.labs.userAgent.browser.getVersionFromTuples_ = function(versionTuples) {
266 // versionTuples[2] (The first X/Y tuple after the parenthesis) contains the
267 // browser version number.
268 goog.asserts.assert(versionTuples.length > 2,
269 'Couldn\'t extract version tuple from user agent string');
270 return versionTuples[2] && versionTuples[2][1] ? versionTuples[2][1] : '';
271};