Selaa lähdekoodia

refactored code realted to identity permission

Former-commit-id: 8c42ac2151a033f8be34980a1c2882bf663bdba2
Gildas 6 vuotta sitten
vanhempi
sitoutus
30b7908f24
2 muutettua tiedostoa jossa 19 lisäystä ja 13 poistoa
  1. 17 3
      extension/core/bg/downloads.js
  2. 2 10
      lib/gdrive/gdrive.js

+ 17 - 3
extension/core/bg/downloads.js

@@ -41,6 +41,7 @@ singlefile.extension.core.bg.downloads = (() => {
 
 	const manifest = browser.runtime.getManifest();
 	const gDrive = new GDrive(CLIENT_ID, SCOPES);
+	let permissionIdentityRequested = manifest.optional_permissions && manifest.optional_permissions.includes("identity");
 
 	return {
 		onMessage,
@@ -110,8 +111,8 @@ singlefile.extension.core.bg.downloads = (() => {
 			return {};
 		}
 		if (message.method.endsWith(".enableGDrive")) {
-			if (!manifest.permissions || !manifest.permissions.includes("identity")) {
-				await browser.permissions.request({ permissions: ["identity"] });
+			if (permissionIdentityRequested) {
+				await requestPermissionIdentity();
 			}
 			return {};
 		}
@@ -132,7 +133,10 @@ singlefile.extension.core.bg.downloads = (() => {
 	async function getAuthInfo(force) {
 		let code, cancelled, authInfo = await singlefile.extension.core.bg.config.getAuthInfo();
 		gDrive.setAuthInfo(authInfo);
-		const options = { interactive: true, auto: true, requestPermissionIdentity: manifest.optional_permissions && manifest.optional_permissions.includes("identity") };
+		const options = { interactive: true, auto: true };
+		if (permissionIdentityRequested) {
+			await requestPermissionIdentity();
+		}
 		if (!authInfo || force || gDrive.managedToken()) {
 			try {
 				if (!gDrive.managedToken()) {
@@ -157,6 +161,16 @@ singlefile.extension.core.bg.downloads = (() => {
 		return authInfo;
 	}
 
+	async function requestPermissionIdentity() {
+		try {
+			await browser.permissions.request({ permissions: ["identity"] });
+			permissionIdentityRequested = false;
+		}
+		catch (error) {
+			// ignored;
+		}
+	}
+
 	async function uploadPage(filename, blob, tabId) {
 		try {
 			await getAuthInfo();

+ 2 - 10
lib/gdrive/gdrive.js

@@ -40,15 +40,7 @@ this.GDrive = this.GDrive || (() => {
 			this.folderIds = new Map();
 			setInterval(() => this.folderIds.clear(), 60 * 1000);
 		}
-		async auth(options = { interactive: true, auto: true, requestPermissionIdentity: true }) {
-			try {
-				if (options.requestPermissionIdentity) {
-					await browser.permissions.request({ permissions: ["identity"] });
-				}
-			}
-			catch (error) {
-				// ignored;
-			}
+		async auth(options = { interactive: true, auto: true }) {
 			if (this.managedToken()) {
 				const token = await browser.identity.getAuthToken({ interactive: options.interactive });
 				if (token) {
@@ -59,7 +51,7 @@ this.GDrive = this.GDrive || (() => {
 				this.getAuthURL(options);
 				return options.code ? authFromCode(this, options) : initAuth(this, options);
 			}
-		}
+		}		
 		managedToken() {
 			return Boolean(browser.identity.getAuthToken);
 		}