| Index: native_client_sdk/src/examples/demo/nacl_io_demo/example.js
|
| diff --git a/native_client_sdk/src/examples/demo/nacl_io_demo/example.js b/native_client_sdk/src/examples/demo/nacl_io_demo/example.js
|
| index 7cb83c15b65a1563f16b4c0f8f2b5cca811224ed..16533d0004a7103faaf204ebc662ed05d2639a6c 100644
|
| --- a/native_client_sdk/src/examples/demo/nacl_io_demo/example.js
|
| +++ b/native_client_sdk/src/examples/demo/nacl_io_demo/example.js
|
| @@ -2,6 +2,108 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| +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 createScopeClause(scopeArray) {
|
| + var scopeClause = scopeArray[0];
|
| + for (var i = 1; i < scopeArray.length; ++i) {
|
| + scopeClause += "+" + scopeArray[i];
|
| + }
|
| + return scopeClause;
|
| +}
|
| +
|
| +function addNextParameter(url, paramKey, paramValue) {
|
| + url += "&";
|
| + url += paramKey;
|
| + url += "=";
|
| + url += paramValue;
|
| + return url;
|
| +}
|
| +
|
| +function addFirstQueryParameter(url, paramKey, paramValue) {
|
| + url += "?";
|
| + url += paramKey;
|
| + url += "=";
|
| + url += paramValue;
|
| + return url;
|
| +}
|
| +
|
| +function addNextQueryParameter(url, paramKey, paramValue) {
|
| + return addNextParameter(url, paramKey, paramValue);
|
| +}
|
| +
|
| +function addNextPostRequestParam(params, paramKey, paramValue) {
|
| + return addNextParameter(params, paramKey, paramValue);
|
| +}
|
| +
|
| +function buttonOnClick() {
|
| + if (CLIENT_ID == "TO FILL" || CLIENT_SECRET == "TO FILL") {
|
| + alert('Error: CLIENT_ID or CLIENT_SECRET is not set.' +
|
| + ' Please fill in CLIENT_ID and CLIENT_SECRET,' +
|
| + ' created from Google Developers Console, in example.js.' +
|
| + ' After that, use makefile on nacl_io_demo and rerun the' +
|
| + ' demo.');
|
| + }
|
| +
|
| + 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";
|
| + var scopeArray = [ "https://www.googleapis.com/auth/drive",
|
| + "https://www.googleapis.com/auth/drive.metadata"
|
| + ];
|
| + url = addFirstQueryParameter(url, "scope",
|
| + createScopeClause(scopeArray));
|
| + url = addNextQueryParameter(url, "state", stateValue);
|
| + url = addNextQueryParameter(url, "redirect_uri", REDIRECT_URI);
|
| + url = addNextQueryParameter(url, "response_type", "code");
|
| + url = addNextQueryParameter(url, "client_id", CLIENT_ID);
|
| +
|
| + window.open(url, "_self");
|
| +}
|
| +
|
| +function executeAfterGettingCode() {
|
| + var codeKeyIndex = currentURL.indexOf("code=");
|
| + if (-1 == codeKeyIndex) {
|
| + return -1;
|
| + }
|
| +
|
| + var xHTTP = new XMLHttpRequest();
|
| + xHTTP.onreadystatechange = function() {
|
| + // Check if the state is a state that the request finished
|
| + // and response is ready and if the status code is OK.
|
| + if (xHTTP.readyState == 4 && xHTTP.status == 200) {
|
| + var obj = JSON.parse(xHTTP.responseText);
|
| + var url = REDIRECT_URI;
|
| + url = addFirstQueryParameter(url, "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 = currentURL.substring(codeKeyIndex);
|
| + params = addNextPostRequestParam(params, "client_id", CLIENT_ID);
|
| + params = addNextPostRequestParam(params, "client_secret", CLIENT_SECRET);
|
| + params = addNextPostRequestParam(params, "redirect_uri", REDIRECT_URI);
|
| + params = addNextPostRequestParam(params, "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();
|
| }
|
| @@ -17,7 +119,20 @@ function domContentLoaded(name, tc, config, width, height) {
|
| common.updateStatus(
|
| 'Allocated ' + bytes + ' bytes of persistant storage.');
|
| common.attachDefaultListeners();
|
| - common.createNaClModule(name, tc, config, width, height);
|
| +
|
| + var tokenKeyAndEqual = "token=";
|
| + var tokenKeyIndex = currentURL.indexOf(tokenKeyAndEqual);
|
| + if (-1 == tokenKeyIndex) {
|
| + common.createNaClModule(name, tc, config, width, height);
|
| + } else {
|
| + tokenValue = currentURL.substring(tokenKeyIndex +
|
| + tokenKeyAndEqual.length);
|
| + var tokenMap = { 'token' : tokenValue };
|
| +
|
| + common.createNaClModule(name, tc, config, width, height, tokenMap);
|
| +
|
| + document.getElementById("buttonsignin").disabled = true;
|
| + }
|
| },
|
| function(e) { alert('Failed to allocate space') });
|
| }
|
| @@ -343,7 +458,7 @@ function handleMessage(message_event) {
|
| callback.apply(null, params);
|
| }
|
| } else {
|
| - common.logMessage('Error: Unknow message `' + data +
|
| + common.logMessage('Error: Unknown message `' + data +
|
| '` received from NaCl module.');
|
| }
|
| }
|
|
|