Index: native_client_sdk/src/examples/demo/googledrivefs_demo/example.js |
diff --git a/native_client_sdk/src/examples/demo/nacl_io_demo/example.js b/native_client_sdk/src/examples/demo/googledrivefs_demo/example.js |
similarity index 61% |
copy from native_client_sdk/src/examples/demo/nacl_io_demo/example.js |
copy to native_client_sdk/src/examples/demo/googledrivefs_demo/example.js |
index 7cb83c15b65a1563f16b4c0f8f2b5cca811224ed..6cc14dad3f94b8ad7721ea1e1e4878831ea9df28 100644 |
--- a/native_client_sdk/src/examples/demo/nacl_io_demo/example.js |
+++ b/native_client_sdk/src/examples/demo/googledrivefs_demo/example.js |
@@ -1,7 +1,65 @@ |
-// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Copyright (c) 2016 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. |
+// Obtain a client ID and client secret from Google Developers Console |
+var CLIENT_ID = "TO FILL"; |
+var CLIENT_SECRET = "TO FILL"; |
+ |
+var REDIRECT_URI = "http://localhost:5103/index.html"; |
+ |
+var currentURL = window.location.href; |
+var tokenValue = "Set at domContentLoaded()"; |
+ |
+function buttonOnClick() { |
+ var array = new Uint32Array(3); |
+ window.crypto.getRandomValues(array); |
+ var stateValue = array[0].toString() + |
+ array[1].toString() + |
+ array[2].toString(); |
+ |
+ var url = "http://accounts.google.com/o/oauth2/v2/auth?" + |
+ "scope=https://www.googleapis.com/auth/drive" |
+ + "+" + "https://www.googleapis.com/auth/drive.metadata" + "&" + |
+ "state=" + stateValue + "&" + |
+ "redirect_uri=http://localhost:5103/index.html" + "&" + |
+ "response_type=code" + "&" + |
+ "client_id=" + CLIENT_ID; |
+ |
+ window.open(url, "_self"); |
+} |
+ |
+function executeAfterGettingCode() { |
+ var codeKeyIndex = currentURL.search("code="); |
+ if (-1 == codeKeyIndex) { |
+ return -1; |
+ } |
+ var codeValue = currentURL.substring(codeKeyIndex + 5); |
+ |
+ var xHTTP = new XMLHttpRequest(); |
+ xHTTP.onreadystatechange = function() { |
+ if (xHTTP.readyState == 4 && xHTTP.status == 200) { |
+ var obj = JSON.parse(xHTTP.responseText); |
+ var url = REDIRECT_URI + "?token=" + obj.access_token; |
+ |
+ window.open(url, "_self"); |
+ } |
+ }; |
+ |
+ xHTTP.timeout = 4000; // Set timeout to 4 seconds (4000 milliseconds) |
+ xHTTP.ontimeout = function () { alert("Timed out"); } |
+ |
+ var params = "code=" + codeValue + "&" + |
+ "client_id=" + CLIENT_ID + "&" + |
+ "client_secret=" + CLIENT_SECRET + "&" + |
+ "redirect_uri=" + REDIRECT_URI + "&" + |
+ "grant_type=authorization_code"; |
+ |
+ xHTTP.open("POST", "https://www.googleapis.com/oauth2/v4/token", true); |
+ xHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); |
+ xHTTP.send(params); |
+} |
+ |
function moduleDidLoad() { |
common.hideModule(); |
} |
@@ -10,19 +68,22 @@ function $(id) { |
return document.getElementById(id); |
} |
-// Called by the common.js module. |
function domContentLoaded(name, tc, config, width, height) { |
- navigator.webkitPersistentStorage.requestQuota(5 * 1024 * 1024, |
- function(bytes) { |
- common.updateStatus( |
- 'Allocated ' + bytes + ' bytes of persistant storage.'); |
- common.attachDefaultListeners(); |
- common.createNaClModule(name, tc, config, width, height); |
- }, |
- function(e) { alert('Failed to allocate space') }); |
+ common.attachDefaultListeners(); |
+ |
+ var tokenKeyIndex = currentURL.search("token="); |
+ if (-1 == tokenKeyIndex) { |
+ common.createNaClModule(name, tc, config, width, height); |
+ } else { |
+ tokenValue = currentURL.substring(tokenKeyIndex + 6); |
+ var tokenMap = { 'token' : tokenValue }; |
+ |
+ common.createNaClModule(name, tc, config, width, height, tokenMap); |
+ |
+ document.getElementById("buttonid").disabled = true; |
+ } |
} |
-// Called by the common.js module. |
function attachListeners() { |
var radioEls = document.querySelectorAll('input[type="radio"]'); |
for (var i = 0; i < radioEls.length; ++i) { |
@@ -40,33 +101,6 @@ function attachListeners() { |
var func = window[id]; |
buttonEl.addEventListener('click', func); |
} |
- |
- $('pipe_input_box').addEventListener('keypress', onPipeInput) |
- $('pipe_output').disabled = true; |
- |
- $('pipe_name').addEventListener('change', |
- function() { $('pipe_output').value = ''; }) |
-} |
- |
-// Called with keypress events on the pipe input box |
-function onPipeInput(e) { |
- // Create an arraybuffer containing the 16-bit char code |
- // from the keypress event. |
- var buffer = new ArrayBuffer(1*2); |
- var bufferView = new Uint16Array(buffer); |
- bufferView[0] = e.charCode; |
- |
- // Pass the buffer in a dictionary over the NaCl module |
- var pipeSelect = $('pipe_name'); |
- var pipeName = pipeSelect[pipeSelect.selectedIndex].value; |
- var message = { |
- pipe: pipeName, |
- operation: 'write', |
- payload: buffer, |
- }; |
- nacl_module.postMessage(message); |
- e.preventDefault(); |
- return false; |
} |
function onRadioClicked(e) { |
@@ -233,67 +267,6 @@ function getcwd(e) { |
}); |
} |
-function getaddrinfo(e) { |
- var name = $('getaddrinfoName').value; |
- var family = $('getaddrinfoFamily').value; |
- postCall('getaddrinfo', name, family, function(name, addrType) { |
- common.logMessage('getaddrinfo returned successfully'); |
- common.logMessage('ai_cannonname = ' + name + '.'); |
- var count = 1; |
- for (var i = 1; i < arguments.length; i+=2) { |
- var msg = 'Address number ' + count + ' = ' + arguments[i] + |
- ' (' + arguments[i+1] + ')'; |
- common.logMessage(msg); |
- count += 1; |
- } |
- }); |
-} |
- |
-function gethostbyname(e) { |
- var name = $('gethostbynameName').value; |
- postCall('gethostbyname', name, function(name, addrType) { |
- common.logMessage('gethostbyname returned successfully'); |
- common.logMessage('h_name = ' + name + '.'); |
- common.logMessage('h_addr_type = ' + addrType + '.'); |
- for (var i = 2; i < arguments.length; i++) { |
- common.logMessage('Address number ' + (i-1) + ' = ' + arguments[i] + '.'); |
- } |
- }); |
-} |
- |
-function connect(e) { |
- var host = $('connectHost').value; |
- var port = parseInt($('connectPort').value, 10); |
- postCall('connect', host, port, function(sockhandle) { |
- common.logMessage('connected'); |
- addNameToSelectElements('.sock-handle', sockhandle, '[socket]'); |
- }); |
-} |
- |
-function recv(e) { |
- var handle = parseInt($('recvHandle').value, 10); |
- var bufferSize = parseInt($('recvBufferSize').value, 10); |
- postCall('recv', handle, bufferSize, function(messageLen, message) { |
- common.logMessage("received " + messageLen + ' bytes: ' + message); |
- }); |
-} |
- |
-function send(e) { |
- var handle = parseInt($('sendHandle').value, 10); |
- var message = $('sendMessage').value; |
- postCall('send', handle, message, function(sentBytes) { |
- common.logMessage("sent bytes: " + sentBytes); |
- }); |
-} |
- |
-function close(e) { |
- var handle = parseInt($('closeHandle').value, 10); |
- postCall('close', handle, function(sock) { |
- removeNameFromSelectElements('.sock-handle', sock, "[socket]"); |
- common.logMessage("closed socket: " + sock); |
- }); |
-} |
- |
var funcToCallback = {}; |
function postCall(func) { |
@@ -310,40 +283,26 @@ function ArrayBufferToString(buf) { |
return String.fromCharCode.apply(null, new Uint16Array(buf)); |
} |
-// Called by the common.js module. |
function handleMessage(message_event) { |
var data = message_event.data; |
if ((typeof(data) === 'string' || data instanceof String)) { |
common.logMessage(data); |
} else if (data instanceof Object) { |
- var pipeName = data['pipe'] |
- if (pipeName !== undefined) { |
- // Message for JavaScript I/O pipe |
- var operation = data['operation']; |
- if (operation == 'write') { |
- $('pipe_output').value += ArrayBufferToString(data['payload']); |
- } else if (operation == 'ack') { |
- common.logMessage(pipeName + ": ack:" + data['payload']); |
- } else { |
- common.logMessage('Got unexpected pipe operation: ' + operation); |
- } |
- } else { |
- // Result from a function call. |
- var params = data.args; |
- var funcName = data.cmd; |
- var callback = funcToCallback[funcName]; |
- |
- if (!callback) { |
- common.logMessage('Error: Bad message ' + funcName + |
- ' received from NaCl module.'); |
- return; |
- } |
- |
- delete funcToCallback[funcName]; |
- callback.apply(null, params); |
+ // Result from a function call. |
+ var params = data.args; |
+ var funcName = data.cmd; |
+ var callback = funcToCallback[funcName]; |
+ |
+ if (!callback) { |
+ common.logMessage('Error: Bad message ' + funcName + |
+ ' received from NaCl module.'); |
+ return; |
} |
+ |
+ delete funcToCallback[funcName]; |
+ callback.apply(null, params); |
} else { |
- common.logMessage('Error: Unknow message `' + data + |
+ common.logMessage('Error: Unknown message `' + data + |
'` received from NaCl module.'); |
} |
} |