Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Unified Diff: chrome/browser/resources/whispernet_proxy/js/nacl.js

Issue 438513002: Add the whispernet proxy. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/whispernet_proxy/js/nacl.js
diff --git a/chrome/browser/resources/whispernet_proxy/js/nacl.js b/chrome/browser/resources/whispernet_proxy/js/nacl.js
new file mode 100644
index 0000000000000000000000000000000000000000..5929cf210a9f8dd34bdcfb505531dea3fd33ca6f
--- /dev/null
+++ b/chrome/browser/resources/whispernet_proxy/js/nacl.js
@@ -0,0 +1,100 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
xiyuan 2014/07/31 19:22:22 nit: 'use strict';
rkc 2014/07/31 23:12:53 Done.
+/**
+ * Constructor for the Nacl bridge to the whispernet wrapper.
+ * @param {string} nmf The relative path to the nmf containing the location of
+ * the whispernet Nacl wrapper.
+ * @param {function()} readyCallback Callback to be called once we've loaded the
+ * whispernet wrapper.
+ */
+function NaclBridge(nmf, readyCallback) {
+ this.readyCallback_ = readyCallback;
+ this.callbacks_ = [];
+ this.isEnabled_ = false;
+ this.naclId_ = this.loadNacl_(nmf);
+}
+
+/**
+ * Method to send generic byte data to the whispernet wrapper.
+ * @param {string} data Raw data to send to the whispernet wrapper.
+ */
+NaclBridge.prototype.send = function(data) {
+ if (this.isEnabled_) {
+ this.embed_.postMessage(data);
+ } else {
+ console.error('Whisper Nacl Bridge not initialized!');
+ }
+};
+
+/**
+ * Method to add a listener to Nacl messages received by this bridge.
+ * @param {function(Event)} messageCallback Callback to receive the messsage.
+ */
+NaclBridge.prototype.addListener = function(messageCallback) {
+ this.callbacks_.push(messageCallback);
+};
+
+/**
+ * Method that receives Nacl messages and forwards them to registered
+ * callbacks.
+ * @param {Event} e Event from the whispernet wrapper.
+ * @private
+ */
+NaclBridge.prototype.onMessage_ = function(e) {
+ if (this.isEnabled_) {
+ this.callbacks_.forEach(function(callback) {
+ callback(e);
+ });
+ }
+};
+
+/**
+ * Injects the <embed> for this nacl manifest URL, generating a unique ID.
+ * @param {string} manifestUrl Url to the nacl manifest to load.
+ * @return {number} generated ID.
+ * @private
+ */
+NaclBridge.prototype.loadNacl_ = function(manifestUrl) {
+ var id = 'nacl-' + Math.floor(Math.random() * 10000);
+ this.embed_ = document.createElement('embed');
+ this.embed_.name = 'nacl_module';
+ this.embed_.width = 1;
+ this.embed_.height = 1;
+ this.embed_.src = manifestUrl;
+ this.embed_.id = id;
+ this.embed_.type = 'application/x-pnacl';
+
+ // Wait for the element to load and callback.
+ this.embed_.addEventListener('load', this.onNaclReady_.bind(this));
+ this.embed_.addEventListener('error', this.onNaclError_.bind(this));
+
+ // Inject the embed string into the page.
+ document.body.appendChild(this.embed_);
+
+ // Listen for messages from the NaCl module.
+ window.addEventListener('message', this.onMessage_.bind(this), true);
+ return id;
+};
+
+/**
+ * Callback that is called when the Whispernet wrapper is loaded and forward
+ * that status to the callback registered with us in the constructor.
+ * @private
+ */
+NaclBridge.prototype.onNaclReady_ = function() {
+ if (this.readyCallback_)
+ this.readyCallback_();
+ this.isEnabled_ = true;
+};
+
+/**
+ * Callback that handles Nacl errors.
+ * @param {string} msg Error string.
+ * @private
+ */
+NaclBridge.prototype.onNaclError_ = function(msg) {
+ // TODO(rkc): Handle error from NaCl better.
+ console.error('NaCl error', msg);
+};

Powered by Google App Engine
This is Rietveld 408576698