platform.js 4.8 KB

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