| 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 platform detection. |
| 17 |
* @see <a href="http://www.useragentstring.com/">User agent strings</a> |
| 18 |
* For more information on browser brand, rendering engine, or device see the |
| 19 |
* other sub-namespaces in goog.labs.userAgent (browser, engine, and device |
| 20 |
* respectively). |
| 21 |
* |
| 22 |
*/ |
| 23 |
|
| 24 |
goog.provide('goog.labs.userAgent.platform'); |
| 25 |
|
| 26 |
goog.require('goog.labs.userAgent.util'); |
| 27 |
goog.require('goog.string'); |
| 28 |
|
| 29 |
|
| 30 |
/** |
| 31 |
* @return {boolean} Whether the platform is Android. |
| 32 |
*/ |
| 33 |
goog.labs.userAgent.platform.isAndroid = function() { |
| 34 |
return goog.labs.userAgent.util.matchUserAgent('Android'); |
| 35 |
}; |
| 36 |
|
| 37 |
|
| 38 |
/** |
| 39 |
* @return {boolean} Whether the platform is iPod. |
| 40 |
*/ |
| 41 |
goog.labs.userAgent.platform.isIpod = function() { |
| 42 |
return goog.labs.userAgent.util.matchUserAgent('iPod'); |
| 43 |
}; |
| 44 |
|
| 45 |
|
| 46 |
/** |
| 47 |
* @return {boolean} Whether the platform is iPhone. |
| 48 |
*/ |
| 49 |
goog.labs.userAgent.platform.isIphone = function() { |
| 50 |
return goog.labs.userAgent.util.matchUserAgent('iPhone') && |
| 51 |
!goog.labs.userAgent.util.matchUserAgent('iPod') && |
| 52 |
!goog.labs.userAgent.util.matchUserAgent('iPad'); |
| 53 |
}; |
| 54 |
|
| 55 |
|
| 56 |
/** |
| 57 |
* @return {boolean} Whether the platform is iPad. |
| 58 |
*/ |
| 59 |
goog.labs.userAgent.platform.isIpad = function() { |
| 60 |
return goog.labs.userAgent.util.matchUserAgent('iPad'); |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
/** |
| 65 |
* @return {boolean} Whether the platform is iOS. |
| 66 |
*/ |
| 67 |
goog.labs.userAgent.platform.isIos = function() { |
| 68 |
return goog.labs.userAgent.platform.isIphone() || |
| 69 |
goog.labs.userAgent.platform.isIpad() || |
| 70 |
goog.labs.userAgent.platform.isIpod(); |
| 71 |
}; |
| 72 |
|
| 73 |
|
| 74 |
/** |
| 75 |
* @return {boolean} Whether the platform is Mac. |
| 76 |
*/ |
| 77 |
goog.labs.userAgent.platform.isMacintosh = function() { |
| 78 |
return goog.labs.userAgent.util.matchUserAgent('Macintosh'); |
| 79 |
}; |
| 80 |
|
| 81 |
|
| 82 |
/** |
| 83 |
* Note: ChromeOS is not considered to be Linux as it does not report itself |
| 84 |
* as Linux in the user agent string. |
| 85 |
* @return {boolean} Whether the platform is Linux. |
| 86 |
*/ |
| 87 |
goog.labs.userAgent.platform.isLinux = function() { |
| 88 |
return goog.labs.userAgent.util.matchUserAgent('Linux'); |
| 89 |
}; |
| 90 |
|
| 91 |
|
| 92 |
/** |
| 93 |
* @return {boolean} Whether the platform is Windows. |
| 94 |
*/ |
| 95 |
goog.labs.userAgent.platform.isWindows = function() { |
| 96 |
return goog.labs.userAgent.util.matchUserAgent('Windows'); |
| 97 |
}; |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* @return {boolean} Whether the platform is ChromeOS. |
| 102 |
*/ |
| 103 |
goog.labs.userAgent.platform.isChromeOS = function() { |
| 104 |
return goog.labs.userAgent.util.matchUserAgent('CrOS'); |
| 105 |
}; |
| 106 |
|
| 107 |
/** |
| 108 |
* @return {boolean} Whether the platform is Chromecast. |
| 109 |
*/ |
| 110 |
goog.labs.userAgent.platform.isChromecast = function() { |
| 111 |
return goog.labs.userAgent.util.matchUserAgent('CrKey'); |
| 112 |
}; |
| 113 |
|
| 114 |
/** |
| 115 |
* The version of the platform. We only determine the version for Windows, |
| 116 |
* Mac, and Chrome OS. It doesn't make much sense on Linux. For Windows, we only |
| 117 |
* look at the NT version. Non-NT-based versions (e.g. 95, 98, etc.) are given |
| 118 |
* version 0.0. |
| 119 |
* |
| 120 |
* @return {string} The platform version or empty string if version cannot be |
| 121 |
* determined. |
| 122 |
*/ |
| 123 |
goog.labs.userAgent.platform.getVersion = function() { |
| 124 |
var userAgentString = goog.labs.userAgent.util.getUserAgent(); |
| 125 |
var version = '', re; |
| 126 |
if (goog.labs.userAgent.platform.isWindows()) { |
| 127 |
re = /Windows (?:NT|Phone) ([0-9.]+)/; |
| 128 |
var match = re.exec(userAgentString); |
| 129 |
if (match) { |
| 130 |
version = match[1]; |
| 131 |
} else { |
| 132 |
version = '0.0'; |
| 133 |
} |
| 134 |
} else if (goog.labs.userAgent.platform.isIos()) { |
| 135 |
re = /(?:iPhone|iPod|iPad|CPU)\s+OS\s+(\S+)/; |
| 136 |
var match = re.exec(userAgentString); |
| 137 |
// Report the version as x.y.z and not x_y_z |
| 138 |
version = match && match[1].replace(/_/g, '.'); |
| 139 |
} else if (goog.labs.userAgent.platform.isMacintosh()) { |
| 140 |
re = /Mac OS X ([0-9_.]+)/; |
| 141 |
var match = re.exec(userAgentString); |
| 142 |
// Note: some old versions of Camino do not report an OSX version. |
| 143 |
// Default to 10. |
| 144 |
version = match ? match[1].replace(/_/g, '.') : '10'; |
| 145 |
} else if (goog.labs.userAgent.platform.isAndroid()) { |
| 146 |
re = /Android\s+([^\);]+)(\)|;)/; |
| 147 |
var match = re.exec(userAgentString); |
| 148 |
version = match && match[1]; |
| 149 |
} else if (goog.labs.userAgent.platform.isChromeOS()) { |
| 150 |
re = /(?:CrOS\s+(?:i686|x86_64)\s+([0-9.]+))/; |
| 151 |
var match = re.exec(userAgentString); |
| 152 |
version = match && match[1]; |
| 153 |
} |
| 154 |
return version || ''; |
| 155 |
}; |
| 156 |
|
| 157 |
|
| 158 |
/** |
| 159 |
* @param {string|number} version The version to check. |
| 160 |
* @return {boolean} Whether the browser version is higher or the same as the |
| 161 |
* given version. |
| 162 |
*/ |
| 163 |
goog.labs.userAgent.platform.isVersionOrHigher = function(version) { |
| 164 |
return goog.string.compareVersions( |
| 165 |
goog.labs.userAgent.platform.getVersion(), version) >= 0; |
| 166 |
}; |