# 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.labs.userAgent.util');28goog.require('goog.object');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  // Android can appear in the user agent string for Chrome on Android.89  // This is not the Android standalone browser if it does.90  return !goog.labs.userAgent.browser.isChrome() &&91      goog.labs.userAgent.util.matchUserAgent('Android');9293};949596/**97 * @return {boolean} Whether the user's browser is Opera.98 */99goog.labs.userAgent.browser.isOpera = goog.labs.userAgent.browser.matchOpera_;100101102/**103 * @return {boolean} Whether the user's browser is IE.104 */105goog.labs.userAgent.browser.isIE = goog.labs.userAgent.browser.matchIE_;106107108/**109 * @return {boolean} Whether the user's browser is Firefox.110 */111goog.labs.userAgent.browser.isFirefox =112    goog.labs.userAgent.browser.matchFirefox_;113114115/**116 * @return {boolean} Whether the user's browser is Safari.117 */118goog.labs.userAgent.browser.isSafari =119    goog.labs.userAgent.browser.matchSafari_;120121122/**123 * @return {boolean} Whether the user's browser is Chrome.124 */125goog.labs.userAgent.browser.isChrome =126    goog.labs.userAgent.browser.matchChrome_;127128129/**130 * @return {boolean} Whether the user's browser is the Android browser.131 */132goog.labs.userAgent.browser.isAndroidBrowser =133    goog.labs.userAgent.browser.matchAndroidBrowser_;134135136/**137 * For more information, see:138 * http://docs.aws.amazon.com/silk/latest/developerguide/user-agent.html139 * @return {boolean} Whether the user's browser is Silk.140 */141goog.labs.userAgent.browser.isSilk = function() {142  return goog.labs.userAgent.util.matchUserAgent('Silk');143};144145146/**147 * @return {string} The browser version or empty string if version cannot be148 *     determined. Note that for Internet Explorer, this returns the version of149 *     the browser, not the version of the rendering engine. (IE 8 in150 *     compatibility mode will return 8.0 rather than 7.0. To determine the151 *     rendering engine version, look at document.documentMode instead. See152 *     http://msdn.microsoft.com/en-us/library/cc196988(v=vs.85).aspx for more153 *     details.)154 */155goog.labs.userAgent.browser.getVersion = function() {156  var userAgentString = goog.labs.userAgent.util.getUserAgent();157  // Special case IE since IE's version is inside the parenthesis and158  // without the '/'.159  if (goog.labs.userAgent.browser.isIE()) {160    return goog.labs.userAgent.browser.getIEVersion_(userAgentString);161  }162163  var versionTuples = goog.labs.userAgent.util.extractVersionTuples(164      userAgentString);165166  // Construct a map for easy lookup.167  var versionMap = {};168  goog.array.forEach(versionTuples, function(tuple) {169    // Note that the tuple is of length three, but we only care about the170    // first two.171    var key = tuple[0];172    var value = tuple[1];173    versionMap[key] = value;174  });175176  var versionMapHasKey = goog.partial(goog.object.containsKey, versionMap);177178  // Gives the value with the first key it finds, otherwise empty string.179  function lookUpValueWithKeys(keys) {180    var key = goog.array.find(keys, versionMapHasKey);181    return versionMap[key] || '';182  }183184  // Check Opera before Chrome since Opera 15+ has "Chrome" in the string.185  // See186  // http://my.opera.com/ODIN/blog/2013/07/15/opera-user-agent-strings-opera-15-and-beyond187  if (goog.labs.userAgent.browser.isOpera()) {188    // Opera 10 has Version/10.0 but Opera/9.8, so look for "Version" first.189    // Opera uses 'OPR' for more recent UAs.190    return lookUpValueWithKeys(['Version', 'Opera', 'OPR']);191  }192193  if (goog.labs.userAgent.browser.isChrome()) {194    return lookUpValueWithKeys(['Chrome', 'CriOS']);195  }196197  // Usually products browser versions are in the third tuple after "Mozilla"198  // and the engine.199  var tuple = versionTuples[2];200  return tuple && tuple[1] || '';201};202203204/**205 * @param {string|number} version The version to check.206 * @return {boolean} Whether the browser version is higher or the same as the207 *     given version.208 */209goog.labs.userAgent.browser.isVersionOrHigher = function(version) {210  return goog.string.compareVersions(goog.labs.userAgent.browser.getVersion(),211                                     version) >= 0;212};213214215/**216 * Determines IE version. More information:217 * http://msdn.microsoft.com/en-us/library/ie/bg182625(v=vs.85).aspx#uaString218 * http://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx219 * http://blogs.msdn.com/b/ie/archive/2010/03/23/introducing-ie9-s-user-agent-string.aspx220 * http://blogs.msdn.com/b/ie/archive/2009/01/09/the-internet-explorer-8-user-agent-string-updated-edition.aspx221 *222 * @param {string} userAgent the User-Agent.223 * @return {string}224 * @private225 */226goog.labs.userAgent.browser.getIEVersion_ = function(userAgent) {227  // IE11 may identify itself as MSIE 9.0 or MSIE 10.0 due to an IE 11 upgrade228  // bug. Example UA:229  // Mozilla/5.0 (MSIE 9.0; Windows NT 6.1; WOW64; Trident/7.0; rv:11.0)230  // like Gecko.231  // See http://www.whatismybrowser.com/developers/unknown-user-agent-fragments.232  var rv = /rv: *([\d\.]*)/.exec(userAgent);233  if (rv && rv[1]) {234    return rv[1];235  }236237  var version = '';238  var msie = /MSIE +([\d\.]+)/.exec(userAgent);239  if (msie && msie[1]) {240    // IE in compatibility mode usually identifies itself as MSIE 7.0; in this241    // case, use the Trident version to determine the version of IE. For more242    // details, see the links above.243    var tridentVersion = /Trident\/(\d.\d)/.exec(userAgent);244    if (msie[1] == '7.0') {245      if (tridentVersion && tridentVersion[1]) {246        switch (tridentVersion[1]) {247          case '4.0':248            version = '8.0';249            break;250          case '5.0':251            version = '9.0';252            break;253          case '6.0':254            version = '10.0';255            break;256          case '7.0':257            version = '11.0';258            break;259        }260      } else {261        version = '7.0';262      }263    } else {264      version = msie[1];265    }266  }267  return version;268};