OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 var CLIENT_ID = "TO FILL"; |
| 6 var CLIENT_SECRET = "TO FILL"; |
| 7 |
| 8 var REDIRECT_URI = "http://localhost:5103/index.html"; |
| 9 |
| 10 var currentURL = window.location.href; |
| 11 var tokenValue = "Set at domContentLoaded()"; |
| 12 |
| 13 function createScopeClause(scopeArray) { |
| 14 var scopeClause = scopeArray[0]; |
| 15 for (var i = 1; i < scopeArray.length; ++i) { |
| 16 scopeClause += "+" + scopeArray[i]; |
| 17 } |
| 18 return scopeClause; |
| 19 } |
| 20 |
| 21 function addNextParameter(url, paramKey, paramValue) { |
| 22 url += "&"; |
| 23 url += paramKey; |
| 24 url += "="; |
| 25 url += paramValue; |
| 26 return url; |
| 27 } |
| 28 |
| 29 function addFirstQueryParameter(url, paramKey, paramValue) { |
| 30 url += "?"; |
| 31 url += paramKey; |
| 32 url += "="; |
| 33 url += paramValue; |
| 34 return url; |
| 35 } |
| 36 |
| 37 function addNextQueryParameter(url, paramKey, paramValue) { |
| 38 return addNextParameter(url, paramKey, paramValue); |
| 39 } |
| 40 |
| 41 function addNextPostRequestParam(params, paramKey, paramValue) { |
| 42 return addNextParameter(params, paramKey, paramValue); |
| 43 } |
| 44 |
| 45 function buttonOnClick() { |
| 46 if (CLIENT_ID == "TO FILL" || CLIENT_SECRET == "TO FILL") { |
| 47 alert('Error: CLIENT_ID or CLIENT_SECRET is not set.' + |
| 48 ' Please fill in CLIENT_ID and CLIENT_SECRET,' + |
| 49 ' created from Google Developers Console, in example.js.' + |
| 50 ' After that, use makefile on nacl_io_demo and rerun the' + |
| 51 ' demo.'); |
| 52 } |
| 53 |
| 54 var array = new Uint32Array(3); |
| 55 window.crypto.getRandomValues(array); |
| 56 var stateValue = array[0].toString() + |
| 57 array[1].toString() + |
| 58 array[2].toString(); |
| 59 |
| 60 var url = "http://accounts.google.com/o/oauth2/v2/auth"; |
| 61 var scopeArray = [ "https://www.googleapis.com/auth/drive", |
| 62 "https://www.googleapis.com/auth/drive.metadata" |
| 63 ]; |
| 64 url = addFirstQueryParameter(url, "scope", |
| 65 createScopeClause(scopeArray)); |
| 66 url = addNextQueryParameter(url, "state", stateValue); |
| 67 url = addNextQueryParameter(url, "redirect_uri", REDIRECT_URI); |
| 68 url = addNextQueryParameter(url, "response_type", "code"); |
| 69 url = addNextQueryParameter(url, "client_id", CLIENT_ID); |
| 70 |
| 71 window.open(url, "_self"); |
| 72 } |
| 73 |
| 74 function executeAfterGettingCode() { |
| 75 var codeKeyIndex = currentURL.indexOf("code="); |
| 76 if (-1 == codeKeyIndex) { |
| 77 return -1; |
| 78 } |
| 79 |
| 80 var xHTTP = new XMLHttpRequest(); |
| 81 xHTTP.onreadystatechange = function() { |
| 82 // Check if the state is a state that the request finished |
| 83 // and response is ready and if the status code is OK. |
| 84 if (xHTTP.readyState == 4 && xHTTP.status == 200) { |
| 85 var obj = JSON.parse(xHTTP.responseText); |
| 86 var url = REDIRECT_URI; |
| 87 url = addFirstQueryParameter(url, "token", obj.access_token); |
| 88 |
| 89 window.open(url, "_self"); |
| 90 } |
| 91 }; |
| 92 |
| 93 xHTTP.timeout = 4000; // Set timeout to 4 seconds (4000 milliseconds) |
| 94 xHTTP.ontimeout = function () { alert("Timed out"); } |
| 95 |
| 96 var params = currentURL.substring(codeKeyIndex); |
| 97 params = addNextPostRequestParam(params, "client_id", CLIENT_ID); |
| 98 params = addNextPostRequestParam(params, "client_secret", CLIENT_SECRET); |
| 99 params = addNextPostRequestParam(params, "redirect_uri", REDIRECT_URI); |
| 100 params = addNextPostRequestParam(params, "grant_type", "authorization_code"); |
| 101 |
| 102 xHTTP.open("POST", "https://www.googleapis.com/oauth2/v4/token", true); |
| 103 xHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); |
| 104 xHTTP.send(params); |
| 105 } |
| 106 |
5 function moduleDidLoad() { | 107 function moduleDidLoad() { |
6 common.hideModule(); | 108 common.hideModule(); |
7 } | 109 } |
8 | 110 |
9 function $(id) { | 111 function $(id) { |
10 return document.getElementById(id); | 112 return document.getElementById(id); |
11 } | 113 } |
12 | 114 |
13 // Called by the common.js module. | 115 // Called by the common.js module. |
14 function domContentLoaded(name, tc, config, width, height) { | 116 function domContentLoaded(name, tc, config, width, height) { |
15 navigator.webkitPersistentStorage.requestQuota(5 * 1024 * 1024, | 117 navigator.webkitPersistentStorage.requestQuota(5 * 1024 * 1024, |
16 function(bytes) { | 118 function(bytes) { |
17 common.updateStatus( | 119 common.updateStatus( |
18 'Allocated ' + bytes + ' bytes of persistant storage.'); | 120 'Allocated ' + bytes + ' bytes of persistant storage.'); |
19 common.attachDefaultListeners(); | 121 common.attachDefaultListeners(); |
20 common.createNaClModule(name, tc, config, width, height); | 122 |
| 123 var tokenKeyAndEqual = "token="; |
| 124 var tokenKeyIndex = currentURL.indexOf(tokenKeyAndEqual); |
| 125 if (-1 == tokenKeyIndex) { |
| 126 common.createNaClModule(name, tc, config, width, height); |
| 127 } else { |
| 128 tokenValue = currentURL.substring(tokenKeyIndex + |
| 129 tokenKeyAndEqual.length); |
| 130 var tokenMap = { 'token' : tokenValue }; |
| 131 |
| 132 common.createNaClModule(name, tc, config, width, height, tokenMap); |
| 133 |
| 134 document.getElementById("buttonsignin").disabled = true; |
| 135 } |
21 }, | 136 }, |
22 function(e) { alert('Failed to allocate space') }); | 137 function(e) { alert('Failed to allocate space') }); |
23 } | 138 } |
24 | 139 |
25 // Called by the common.js module. | 140 // Called by the common.js module. |
26 function attachListeners() { | 141 function attachListeners() { |
27 var radioEls = document.querySelectorAll('input[type="radio"]'); | 142 var radioEls = document.querySelectorAll('input[type="radio"]'); |
28 for (var i = 0; i < radioEls.length; ++i) { | 143 for (var i = 0; i < radioEls.length; ++i) { |
29 radioEls[i].addEventListener('click', onRadioClicked); | 144 radioEls[i].addEventListener('click', onRadioClicked); |
30 } | 145 } |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 if (!callback) { | 451 if (!callback) { |
337 common.logMessage('Error: Bad message ' + funcName + | 452 common.logMessage('Error: Bad message ' + funcName + |
338 ' received from NaCl module.'); | 453 ' received from NaCl module.'); |
339 return; | 454 return; |
340 } | 455 } |
341 | 456 |
342 delete funcToCallback[funcName]; | 457 delete funcToCallback[funcName]; |
343 callback.apply(null, params); | 458 callback.apply(null, params); |
344 } | 459 } |
345 } else { | 460 } else { |
346 common.logMessage('Error: Unknow message `' + data + | 461 common.logMessage('Error: Unknown message `' + data + |
347 '` received from NaCl module.'); | 462 '` received from NaCl module.'); |
348 } | 463 } |
349 } | 464 } |
OLD | NEW |