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

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

Issue 14500010: [NaCl SDK] Google Drive example (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 7 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013 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 var authToken = '';
8 var driveFilesUrl = 'https://www.googleapis.com/drive/v2/files';
9 var driveFilesUploadUrl = 'https://www.googleapis.com/upload/drive/v2/files';
10 var multipartBoundary = 'END_OF_PART';
11
12 function $(id) {
13 return document.getElementById(id);
14 }
15
16 function getAuthToken(interactive) {
17 chrome.experimental.identity.getAuthToken(
18 {'interactive': interactive}, onGetAuthToken);
19 }
20
21 function onGetAuthToken(authToken) {
22 var signInEl = $('signIn');
23 var getFileEl = $('getFile');
24 if (authToken) {
25 signInEl.style.display = 'none';
26 getFileEl.style.display = 'block';
27 window.authToken = authToken;
28
29 // Send the auth token to the NaCl module.
30 common.naclModule.postMessage('token:'+authToken);
31 } else {
32 signInEl.style.display = 'block';
33 getFileEl.style.display = 'none';
34 }
35 };
36
37 // Called by the common.js module.
38 function moduleDidLoad() {
39 // The module is not hidden by default so we can easily see if the plugin
40 // failed to load.
41 common.hideModule();
42
43 getAuthToken(false);
44 }
45
46 function handleMessage(e) {
47 var msg = e.data;
48 $('result').textContent = msg;
49 }
50
51 // Called by the common.js module.
52 function attachListeners() {
53 $('signIn').addEventListener('click', function () {
54 getAuthToken(true);
55 });
56
57 $('getFile').addEventListener('click', function () {
58 common.naclModule.postMessage('getFile');
59 });
60 }
61
62 /*
binji 2013/04/29 18:48:24 This is the JS equivalent code that is unused
63 function encodeQueryString(o) {
64 var r = [];
65 for (var k in o)
66 r.push(encodeURIComponent(k) + '=' + encodeURIComponent(o[k]));
67 return r.join('&');
68 }
69
70 function myXhr(baseUrl, queryObj, method, requestObj, okCb, errCb) {
71 var xhr = new XMLHttpRequest();
72 var url = baseUrl + '?' + encodeQueryString(queryObj);
73
74 xhr.open(method, url);
75 xhr.setRequestHeader('Authorization', 'Bearer ' + authToken);
76 if (requestObj)
77 xhr.setRequestHeader('Content-Type', 'multipart/related; boundary="'+
78 multipartBoundary+'"');
79 xhr.onload = function (e) {
80 if (this.status != 200) {
81 errCb(this, e);
82 } else {
83 okCb(this, e);
84 }
85 };
86 if (requestObj)
87 xhr.send(requestObj);
88 else
89 xhr.send();
90 }
91
92 function getFiles(q, maxResults, okCb, errCb) {
93 var queryObj = {'q': q, 'maxResults': maxResults};
94 myXhr(driveFilesUrl, queryObj, 'GET', undefined, okCb, errCb);
95 }
96
97 function writeAsciiStringToArray(s, a, offset) {
98 for (var i = 0; i < s.length; ++i) {
99 a[offset + i] = s.charCodeAt(i) & 0xff;
100 }
101 return offset + s.length;
102 }
103
104 function writeArrayToArray(src, dst, offset) {
105 for (var i = 0; i < src.byteLength; ++i) {
106 dst[offset + i] = src[i];
107 }
108 return offset + src.byteLength;
109 }
110
111 function encodeUtf8(s) {
112 // See http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascri pt.html.
113 return unescape(encodeURIComponent(s));
114 }
115
116 function makeMultipartRelated(metadata, data, mimeType) {
117 var prefix = '--'+multipartBoundary+'\n';
118 prefix += 'Content-Type: application/json; charset=UTF-8\n\n';
119 var metadataString = encodeUtf8(JSON.stringify(metadata));
120 var infix = '\n--'+multipartBoundary+'\n';
121 infix += 'Content-Type: '+mimeType+'\n\n';
122 var postfix = '\n--'+multipartBoundary+'--';
123 var length = prefix.length + metadataString.length + infix.length +
124 data.byteLength + postfix.length;
125 var a = new Uint8Array(length);
126
127 var offset = 0;
128 offset = writeAsciiStringToArray(prefix, a, offset);
129 offset = writeAsciiStringToArray(metadataString, a, offset);
130 offset = writeAsciiStringToArray(infix, a, offset);
131 offset = writeArrayToArray(data, a, offset);
132 offset = writeAsciiStringToArray(postfix, a, offset);
133
134 if (offset != length) {
135 console.log('ERROR: expected length: '+length+' actual: ' +offset);
136 }
137
138 return a;
139 }
140
141 function ab2str(buf) {
142 return String.fromCharCode.apply(null, new Uint16Array(buf));
143 }
144
145 function insertFile(params, metadata, data, mimeType, okCb, errCb) {
146 var queryObj = params;
147 queryObj['uploadType'] = 'multipart';
148 var requestObj = makeMultipartRelated(metadata, data, mimeType);
149 $('result').textContent = ab2str(requestObj);
150 myXhr(driveFilesUploadUrl, queryObj, 'POST', requestObj, okCb, errCb);
151 }
152
153 document.addEventListener('DOMContentLoaded', function() {
154 $('signIn').addEventListener('click', function () {
155 getAuthToken(true);
156 });
157
158 $('getFile').addEventListener('click', function () {
159 getFiles('title = \'hello nacl.txt\'', 1, function (xhr, e) {
160 console.log('success!');
161 var data = JSON.parse(xhr.response);
162 $('result').textContent = JSON.stringify(data, null, '\t');
163
164 if (!data.items.length) {
165 var dataString = 'Hello, JS Drive!';
166 var data = new Uint8Array(dataString.length);
167 writeAsciiStringToArray(dataString, data, 0);
168 var metadata = {
169 'description': 'A little message saying hello!',
170 'title': 'hello nacl.txt',
171 };
172 insertFile({}, metadata, data, 'text/plain', function (xhr, e) {
173 $('result').textContent = 'Uploaded file!';
174 }, function (xhr, e) {
175 $('result').textContent = JSON.stringify(JSON.parse(xhr.response), nul l, '\t');
176 console.log('insertFile failed. :(');
177 });
178 } else {
179 // Found the file, download it!
180 var url = data.items[0].downloadUrl;
181 $('result').textContent = 'Downloading '+url;
182
183 var xhr = new XMLHttpRequest();
184 xhr.open('GET', url);
185 xhr.setRequestHeader('Authorization', 'Bearer ' + authToken);
186 xhr.onload = function (e) {
187 if (this.status == 200) {
188 $('result').textContent = this.response;
189 }
190 };
191 xhr.send();
192 }
193 }, function (xhr, e) {
194 console.log('getFiles failed. :(');
195 });
196 });
197
198 getAuthToken(false);
199 });
200 */
OLDNEW
« no previous file with comments | « native_client_sdk/src/examples/demo/drive/example.dsc ('k') | native_client_sdk/src/examples/demo/drive/index.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698