business.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright 2010-2020 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * The code in this file is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU Affero General Public License
  9. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * The code in this file is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  15. * General Public License for more details.
  16. *
  17. * As additional permission under GNU AGPL version 3 section 7, you may
  18. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  19. * AGPL normally required by section 4, provided you include this license
  20. * notice and a URL through which recipients can access the Corresponding
  21. * Source.
  22. */
  23. /* global extension */
  24. extension.core.bg.business = (() => {
  25. const ERROR_CONNECTION_ERROR_CHROMIUM = "Could not establish connection. Receiving end does not exist.";
  26. const ERROR_CONNECTION_LOST_CHROMIUM = "The message port closed before a response was received.";
  27. const ERROR_CONNECTION_LOST_GECKO = "Message manager disconnected";
  28. const ERROR_EDITOR_PAGE_CHROMIUM = "Cannot access contents of url ";
  29. const INJECT_SCRIPTS_STEP = 1;
  30. const EXECUTE_SCRIPTS_STEP = 2;
  31. const TASK_PENDING_STATE = "pending";
  32. const TASK_PROCESSING_STATE = "processing";
  33. const extensionScriptFiles = [
  34. "common/index.js",
  35. "common/ui/content/content-infobar.js",
  36. "extension/index.js",
  37. "extension/lib/single-file/index.js",
  38. "extension/core/index.js",
  39. "extension/ui/index.js",
  40. "extension/core/content/content-main.js",
  41. "extension/core/content/content-download.js",
  42. "extension/ui/content/content-ui-main.js"
  43. ];
  44. const tasks = [];
  45. let currentTaskId = 0, maxParallelWorkers;
  46. return {
  47. isSavingTab,
  48. saveTabs,
  49. saveUrls,
  50. saveSelectedLinks,
  51. cancelTab,
  52. cancelTask,
  53. cancelAllTasks,
  54. getTasksInfo,
  55. getTaskInfo,
  56. setCancelCallback,
  57. openEditor,
  58. onSaveEnd,
  59. onInit,
  60. onTabRemoved: cancelTab
  61. };
  62. async function saveSelectedLinks(tab) {
  63. const tabs = extension.core.bg.tabs;
  64. const tabOptions = { extensionScriptFiles, tabId: tab.id, tabIndex: tab.index };
  65. const scriptsInjected = await extension.injectScript(tab.id, tabOptions);
  66. if (scriptsInjected) {
  67. const response = await tabs.sendMessage(tab.id, { method: "content.getSelectedLinks" });
  68. if (response.urls && response.urls.length) {
  69. await saveUrls(response.urls);
  70. }
  71. } else {
  72. extension.ui.bg.main.onForbiddenDomain(tab);
  73. }
  74. }
  75. async function saveUrls(urls, options = {}) {
  76. await initMaxParallelWorkers();
  77. await Promise.all(urls.map(async url => {
  78. const tabOptions = await extension.core.bg.config.getOptions(url);
  79. Object.keys(options).forEach(key => tabOptions[key] = options[key]);
  80. tabOptions.autoClose = true;
  81. tabOptions.extensionScriptFiles = extensionScriptFiles;
  82. if (tabOptions.passReferrerOnError) {
  83. await extension.core.bg.requests.enableReferrerOnError();
  84. }
  85. addTask({
  86. tab: { url },
  87. status: TASK_PENDING_STATE,
  88. options: tabOptions,
  89. method: "content.save"
  90. });
  91. }));
  92. runTasks();
  93. }
  94. async function saveTabs(tabs, options = {}) {
  95. const config = extension.core.bg.config;
  96. const autosave = extension.core.bg.autosave;
  97. const ui = extension.ui.bg.main;
  98. await initMaxParallelWorkers();
  99. await Promise.all(tabs.map(async tab => {
  100. const tabId = tab.id;
  101. const tabOptions = await config.getOptions(tab.url);
  102. Object.keys(options).forEach(key => tabOptions[key] = options[key]);
  103. tabOptions.tabId = tabId;
  104. tabOptions.tabIndex = tab.index;
  105. tabOptions.extensionScriptFiles = extensionScriptFiles;
  106. if (tabOptions.passReferrerOnError) {
  107. await extension.core.bg.requests.enableReferrerOnError();
  108. }
  109. if (options.autoSave) {
  110. if (autosave.isEnabled(tab)) {
  111. const taskInfo = addTask({
  112. status: TASK_PROCESSING_STATE,
  113. tab,
  114. options: tabOptions,
  115. method: "content.autosave"
  116. });
  117. runTask(taskInfo);
  118. }
  119. } else {
  120. ui.onStart(tabId, INJECT_SCRIPTS_STEP);
  121. const scriptsInjected = await extension.injectScript(tabId, tabOptions);
  122. if (scriptsInjected || extension.core.bg.editor.isEditor(tab)) {
  123. ui.onStart(tabId, EXECUTE_SCRIPTS_STEP);
  124. addTask({
  125. status: TASK_PENDING_STATE,
  126. tab,
  127. options: tabOptions,
  128. method: "content.save"
  129. });
  130. } else {
  131. ui.onForbiddenDomain(tab);
  132. }
  133. }
  134. }));
  135. runTasks();
  136. }
  137. function addTask(info) {
  138. const taskInfo = {
  139. id: currentTaskId,
  140. status: info.status,
  141. tab: info.tab,
  142. options: info.options,
  143. method: info.method,
  144. done: function () {
  145. tasks.splice(tasks.findIndex(taskInfo => taskInfo.id == this.id), 1);
  146. runTasks();
  147. }
  148. };
  149. tasks.push(taskInfo);
  150. currentTaskId++;
  151. return taskInfo;
  152. }
  153. function openEditor(tab) {
  154. extension.core.bg.tabs.sendMessage(tab.id, { method: "content.openEditor" });
  155. }
  156. async function initMaxParallelWorkers() {
  157. if (!maxParallelWorkers) {
  158. maxParallelWorkers = (await extension.core.bg.config.get()).maxParallelWorkers;
  159. }
  160. }
  161. function runTasks() {
  162. const processingCount = tasks.filter(taskInfo => taskInfo.status == TASK_PROCESSING_STATE).length;
  163. for (let index = 0; index < Math.min(tasks.length - processingCount, (maxParallelWorkers - processingCount)); index++) {
  164. const taskInfo = tasks.find(taskInfo => taskInfo.status == TASK_PENDING_STATE);
  165. if (taskInfo) {
  166. runTask(taskInfo);
  167. }
  168. }
  169. }
  170. async function runTask(taskInfo) {
  171. const ui = extension.ui.bg.main;
  172. const tabs = extension.core.bg.tabs;
  173. const taskId = taskInfo.id;
  174. taskInfo.status = TASK_PROCESSING_STATE;
  175. if (!taskInfo.tab.id) {
  176. let scriptsInjected;
  177. try {
  178. const tab = await tabs.createAndWait({ url: taskInfo.tab.url, active: false });
  179. taskInfo.tab.id = taskInfo.options.tabId = tab.id;
  180. taskInfo.tab.index = taskInfo.options.tabIndex = tab.index;
  181. ui.onStart(taskInfo.tab.id, INJECT_SCRIPTS_STEP);
  182. scriptsInjected = await extension.injectScript(taskInfo.tab.id, taskInfo.options);
  183. } catch (tabId) {
  184. taskInfo.tab.id = tabId;
  185. }
  186. if (scriptsInjected) {
  187. ui.onStart(taskInfo.tab.id, EXECUTE_SCRIPTS_STEP);
  188. } else {
  189. taskInfo.done();
  190. return;
  191. }
  192. }
  193. taskInfo.options.taskId = taskId;
  194. try {
  195. await tabs.sendMessage(taskInfo.tab.id, { method: taskInfo.method, options: taskInfo.options });
  196. } catch (error) {
  197. if (error && (!error.message || !isIgnoredError(error))) {
  198. console.log(error.message ? error.message : error); // eslint-disable-line no-console
  199. ui.onError(taskInfo.tab.id);
  200. taskInfo.done();
  201. }
  202. }
  203. }
  204. function isIgnoredError(error) {
  205. return error.message == ERROR_CONNECTION_LOST_CHROMIUM ||
  206. error.message == ERROR_CONNECTION_ERROR_CHROMIUM ||
  207. error.message == ERROR_CONNECTION_LOST_GECKO ||
  208. error.message.startsWith(ERROR_EDITOR_PAGE_CHROMIUM + JSON.stringify(extension.core.bg.editor.EDITOR_URL));
  209. }
  210. function isSavingTab(tab) {
  211. return Boolean(tasks.find(taskInfo => taskInfo.tab.id == tab.id));
  212. }
  213. function onInit(tab) {
  214. cancelTab(tab.id);
  215. }
  216. function onSaveEnd(taskId) {
  217. const taskInfo = tasks.find(taskInfo => taskInfo.id == taskId);
  218. if (taskInfo) {
  219. if (taskInfo.options.autoClose && !taskInfo.cancelled) {
  220. extension.core.bg.tabs.remove(taskInfo.tab.id);
  221. }
  222. taskInfo.done();
  223. }
  224. }
  225. function setCancelCallback(taskId, cancelCallback) {
  226. const taskInfo = tasks.find(taskInfo => taskInfo.id == taskId);
  227. if (taskInfo) {
  228. taskInfo.cancel = cancelCallback;
  229. }
  230. }
  231. function cancelTab(tabId) {
  232. Array.from(tasks).filter(taskInfo => taskInfo.tab.id == tabId && !taskInfo.options.autoSave).forEach(cancel);
  233. }
  234. function cancelTask(taskId) {
  235. cancel(tasks.find(taskInfo => taskInfo.id == taskId));
  236. }
  237. function cancelAllTasks() {
  238. Array.from(tasks).forEach(cancel);
  239. }
  240. function getTasksInfo() {
  241. return tasks.map(mapTaskInfo);
  242. }
  243. function getTaskInfo(taskId) {
  244. return tasks.find(taskInfo => taskInfo.id == taskId);
  245. }
  246. function cancel(taskInfo) {
  247. const tabId = taskInfo.tab.id;
  248. taskInfo.cancelled = true;
  249. extension.core.bg.tabs.sendMessage(tabId, {
  250. method: "content.cancelSave",
  251. options: {
  252. loadDeferredImages: taskInfo.options.loadDeferredImages,
  253. loadDeferredImagesKeepZoomLevel: taskInfo.options.loadDeferredImagesKeepZoomLevel
  254. }
  255. });
  256. if (taskInfo.cancel) {
  257. taskInfo.cancel();
  258. }
  259. if (taskInfo.method == "content.autosave") {
  260. extension.ui.bg.main.onEnd(tabId, true);
  261. }
  262. extension.ui.bg.main.onCancelled(taskInfo.tab);
  263. taskInfo.done();
  264. }
  265. function mapTaskInfo(taskInfo) {
  266. return { id: taskInfo.id, tabId: taskInfo.tab.id, index: taskInfo.tab.index, url: taskInfo.tab.url, title: taskInfo.tab.title, cancelled: taskInfo.cancelled, status: taskInfo.status };
  267. }
  268. })();