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

Unified Diff: chrome/browser/resources/whispernet_proxy/js/init.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/init.js
diff --git a/chrome/browser/resources/whispernet_proxy/js/init.js b/chrome/browser/resources/whispernet_proxy/js/init.js
new file mode 100644
index 0000000000000000000000000000000000000000..cea8be9091867acdfa6b06544a6d11c32fd44ee3
--- /dev/null
+++ b/chrome/browser/resources/whispernet_proxy/js/init.js
@@ -0,0 +1,102 @@
+// 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.
+// Globals holding our encoder and decoder. We will never have more than one
+// Global variable that will be used to access this Nacl bridge.
+var whispernetNacl = null;
+
+// copy of an encoder or a decoder at a time.
+var whisperEncoder = null;
+var whisperDecoder = null;
+
+/**
+ * Initialize the whispernet encoder and decoder.
+ * @param {Object} audioParams Audio parameters used to initialize the encoder
+ * and decoder.
+ */
+function initialize(audioParams) {
+ if (!whispernetNacl) {
+ chrome.copresencePrivate.sendInitialized(false);
+ return;
+ }
+
+ console.log('init: creating encoder!');
+ whisperEncoder = new WhisperEncoder(audioParams.play, whispernetNacl);
+ whisperEncoder.onAudioData(chrome.copresencePrivate.sendSamples);
+
+ console.log('init: creating decoder!');
+ whisperDecoder = new WhisperDecoder(audioParams.record, whispernetNacl);
+ whisperDecoder.onReceive(chrome.copresencePrivate.sendFound);
+ whisperDecoder.onDetectBroadcast(chrome.copresencePrivate.sendDetect);
+
+ chrome.copresencePrivate.sendInitialized(true);
+}
+
+/**
+ * Sends a request to whispernet to encode a token.
+ * @param {string} token Token to encode. This needs to be a base64 string.
+ */
+function encodeTokenRequest(token) {
+ if (whisperEncoder) {
+ whisperEncoder.encode(atob(token), true);
+ } else {
+ console.error('encodeTokenRequest: Whisper not initialized!');
+ }
+}
+
+/**
+ * Sends a request to whispernet to decode samples.
+ * @param {ArrayBuffer} samples Array of samples to decode.
+ */
+function decodeSamplesRequest(samples) {
+ if (whisperDecoder) {
+ whisperDecoder.processSamples(samples);
+ } else {
+ console.error('decodeSamplesRequest: Whisper not initialized!');
+ }
+}
+
+/**
+ * Sends a request to whispernet to detect broadcast.
+ */
+function detectBroadcastRequest() {
+ if (whisperDecoder) {
+ whisperDecoder.detectBroadcast();
+ } else {
+ console.error('detectBroadcastRequest: Whisper not initialized!');
+ }
+}
+
+/**
+ * Initialize our listerners and signal that the extension is loaded.
+ */
+function onWhispernetLoaded() {
+ console.log('init: Nacl ready!');
+
+ // Setup all the listeners for the private API.
+ chrome.copresencePrivate.onInitialize.addListener(initialize);
+ chrome.copresencePrivate.onEncodeTokenRequest.addListener(encodeTokenRequest);
+ chrome.copresencePrivate.onDecodeSamplesRequest.addListener(
+ decodeSamplesRequest);
+ chrome.copresencePrivate.onDetectBroadcastRequest.addListener(
+ detectBroadcastRequest);
+
+ // This first initialized is sent to indicate that the library is loaded.
+ // Every other time, it will be sent only when Chrome wants to reinitialize
+ // the encoder and decoder.
+ chrome.copresencePrivate.sendInitialized(true);
+}
+
+/**
+ * Initialize the whispernet Nacl bridge.
+ */
+function initWhispernet() {
+ console.log('init: Starting Nacl bridge.');
+ // TODO(rkc): Figure out how to embed the .nmf and the .pexe into component
+ // resources without having to rename them to .js.
+ whispernetNacl = new NaclBridge('whispernet_proxy.nmf.html',
+ onWhispernetLoaded);
+}
+
+window.addEventListener('DOMContentLoaded', initWhispernet);

Powered by Google App Engine
This is Rietveld 408576698