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

Side by Side Diff: chrome/browser/resources/whispernet_proxy/js/wrapper.js

Issue 2131993002: Delete the audio modem and copresence private APIs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@eol
Patch Set: Sync again Created 4 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 unified diff | Download patch
OLDNEW
(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
5 'use strict';
6
7 /**
8 * Function to convert an array of bytes to a base64 string
9 * TODO(rkc): Change this to use a Uint8array instead of a string.
10 * @param {string} bytes String containing the bytes we want to convert.
11 * @return {string} String containing the base64 representation.
12 */
13 function bytesToBase64(bytes) {
14 var bstr = '';
15 for (var i = 0; i < bytes.length; ++i)
16 bstr += String.fromCharCode(bytes[i]);
17 return btoa(bstr).replace(/=/g, '');
18 }
19
20 /**
21 * Function to convert a string to an array of bytes.
22 * @param {string} str String to convert.
23 * @return {Array} Array containing the string.
24 */
25 function stringToArray(str) {
26 var buffer = [];
27 for (var i = 0; i < str.length; ++i)
28 buffer[i] = str.charCodeAt(i);
29 return buffer;
30 }
31
32 /**
33 * Creates a whispernet encoder.
34 * @constructor
35 * @param {Object} params Audio parameters for the whispernet encoder.
36 * @param {Object} whisperNacl The NaclBridge object, used to communicate with
37 * the whispernet wrapper.
38 * @param {string} clientId A string identifying the requester.
39 */
40 function WhisperEncoder(params, whisperNacl, clientId) {
41 this.whisperNacl_ = whisperNacl;
42 this.whisperNacl_.addListener(this.onNaclMessage_.bind(this));
43 this.clientId_ = clientId;
44
45 var msg = {
46 type: 'initialize_encoder',
47 client_id: clientId,
48 params: params
49 };
50
51 this.whisperNacl_.send(msg);
52 }
53
54 /**
55 * Method to encode a token.
56 * @param {Object} params Encode token parameters object.
57 */
58 WhisperEncoder.prototype.encode = function(params) {
59 // Pad the token before decoding it.
60 var token = params.token.token;
61 while (token.length % 4 > 0)
62 token += '=';
63
64 var msg = {
65 type: 'encode_token',
66 client_id: this.clientId_,
67 // Trying to send the token in binary form to Nacl doesn't work correctly.
68 // We end up with the correct string + a bunch of extra characters. This is
69 // true of returning a binary string too; hence we communicate back and
70 // forth by converting the bytes into an array of integers.
71 token: stringToArray(atob(token)),
72 repetitions: params.repetitions,
73 use_dtmf: params.token.audible,
74 use_crc: params.tokenParams.crc,
75 use_parity: params.tokenParams.parity
76 };
77
78 this.whisperNacl_.send(msg);
79 };
80
81 /**
82 * Method to handle messages from the whispernet NaCl wrapper.
83 * @param {Event} e Event from the whispernet wrapper.
84 * @private
85 */
86 WhisperEncoder.prototype.onNaclMessage_ = function(e) {
87 var msg = e.data;
88 if (msg.type == 'encode_token_response' && msg.client_id == this.clientId_) {
89 chrome.copresencePrivate.sendSamples(this.clientId_,
90 { token: bytesToBase64(msg.token), audible: msg.audible }, msg.samples);
91 }
92 };
93
94 /**
95 * Creates a whispernet decoder.
96 * @constructor
97 * @param {Object} params Audio parameters for the whispernet decoder.
98 * @param {Object} whisperNacl The NaclBridge object, used to communicate with
99 * the whispernet wrapper.
100 * @param {string} clientId A string identifying the requester.
101 */
102 function WhisperDecoder(params, whisperNacl, clientId) {
103 this.whisperNacl_ = whisperNacl;
104 this.whisperNacl_.addListener(this.onNaclMessage_.bind(this));
105 this.clientId_ = clientId;
106
107 var msg = {
108 type: 'initialize_decoder',
109 client_id: clientId,
110 params: params
111 };
112 this.whisperNacl_.send(msg);
113 }
114
115 /**
116 * Method to request the decoder to process samples.
117 * @param {Object} params Process samples parameters object.
118 */
119 WhisperDecoder.prototype.processSamples = function(params) {
120 var msg = {
121 type: 'decode_tokens',
122 client_id: this.clientId_,
123 data: params.samples,
124
125 decode_audible: params.decodeAudible,
126 token_length_dtmf: params.audibleTokenParams.length,
127 crc_dtmf: params.audibleTokenParams.crc,
128 parity_dtmf: params.audibleTokenParams.parity,
129
130 decode_inaudible: params.decodeInaudible,
131 token_length_dsss: params.inaudibleTokenParams.length,
132 crc_dsss: params.inaudibleTokenParams.crc,
133 parity_dsss: params.inaudibleTokenParams.parity,
134 };
135
136 this.whisperNacl_.send(msg);
137 };
138
139 /**
140 * Method to handle messages from the whispernet NaCl wrapper.
141 * @param {Event} e Event from the whispernet wrapper.
142 * @private
143 */
144 WhisperDecoder.prototype.onNaclMessage_ = function(e) {
145 var msg = e.data;
146 if (msg.type == 'decode_tokens_response' && msg.client_id == this.clientId_) {
147 this.handleCandidates_(msg.tokens, msg.audible);
148 }
149 };
150
151 /**
152 * Method to receive tokens from the decoder and process and forward them to the
153 * token callback registered with us.
154 * @param {!Array.string} candidates Array of token candidates.
155 * @param {boolean} audible Whether the received candidates are from the audible
156 * decoder or not.
157 * @private
158 */
159 WhisperDecoder.prototype.handleCandidates_ = function(candidates, audible) {
160 if (!candidates || candidates.length == 0)
161 return;
162
163 var returnCandidates = [];
164 for (var i = 0; i < candidates.length; ++i) {
165 returnCandidates[i] = { token: bytesToBase64(candidates[i]),
166 audible: audible };
167 }
168 chrome.copresencePrivate.sendFound(this.clientId_, returnCandidates);
169 };
OLDNEW
« no previous file with comments | « chrome/browser/resources/whispernet_proxy/js/nacl.js ('k') | chrome/browser/resources/whispernet_proxy/manifest.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698