summaryrefslogtreecommitdiff
path: root/platform/nacl/html/check_browser.js
blob: 9636ad0384752918e7f3b7260d35930110bfc376 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * Copyright (c) 2011 The Native Client Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/**
 * @fileoverview This file provides a BrowserChecker Javascript class.
 * Users can create a BrowserChecker object, invoke checkBrowser(|version|),
 * and then use getIsValidBrowser() and getBrowserSupportStatus()
 * to determine if the browser version is greater than |version|
 * and if the Native Client plugin is found.
 */

// Create a namespace object
var browser_version = browser_version || {};

/**
 * Class to provide checking for version and NativeClient.
 * @param {integer} arg1 An argument that indicates major version of Chrome we
 *     require, such as 14.
 */

/**
 * Constructor for the BrowserChecker.  Sets the major version of
 * Chrome that is required to |minChromeVersion|.
 * @param minChromeVersion   The earliest major version of chrome that
 *     is supported.  If the Chrome browser version is less than
 *     |minChromeVersion| then |isValidBrowswer| will be set to false.
 * @param opt_maxChromeVersion   Ignored.  Retained for backwards compatibility.
 * @param appVersion  The application version string.
 * @param plugins     The plugins that exist in the browser.
 * @constructor
 */
browser_version.BrowserChecker = function(minChromeVersion,
                                          appVersion, plugins,
                                          opt_maxChromeVersion) {
  /**
   * Version specified by the user. This class looks to see if the browser
   * version is >= |minChromeVersion_|.
   * @type {integer}
   * @private
   */
  this.minChromeVersion_ = minChromeVersion;

  /**
   * List of Browser plugin objects.
   * @type {Ojbect array}
   * @private
   */
  this.plugins_ = plugins;

  /**
   * Application version string from the Browser.
   * @type {integer}
   * @private
   */
  this.appVersion_ = appVersion;

  /**
   * Flag used to indicate if the browser has Native Client and is if the
   * browser version is recent enough.
   * @type {boolean}
   * @private
   */
  this.isValidBrowser_ = false;

  /**
   * Actual major version of Chrome -- found by querying the browser.
   * @type {integer}
   * @private
   */
  this.chromeVersion_ = null;

  /**
   * Browser support status. This allows the user to get a detailed status
   * rather than using this.browserSupportMessage.
   */
  this.browserSupportStatus_ =
      browser_version.BrowserChecker.StatusValues.UNKNOWN;
}

/**
 * The values used for BrowserChecker status to indicate success or
 * a specific error.
 * @enum {id}
 */
browser_version.BrowserChecker.StatusValues = {
  UNKNOWN: 0,
  NACL_ENABLED: 1,
  UNKNOWN_BROWSER: 2,
  CHROME_VERSION_TOO_OLD: 3,
  NACL_NOT_ENABLED: 4,
  NOT_USING_SERVER: 5
};

/**
 * Determines if the plugin with name |name| exists in the browser.
 * @param {string} name The name of the plugin.
 * @param {Object array} plugins The plugins in this browser.
 * @return {bool} |true| if the plugin is found.
 */
browser_version.BrowserChecker.prototype.pluginExists = function(name,
                                                                 plugins) {
  for (var index=0; index < plugins.length; index++) {
    var plugin = this.plugins_[index];
    var plugin_name = plugin['name'];
    // If the plugin is not found, you can use the Javascript console
    // to see the names of the plugins that were found when debugging.
    if (plugin_name.indexOf(name) != -1) {
      return true;
    }
  }
  return false;
}

/**
 * Returns browserSupportStatus_ which indicates if the browser supports
 * Native Client.  Values are defined as literals in
 * browser_version.BrowserChecker.StatusValues.
 * @ return {int} Level of NaCl support.
 */
browser_version.BrowserChecker.prototype.getBrowserSupportStatus = function() {
  return this.browserSupportStatus_;
}

/**
 * Returns isValidBrowser (true/false) to indicate if the browser supports
 * Native Client.
 * @ return {bool} If this browser has NativeClient and correct version.
 */
browser_version.BrowserChecker.prototype.getIsValidBrowser = function() {
  return this.isValidBrowser_;
}

/**
 * Checks to see if this browser can support Native Client applications.
 * For Chrome browsers, checks to see if the "Native Client" plugin is
 * enabled.
 */
browser_version.BrowserChecker.prototype.checkBrowser = function() {
  var versionPatt = /Chrome\/(\d+)\.(\d+)\.(\d+)\.(\d+)/;
  var result = this.appVersion_.match(versionPatt);

  // |result| stores the Chrome version number.
  if (!result) {
    this.isValidBrowser_ = false;
    this.browserSupportStatus_ =
        browser_version.BrowserChecker.StatusValues.UNKNOWN_BROWSER;
  } else {
    this.chromeVersion_ = result[1];
    // We know we have Chrome, check version and/or plugin named Native Client
    if (this.chromeVersion_ >= this.minChromeVersion_) {
      var found_nacl = this.pluginExists('Native Client', this.plugins_);
      if (found_nacl) {
        this.isValidBrowser_ = true;
        this.browserSupportStatus_ =
            browser_version.BrowserChecker.StatusValues.NACL_ENABLED;
      } else {
        this.isValidBrowser_ = false;
        this.browserSupportStatus_ =
            browser_version.BrowserChecker.StatusValues.NACL_NOT_ENABLED;
      }
    } else {
      // We are in a version that is less than |minChromeVersion_|
      this.isValidBrowser_ = false;
      this.browserSupportStatus_ =
          browser_version.BrowserChecker.StatusValues.CHROME_VERSION_TOO_OLD;
    }
  }
  var my_protocol = window.location.protocol;
  if (my_protocol.indexOf('file') == 0) {
    this.isValidBrowser_ = false;
    this.browserSupportStatus_ =
        browser_version.BrowserChecker.StatusValues.NOT_USING_SERVER;
  }
}