Fix multi-arch support for Chrome: start downloading the nexe when SetWindow is called instead of doing it during initialization. Note that Firefox does not call SetWindow, so to keep it working we need to handle the "nexes" property at different times in Chrome and in Firefox.
Ideally we will probably want to switch all the tests to use multiarch support, but first we need to get one such test running on Chrome bots (see the link to a Chrome CL below).
BUG=http://code.google.com/p/nativeclient/issues/detail?id=500
TEST=http://codereview.chromium.org/3171010/show


Review URL: http://codereview.chromium.org/3145013

git-svn-id: svn://svn.chromium.org/native_client/trunk/src/native_client/tests/prebuilt@3005 fcba33aa-ac0c-11dd-b9e7-8d5594d729c2
diff --git a/nacl_js_lib.js b/nacl_js_lib.js
new file mode 100644
index 0000000..42d1c48
--- /dev/null
+++ b/nacl_js_lib.js
@@ -0,0 +1,149 @@
+// JavaScript Library for Nacl Tests and Demos
+
+function NaclLib(embed_name, status_id, num_retries) {
+   this.embed_name_ = embed_name;
+   this.statusfield_ = document.getElementById(status_id);
+   this.status_ = "WAIT";
+   this.message_ = "";
+   this.handler_ = null;
+   this.retries_ = num_retries;
+};
+
+
+NaclLib.prototype.getStatus = function() {
+  return this.status_;
+};
+
+
+NaclLib.prototype.getMessage = function() {
+  return this.message_;
+};
+
+
+NaclLib.prototype.cleanUp = function() {
+  if (this.handler_) {
+     clearInterval(this._handler);
+     this.handler_ = null;
+  }
+};
+
+
+NaclLib.prototype.setStatus = function() {
+  this.statusfield_.innerHTML =
+      this.status_ + ": " + this.message_;
+};
+
+
+NaclLib.prototype.setStatusWait = function(message) {
+  this.status_ = "WAIT";
+  this.message_ = "" + this.retries_ + " " + message;
+  this.setStatus()
+};
+
+
+NaclLib.prototype.setStatusError = function(message) {
+  this.status_ = "ERROR";
+  this.message_ = message;
+  this.setStatus()
+};
+
+
+NaclLib.prototype.setStatusSuccess = function(message) {
+  this.status_ = "SUCCESS";
+  this.message_ = message;
+  this.setStatus()
+};
+
+
+NaclLib.prototype.numModulesReady = function(modules) {
+  var count = 0;
+  for (var i = 0; i < modules.length; i++) {
+    if (modules[i].__moduleReady == 1) {
+      count += 1;
+    }
+  }
+  return count;
+};
+
+
+NaclLib.prototype.areTherePluginProblems = function(modules) {
+  for (var i = 0; i < modules.length; i++) {
+    if (modules[i].__moduleReady == undefined) return 1;
+  }
+  return 0;
+};
+
+
+NaclLib.prototype.checkModuleReadiness = function() {
+  // Work around bug that does not disable the handler.
+  if (!this.handler_)
+    return;
+
+  if (this.retries_ == 0) {
+    this.cleanUp();
+    this.setStatusError("The Native Client modules are loading too slowly");
+    return;
+  }
+  this.retries_ -= 1;
+
+  // Find all elements with name "this.embed_name_". This should be the list
+  // of all NaCl modules on the page. Note that passing in such a list at
+  // initialization time would sometimes pass the list of scriptable objects
+  // (as desired) but would sometimes pass the list of embed tags, depending
+  // on a start-up race condition. As such, pass the "name" attribute of the
+  // <embed> tags and then gather the list of all of those scriptable objects
+  // each time this method is invoked.
+  var module_list = document.getElementsByName(this.embed_name_);
+  var num_ready = this.numModulesReady(module_list);
+
+  if (module_list.length == num_ready) {
+    if (this.wait) {
+      var result = this.wait();
+      if (result) {
+        this.setStatusWait(result);
+        return;
+      }
+    }
+
+    this.cleanUp();
+
+    var result;
+    try {
+      result = this.test();
+    } catch(e) {
+      this.setStatusError(e);
+      return;
+    }
+
+    if (result == "") {
+      this.setStatusSuccess("");
+    } else {
+      this.setStatusError(result);
+    }
+
+    return;
+  }
+
+  this.setStatusWait("Loaded " + num_ready + "/" + module_list.length +
+                     " modules");
+
+  if (this.areTherePluginProblems(module_list)) {
+    this.cleanUp();
+    this.setStatusError("The Native Client plugin was unable to load");
+    return;
+  }
+};
+
+
+// Workaround for JS inconsistent scoping behavior
+function wrapperForCheckModuleReadiness(that) {
+  that.checkModuleReadiness();
+}
+
+
+NaclLib.prototype.waitForModulesAndRunTests = function() {
+  // avoid regsitering two handlers
+  if (!this.handler_) {
+    this.handler_ = setInterval(wrapperForCheckModuleReadiness, 100, this);
+  }
+};
diff --git a/srpc_hw.html b/srpc_hw.html
new file mode 100644
index 0000000..8b12ac8
--- /dev/null
+++ b/srpc_hw.html
@@ -0,0 +1,81 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <title>SRPC Simple Plug-in</title>
+    <META HTTP-EQUIV="Pragma" CONTENT="no-cache" />
+    <META HTTP-EQUIV="Expires" CONTENT="-1" />
+  </head>
+  <body onload="nacllib.waitForModulesAndRunTests();"
+        onunload="nacllib.cleanUp();" >
+<script type="text/javascript">
+//<![CDATA[
+function fortytwo() {
+  var result = "";
+  try {
+    result = document.getElementById('pluginobj').fortytwo();
+  } catch(e) {
+    result = "ERROR: " + e;
+  }
+  alert("" + result);
+}
+
+function helloworld() {
+  var result = "";
+  try {
+    result = document.getElementById('pluginobj').helloworld();
+  } catch(e) {
+    result = "ERROR: " + e;
+  }
+  alert("" + result);
+}
+//]]>
+</script>
+
+<h1>Native Client SRPC Simple Plug-in</h1>
+<p>
+  <button onclick='fortytwo()'>Call fortytwo()</button>
+  <button onclick='helloworld()'>Call helloworld()</button>
+
+  <embed name="nacl_module"
+         id="pluginobj"
+         width=0 height=0
+         nexes="
+x86-32:  /tests/prebuilt/x86/srpc_hw.nexe
+x86-64:  /tests/prebuilt/x64/srpc_hw.nexe
+ARM:     /tests/prebuilt/arm/srpc_hw.nexe"
+         type="application/x-nacl-srpc" />
+</p>
+
+<p>If the plug-in is working correctly, a click on the "Call fortytwo" button
+  should open a popup dialog containing <b>42</b> as value.</p>
+
+<p> Clicking on the "Call helloworld" button
+  should open a popup dialog containing <b>hello, world</b> as value.</p>
+
+<h2>Status</h2>
+<div id=status>NO-STATUS</div>
+
+<script type="text/javascript" src="nacl_js_lib.js"></script>
+<script type="text/javascript">
+//<![CDATA[
+var nacllib = new NaclLib("nacl_module", "status", 500);
+
+nacllib.test = function() {
+  var plugin = document.getElementById("pluginobj");
+  if ('42' != plugin.fortytwo()) {
+    return "expected 42";
+  }
+
+  if ('hello, world.' != plugin.helloworld()) {
+    return "expected 'hello, world.'";
+  }
+
+  document.cookie = 'status=OK';
+
+  return "";
+};
+//]]>
+</script>
+
+  </body>
+</html>