Explorar o código

fixed issue when optionally prompting the auth code

Former-commit-id: 22401cb844466a4012633f2b92b8c69a4b36410d
Gildas %!s(int64=6) %!d(string=hai) anos
pai
achega
6ae0755ee5
Modificáronse 3 ficheiros con 20 adicións e 7 borrados
  1. 2 2
      extension/core/bg/downloads.js
  2. 17 4
      extension/core/bg/tabs.js
  3. 1 1
      lib/gdrive/gdrive.js

+ 2 - 2
extension/core/bg/downloads.js

@@ -144,7 +144,7 @@ singlefile.extension.core.bg.downloads = (() => {
 		const options = { interactive: true, auto: true };
 		if (!authInfo || force || gDrive.managedToken()) {
 			try {
-				if (options.auto && !gDrive.managedToken()) {
+				if (!gDrive.managedToken()) {
 					singlefile.extension.core.bg.tabs.getAuthCode(gDrive.getAuthURL(options))
 						.then(authCode => code = authCode)
 						.catch(() => { cancelled = true; });
@@ -165,7 +165,7 @@ singlefile.extension.core.bg.downloads = (() => {
 			}
 			await singlefile.extension.core.bg.config.setAuthInfo(authInfo);
 		}
-		if (!gDrive.managedToken()) {
+		if (!gDrive.managedToken() && authInfo.expirationDate) {
 			browser.alarms.create("refreshAuthToken", { when: Math.max(Date.now() + 60000, authInfo.expirationDate - 60000) });
 		}
 		return authInfo;

+ 17 - 4
extension/core/bg/tabs.js

@@ -61,9 +61,18 @@ singlefile.extension.core.bg.tabs = (() => {
 		promptValue: async promptMessage => {
 			const tabs = await browser.tabs.query({ currentWindow: true, active: true });
 			return new Promise(async (resolve, reject) => {
-				const tabId = tabs[0].id;
-				pendingPrompts.set(tabId, { resolve, reject });
-				browser.tabs.sendMessage(tabId, { method: "common.promptValueRequest", promptMessage });
+				const selectedTabId = tabs[0].id;
+				browser.tabs.onRemoved.addListener(onTabRemoved);
+				pendingPrompts.set(selectedTabId, { resolve, reject });
+				browser.tabs.sendMessage(selectedTabId, { method: "common.promptValueRequest", promptMessage });
+
+				function onTabRemoved(tabId) {
+					if (tabId == selectedTabId) {
+						pendingPrompts.delete(tabId);
+						browser.tabs.onUpdated.removeListener(onTabRemoved);
+						reject();
+					}
+				}
 			});
 		},
 		getAuthCode: authURL => {
@@ -96,7 +105,11 @@ singlefile.extension.core.bg.tabs = (() => {
 
 	async function onMessage(message, sender) {
 		if (message.method.endsWith(".promptValueResponse")) {
-			pendingPrompts.get(sender.tab.id).resolve(message.value);
+			const promptPromise = pendingPrompts.get(sender.tab.id);
+			if (promptPromise) {
+				promptPromise.resolve(message.value);
+				pendingPrompts.delete(sender.tab.id);
+			}
 		}
 		if (message.method.endsWith(".getOptions")) {
 			return singlefile.extension.core.bg.config.getOptions(message.url);

+ 1 - 1
lib/gdrive/gdrive.js

@@ -203,7 +203,7 @@ this.GDrive = this.GDrive || (() => {
 
 	async function initAuth(gdrive, options) {
 		try {
-			return browser.identity.launchWebAuthFlow({
+			return await browser.identity.launchWebAuthFlow({
 				interactive: options.interactive,
 				url: gdrive.authURL
 			});