浏览代码

fix batch save (fix #1314)

Gildas 2 年之前
父节点
当前提交
988c7ffd0f
共有 1 个文件被更改,包括 30 次插入18 次删除
  1. 30 18
      src/core/bg/business.js

+ 30 - 18
src/core/bg/business.js

@@ -34,6 +34,7 @@ const ERROR_CONNECTION_ERROR_CHROMIUM = "Could not establish connection. Receivi
 const ERROR_CONNECTION_LOST_CHROMIUM = "The message port closed before a response was received.";
 const ERROR_CONNECTION_LOST_GECKO = "Message manager disconnected";
 const ERROR_EDITOR_PAGE_CHROMIUM = "Cannot access contents of url ";
+const ERROR_CHANNEL_CLOSED_CHROMIUM = "A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received";
 const INJECT_SCRIPTS_STEP = 1;
 const EXECUTE_SCRIPTS_STEP = 2;
 const TASK_PENDING_STATE = "pending";
@@ -160,11 +161,13 @@ function addTask(info) {
 		tab: info.tab,
 		options: info.options,
 		method: info.method,
-		done: function () {
+		done: function (runNextTasks = true) {
 			const index = tasks.findIndex(taskInfo => taskInfo.id == this.id);
 			if (index > -1) {
 				tasks.splice(index, 1);
-				runTasks();
+				if (runNextTasks) {
+					runTasks();
+				}
 			}
 		}
 	};
@@ -235,6 +238,7 @@ function isIgnoredError(error) {
 	return error.message == ERROR_CONNECTION_LOST_CHROMIUM ||
 		error.message == ERROR_CONNECTION_ERROR_CHROMIUM ||
 		error.message == ERROR_CONNECTION_LOST_GECKO ||
+		error.message == ERROR_CHANNEL_CLOSED_CHROMIUM ||
 		error.message.startsWith(ERROR_EDITOR_PAGE_CHROMIUM + JSON.stringify(editor.EDITOR_URL));
 }
 
@@ -243,7 +247,7 @@ function isSavingTab(tab) {
 }
 
 function onInit(tab) {
-	cancelTab(tab.id);
+	cancelTab(tab.id, false);
 }
 
 function onTabReplaced(addedTabId, removedTabId) {
@@ -292,8 +296,12 @@ function setCancelCallback(taskId, cancelCallback) {
 	}
 }
 
-function cancelTab(tabId) {
-	Array.from(tasks).filter(taskInfo => taskInfo.tab.id == tabId && !taskInfo.options.autoSave).forEach(cancel);
+function cancelTab(tabId, stopProcessing = true) {
+	Array.from(tasks).filter(taskInfo =>
+		taskInfo.tab.id == tabId &&
+		!taskInfo.options.autoSave &&
+		(stopProcessing || taskInfo.status != TASK_PROCESSING_STATE)
+	).forEach(cancel);
 }
 
 function cancelTask(taskId) {
@@ -301,7 +309,7 @@ function cancelTask(taskId) {
 }
 
 function cancelAllTasks() {
-	Array.from(tasks).forEach(cancel);
+	Array.from(tasks).forEach(taskInfo => cancel(taskInfo, false));
 }
 
 function getTasksInfo() {
@@ -312,24 +320,28 @@ function getTaskInfo(taskId) {
 	return tasks.find(taskInfo => taskInfo.id == taskId);
 }
 
-function cancel(taskInfo) {
+function cancel(taskInfo, runNextTasks) {
 	const tabId = taskInfo.tab.id;
 	taskInfo.cancelled = true;
-	browser.tabs.sendMessage(tabId, {
-		method: "content.cancelSave",
-		options: {
-			loadDeferredImages: taskInfo.options.loadDeferredImages,
-			loadDeferredImagesKeepZoomLevel: taskInfo.options.loadDeferredImagesKeepZoomLevel
+	if (tabId) {
+		browser.tabs.sendMessage(tabId, {
+			method: "content.cancelSave",
+			options: {
+				loadDeferredImages: taskInfo.options.loadDeferredImages,
+				loadDeferredImagesKeepZoomLevel: taskInfo.options.loadDeferredImagesKeepZoomLevel
+			}
+		}).catch(() => {
+			// ignored
+		});
+		if (taskInfo.method == "content.autosave") {
+			ui.onEnd(tabId, true);
 		}
-	});
+		ui.onCancelled(taskInfo.tab);
+	}
 	if (taskInfo.cancel) {
 		taskInfo.cancel();
 	}
-	if (taskInfo.method == "content.autosave") {
-		ui.onEnd(tabId, true);
-	}
-	ui.onCancelled(taskInfo.tab);
-	taskInfo.done();
+	taskInfo.done(runNextTasks);
 }
 
 function mapTaskInfo(taskInfo) {