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 getBrowserSupportStatus() | |
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 * Browser support status. This allows the user to get a detailed status | |
77 * rather than using this.browserSupportMessage. | |
78 */ | |
79 this.browserSupportStatus_ = | |
80 browser_version.BrowserChecker.StatusValues.UNKNOWN; | |
81 } | |
82 | |
83 /** | |
84 * The values used for BrowserChecker status to indicate success or | |
85 * a specific error. | |
86 * @enum {id} | |
87 */ | |
88 browser_version.BrowserChecker.StatusValues = { | |
89 UNKNOWN: 0, | |
90 NACL_ENABLED: 1, | |
91 UNKNOWN_BROWSER: 2, | |
92 CHROME_VERSION_TOO_OLD: 3, | |
93 NACL_NOT_ENABLED: 4, | |
94 NOT_USING_SERVER: 5 | |
95 }; | |
96 | |
97 /** | |
98 * Determines if the plugin with name |name| exists in the browser. | |
99 * @param {string} name The name of the plugin. | |
100 * @param {Object array} plugins The plugins in this browser. | |
101 * @return {bool} |true| if the plugin is found. | |
102 */ | |
103 browser_version.BrowserChecker.prototype.pluginExists = function(name, | |
104 plugins) { | |
105 for (var index=0; index < plugins.length; index++) { | |
106 var plugin = this.plugins_[index]; | |
107 var plugin_name = plugin['name']; | |
108 // If the plugin is not found, you can use the Javascript console | |
109 // to see the names of the plugins that were found when debugging. | |
110 if (plugin_name.indexOf(name) != -1) { | |
111 return true; | |
112 } | |
113 } | |
114 return false; | |
115 } | |
116 | |
117 /** | |
118 * Returns browserSupportStatus_ which indicates if the browser supports | |
119 * Native Client. Values are defined as literals in | |
120 * browser_version.BrowserChecker.StatusValues. | |
121 * @ return {int} Level of NaCl support. | |
122 */ | |
123 browser_version.BrowserChecker.prototype.getBrowserSupportStatus = function() { | |
124 return this.browserSupportStatus_; | |
125 } | |
126 | |
127 /** | |
128 * Returns isValidBrowser (true/false) to indicate if the browser supports | |
129 * Native Client. | |
130 * @ return {bool} If this browser has NativeClient and correct version. | |
131 */ | |
132 browser_version.BrowserChecker.prototype.getIsValidBrowser = function() { | |
133 return this.isValidBrowser_; | |
134 } | |
135 | |
136 /** | |
137 * Checks to see if this browser can support Native Client applications. | |
138 * For Chrome browsers, checks to see if the "Native Client" plugin is | |
139 * enabled. | |
140 */ | |
141 browser_version.BrowserChecker.prototype.checkBrowser = function() { | |
142 var versionPatt = /Chrome\/(\d+)\.(\d+)\.(\d+)\.(\d+)/; | |
143 var result = this.appVersion_.match(versionPatt); | |
144 | |
145 // |result| stores the Chrome version number. | |
146 if (!result) { | |
147 this.isValidBrowser_ = false; | |
148 this.browserSupportStatus_ = | |
149 browser_version.BrowserChecker.StatusValues.UNKNOWN_BROWSER; | |
150 } else { | |
151 this.chromeVersion_ = result[1]; | |
152 // We know we have Chrome, check version and/or plugin named Native Client | |
153 if (this.chromeVersion_ >= this.minChromeVersion_) { | |
154 var found_nacl = this.pluginExists('Native Client', this.plugins_); | |
155 if (found_nacl) { | |
156 this.isValidBrowser_ = true; | |
157 this.browserSupportStatus_ = | |
158 browser_version.BrowserChecker.StatusValues.NACL_ENABLED; | |
159 } else { | |
160 this.isValidBrowser_ = false; | |
161 this.browserSupportStatus_ = | |
162 browser_version.BrowserChecker.StatusValues.NACL_NOT_ENABLED; | |
163 } | |
164 } else { | |
165 // We are in a version that is less than |minChromeVersion_| | |
166 this.isValidBrowser_ = false; | |
167 this.browserSupportStatus_ = | |
168 browser_version.BrowserChecker.StatusValues.CHROME_VERSION_TOO_OLD; | |
169 } | |
170 } | |
171 var my_protocol = window.location.protocol; | |
172 if (my_protocol.indexOf('file') == 0) { | |
173 this.isValidBrowser_ = false; | |
174 this.browserSupportStatus_ = | |
175 browser_version.BrowserChecker.StatusValues.NOT_USING_SERVER; | |
176 } | |
177 } | |
178 | |
OLD | NEW |