123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- // Copyright 2013 The Closure Library Authors. All Rights Reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS-IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- /**
- * @fileoverview Closure user agent platform detection.
- * @see <a href="http://www.useragentstring.com/">User agent strings</a>
- * For more information on browser brand, rendering engine, or device see the
- * other sub-namespaces in goog.labs.userAgent (browser, engine, and device
- * respectively).
- *
- */
- goog.provide('goog.labs.userAgent.platform');
- goog.require('goog.labs.userAgent.util');
- goog.require('goog.string');
- /**
- * @return {boolean} Whether the platform is Android.
- */
- goog.labs.userAgent.platform.isAndroid = function() {
- return goog.labs.userAgent.util.matchUserAgent('Android');
- };
- /**
- * @return {boolean} Whether the platform is iPod.
- */
- goog.labs.userAgent.platform.isIpod = function() {
- return goog.labs.userAgent.util.matchUserAgent('iPod');
- };
- /**
- * @return {boolean} Whether the platform is iPhone.
- */
- goog.labs.userAgent.platform.isIphone = function() {
- return goog.labs.userAgent.util.matchUserAgent('iPhone') &&
- !goog.labs.userAgent.util.matchUserAgent('iPod') &&
- !goog.labs.userAgent.util.matchUserAgent('iPad');
- };
- /**
- * @return {boolean} Whether the platform is iPad.
- */
- goog.labs.userAgent.platform.isIpad = function() {
- return goog.labs.userAgent.util.matchUserAgent('iPad');
- };
- /**
- * @return {boolean} Whether the platform is iOS.
- */
- goog.labs.userAgent.platform.isIos = function() {
- return goog.labs.userAgent.platform.isIphone() ||
- goog.labs.userAgent.platform.isIpad() ||
- goog.labs.userAgent.platform.isIpod();
- };
- /**
- * @return {boolean} Whether the platform is Mac.
- */
- goog.labs.userAgent.platform.isMacintosh = function() {
- return goog.labs.userAgent.util.matchUserAgent('Macintosh');
- };
- /**
- * Note: ChromeOS is not considered to be Linux as it does not report itself
- * as Linux in the user agent string.
- * @return {boolean} Whether the platform is Linux.
- */
- goog.labs.userAgent.platform.isLinux = function() {
- return goog.labs.userAgent.util.matchUserAgent('Linux');
- };
- /**
- * @return {boolean} Whether the platform is Windows.
- */
- goog.labs.userAgent.platform.isWindows = function() {
- return goog.labs.userAgent.util.matchUserAgent('Windows');
- };
- /**
- * @return {boolean} Whether the platform is ChromeOS.
- */
- goog.labs.userAgent.platform.isChromeOS = function() {
- return goog.labs.userAgent.util.matchUserAgent('CrOS');
- };
- /**
- * The version of the platform. We only determine the version for Windows,
- * Mac, and Chrome OS. It doesn't make much sense on Linux. For Windows, we only
- * look at the NT version. Non-NT-based versions (e.g. 95, 98, etc.) are given
- * version 0.0.
- *
- * @return {string} The platform version or empty string if version cannot be
- * determined.
- */
- goog.labs.userAgent.platform.getVersion = function() {
- var userAgentString = goog.labs.userAgent.util.getUserAgent();
- var version = '', re;
- if (goog.labs.userAgent.platform.isWindows()) {
- re = /Windows (?:NT|Phone) ([0-9.]+)/;
- var match = re.exec(userAgentString);
- if (match) {
- version = match[1];
- } else {
- version = '0.0';
- }
- } else if (goog.labs.userAgent.platform.isIos()) {
- re = /(?:iPhone|iPod|iPad|CPU)\s+OS\s+(\S+)/;
- var match = re.exec(userAgentString);
- // Report the version as x.y.z and not x_y_z
- version = match && match[1].replace(/_/g, '.');
- } else if (goog.labs.userAgent.platform.isMacintosh()) {
- re = /Mac OS X ([0-9_.]+)/;
- var match = re.exec(userAgentString);
- // Note: some old versions of Camino do not report an OSX version.
- // Default to 10.
- version = match ? match[1].replace(/_/g, '.') : '10';
- } else if (goog.labs.userAgent.platform.isAndroid()) {
- re = /Android\s+([^\);]+)(\)|;)/;
- var match = re.exec(userAgentString);
- version = match && match[1];
- } else if (goog.labs.userAgent.platform.isChromeOS()) {
- re = /(?:CrOS\s+(?:i686|x86_64)\s+([0-9.]+))/;
- var match = re.exec(userAgentString);
- version = match && match[1];
- }
- return version || '';
- };
- /**
- * @param {string|number} version The version to check.
- * @return {boolean} Whether the browser version is higher or the same as the
- * given version.
- */
- goog.labs.userAgent.platform.isVersionOrHigher = function(version) {
- return goog.string.compareVersions(
- goog.labs.userAgent.platform.getVersion(), version) >= 0;
- };
|