'use strict';
let g_onAuthScriptLoaded = null;
const g_authScriptPromise = new Promise((resolve, reject) => {
g_onAuthScriptLoaded = resolve;
});
let g_onTokenReceived = null;
const g_authTokenPromise = new Promise((resolve, reject) => {
g_onTokenReceived = resolve;
});
let g_authFetchCalled = false;
async function initClient() {
let tokenClient = google.accounts.oauth2.initTokenClient({
client_id: AUTH_CLIENT_ID,
scope: AUTH_SCOPE,
callback: '',
});
g_onAuthScriptLoaded(tokenClient);
}
async function getToken() {
let tokenClient = await g_authScriptPromise;
try {
tokenClient.callback = (resp) => {
if (resp.error === undefined) {
g_onTokenReceived(resp.access_token);
}
};
tokenClient.requestAccessToken();
} catch (err) {
console.error(err)
}
}
async function doAuthFetch() {
toggleSigninModal(true );
const btnSignin = g_el.divSigninModal.querySelector('button.signin');
btnSignin.addEventListener('click', getToken);
await g_authTokenPromise;
toggleSigninModal(false );
}
async function fetchAccessToken() {
if (!g_authFetchCalled) {
doAuthFetch();
g_authFetchCalled = true;
}
return await g_authTokenPromise;
}
function toggleSigninModal(show) {
g_el.divSigninModal.style.display = show ? '' : 'none';
}
* @param {!Array<!URL>} urlsToLoad
* @return {boolean}
*/
function requiresAuthentication(urlsToLoad) {
for (const url of urlsToLoad) {
if (url.hostname === 'localhost')
continue;
if (url.pathname.startsWith(
'/chromium-binary-size-trybot-results/android-binary-size/')) {
continue;
}
if (url.pathname.indexOf('/oneoffs/') >= 0)
continue;
return true;
}
return false;
}