| OLD | NEW |
| (Empty) |
| 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 /** | |
| 8 * @fileoverview This file provides a BrowserChecker Javascript class. | |
| 9 * Users can create a BrowserChecker object, invoke checkBrowser(|version|), | |
| 10 * and then use getIsValidBrowser() and getBrowserSupportMessage() | |
| 11 * to determine if the browser version is greater than |version| | |
| 12 * and if the Native Client plugin is found. | |
| 13 */ | |
| 14 | |
| 15 // Create a namespace object | |
| 16 var browser_version = browser_version || {}; | |
| 17 | |
| 18 /** | |
| 19 * Class to provide checking for version and NativeClient. | |
| 20 * @param {integer} arg1 An argument that indicates major version of Chrome we | |
| 21 * require, such as 14. | |
| 22 */ | |
| 23 | |
| 24 /** | |
| 25 * Constructor for the BrowserChecker. Sets the major version of | |
| 26 * Chrome that is required to |minChromeVersion|. | |
| 27 * @param minChromeVersion The earliest major version of chrome that | |
| 28 * is supported. If the Chrome browser version is less than | |
| 29 * |minChromeVersion| then |isValidBrowswer| will be set to false. | |
| 30 * @param opt_maxChromeVersion Ignored. Retained for backwards compatibility. | |
| 31 * @param appVersion The application version string. | |
| 32 * @param plugins The plugins that exist in the browser. | |
| 33 * @constructor | |
| 34 */ | |
| 35 browser_version.BrowserChecker = function(minChromeVersion, | |
| 36 appVersion, plugins, | |
| 37 opt_maxChromeVersion) { | |
| 38 /** | |
| 39 * Version specified by the user. This class looks to see if the browser | |
| 40 * version is >= |minChromeVersion_|. | |
| 41 * @type {integer} | |
| 42 * @private | |
| 43 */ | |
| 44 this.minChromeVersion_ = minChromeVersion; | |
| 45 | |
| 46 /** | |
| 47 * List of Browser plugin objects. | |
| 48 * @type {Ojbect array} | |
| 49 * @private | |
| 50 */ | |
| 51 this.plugins_ = plugins; | |
| 52 | |
| 53 /** | |
| 54 * Application version string from the Browser. | |
| 55 * @type {integer} | |
| 56 * @private | |
| 57 */ | |
| 58 this.appVersion_ = appVersion; | |
| 59 | |
| 60 /** | |
| 61 * Flag used to indicate if the browser has Native Client and is if the | |
| 62 * browser version is recent enough. | |
| 63 * @type {boolean} | |
| 64 * @private | |
| 65 */ | |
| 66 this.isValidBrowser_ = false; | |
| 67 | |
| 68 /** | |
| 69 * Actual major version of Chrome -- found by querying the browser. | |
| 70 * @type {integer} | |
| 71 * @private | |
| 72 */ | |
| 73 this.chromeVersion_ = null; | |
| 74 | |
| 75 /** | |
| 76 * Message generated if Native Client is found and if | |
| 77 * there is not a version problem. | |
| 78 * @type {string} | |
| 79 * @private | |
| 80 */ | |
| 81 this.browserSupportMessage_ = 'Unable to detect browser and/or' | |
| 82 + ' Native Client support.<br>'; | |
| 83 | |
| 84 /** | |
| 85 * Browser support status. This allows the user to get a detailed status | |
| 86 * rather than using this.browserSupportMessage. | |
| 87 */ | |
| 88 this.browserSupportStatus_ = | |
| 89 browser_version.BrowserChecker.StatusValues.UNKNOWN; | |
| 90 } | |
| 91 | |
| 92 /** | |
| 93 * The values used for BrowserChecker status to indicate success or | |
| 94 * a specific error. This can be used instead of this.browserSupportMessage_. | |
| 95 * @enum {id} | |
| 96 */ | |
| 97 browser_version.BrowserChecker.StatusValues = { | |
| 98 UNKNOWN: 0, | |
| 99 NACL_ENABLED: 1, | |
| 100 NON_CHROME_BROWSER: 2, | |
| 101 CHROME_VERSION_TOO_OLD: 3, | |
| 102 NACL_NOT_ENABLED: 4, | |
| 103 NOT_USING_SERVER: 5 | |
| 104 }; | |
| 105 | |
| 106 /** | |
| 107 * Determines if the plugin with name |name| exists in the browser. | |
| 108 * @param {string} name The name of the plugin. | |
| 109 * @param {Object array} plugins The plugins in this browser. | |
| 110 * @return {bool} |true| if the plugin is found. | |
| 111 */ | |
| 112 browser_version.BrowserChecker.prototype.pluginExists = function(name, | |
| 113 plugins) { | |
| 114 for (var index=0; index < plugins.length; index++) { | |
| 115 var plugin = this.plugins_[index]; | |
| 116 var plugin_name = plugin['name']; | |
| 117 // If the plugin is not found, you can use the Javascript console | |
| 118 // to see the names of the plugins that were found when debugging. | |
| 119 if (plugin_name.indexOf(name) != -1) { | |
| 120 return true; | |
| 121 } | |
| 122 } | |
| 123 return false; | |
| 124 } | |
| 125 | |
| 126 /** | |
| 127 * Returns browserSupportStatus_ which indicates if the browser supports | |
| 128 * Native Client. Values are defined as literals in | |
| 129 * browser_version.BrowserChecker.StatusValues. | |
| 130 * @ return {int} Level of NaCl support. | |
| 131 */ | |
| 132 browser_version.BrowserChecker.prototype.getBrowserSupportStatus = function() { | |
| 133 return this.browserSupportStatus_; | |
| 134 } | |
| 135 | |
| 136 /** | |
| 137 * Returns isValidBrowser (true/false) to indicate if the browser supports | |
| 138 * Native Client. | |
| 139 * @ return {bool} If this browser has NativeClient and correct version. | |
| 140 */ | |
| 141 browser_version.BrowserChecker.prototype.getIsValidBrowser = function() { | |
| 142 return this.isValidBrowser_; | |
| 143 } | |
| 144 | |
| 145 /** | |
| 146 * Returns a message that indicates if the browser supports Native Client, | |
| 147 * possibly including information on what prevents this browser from | |
| 148 * supporting it. | |
| 149 * @ return {string} The string that can be displayed by the browser. | |
| 150 */ | |
| 151 browser_version.BrowserChecker.prototype.getBrowserSupportMessage = function() { | |
| 152 return this.browserSupportMessage_; | |
| 153 } | |
| 154 | |
| 155 /** | |
| 156 * Extracts the Chrome version from this.appVersion_ to set | |
| 157 * |this.chromeVersion| and checks that |this.chromeVersion| is >= | |
| 158 * |this.minChromeVersion_|. | |
| 159 * In addition, checks for the "Native Client" plugin as a member of | |
| 160 * this.plugins_. | |
| 161 */ | |
| 162 browser_version.BrowserChecker.prototype.checkBrowser = function() { | |
| 163 var versionPatt = /Chrome\/(\d+)\.(\d+)\.(\d+)\.(\d+)/; | |
| 164 var result = this.appVersion_.match(versionPatt); | |
| 165 | |
| 166 // |result| stores the Chrome version number. | |
| 167 if (!result) { | |
| 168 this.browserSupportMessage_ = 'This browser does NOT ' | |
| 169 + 'support Native Client.'; | |
| 170 this.isValidBrowser_ = false; | |
| 171 this.browserSupportStatus_ = | |
| 172 browser_version.BrowserChecker.StatusValues.NON_CHROME_BROWSER; | |
| 173 } else { | |
| 174 this.chromeVersion_ = result[1]; | |
| 175 // We know we have Chrome, check version and/or plugin named Native Client | |
| 176 if (this.chromeVersion_ >= this.minChromeVersion_) { | |
| 177 var found_nacl = this.pluginExists('Native Client', this.plugins_); | |
| 178 if (found_nacl) { | |
| 179 this.browserSupportMessage_ = 'Native Client enabled' | |
| 180 + ' in Google Chrome version ' + this.chromeVersion_ + '.'; | |
| 181 this.isValidBrowser_ = true; | |
| 182 this.browserSupportStatus_ = | |
| 183 browser_version.BrowserChecker.StatusValues.NACL_ENABLED; | |
| 184 } else { | |
| 185 this.browserSupportMessage_ = 'Native Client not enabled!<br>' | |
| 186 + 'To run Native Client modules, go to about:flags in a tab,<br>' | |
| 187 + 'click the Enable button beneath the Native Client section,<br>' | |
| 188 + 'and then scroll down to the bottom of the page and press<br>' | |
| 189 + ' the "Restart Now" button.<br><br>' | |
| 190 + 'For more information see ' | |
| 191 // TODO: Fix the link below when we have the new external site | |
| 192 // BUG: http://code.google.com/p/nativeclient/issues/detail?id=1386 | |
| 193 + '<a href="http://code.google.com/p/nativeclient-sdk/wiki/' | |
| 194 + 'HowTo_RunModules#Step_2:_Launch_the_browser_with_--enable-nacl">' | |
| 195 + ' Run Native Client applications</a>.'; | |
| 196 this.isValidBrowser_ = false; | |
| 197 this.browserSupportStatus_ = | |
| 198 browser_version.BrowserChecker.StatusValues.NACL_NOT_ENABLED; | |
| 199 } | |
| 200 } else { | |
| 201 // we are in a version that is less than |minChromeVersion_| | |
| 202 this.browserSupportMessage_ = 'The minimum version of Google Chrome ' | |
| 203 + ' that works with these Native Client examples is version ' | |
| 204 + this.minChromeVersion_ + '.'; | |
| 205 this.isValidBrowser_ = false; | |
| 206 this.browserSupportStatus_ = | |
| 207 browser_version.BrowserChecker.StatusValues.CHROME_VERSION_TOO_OLD; | |
| 208 } | |
| 209 } | |
| 210 var my_protocol = window.location.protocol; | |
| 211 if (my_protocol.indexOf('file') == 0) { | |
| 212 this.browserSupportMessage_ += '<br><br>ERROR: You cannot use local url' | |
| 213 + ' (file:). You need a server, such as "localhost:5103"<br>'; | |
| 214 this.isValidBrowser_ = false; | |
| 215 this.browserSupportStatus_ = | |
| 216 browser_version.BrowserChecker.StatusValues.NOT_USING_SERVER; | |
| 217 } | |
| 218 } | |
| 219 | |
| OLD | NEW |