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 9 function getAuthToken(interactive) { 10 chrome.identity.getAuthToken( 11 {'interactive': interactive}, onGetAuthToken); 12 } 13 14 function onGetAuthToken(authToken) { 15 var signInEl = document.getElementById('signIn'); 16 var getFileEl = document.getElementById('getFile'); 17 if (authToken) { 18 signInEl.setAttribute('hidden', ''); 19 getFileEl.removeAttribute('hidden'); 20 window.authToken = authToken; 21 22 // Send the auth token to the NaCl module. 23 common.naclModule.postMessage('token:'+authToken); 24 } else { 25 // There is no auth token; this means that the user has yet to authorize 26 // this app. Display a button to let the user sign in and authorize this 27 // application. 28 signInEl.removeAttribute('hidden'); 29 getFileEl.setAttribute('hidden', ''); 30 } 31 }; 32 33 // Called by the common.js module. 34 function moduleDidLoad() { 35 // The module is not hidden by default so we can easily see if the plugin 36 // failed to load. 37 common.hideModule(); 38 39 // Make sure this example is running as a packaged app. If not, display a 40 // warning. 41 if (!chrome.identity) { 42 common.updateStatus('Error: must be run as a packged app.'); 43 return; 44 } 45 46 // Try to get the authorization token non-interactively. This will often work 47 // if the user has already authorized the app, and the token is cached. 48 getAuthToken(false); 49 } 50 51 function handleMessage(e) { 52 var msg = e.data; 53 document.getElementById('contents').textContent = msg; 54 } 55 56 // Called by the common.js module. 57 function attachListeners() { 58 document.getElementById('signIn').addEventListener('click', function () { 59 // Get the authorization token interactively. A dialog box will pop up 60 // asking the user to authorize access to their Drive account. 61 getAuthToken(true); 62 }); 63 64 document.getElementById('getFile').addEventListener('click', function () { 65 // Clear the file contents dialog box. 66 document.getElementById('contents').textContent = ''; 67 68 common.naclModule.postMessage('getFile'); 69 }); 70 } 71