190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved. 290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// found in the LICENSE file. 490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)"use strict"; 690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)var authToken = ''; 890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)function getAuthToken(interactive) { 101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) chrome.identity.getAuthToken( 1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) {'interactive': interactive}, onGetAuthToken); 1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 1390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)function onGetAuthToken(authToken) { 1590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) var signInEl = document.getElementById('signIn'); 1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) var getFileEl = document.getElementById('getFile'); 1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (authToken) { 18a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) signInEl.setAttribute('hidden', ''); 1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) getFileEl.removeAttribute('hidden'); 2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) window.authToken = authToken; 2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Send the auth token to the NaCl module. 2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) common.naclModule.postMessage('token:'+authToken); 2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } else { 2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // There is no auth token; this means that the user has yet to authorize 2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // this app. Display a button to let the user sign in and authorize this 2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // application. 2890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) signInEl.removeAttribute('hidden'); 29a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) getFileEl.setAttribute('hidden', ''); 3090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}; 3290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Called by the common.js module. 3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)function moduleDidLoad() { 3590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // The module is not hidden by default so we can easily see if the plugin 3690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // failed to load. 3790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) common.hideModule(); 3890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 3990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Make sure this example is running as a packaged app. If not, display a 4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // warning. 411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (!chrome.identity) { 4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) common.updateStatus('Error: must be run as a packged app.'); 4390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return; 4490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 4590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 4690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Try to get the authorization token non-interactively. This will often work 4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // if the user has already authorized the app, and the token is cached. 4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) getAuthToken(false); 4990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 5090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 5190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)function handleMessage(e) { 5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) var msg = e.data; 5390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) document.getElementById('contents').textContent = msg; 5490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 5590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 5690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Called by the common.js module. 5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)function attachListeners() { 5890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) document.getElementById('signIn').addEventListener('click', function () { 5990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Get the authorization token interactively. A dialog box will pop up 6090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // asking the user to authorize access to their Drive account. 6190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) getAuthToken(true); 6290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) }); 6390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 6490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) document.getElementById('getFile').addEventListener('click', function () { 6590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Clear the file contents dialog box. 6690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) document.getElementById('contents').textContent = ''; 6790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 6890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) common.naclModule.postMessage('getFile'); 6990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) }); 7090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} 71