platform.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2010 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 Utilities for getting details about the user's platform.
  16. */
  17. goog.provide('goog.userAgent.platform');
  18. goog.require('goog.string');
  19. goog.require('goog.userAgent');
  20. /**
  21. * Detects the version of Windows or Mac OS that is running.
  22. *
  23. * @private
  24. * @return {string} The platform version.
  25. */
  26. goog.userAgent.platform.determineVersion_ = function() {
  27. var re;
  28. if (goog.userAgent.WINDOWS) {
  29. re = /Windows NT ([0-9.]+)/;
  30. var match = re.exec(goog.userAgent.getUserAgentString());
  31. if (match) {
  32. return match[1];
  33. } else {
  34. return '0';
  35. }
  36. } else if (goog.userAgent.MAC) {
  37. re = /10[_.][0-9_.]+/;
  38. var match = re.exec(goog.userAgent.getUserAgentString());
  39. // Note: some old versions of Camino do not report an OSX version.
  40. // Default to 10.
  41. return match ? match[0].replace(/_/g, '.') : '10';
  42. } else if (goog.userAgent.ANDROID) {
  43. re = /Android\s+([^\);]+)(\)|;)/;
  44. var match = re.exec(goog.userAgent.getUserAgentString());
  45. return match ? match[1] : '';
  46. } else if (
  47. goog.userAgent.IPHONE || goog.userAgent.IPAD || goog.userAgent.IPOD) {
  48. re = /(?:iPhone|CPU)\s+OS\s+(\S+)/;
  49. var match = re.exec(goog.userAgent.getUserAgentString());
  50. // Report the version as x.y.z and not x_y_z
  51. return match ? match[1].replace(/_/g, '.') : '';
  52. }
  53. return '';
  54. };
  55. /**
  56. * The version of the platform. We only determine the version for Windows and
  57. * Mac, since it doesn't make much sense on Linux. For Windows, we only look at
  58. * the NT version. Non-NT-based versions (e.g. 95, 98, etc.) are given version
  59. * 0.0
  60. * @type {string}
  61. */
  62. goog.userAgent.platform.VERSION = goog.userAgent.platform.determineVersion_();
  63. /**
  64. * Whether the user agent platform version is higher or the same as the given
  65. * version.
  66. *
  67. * @param {string|number} version The version to check.
  68. * @return {boolean} Whether the user agent platform version is higher or the
  69. * same as the given version.
  70. */
  71. goog.userAgent.platform.isVersion = function(version) {
  72. return goog.string.compareVersions(
  73. goog.userAgent.platform.VERSION, version) >= 0;
  74. };