Path: blob/trunk/third_party/closure/goog/labs/useragent/browser.js
2868 views
// Copyright 2013 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Closure user agent detection (Browser).16* @see <a href="http://www.useragentstring.com/">User agent strings</a>17* For more information on rendering engine, platform, or device see the other18* sub-namespaces in goog.labs.userAgent, goog.labs.userAgent.platform,19* goog.labs.userAgent.device respectively.)20*21* @author [email protected] (Andy Martone)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// TODO(nnaze): Refactor to remove excessive exclusion logic in matching33// functions.343536/**37* @return {boolean} Whether the user's browser is Opera. Note: Chromium38* based Opera (Opera 15+) is detected as Chrome to avoid unnecessary39* special casing.40* @private41*/42goog.labs.userAgent.browser.matchOpera_ = function() {43return goog.labs.userAgent.util.matchUserAgent('Opera');44};454647/**48* @return {boolean} Whether the user's browser is IE.49* @private50*/51goog.labs.userAgent.browser.matchIE_ = function() {52return goog.labs.userAgent.util.matchUserAgent('Trident') ||53goog.labs.userAgent.util.matchUserAgent('MSIE');54};555657/**58* @return {boolean} Whether the user's browser is Edge.59* @private60*/61goog.labs.userAgent.browser.matchEdge_ = function() {62return goog.labs.userAgent.util.matchUserAgent('Edge');63};646566/**67* @return {boolean} Whether the user's browser is Firefox.68* @private69*/70goog.labs.userAgent.browser.matchFirefox_ = function() {71return goog.labs.userAgent.util.matchUserAgent('Firefox');72};737475/**76* @return {boolean} Whether the user's browser is Safari.77* @private78*/79goog.labs.userAgent.browser.matchSafari_ = function() {80return goog.labs.userAgent.util.matchUserAgent('Safari') &&81!(goog.labs.userAgent.browser.matchChrome_() ||82goog.labs.userAgent.browser.matchCoast_() ||83goog.labs.userAgent.browser.matchOpera_() ||84goog.labs.userAgent.browser.matchEdge_() ||85goog.labs.userAgent.browser.isSilk() ||86goog.labs.userAgent.util.matchUserAgent('Android'));87};888990/**91* @return {boolean} Whether the user's browser is Coast (Opera's Webkit-based92* iOS browser).93* @private94*/95goog.labs.userAgent.browser.matchCoast_ = function() {96return goog.labs.userAgent.util.matchUserAgent('Coast');97};9899100/**101* @return {boolean} Whether the user's browser is iOS Webview.102* @private103*/104goog.labs.userAgent.browser.matchIosWebview_ = function() {105// iOS Webview does not show up as Chrome or Safari. Also check for Opera's106// WebKit-based iOS browser, Coast.107return (goog.labs.userAgent.util.matchUserAgent('iPad') ||108goog.labs.userAgent.util.matchUserAgent('iPhone')) &&109!goog.labs.userAgent.browser.matchSafari_() &&110!goog.labs.userAgent.browser.matchChrome_() &&111!goog.labs.userAgent.browser.matchCoast_() &&112goog.labs.userAgent.util.matchUserAgent('AppleWebKit');113};114115116/**117* @return {boolean} Whether the user's browser is Chrome.118* @private119*/120goog.labs.userAgent.browser.matchChrome_ = function() {121return (goog.labs.userAgent.util.matchUserAgent('Chrome') ||122goog.labs.userAgent.util.matchUserAgent('CriOS')) &&123!goog.labs.userAgent.browser.matchEdge_();124};125126127/**128* @return {boolean} Whether the user's browser is the Android browser.129* @private130*/131goog.labs.userAgent.browser.matchAndroidBrowser_ = function() {132// Android can appear in the user agent string for Chrome on Android.133// This is not the Android standalone browser if it does.134return goog.labs.userAgent.util.matchUserAgent('Android') &&135!(goog.labs.userAgent.browser.isChrome() ||136goog.labs.userAgent.browser.isFirefox() ||137goog.labs.userAgent.browser.isOpera() ||138goog.labs.userAgent.browser.isSilk());139};140141142/**143* @return {boolean} Whether the user's browser is Opera.144*/145goog.labs.userAgent.browser.isOpera = goog.labs.userAgent.browser.matchOpera_;146147148/**149* @return {boolean} Whether the user's browser is IE.150*/151goog.labs.userAgent.browser.isIE = goog.labs.userAgent.browser.matchIE_;152153154/**155* @return {boolean} Whether the user's browser is Edge.156*/157goog.labs.userAgent.browser.isEdge = goog.labs.userAgent.browser.matchEdge_;158159160/**161* @return {boolean} Whether the user's browser is Firefox.162*/163goog.labs.userAgent.browser.isFirefox =164goog.labs.userAgent.browser.matchFirefox_;165166167/**168* @return {boolean} Whether the user's browser is Safari.169*/170goog.labs.userAgent.browser.isSafari = goog.labs.userAgent.browser.matchSafari_;171172173/**174* @return {boolean} Whether the user's browser is Coast (Opera's Webkit-based175* iOS browser).176*/177goog.labs.userAgent.browser.isCoast = goog.labs.userAgent.browser.matchCoast_;178179180/**181* @return {boolean} Whether the user's browser is iOS Webview.182*/183goog.labs.userAgent.browser.isIosWebview =184goog.labs.userAgent.browser.matchIosWebview_;185186187/**188* @return {boolean} Whether the user's browser is Chrome.189*/190goog.labs.userAgent.browser.isChrome = goog.labs.userAgent.browser.matchChrome_;191192193/**194* @return {boolean} Whether the user's browser is the Android browser.195*/196goog.labs.userAgent.browser.isAndroidBrowser =197goog.labs.userAgent.browser.matchAndroidBrowser_;198199200/**201* For more information, see:202* http://docs.aws.amazon.com/silk/latest/developerguide/user-agent.html203* @return {boolean} Whether the user's browser is Silk.204*/205goog.labs.userAgent.browser.isSilk = function() {206return goog.labs.userAgent.util.matchUserAgent('Silk');207};208209210/**211* @return {string} The browser version or empty string if version cannot be212* determined. Note that for Internet Explorer, this returns the version of213* the browser, not the version of the rendering engine. (IE 8 in214* compatibility mode will return 8.0 rather than 7.0. To determine the215* rendering engine version, look at document.documentMode instead. See216* http://msdn.microsoft.com/en-us/library/cc196988(v=vs.85).aspx for more217* details.)218*/219goog.labs.userAgent.browser.getVersion = function() {220var userAgentString = goog.labs.userAgent.util.getUserAgent();221// Special case IE since IE's version is inside the parenthesis and222// without the '/'.223if (goog.labs.userAgent.browser.isIE()) {224return goog.labs.userAgent.browser.getIEVersion_(userAgentString);225}226227var versionTuples =228goog.labs.userAgent.util.extractVersionTuples(userAgentString);229230// Construct a map for easy lookup.231var versionMap = {};232goog.array.forEach(versionTuples, function(tuple) {233// Note that the tuple is of length three, but we only care about the234// first two.235var key = tuple[0];236var value = tuple[1];237versionMap[key] = value;238});239240var versionMapHasKey = goog.partial(goog.object.containsKey, versionMap);241242// Gives the value with the first key it finds, otherwise empty string.243function lookUpValueWithKeys(keys) {244var key = goog.array.find(keys, versionMapHasKey);245return versionMap[key] || '';246}247248// Check Opera before Chrome since Opera 15+ has "Chrome" in the string.249// See250// http://my.opera.com/ODIN/blog/2013/07/15/opera-user-agent-strings-opera-15-and-beyond251if (goog.labs.userAgent.browser.isOpera()) {252// Opera 10 has Version/10.0 but Opera/9.8, so look for "Version" first.253// Opera uses 'OPR' for more recent UAs.254return lookUpValueWithKeys(['Version', 'Opera']);255}256257// Check Edge before Chrome since it has Chrome in the string.258if (goog.labs.userAgent.browser.isEdge()) {259return lookUpValueWithKeys(['Edge']);260}261262if (goog.labs.userAgent.browser.isChrome()) {263return lookUpValueWithKeys(['Chrome', 'CriOS']);264}265266// Usually products browser versions are in the third tuple after "Mozilla"267// and the engine.268var tuple = versionTuples[2];269return tuple && tuple[1] || '';270};271272273/**274* @param {string|number} version The version to check.275* @return {boolean} Whether the browser version is higher or the same as the276* given version.277*/278goog.labs.userAgent.browser.isVersionOrHigher = function(version) {279return goog.string.compareVersions(280goog.labs.userAgent.browser.getVersion(), version) >= 0;281};282283284/**285* Determines IE version. More information:286* http://msdn.microsoft.com/en-us/library/ie/bg182625(v=vs.85).aspx#uaString287* http://msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx288* http://blogs.msdn.com/b/ie/archive/2010/03/23/introducing-ie9-s-user-agent-string.aspx289* http://blogs.msdn.com/b/ie/archive/2009/01/09/the-internet-explorer-8-user-agent-string-updated-edition.aspx290*291* @param {string} userAgent the User-Agent.292* @return {string}293* @private294*/295goog.labs.userAgent.browser.getIEVersion_ = function(userAgent) {296// IE11 may identify itself as MSIE 9.0 or MSIE 10.0 due to an IE 11 upgrade297// bug. Example UA:298// Mozilla/5.0 (MSIE 9.0; Windows NT 6.1; WOW64; Trident/7.0; rv:11.0)299// like Gecko.300// See http://www.whatismybrowser.com/developers/unknown-user-agent-fragments.301var rv = /rv: *([\d\.]*)/.exec(userAgent);302if (rv && rv[1]) {303return rv[1];304}305306var version = '';307var msie = /MSIE +([\d\.]+)/.exec(userAgent);308if (msie && msie[1]) {309// IE in compatibility mode usually identifies itself as MSIE 7.0; in this310// case, use the Trident version to determine the version of IE. For more311// details, see the links above.312var tridentVersion = /Trident\/(\d.\d)/.exec(userAgent);313if (msie[1] == '7.0') {314if (tridentVersion && tridentVersion[1]) {315switch (tridentVersion[1]) {316case '4.0':317version = '8.0';318break;319case '5.0':320version = '9.0';321break;322case '6.0':323version = '10.0';324break;325case '7.0':326version = '11.0';327break;328}329} else {330version = '7.0';331}332} else {333version = msie[1];334}335}336return version;337};338339340