| 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. |
| 17 | * @see http://en.wikipedia.org/wiki/User_agent |
| 18 | * For more information on browser brand, platform, or device see the other |
| 19 | * sub-namespaces in goog.labs.userAgent (browser, platform, and device). |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | goog.provide('goog.labs.userAgent.engine'); |
| 24 | |
| 25 | goog.require('goog.array'); |
| 26 | goog.require('goog.labs.userAgent.util'); |
| 27 | goog.require('goog.string'); |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * @return {boolean} Whether the rendering engine is Presto. |
| 32 | */ |
| 33 | goog.labs.userAgent.engine.isPresto = function() { |
| 34 | return goog.labs.userAgent.util.matchUserAgent('Presto'); |
| 35 | }; |
| 36 | |
| 37 | |
| 38 | /** |
| 39 | * @return {boolean} Whether the rendering engine is Trident. |
| 40 | */ |
| 41 | goog.labs.userAgent.engine.isTrident = function() { |
| 42 | // IE only started including the Trident token in IE8. |
| 43 | return goog.labs.userAgent.util.matchUserAgent('Trident') || |
| 44 | goog.labs.userAgent.util.matchUserAgent('MSIE'); |
| 45 | }; |
| 46 | |
| 47 | |
| 48 | /** |
| 49 | * @return {boolean} Whether the rendering engine is WebKit. |
| 50 | */ |
| 51 | goog.labs.userAgent.engine.isWebKit = function() { |
| 52 | return goog.labs.userAgent.util.matchUserAgentIgnoreCase('WebKit'); |
| 53 | }; |
| 54 | |
| 55 | |
| 56 | /** |
| 57 | * @return {boolean} Whether the rendering engine is Gecko. |
| 58 | */ |
| 59 | goog.labs.userAgent.engine.isGecko = function() { |
| 60 | return goog.labs.userAgent.util.matchUserAgent('Gecko') && |
| 61 | !goog.labs.userAgent.engine.isWebKit() && |
| 62 | !goog.labs.userAgent.engine.isTrident(); |
| 63 | }; |
| 64 | |
| 65 | |
| 66 | /** |
| 67 | * @return {string} The rendering engine's version or empty string if version |
| 68 | * can't be determined. |
| 69 | */ |
| 70 | goog.labs.userAgent.engine.getVersion = function() { |
| 71 | var userAgentString = goog.labs.userAgent.util.getUserAgent(); |
| 72 | if (userAgentString) { |
| 73 | var tuples = goog.labs.userAgent.util.extractVersionTuples( |
| 74 | userAgentString); |
| 75 | |
| 76 | var engineTuple = tuples[1]; |
| 77 | if (engineTuple) { |
| 78 | // In Gecko, the version string is either in the browser info or the |
| 79 | // Firefox version. See Gecko user agent string reference: |
| 80 | // http://goo.gl/mULqa |
| 81 | if (engineTuple[0] == 'Gecko') { |
| 82 | return goog.labs.userAgent.engine.getVersionForKey_( |
| 83 | tuples, 'Firefox'); |
| 84 | } |
| 85 | |
| 86 | return engineTuple[1]; |
| 87 | } |
| 88 | |
| 89 | // IE has only one version identifier, and the Trident version is |
| 90 | // specified in the parenthetical. |
| 91 | var browserTuple = tuples[0]; |
| 92 | var info; |
| 93 | if (browserTuple && (info = browserTuple[2])) { |
| 94 | var match = /Trident\/([^\s;]+)/.exec(info); |
| 95 | if (match) { |
| 96 | return match[1]; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | return ''; |
| 101 | }; |
| 102 | |
| 103 | |
| 104 | /** |
| 105 | * @param {string|number} version The version to check. |
| 106 | * @return {boolean} Whether the rendering engine version is higher or the same |
| 107 | * as the given version. |
| 108 | */ |
| 109 | goog.labs.userAgent.engine.isVersionOrHigher = function(version) { |
| 110 | return goog.string.compareVersions(goog.labs.userAgent.engine.getVersion(), |
| 111 | version) >= 0; |
| 112 | }; |
| 113 | |
| 114 | |
| 115 | /** |
| 116 | * @param {!Array.<!Array.<string>>} tuples Version tuples. |
| 117 | * @param {string} key The key to look for. |
| 118 | * @return {string} The version string of the given key, if present. |
| 119 | * Otherwise, the empty string. |
| 120 | * @private |
| 121 | */ |
| 122 | goog.labs.userAgent.engine.getVersionForKey_ = function(tuples, key) { |
| 123 | // TODO(nnaze): Move to util if useful elsewhere. |
| 124 | |
| 125 | var pair = goog.array.find(tuples, function(pair) { |
| 126 | return key == pair[0]; |
| 127 | }); |
| 128 | |
| 129 | return pair && pair[1] || ''; |
| 130 | }; |