Path: blob/trunk/third_party/closure/goog/labs/useragent/platform.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 platform detection.16* @see <a href="http://www.useragentstring.com/">User agent strings</a>17* For more information on browser brand, rendering engine, or device see the18* other sub-namespaces in goog.labs.userAgent (browser, engine, and device19* respectively).20*21*/2223goog.provide('goog.labs.userAgent.platform');2425goog.require('goog.labs.userAgent.util');26goog.require('goog.string');272829/**30* @return {boolean} Whether the platform is Android.31*/32goog.labs.userAgent.platform.isAndroid = function() {33return goog.labs.userAgent.util.matchUserAgent('Android');34};353637/**38* @return {boolean} Whether the platform is iPod.39*/40goog.labs.userAgent.platform.isIpod = function() {41return goog.labs.userAgent.util.matchUserAgent('iPod');42};434445/**46* @return {boolean} Whether the platform is iPhone.47*/48goog.labs.userAgent.platform.isIphone = function() {49return goog.labs.userAgent.util.matchUserAgent('iPhone') &&50!goog.labs.userAgent.util.matchUserAgent('iPod') &&51!goog.labs.userAgent.util.matchUserAgent('iPad');52};535455/**56* @return {boolean} Whether the platform is iPad.57*/58goog.labs.userAgent.platform.isIpad = function() {59return goog.labs.userAgent.util.matchUserAgent('iPad');60};616263/**64* @return {boolean} Whether the platform is iOS.65*/66goog.labs.userAgent.platform.isIos = function() {67return goog.labs.userAgent.platform.isIphone() ||68goog.labs.userAgent.platform.isIpad() ||69goog.labs.userAgent.platform.isIpod();70};717273/**74* @return {boolean} Whether the platform is Mac.75*/76goog.labs.userAgent.platform.isMacintosh = function() {77return goog.labs.userAgent.util.matchUserAgent('Macintosh');78};798081/**82* Note: ChromeOS is not considered to be Linux as it does not report itself83* as Linux in the user agent string.84* @return {boolean} Whether the platform is Linux.85*/86goog.labs.userAgent.platform.isLinux = function() {87return goog.labs.userAgent.util.matchUserAgent('Linux');88};899091/**92* @return {boolean} Whether the platform is Windows.93*/94goog.labs.userAgent.platform.isWindows = function() {95return goog.labs.userAgent.util.matchUserAgent('Windows');96};979899/**100* @return {boolean} Whether the platform is ChromeOS.101*/102goog.labs.userAgent.platform.isChromeOS = function() {103return goog.labs.userAgent.util.matchUserAgent('CrOS');104};105106107/**108* The version of the platform. We only determine the version for Windows,109* Mac, and Chrome OS. It doesn't make much sense on Linux. For Windows, we only110* look at the NT version. Non-NT-based versions (e.g. 95, 98, etc.) are given111* version 0.0.112*113* @return {string} The platform version or empty string if version cannot be114* determined.115*/116goog.labs.userAgent.platform.getVersion = function() {117var userAgentString = goog.labs.userAgent.util.getUserAgent();118var version = '', re;119if (goog.labs.userAgent.platform.isWindows()) {120re = /Windows (?:NT|Phone) ([0-9.]+)/;121var match = re.exec(userAgentString);122if (match) {123version = match[1];124} else {125version = '0.0';126}127} else if (goog.labs.userAgent.platform.isIos()) {128re = /(?:iPhone|iPod|iPad|CPU)\s+OS\s+(\S+)/;129var match = re.exec(userAgentString);130// Report the version as x.y.z and not x_y_z131version = match && match[1].replace(/_/g, '.');132} else if (goog.labs.userAgent.platform.isMacintosh()) {133re = /Mac OS X ([0-9_.]+)/;134var match = re.exec(userAgentString);135// Note: some old versions of Camino do not report an OSX version.136// Default to 10.137version = match ? match[1].replace(/_/g, '.') : '10';138} else if (goog.labs.userAgent.platform.isAndroid()) {139re = /Android\s+([^\);]+)(\)|;)/;140var match = re.exec(userAgentString);141version = match && match[1];142} else if (goog.labs.userAgent.platform.isChromeOS()) {143re = /(?:CrOS\s+(?:i686|x86_64)\s+([0-9.]+))/;144var match = re.exec(userAgentString);145version = match && match[1];146}147return version || '';148};149150151/**152* @param {string|number} version The version to check.153* @return {boolean} Whether the browser version is higher or the same as the154* given version.155*/156goog.labs.userAgent.platform.isVersionOrHigher = function(version) {157return goog.string.compareVersions(158goog.labs.userAgent.platform.getVersion(), version) >= 0;159};160161162