OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
xiyuan
2014/07/31 19:22:22
nit: 'use strict';
rkc
2014/07/31 23:12:53
Done.
| |
5 /** | |
6 * Constructor for the Nacl bridge to the whispernet wrapper. | |
7 * @param {string} nmf The relative path to the nmf containing the location of | |
8 * the whispernet Nacl wrapper. | |
9 * @param {function()} readyCallback Callback to be called once we've loaded the | |
10 * whispernet wrapper. | |
11 */ | |
12 function NaclBridge(nmf, readyCallback) { | |
13 this.readyCallback_ = readyCallback; | |
14 this.callbacks_ = []; | |
15 this.isEnabled_ = false; | |
16 this.naclId_ = this.loadNacl_(nmf); | |
17 } | |
18 | |
19 /** | |
20 * Method to send generic byte data to the whispernet wrapper. | |
21 * @param {string} data Raw data to send to the whispernet wrapper. | |
22 */ | |
23 NaclBridge.prototype.send = function(data) { | |
24 if (this.isEnabled_) { | |
25 this.embed_.postMessage(data); | |
26 } else { | |
27 console.error('Whisper Nacl Bridge not initialized!'); | |
28 } | |
29 }; | |
30 | |
31 /** | |
32 * Method to add a listener to Nacl messages received by this bridge. | |
33 * @param {function(Event)} messageCallback Callback to receive the messsage. | |
34 */ | |
35 NaclBridge.prototype.addListener = function(messageCallback) { | |
36 this.callbacks_.push(messageCallback); | |
37 }; | |
38 | |
39 /** | |
40 * Method that receives Nacl messages and forwards them to registered | |
41 * callbacks. | |
42 * @param {Event} e Event from the whispernet wrapper. | |
43 * @private | |
44 */ | |
45 NaclBridge.prototype.onMessage_ = function(e) { | |
46 if (this.isEnabled_) { | |
47 this.callbacks_.forEach(function(callback) { | |
48 callback(e); | |
49 }); | |
50 } | |
51 }; | |
52 | |
53 /** | |
54 * Injects the <embed> for this nacl manifest URL, generating a unique ID. | |
55 * @param {string} manifestUrl Url to the nacl manifest to load. | |
56 * @return {number} generated ID. | |
57 * @private | |
58 */ | |
59 NaclBridge.prototype.loadNacl_ = function(manifestUrl) { | |
60 var id = 'nacl-' + Math.floor(Math.random() * 10000); | |
61 this.embed_ = document.createElement('embed'); | |
62 this.embed_.name = 'nacl_module'; | |
63 this.embed_.width = 1; | |
64 this.embed_.height = 1; | |
65 this.embed_.src = manifestUrl; | |
66 this.embed_.id = id; | |
67 this.embed_.type = 'application/x-pnacl'; | |
68 | |
69 // Wait for the element to load and callback. | |
70 this.embed_.addEventListener('load', this.onNaclReady_.bind(this)); | |
71 this.embed_.addEventListener('error', this.onNaclError_.bind(this)); | |
72 | |
73 // Inject the embed string into the page. | |
74 document.body.appendChild(this.embed_); | |
75 | |
76 // Listen for messages from the NaCl module. | |
77 window.addEventListener('message', this.onMessage_.bind(this), true); | |
78 return id; | |
79 }; | |
80 | |
81 /** | |
82 * Callback that is called when the Whispernet wrapper is loaded and forward | |
83 * that status to the callback registered with us in the constructor. | |
84 * @private | |
85 */ | |
86 NaclBridge.prototype.onNaclReady_ = function() { | |
87 if (this.readyCallback_) | |
88 this.readyCallback_(); | |
89 this.isEnabled_ = true; | |
90 }; | |
91 | |
92 /** | |
93 * Callback that handles Nacl errors. | |
94 * @param {string} msg Error string. | |
95 * @private | |
96 */ | |
97 NaclBridge.prototype.onNaclError_ = function(msg) { | |
98 // TODO(rkc): Handle error from NaCl better. | |
99 console.error('NaCl error', msg); | |
100 }; | |
OLD | NEW |