| 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 | /** |
| 109 | * The version of the platform. We only determine the version for Windows, |
| 110 | * Mac, and Chrome OS. It doesn't make much sense on Linux. For Windows, we only |
| 111 | * look at the NT version. Non-NT-based versions (e.g. 95, 98, etc.) are given |
| 112 | * version 0.0. |
| 113 | * |
| 114 | * @return {string} The platform version or empty string if version cannot be |
| 115 | * determined. |
| 116 | */ |
| 117 | goog.labs.userAgent.platform.getVersion = function() { |
| 118 | var userAgentString = goog.labs.userAgent.util.getUserAgent(); |
| 119 | var version = '', re; |
| 120 | if (goog.labs.userAgent.platform.isWindows()) { |
| 121 | re = /Windows (?:NT|Phone) ([0-9.]+)/; |
| 122 | var match = re.exec(userAgentString); |
| 123 | if (match) { |
| 124 | version = match[1]; |
| 125 | } else { |
| 126 | version = '0.0'; |
| 127 | } |
| 128 | } else if (goog.labs.userAgent.platform.isIos()) { |
| 129 | re = /(?:iPhone|iPod|iPad|CPU)\s+OS\s+(\S+)/; |
| 130 | var match = re.exec(userAgentString); |
| 131 | // Report the version as x.y.z and not x_y_z |
| 132 | version = match && match[1].replace(/_/g, '.'); |
| 133 | } else if (goog.labs.userAgent.platform.isMacintosh()) { |
| 134 | re = /Mac OS X ([0-9_.]+)/; |
| 135 | var match = re.exec(userAgentString); |
| 136 | // Note: some old versions of Camino do not report an OSX version. |
| 137 | // Default to 10. |
| 138 | version = match ? match[1].replace(/_/g, '.') : '10'; |
| 139 | } else if (goog.labs.userAgent.platform.isAndroid()) { |
| 140 | re = /Android\s+([^\);]+)(\)|;)/; |
| 141 | var match = re.exec(userAgentString); |
| 142 | version = match && match[1]; |
| 143 | } else if (goog.labs.userAgent.platform.isChromeOS()) { |
| 144 | re = /(?:CrOS\s+(?:i686|x86_64)\s+([0-9.]+))/; |
| 145 | var match = re.exec(userAgentString); |
| 146 | version = match && match[1]; |
| 147 | } |
| 148 | return version || ''; |
| 149 | }; |
| 150 | |
| 151 | |
| 152 | /** |
| 153 | * @param {string|number} version The version to check. |
| 154 | * @return {boolean} Whether the browser version is higher or the same as the |
| 155 | * given version. |
| 156 | */ |
| 157 | goog.labs.userAgent.platform.isVersionOrHigher = function(version) { |
| 158 | return goog.string.compareVersions(goog.labs.userAgent.platform.getVersion(), |
| 159 | version) >= 0; |
| 160 | }; |