check_browser.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2011 The Native Client Authors. All rights reserved.
  3. * Use of this source code is governed by a BSD-style license that can be
  4. * found in the LICENSE file.
  5. */
  6. /**
  7. * @fileoverview This file provides a BrowserChecker Javascript class.
  8. * Users can create a BrowserChecker object, invoke checkBrowser(|version|),
  9. * and then use getIsValidBrowser() and getBrowserSupportStatus()
  10. * to determine if the browser version is greater than |version|
  11. * and if the Native Client plugin is found.
  12. */
  13. // Create a namespace object
  14. var browser_version = browser_version || {};
  15. /**
  16. * Class to provide checking for version and NativeClient.
  17. * @param {integer} arg1 An argument that indicates major version of Chrome we
  18. * require, such as 14.
  19. */
  20. /**
  21. * Constructor for the BrowserChecker. Sets the major version of
  22. * Chrome that is required to |minChromeVersion|.
  23. * @param minChromeVersion The earliest major version of chrome that
  24. * is supported. If the Chrome browser version is less than
  25. * |minChromeVersion| then |isValidBrowswer| will be set to false.
  26. * @param opt_maxChromeVersion Ignored. Retained for backwards compatibility.
  27. * @param appVersion The application version string.
  28. * @param plugins The plugins that exist in the browser.
  29. * @constructor
  30. */
  31. browser_version.BrowserChecker = function(minChromeVersion,
  32. appVersion, plugins,
  33. opt_maxChromeVersion) {
  34. /**
  35. * Version specified by the user. This class looks to see if the browser
  36. * version is >= |minChromeVersion_|.
  37. * @type {integer}
  38. * @private
  39. */
  40. this.minChromeVersion_ = minChromeVersion;
  41. /**
  42. * List of Browser plugin objects.
  43. * @type {Ojbect array}
  44. * @private
  45. */
  46. this.plugins_ = plugins;
  47. /**
  48. * Application version string from the Browser.
  49. * @type {integer}
  50. * @private
  51. */
  52. this.appVersion_ = appVersion;
  53. /**
  54. * Flag used to indicate if the browser has Native Client and is if the
  55. * browser version is recent enough.
  56. * @type {boolean}
  57. * @private
  58. */
  59. this.isValidBrowser_ = false;
  60. /**
  61. * Actual major version of Chrome -- found by querying the browser.
  62. * @type {integer}
  63. * @private
  64. */
  65. this.chromeVersion_ = null;
  66. /**
  67. * Browser support status. This allows the user to get a detailed status
  68. * rather than using this.browserSupportMessage.
  69. */
  70. this.browserSupportStatus_ =
  71. browser_version.BrowserChecker.StatusValues.UNKNOWN;
  72. }
  73. /**
  74. * The values used for BrowserChecker status to indicate success or
  75. * a specific error.
  76. * @enum {id}
  77. */
  78. browser_version.BrowserChecker.StatusValues = {
  79. UNKNOWN: 0,
  80. NACL_ENABLED: 1,
  81. UNKNOWN_BROWSER: 2,
  82. CHROME_VERSION_TOO_OLD: 3,
  83. NACL_NOT_ENABLED: 4,
  84. NOT_USING_SERVER: 5
  85. };
  86. /**
  87. * Determines if the plugin with name |name| exists in the browser.
  88. * @param {string} name The name of the plugin.
  89. * @param {Object array} plugins The plugins in this browser.
  90. * @return {bool} |true| if the plugin is found.
  91. */
  92. browser_version.BrowserChecker.prototype.pluginExists = function(name,
  93. plugins) {
  94. for (var index=0; index < plugins.length; index++) {
  95. var plugin = this.plugins_[index];
  96. var plugin_name = plugin['name'];
  97. // If the plugin is not found, you can use the Javascript console
  98. // to see the names of the plugins that were found when debugging.
  99. if (plugin_name.indexOf(name) != -1) {
  100. return true;
  101. }
  102. }
  103. return false;
  104. }
  105. /**
  106. * Returns browserSupportStatus_ which indicates if the browser supports
  107. * Native Client. Values are defined as literals in
  108. * browser_version.BrowserChecker.StatusValues.
  109. * @ return {int} Level of NaCl support.
  110. */
  111. browser_version.BrowserChecker.prototype.getBrowserSupportStatus = function() {
  112. return this.browserSupportStatus_;
  113. }
  114. /**
  115. * Returns isValidBrowser (true/false) to indicate if the browser supports
  116. * Native Client.
  117. * @ return {bool} If this browser has NativeClient and correct version.
  118. */
  119. browser_version.BrowserChecker.prototype.getIsValidBrowser = function() {
  120. return this.isValidBrowser_;
  121. }
  122. /**
  123. * Checks to see if this browser can support Native Client applications.
  124. * For Chrome browsers, checks to see if the "Native Client" plugin is
  125. * enabled.
  126. */
  127. browser_version.BrowserChecker.prototype.checkBrowser = function() {
  128. var versionPatt = /Chrome\/(\d+)\.(\d+)\.(\d+)\.(\d+)/;
  129. var result = this.appVersion_.match(versionPatt);
  130. // |result| stores the Chrome version number.
  131. if (!result) {
  132. this.isValidBrowser_ = false;
  133. this.browserSupportStatus_ =
  134. browser_version.BrowserChecker.StatusValues.UNKNOWN_BROWSER;
  135. } else {
  136. this.chromeVersion_ = result[1];
  137. // We know we have Chrome, check version and/or plugin named Native Client
  138. if (this.chromeVersion_ >= this.minChromeVersion_) {
  139. var found_nacl = this.pluginExists('Native Client', this.plugins_);
  140. if (found_nacl) {
  141. this.isValidBrowser_ = true;
  142. this.browserSupportStatus_ =
  143. browser_version.BrowserChecker.StatusValues.NACL_ENABLED;
  144. } else {
  145. this.isValidBrowser_ = false;
  146. this.browserSupportStatus_ =
  147. browser_version.BrowserChecker.StatusValues.NACL_NOT_ENABLED;
  148. }
  149. } else {
  150. // We are in a version that is less than |minChromeVersion_|
  151. this.isValidBrowser_ = false;
  152. this.browserSupportStatus_ =
  153. browser_version.BrowserChecker.StatusValues.CHROME_VERSION_TOO_OLD;
  154. }
  155. }
  156. var my_protocol = window.location.protocol;
  157. if (my_protocol.indexOf('file') == 0) {
  158. this.isValidBrowser_ = false;
  159. this.browserSupportStatus_ =
  160. browser_version.BrowserChecker.StatusValues.NOT_USING_SERVER;
  161. }
  162. }