From 6632cd1e0937fc93f12ccf96abafab1d23ad4184 Mon Sep 17 00:00:00 2001 From: Barry Chen Date: Fri, 23 Mar 2018 13:30:18 -0500 Subject: [PATCH] Wait for onboarding status. (#3954) --- addon/webextension/background/main.js | 66 ++++++++++--------- .../webextension/background/selectorLoader.js | 36 +++++----- 2 files changed, 53 insertions(+), 49 deletions(-) diff --git a/addon/webextension/background/main.js b/addon/webextension/background/main.js index a54a1d1e0c..07e996271f 100644 --- a/addon/webextension/background/main.js +++ b/addon/webextension/background/main.js @@ -11,11 +11,9 @@ this.main = (function() { const manifest = browser.runtime.getManifest(); let backend; - let hasSeenOnboarding; - - browser.storage.local.get(["hasSeenOnboarding"]).then((result) => { - hasSeenOnboarding = !!result.hasSeenOnboarding; - if (!hasSeenOnboarding) { + let hasSeenOnboarding = browser.storage.local.get(["hasSeenOnboarding"]).then((result) => { + const onboarded = !!result.hasSeenOnboarding; + if (!onboarded) { setIconActive(false, null); // Note that the branded name 'Firefox Screenshots' is not localized: startBackground.photonPageActionPort.postMessage({ @@ -23,6 +21,8 @@ this.main = (function() { title: "Firefox Screenshots" }); } + hasSeenOnboarding = Promise.resolve(onboarded); + return hasSeenOnboarding; }).catch((error) => { log.error("Error getting hasSeenOnboarding:", error); }); @@ -93,34 +93,36 @@ this.main = (function() { // This is called by startBackground.js, directly in response to clicks on the Photon page action exports.onClicked = catcher.watchFunction((tab) => { - if (shouldOpenMyShots(tab.url)) { - if (!hasSeenOnboarding) { + catcher.watchPromise(hasSeenOnboarding.then(onboarded => { + if (shouldOpenMyShots(tab.url)) { + if (!onboarded) { + catcher.watchPromise(analytics.refreshTelemetryPref().then(() => { + sendEvent("goto-onboarding", "selection-button", {incognito: tab.incognito}); + return forceOnboarding(); + })); + return; + } catcher.watchPromise(analytics.refreshTelemetryPref().then(() => { - sendEvent("goto-onboarding", "selection-button", {incognito: tab.incognito}); - return forceOnboarding(); + sendEvent("goto-myshots", "about-newtab", {incognito: tab.incognito}); })); - return; + catcher.watchPromise( + auth.authHeaders() + .then(() => browser.tabs.update({url: backend + "/shots"}))); + } else { + catcher.watchPromise( + toggleSelector(tab) + .then(active => { + const event = active ? "start-shot" : "cancel-shot"; + sendEvent(event, "toolbar-button", {incognito: tab.incognito}); + }, (error) => { + if ((!onboarded) && error.popupMessage === "UNSHOOTABLE_PAGE") { + sendEvent("goto-onboarding", "selection-button", {incognito: tab.incognito}); + return forceOnboarding(); + } + throw error; + })); } - catcher.watchPromise(analytics.refreshTelemetryPref().then(() => { - sendEvent("goto-myshots", "about-newtab", {incognito: tab.incognito}); - })); - catcher.watchPromise( - auth.authHeaders() - .then(() => browser.tabs.update({url: backend + "/shots"}))); - } else { - catcher.watchPromise( - toggleSelector(tab) - .then(active => { - const event = active ? "start-shot" : "cancel-shot"; - sendEvent(event, "toolbar-button", {incognito: tab.incognito}); - }, (error) => { - if ((!hasSeenOnboarding) && error.popupMessage === "UNSHOOTABLE_PAGE") { - sendEvent("goto-onboarding", "selection-button", {incognito: tab.incognito}); - return forceOnboarding(); - } - throw error; - })); - } + })); }); function forceOnboarding() { @@ -273,8 +275,8 @@ this.main = (function() { }); communication.register("hasSeenOnboarding", () => { - hasSeenOnboarding = true; - catcher.watchPromise(browser.storage.local.set({hasSeenOnboarding})); + hasSeenOnboarding = Promise.resolve(true); + catcher.watchPromise(browser.storage.local.set({hasSeenOnboarding: true})); setIconActive(false, null); startBackground.photonPageActionPort.postMessage({ type: "setProperties", diff --git a/addon/webextension/background/selectorLoader.js b/addon/webextension/background/selectorLoader.js index 6978b4ad19..49faf97826 100644 --- a/addon/webextension/background/selectorLoader.js +++ b/addon/webextension/background/selectorLoader.js @@ -66,24 +66,26 @@ this.selectorLoader = (function() { const loadingTabs = new Set(); exports.loadModules = function(tabId, hasSeenOnboarding) { - loadingTabs.add(tabId); - let promise = downloadOnlyCheck(tabId); - if (hasSeenOnboarding) { - promise = promise.then(() => { - return executeModules(tabId, standardScripts.concat(selectorScripts)); - }); - } else { - promise = promise.then(() => { - return executeModules(tabId, standardScripts.concat(onboardingScripts).concat(selectorScripts)); + catcher.watchPromise(hasSeenOnboarding.then(onboarded => { + loadingTabs.add(tabId); + let promise = downloadOnlyCheck(tabId); + if (onboarded) { + promise = promise.then(() => { + return executeModules(tabId, standardScripts.concat(selectorScripts)); + }); + } else { + promise = promise.then(() => { + return executeModules(tabId, standardScripts.concat(onboardingScripts).concat(selectorScripts)); + }); + } + return promise.then((result) => { + loadingTabs.delete(tabId); + return result; + }, (error) => { + loadingTabs.delete(tabId); + throw error; }); - } - return promise.then((result) => { - loadingTabs.delete(tabId); - return result; - }, (error) => { - loadingTabs.delete(tabId); - throw error; - }); + })); }; // TODO: since bootstrap communication is now required, would this function