Index: native_client_sdk/src/examples/demo/drive/example.js |
diff --git a/native_client_sdk/src/examples/demo/drive/example.js b/native_client_sdk/src/examples/demo/drive/example.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3c2f574a0e5d5f1d847d1dbe0ff60e1b33adb9d4 |
--- /dev/null |
+++ b/native_client_sdk/src/examples/demo/drive/example.js |
@@ -0,0 +1,200 @@ |
+// Copyright (c) 2013 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. |
+ |
+"use strict"; |
+ |
+var authToken = ''; |
+var driveFilesUrl = 'https://www.googleapis.com/drive/v2/files'; |
+var driveFilesUploadUrl = 'https://www.googleapis.com/upload/drive/v2/files'; |
+var multipartBoundary = 'END_OF_PART'; |
+ |
+function $(id) { |
+ return document.getElementById(id); |
+} |
+ |
+function getAuthToken(interactive) { |
+ chrome.experimental.identity.getAuthToken( |
+ {'interactive': interactive}, onGetAuthToken); |
+} |
+ |
+function onGetAuthToken(authToken) { |
+ var signInEl = $('signIn'); |
+ var getFileEl = $('getFile'); |
+ if (authToken) { |
+ signInEl.style.display = 'none'; |
+ getFileEl.style.display = 'block'; |
+ window.authToken = authToken; |
+ |
+ // Send the auth token to the NaCl module. |
+ common.naclModule.postMessage('token:'+authToken); |
+ } else { |
+ signInEl.style.display = 'block'; |
+ getFileEl.style.display = 'none'; |
+ } |
+}; |
+ |
+// Called by the common.js module. |
+function moduleDidLoad() { |
+ // The module is not hidden by default so we can easily see if the plugin |
+ // failed to load. |
+ common.hideModule(); |
+ |
+ getAuthToken(false); |
+} |
+ |
+function handleMessage(e) { |
+ var msg = e.data; |
+ $('result').textContent = msg; |
+} |
+ |
+// Called by the common.js module. |
+function attachListeners() { |
+ $('signIn').addEventListener('click', function () { |
+ getAuthToken(true); |
+ }); |
+ |
+ $('getFile').addEventListener('click', function () { |
+ common.naclModule.postMessage('getFile'); |
+ }); |
+} |
+ |
+/* |
binji
2013/04/29 18:48:24
This is the JS equivalent code that is unused
|
+function encodeQueryString(o) { |
+ var r = []; |
+ for (var k in o) |
+ r.push(encodeURIComponent(k) + '=' + encodeURIComponent(o[k])); |
+ return r.join('&'); |
+} |
+ |
+function myXhr(baseUrl, queryObj, method, requestObj, okCb, errCb) { |
+ var xhr = new XMLHttpRequest(); |
+ var url = baseUrl + '?' + encodeQueryString(queryObj); |
+ |
+ xhr.open(method, url); |
+ xhr.setRequestHeader('Authorization', 'Bearer ' + authToken); |
+ if (requestObj) |
+ xhr.setRequestHeader('Content-Type', 'multipart/related; boundary="'+ |
+ multipartBoundary+'"'); |
+ xhr.onload = function (e) { |
+ if (this.status != 200) { |
+ errCb(this, e); |
+ } else { |
+ okCb(this, e); |
+ } |
+ }; |
+ if (requestObj) |
+ xhr.send(requestObj); |
+ else |
+ xhr.send(); |
+} |
+ |
+function getFiles(q, maxResults, okCb, errCb) { |
+ var queryObj = {'q': q, 'maxResults': maxResults}; |
+ myXhr(driveFilesUrl, queryObj, 'GET', undefined, okCb, errCb); |
+} |
+ |
+function writeAsciiStringToArray(s, a, offset) { |
+ for (var i = 0; i < s.length; ++i) { |
+ a[offset + i] = s.charCodeAt(i) & 0xff; |
+ } |
+ return offset + s.length; |
+} |
+ |
+function writeArrayToArray(src, dst, offset) { |
+ for (var i = 0; i < src.byteLength; ++i) { |
+ dst[offset + i] = src[i]; |
+ } |
+ return offset + src.byteLength; |
+} |
+ |
+function encodeUtf8(s) { |
+ // See http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html. |
+ return unescape(encodeURIComponent(s)); |
+} |
+ |
+function makeMultipartRelated(metadata, data, mimeType) { |
+ var prefix = '--'+multipartBoundary+'\n'; |
+ prefix += 'Content-Type: application/json; charset=UTF-8\n\n'; |
+ var metadataString = encodeUtf8(JSON.stringify(metadata)); |
+ var infix = '\n--'+multipartBoundary+'\n'; |
+ infix += 'Content-Type: '+mimeType+'\n\n'; |
+ var postfix = '\n--'+multipartBoundary+'--'; |
+ var length = prefix.length + metadataString.length + infix.length + |
+ data.byteLength + postfix.length; |
+ var a = new Uint8Array(length); |
+ |
+ var offset = 0; |
+ offset = writeAsciiStringToArray(prefix, a, offset); |
+ offset = writeAsciiStringToArray(metadataString, a, offset); |
+ offset = writeAsciiStringToArray(infix, a, offset); |
+ offset = writeArrayToArray(data, a, offset); |
+ offset = writeAsciiStringToArray(postfix, a, offset); |
+ |
+ if (offset != length) { |
+ console.log('ERROR: expected length: '+length+' actual: ' +offset); |
+ } |
+ |
+ return a; |
+} |
+ |
+function ab2str(buf) { |
+ return String.fromCharCode.apply(null, new Uint16Array(buf)); |
+} |
+ |
+function insertFile(params, metadata, data, mimeType, okCb, errCb) { |
+ var queryObj = params; |
+ queryObj['uploadType'] = 'multipart'; |
+ var requestObj = makeMultipartRelated(metadata, data, mimeType); |
+ $('result').textContent = ab2str(requestObj); |
+ myXhr(driveFilesUploadUrl, queryObj, 'POST', requestObj, okCb, errCb); |
+} |
+ |
+document.addEventListener('DOMContentLoaded', function() { |
+ $('signIn').addEventListener('click', function () { |
+ getAuthToken(true); |
+ }); |
+ |
+ $('getFile').addEventListener('click', function () { |
+ getFiles('title = \'hello nacl.txt\'', 1, function (xhr, e) { |
+ console.log('success!'); |
+ var data = JSON.parse(xhr.response); |
+ $('result').textContent = JSON.stringify(data, null, '\t'); |
+ |
+ if (!data.items.length) { |
+ var dataString = 'Hello, JS Drive!'; |
+ var data = new Uint8Array(dataString.length); |
+ writeAsciiStringToArray(dataString, data, 0); |
+ var metadata = { |
+ 'description': 'A little message saying hello!', |
+ 'title': 'hello nacl.txt', |
+ }; |
+ insertFile({}, metadata, data, 'text/plain', function (xhr, e) { |
+ $('result').textContent = 'Uploaded file!'; |
+ }, function (xhr, e) { |
+ $('result').textContent = JSON.stringify(JSON.parse(xhr.response), null, '\t'); |
+ console.log('insertFile failed. :('); |
+ }); |
+ } else { |
+ // Found the file, download it! |
+ var url = data.items[0].downloadUrl; |
+ $('result').textContent = 'Downloading '+url; |
+ |
+ var xhr = new XMLHttpRequest(); |
+ xhr.open('GET', url); |
+ xhr.setRequestHeader('Authorization', 'Bearer ' + authToken); |
+ xhr.onload = function (e) { |
+ if (this.status == 200) { |
+ $('result').textContent = this.response; |
+ } |
+ }; |
+ xhr.send(); |
+ } |
+ }, function (xhr, e) { |
+ console.log('getFiles failed. :('); |
+ }); |
+ }); |
+ |
+ getAuthToken(false); |
+}); |
+*/ |