# 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 at6//7//      http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// 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 and13// limitations under the License.1415/**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 other19 * sub-namespaces in goog.labs.userAgent, goog.labs.userAgent.platform,20 * goog.labs.userAgent.device respectively.)21 *22 */2324goog.provide('goog.labs.userAgent.browser');2526goog.require('goog.array');27goog.require('goog.asserts');28goog.require('goog.labs.userAgent.util');29goog.require('goog.string');303132/**33 * @return {boolean} Whether the user's browser is Opera.34 * @private35 */36goog.labs.userAgent.browser.matchOpera_ = function() {37  return goog.labs.userAgent.util.matchUserAgent('Opera') ||38      goog.labs.userAgent.util.matchUserAgent('OPR');39};404142/**43 * @return {boolean} Whether the user's browser is IE.44 * @private45 */46goog.labs.userAgent.browser.matchIE_ = function() {47  return goog.labs.userAgent.util.matchUserAgent('Trident') ||48      goog.labs.userAgent.util.matchUserAgent('MSIE');49};505152/**53 * @return {boolean} Whether the user's browser is Firefox.54 * @private55 */56goog.labs.userAgent.browser.matchFirefox_ = function() {57  return goog.labs.userAgent.util.matchUserAgent('Firefox');58};596061/**62 * @return {boolean} Whether the user's browser is Safari.63 * @private64 */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};717273/**74 * @return {boolean} Whether the user's browser is Chrome.75 * @private76 */77goog.labs.userAgent.browser.matchChrome_ = function() {78  return goog.labs.userAgent.util.matchUserAgent('Chrome') ||79      goog.labs.userAgent.util.matchUserAgent('CriOS');80};818283/**84 * @return {boolean} Whether the user's browser is the Android browser.85 * @private86 */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};929394/**95 * @return {boolean} Whether the user's browser is Opera.96 */97goog.labs.userAgent.browser.isOpera = goog.labs.userAgent.browser.matchOpera_;9899100/**101 * @return {boolean} Whether the user's browser is IE.102 */103goog.labs.userAgent.browser.isIE = goog.labs.userAgent.browser.matchIE_;104105106/**107 * @return {boolean} Whether the user's browser is Firefox.108 */109goog.labs.userAgent.browser.isFirefox =110    goog.labs.userAgent.browser.matchFirefox_;111112113/**114 * @return {boolean} Whether the user's browser is Safari.115 */116goog.labs.userAgent.browser.isSafari =117    goog.labs.userAgent.browser.matchSafari_;118119120/**121 * @return {boolean} Whether the user's browser is Chrome.122 */123goog.labs.userAgent.browser.isChrome =124    goog.labs.userAgent.browser.matchChrome_;125126127/**128 * @return {boolean} Whether the user's browser is the Android browser.129 */130goog.labs.userAgent.browser.isAndroidBrowser =131    goog.labs.userAgent.browser.matchAndroidBrowser_;132133134/**135 * For more information, see:136 * http://docs.aws.amazon.com/silk/latest/developerguide/user-agent.html137 * @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};142143144/**145 * @return {string} The browser version or empty string if version cannot be146 *     determined. Note that for Internet Explorer, this returns the version of147 *     the browser, not the version of the rendering engine. (IE 8 in148 *     compatibility mode will return 8.0 rather than 7.0. To determine the149 *     rendering engine version, look at document.documentMode instead. See150 *     http://msdn.microsoft.com/en-us/library/cc196988(v=vs.85).aspx for more151 *     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 and156  // without the '/'.157  if (goog.labs.userAgent.browser.isIE()) {158    return goog.labs.userAgent.browser.getIEVersion_(userAgentString);159  }160161  if (goog.labs.userAgent.browser.isOpera()) {162    return goog.labs.userAgent.browser.getOperaVersion_(userAgentString);163  }164165  var versionTuples =166      goog.labs.userAgent.util.extractVersionTuples(userAgentString);167  return goog.labs.userAgent.browser.getVersionFromTuples_(versionTuples);168};169170171/**172 * @param {string|number} version The version to check.173 * @return {boolean} Whether the browser version is higher or the same as the174 *     given version.175 */176goog.labs.userAgent.browser.isVersionOrHigher = function(version) {177  return goog.string.compareVersions(goog.labs.userAgent.browser.getVersion(),178                                     version) >= 0;179};180181182/**183 * Determines IE version. More information:184 * http://msdn.microsoft.com/en-us/library/ie/bg182625(v=vs.85).aspx#uaString185 * http://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx186 * http://blogs.msdn.com/b/ie/archive/2010/03/23/introducing-ie9-s-user-agent-string.aspx187 * http://blogs.msdn.com/b/ie/archive/2009/01/09/the-internet-explorer-8-user-agent-string-updated-edition.aspx188 *189 * @param {string} userAgent the User-Agent.190 * @return {string}191 * @private192 */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 upgrade195  // 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  }203204  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 this208    // case, use the Trident version to determine the version of IE. For more209    // 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};236237238/**239 * Determines Opera version. More information:240 * http://my.opera.com/ODIN/blog/2013/07/15/opera-user-agent-strings-opera-15-and-beyond241 *242 * @param {string} userAgent The User-Agent.243 * @return {string}244 * @private245 */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  }253254  return goog.labs.userAgent.browser.getVersionFromTuples_(versionTuples);255};256257258/**259 * Nearly all User-Agents start with Mozilla/N.0. This looks at the second tuple260 * for the actual browser version number.261 * @param {!Array.<!Array.<string>>} versionTuples262 * @return {string} The version or empty string if it cannot be determined.263 * @private264 */265goog.labs.userAgent.browser.getVersionFromTuples_ = function(versionTuples) {266  // versionTuples[2] (The first X/Y tuple after the parenthesis) contains the267  // 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};