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

Side by Side Diff: native_client_sdk/src/examples/demo/googledrivefs_demo/example.js

Issue 2156503002: [NaCl SDK] Expose Google Drive to nacl_io. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Obtain a client ID and client secret from Google Developers Console
6 var CLIENT_ID = "TO FILL";
7 var CLIENT_SECRET = "TO FILL";
8
9 var REDIRECT_URI = "http://localhost:5103/index.html";
10
11 var currentURL = window.location.href;
12 var tokenValue = "Set at domContentLoaded()";
13
14 function buttonOnClick() {
15 var array = new Uint32Array(3);
16 window.crypto.getRandomValues(array);
17 var stateValue = array[0].toString() +
18 array[1].toString() +
19 array[2].toString();
20
21 var url = "http://accounts.google.com/o/oauth2/v2/auth?" +
22 "scope=https://www.googleapis.com/auth/drive"
23 + "+" + "https://www.googleapis.com/auth/drive.metadata" + "&" +
24 "state=" + stateValue + "&" +
25 "redirect_uri=http://localhost:5103/index.html" + "&" +
26 "response_type=code" + "&" +
27 "client_id=" + CLIENT_ID;
28
29 window.open(url, "_self");
30 }
31
32 function executeAfterGettingCode() {
33 var codeKeyIndex = currentURL.search("code=");
34 if (-1 == codeKeyIndex) {
35 return -1;
36 }
37 var codeValue = currentURL.substring(codeKeyIndex + 5);
38
39 var xHTTP = new XMLHttpRequest();
40 xHTTP.onreadystatechange = function() {
41 if (xHTTP.readyState == 4 && xHTTP.status == 200) {
42 var obj = JSON.parse(xHTTP.responseText);
43 var url = REDIRECT_URI + "?token=" + obj.access_token;
44
45 window.open(url, "_self");
46 }
47 };
48
49 xHTTP.timeout = 4000; // Set timeout to 4 seconds (4000 milliseconds)
50 xHTTP.ontimeout = function () { alert("Timed out"); }
51
52 var params = "code=" + codeValue + "&" +
53 "client_id=" + CLIENT_ID + "&" +
54 "client_secret=" + CLIENT_SECRET + "&" +
55 "redirect_uri=" + REDIRECT_URI + "&" +
56 "grant_type=authorization_code";
57
58 xHTTP.open("POST", "https://www.googleapis.com/oauth2/v4/token", true);
59 xHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
60 xHTTP.send(params);
61 }
62
5 function moduleDidLoad() { 63 function moduleDidLoad() {
6 common.hideModule(); 64 common.hideModule();
7 } 65 }
8 66
9 function $(id) { 67 function $(id) {
10 return document.getElementById(id); 68 return document.getElementById(id);
11 } 69 }
12 70
13 // Called by the common.js module.
14 function domContentLoaded(name, tc, config, width, height) { 71 function domContentLoaded(name, tc, config, width, height) {
15 navigator.webkitPersistentStorage.requestQuota(5 * 1024 * 1024, 72 common.attachDefaultListeners();
16 function(bytes) { 73
17 common.updateStatus( 74 var tokenKeyIndex = currentURL.search("token=");
18 'Allocated ' + bytes + ' bytes of persistant storage.'); 75 if (-1 == tokenKeyIndex) {
19 common.attachDefaultListeners(); 76 common.createNaClModule(name, tc, config, width, height);
20 common.createNaClModule(name, tc, config, width, height); 77 } else {
21 }, 78 tokenValue = currentURL.substring(tokenKeyIndex + 6);
22 function(e) { alert('Failed to allocate space') }); 79 var tokenMap = { 'token' : tokenValue };
80
81 common.createNaClModule(name, tc, config, width, height, tokenMap);
82
83 document.getElementById("buttonid").disabled = true;
84 }
23 } 85 }
24 86
25 // Called by the common.js module.
26 function attachListeners() { 87 function attachListeners() {
27 var radioEls = document.querySelectorAll('input[type="radio"]'); 88 var radioEls = document.querySelectorAll('input[type="radio"]');
28 for (var i = 0; i < radioEls.length; ++i) { 89 for (var i = 0; i < radioEls.length; ++i) {
29 radioEls[i].addEventListener('click', onRadioClicked); 90 radioEls[i].addEventListener('click', onRadioClicked);
30 } 91 }
31 92
32 // Wire up the 'click' event for each function's button. 93 // Wire up the 'click' event for each function's button.
33 var functionEls = document.querySelectorAll('.function'); 94 var functionEls = document.querySelectorAll('.function');
34 for (var i = 0; i < functionEls.length; ++i) { 95 for (var i = 0; i < functionEls.length; ++i) {
35 var functionEl = functionEls[i]; 96 var functionEl = functionEls[i];
36 var id = functionEl.getAttribute('id'); 97 var id = functionEl.getAttribute('id');
37 var buttonEl = functionEl.querySelector('button'); 98 var buttonEl = functionEl.querySelector('button');
38 99
39 // The function name matches the element id. 100 // The function name matches the element id.
40 var func = window[id]; 101 var func = window[id];
41 buttonEl.addEventListener('click', func); 102 buttonEl.addEventListener('click', func);
42 } 103 }
43
44 $('pipe_input_box').addEventListener('keypress', onPipeInput)
45 $('pipe_output').disabled = true;
46
47 $('pipe_name').addEventListener('change',
48 function() { $('pipe_output').value = ''; })
49 }
50
51 // Called with keypress events on the pipe input box
52 function onPipeInput(e) {
53 // Create an arraybuffer containing the 16-bit char code
54 // from the keypress event.
55 var buffer = new ArrayBuffer(1*2);
56 var bufferView = new Uint16Array(buffer);
57 bufferView[0] = e.charCode;
58
59 // Pass the buffer in a dictionary over the NaCl module
60 var pipeSelect = $('pipe_name');
61 var pipeName = pipeSelect[pipeSelect.selectedIndex].value;
62 var message = {
63 pipe: pipeName,
64 operation: 'write',
65 payload: buffer,
66 };
67 nacl_module.postMessage(message);
68 e.preventDefault();
69 return false;
70 } 104 }
71 105
72 function onRadioClicked(e) { 106 function onRadioClicked(e) {
73 var divId = this.id.slice(5); // skip "radio" 107 var divId = this.id.slice(5); // skip "radio"
74 var functionEls = document.querySelectorAll('.function'); 108 var functionEls = document.querySelectorAll('.function');
75 for (var i = 0; i < functionEls.length; ++i) { 109 for (var i = 0; i < functionEls.length; ++i) {
76 var visible = functionEls[i].id === divId; 110 var visible = functionEls[i].id === divId;
77 if (functionEls[i].id === divId) 111 if (functionEls[i].id === divId)
78 functionEls[i].removeAttribute('hidden'); 112 functionEls[i].removeAttribute('hidden');
79 else 113 else
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 common.logMessage('Changed directory to: ' + dirname + '.'); 260 common.logMessage('Changed directory to: ' + dirname + '.');
227 }); 261 });
228 } 262 }
229 263
230 function getcwd(e) { 264 function getcwd(e) {
231 postCall('getcwd', function(dirname) { 265 postCall('getcwd', function(dirname) {
232 common.logMessage('getcwd: ' + dirname + '.'); 266 common.logMessage('getcwd: ' + dirname + '.');
233 }); 267 });
234 } 268 }
235 269
236 function getaddrinfo(e) {
237 var name = $('getaddrinfoName').value;
238 var family = $('getaddrinfoFamily').value;
239 postCall('getaddrinfo', name, family, function(name, addrType) {
240 common.logMessage('getaddrinfo returned successfully');
241 common.logMessage('ai_cannonname = ' + name + '.');
242 var count = 1;
243 for (var i = 1; i < arguments.length; i+=2) {
244 var msg = 'Address number ' + count + ' = ' + arguments[i] +
245 ' (' + arguments[i+1] + ')';
246 common.logMessage(msg);
247 count += 1;
248 }
249 });
250 }
251
252 function gethostbyname(e) {
253 var name = $('gethostbynameName').value;
254 postCall('gethostbyname', name, function(name, addrType) {
255 common.logMessage('gethostbyname returned successfully');
256 common.logMessage('h_name = ' + name + '.');
257 common.logMessage('h_addr_type = ' + addrType + '.');
258 for (var i = 2; i < arguments.length; i++) {
259 common.logMessage('Address number ' + (i-1) + ' = ' + arguments[i] + '.');
260 }
261 });
262 }
263
264 function connect(e) {
265 var host = $('connectHost').value;
266 var port = parseInt($('connectPort').value, 10);
267 postCall('connect', host, port, function(sockhandle) {
268 common.logMessage('connected');
269 addNameToSelectElements('.sock-handle', sockhandle, '[socket]');
270 });
271 }
272
273 function recv(e) {
274 var handle = parseInt($('recvHandle').value, 10);
275 var bufferSize = parseInt($('recvBufferSize').value, 10);
276 postCall('recv', handle, bufferSize, function(messageLen, message) {
277 common.logMessage("received " + messageLen + ' bytes: ' + message);
278 });
279 }
280
281 function send(e) {
282 var handle = parseInt($('sendHandle').value, 10);
283 var message = $('sendMessage').value;
284 postCall('send', handle, message, function(sentBytes) {
285 common.logMessage("sent bytes: " + sentBytes);
286 });
287 }
288
289 function close(e) {
290 var handle = parseInt($('closeHandle').value, 10);
291 postCall('close', handle, function(sock) {
292 removeNameFromSelectElements('.sock-handle', sock, "[socket]");
293 common.logMessage("closed socket: " + sock);
294 });
295 }
296
297 var funcToCallback = {}; 270 var funcToCallback = {};
298 271
299 function postCall(func) { 272 function postCall(func) {
300 var callback = arguments[arguments.length - 1]; 273 var callback = arguments[arguments.length - 1];
301 funcToCallback[func] = callback; 274 funcToCallback[func] = callback;
302 275
303 nacl_module.postMessage({ 276 nacl_module.postMessage({
304 cmd: func, 277 cmd: func,
305 args: Array.prototype.slice.call(arguments, 1, -1) 278 args: Array.prototype.slice.call(arguments, 1, -1)
306 }); 279 });
307 } 280 }
308 281
309 function ArrayBufferToString(buf) { 282 function ArrayBufferToString(buf) {
310 return String.fromCharCode.apply(null, new Uint16Array(buf)); 283 return String.fromCharCode.apply(null, new Uint16Array(buf));
311 } 284 }
312 285
313 // Called by the common.js module.
314 function handleMessage(message_event) { 286 function handleMessage(message_event) {
315 var data = message_event.data; 287 var data = message_event.data;
316 if ((typeof(data) === 'string' || data instanceof String)) { 288 if ((typeof(data) === 'string' || data instanceof String)) {
317 common.logMessage(data); 289 common.logMessage(data);
318 } else if (data instanceof Object) { 290 } else if (data instanceof Object) {
319 var pipeName = data['pipe'] 291 // Result from a function call.
320 if (pipeName !== undefined) { 292 var params = data.args;
321 // Message for JavaScript I/O pipe 293 var funcName = data.cmd;
322 var operation = data['operation']; 294 var callback = funcToCallback[funcName];
323 if (operation == 'write') {
324 $('pipe_output').value += ArrayBufferToString(data['payload']);
325 } else if (operation == 'ack') {
326 common.logMessage(pipeName + ": ack:" + data['payload']);
327 } else {
328 common.logMessage('Got unexpected pipe operation: ' + operation);
329 }
330 } else {
331 // Result from a function call.
332 var params = data.args;
333 var funcName = data.cmd;
334 var callback = funcToCallback[funcName];
335 295
336 if (!callback) { 296 if (!callback) {
337 common.logMessage('Error: Bad message ' + funcName + 297 common.logMessage('Error: Bad message ' + funcName +
338 ' received from NaCl module.'); 298 ' received from NaCl module.');
339 return; 299 return;
340 } 300 }
341 301
342 delete funcToCallback[funcName]; 302 delete funcToCallback[funcName];
343 callback.apply(null, params); 303 callback.apply(null, params);
344 }
345 } else { 304 } else {
346 common.logMessage('Error: Unknow message `' + data + 305 common.logMessage('Error: Unknown message `' + data +
347 '` received from NaCl module.'); 306 '` received from NaCl module.');
348 } 307 }
349 } 308 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698