From a26d8a8a1bed48a77e062914c120a23ace7bb8cf Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 24 Feb 2019 20:54:35 +0900 Subject: Capitalize background scripts --- src/background/infrastructures/ConsoleClient.js | 37 ++++++ .../infrastructures/ContentMessageClient.js | 33 +++++ .../infrastructures/ContentMessageListener.js | 141 +++++++++++++++++++++ src/background/infrastructures/MemoryStorage.js | 19 +++ src/background/infrastructures/Notifier.js | 23 ++++ .../infrastructures/content-message-client.js | 33 ----- .../infrastructures/content-message-listener.js | 141 --------------------- src/background/infrastructures/memory-storage.js | 19 --- src/background/infrastructures/notifier.js | 23 ---- 9 files changed, 253 insertions(+), 216 deletions(-) create mode 100644 src/background/infrastructures/ConsoleClient.js create mode 100644 src/background/infrastructures/ContentMessageClient.js create mode 100644 src/background/infrastructures/ContentMessageListener.js create mode 100644 src/background/infrastructures/MemoryStorage.js create mode 100644 src/background/infrastructures/Notifier.js delete mode 100644 src/background/infrastructures/content-message-client.js delete mode 100644 src/background/infrastructures/content-message-listener.js delete mode 100644 src/background/infrastructures/memory-storage.js delete mode 100644 src/background/infrastructures/notifier.js (limited to 'src/background/infrastructures') diff --git a/src/background/infrastructures/ConsoleClient.js b/src/background/infrastructures/ConsoleClient.js new file mode 100644 index 0000000..f691515 --- /dev/null +++ b/src/background/infrastructures/ConsoleClient.js @@ -0,0 +1,37 @@ +import messages from '../../shared/messages'; + +export default class ConsoleClient { + showCommand(tabId, command) { + return browser.tabs.sendMessage(tabId, { + type: messages.CONSOLE_SHOW_COMMAND, + command, + }); + } + + showFind(tabId) { + return browser.tabs.sendMessage(tabId, { + type: messages.CONSOLE_SHOW_FIND + }); + } + + showInfo(tabId, message) { + return browser.tabs.sendMessage(tabId, { + type: messages.CONSOLE_SHOW_INFO, + text: message, + }); + } + + showError(tabId, message) { + return browser.tabs.sendMessage(tabId, { + type: messages.CONSOLE_SHOW_ERROR, + text: message, + }); + } + + hide(tabId) { + return browser.tabs.sendMessage(tabId, { + type: messages.CONSOLE_HIDE, + }); + } +} + diff --git a/src/background/infrastructures/ContentMessageClient.js b/src/background/infrastructures/ContentMessageClient.js new file mode 100644 index 0000000..7e7e602 --- /dev/null +++ b/src/background/infrastructures/ContentMessageClient.js @@ -0,0 +1,33 @@ +import messages from '../../shared/messages'; + +export default class ContentMessageClient { + async broadcastSettingsChanged() { + let tabs = await browser.tabs.query({}); + for (let tab of tabs) { + browser.tabs.sendMessage(tab.id, { + type: messages.SETTINGS_CHANGED, + }); + } + } + + async getAddonEnabled(tabId) { + let { enabled } = await browser.tabs.sendMessage(tabId, { + type: messages.ADDON_ENABLED_QUERY, + }); + return enabled; + } + + toggleAddonEnabled(tabId) { + return browser.tabs.sendMessage(tabId, { + type: messages.ADDON_TOGGLE_ENABLED, + }); + } + + scrollTo(tabId, x, y) { + return browser.tabs.sendMessage(tabId, { + type: messages.TAB_SCROLL_TO, + x, + y, + }); + } +} diff --git a/src/background/infrastructures/ContentMessageListener.js b/src/background/infrastructures/ContentMessageListener.js new file mode 100644 index 0000000..1179a8c --- /dev/null +++ b/src/background/infrastructures/ContentMessageListener.js @@ -0,0 +1,141 @@ +import messages from '../../shared/messages'; +import CommandController from '../controllers/CommandController'; +import SettingController from '../controllers/SettingController'; +import FindController from '../controllers/FindController'; +import AddonEnabledController from '../controllers/AddonEnabledController'; +import LinkController from '../controllers/LinkController'; +import OperationController from '../controllers/OperationController'; +import MarkController from '../controllers/MarkController'; + +export default class ContentMessageListener { + constructor() { + this.settingController = new SettingController(); + this.commandController = new CommandController(); + this.findController = new FindController(); + this.addonEnabledController = new AddonEnabledController(); + this.linkController = new LinkController(); + this.backgroundOperationController = new OperationController(); + this.markController = new MarkController(); + + this.consolePorts = {}; + } + + run() { + browser.runtime.onMessage.addListener((message, sender) => { + try { + let ret = this.onMessage(message, sender); + if (!(ret instanceof Promise)) { + return {}; + } + return ret.catch((e) => { + return browser.tabs.sendMessage(sender.tab.id, { + type: messages.CONSOLE_SHOW_ERROR, + text: e.message, + }); + }); + } catch (e) { + return browser.tabs.sendMessage(sender.tab.id, { + type: messages.CONSOLE_SHOW_ERROR, + text: e.message, + }); + } + }); + browser.runtime.onConnect.addListener(this.onConnected.bind(this)); + } + + onMessage(message, sender) { + switch (message.type) { + case messages.CONSOLE_QUERY_COMPLETIONS: + return this.onConsoleQueryCompletions(message.text); + case messages.CONSOLE_ENTER_COMMAND: + return this.onConsoleEnterCommand(message.text); + case messages.SETTINGS_QUERY: + return this.onSettingsQuery(); + case messages.SETTINGS_RELOAD: + return this.onSettingsReload(); + case messages.FIND_GET_KEYWORD: + return this.onFindGetKeyword(); + case messages.FIND_SET_KEYWORD: + return this.onFindSetKeyword(message.keyword); + case messages.ADDON_ENABLED_RESPONSE: + return this.onAddonEnabledResponse(message.enabled); + case messages.OPEN_URL: + return this.onOpenUrl( + message.newTab, message.url, sender.tab.id, message.background); + case messages.BACKGROUND_OPERATION: + return this.onBackgroundOperation(message.operation); + case messages.MARK_SET_GLOBAL: + return this.onMarkSetGlobal(message.key, message.x, message.y); + case messages.MARK_JUMP_GLOBAL: + return this.onMarkJumpGlobal(message.key); + case messages.CONSOLE_FRAME_MESSAGE: + return this.onConsoleFrameMessage(sender.tab.id, message.message); + } + } + + async onConsoleQueryCompletions(line) { + let completions = await this.commandController.getCompletions(line); + return Promise.resolve(completions.serialize()); + } + + onConsoleEnterCommand(text) { + return this.commandController.exec(text); + } + + + onSettingsQuery() { + return this.settingController.getSetting(); + } + + onSettingsReload() { + return this.settingController.reload(); + } + + onFindGetKeyword() { + return this.findController.getKeyword(); + } + + onFindSetKeyword(keyword) { + return this.findController.setKeyword(keyword); + } + + onAddonEnabledResponse(enabled) { + return this.addonEnabledController.indicate(enabled); + } + + onOpenUrl(newTab, url, openerId, background) { + if (newTab) { + return this.linkController.openNewTab(url, openerId, background); + } + return this.linkController.openToTab(url, openerId); + } + + onBackgroundOperation(operation) { + return this.backgroundOperationController.exec(operation); + } + + onMarkSetGlobal(key, x, y) { + return this.markController.setGlobal(key, x, y); + } + + onMarkJumpGlobal(key) { + return this.markController.jumpGlobal(key); + } + + onConsoleFrameMessage(tabId, message) { + let port = this.consolePorts[tabId]; + if (!port) { + return; + } + port.postMessage(message); + } + + onConnected(port) { + if (port.name !== 'vimvixen-console') { + return; + } + + let id = port.sender.tab.id; + this.consolePorts[id] = port; + } +} diff --git a/src/background/infrastructures/MemoryStorage.js b/src/background/infrastructures/MemoryStorage.js new file mode 100644 index 0000000..3a7e4f2 --- /dev/null +++ b/src/background/infrastructures/MemoryStorage.js @@ -0,0 +1,19 @@ +const db = {}; + +export default class MemoryStorage { + set(name, value) { + let data = JSON.stringify(value); + if (typeof data === 'undefined') { + throw new Error('value is not serializable'); + } + db[name] = data; + } + + get(name) { + let data = db[name]; + if (!data) { + return undefined; + } + return JSON.parse(data); + } +} diff --git a/src/background/infrastructures/Notifier.js b/src/background/infrastructures/Notifier.js new file mode 100644 index 0000000..1eccc47 --- /dev/null +++ b/src/background/infrastructures/Notifier.js @@ -0,0 +1,23 @@ +const NOTIFICATION_ID = 'vimvixen-update'; + +export default class Notifier { + notify(title, message, onclick) { + const listener = (id) => { + if (id !== NOTIFICATION_ID) { + return; + } + + onclick(); + + browser.notifications.onClicked.removeListener(listener); + }; + browser.notifications.onClicked.addListener(listener); + + return browser.notifications.create(NOTIFICATION_ID, { + 'type': 'basic', + 'iconUrl': browser.extension.getURL('resources/icon_48x48.png'), + title, + message, + }); + } +} diff --git a/src/background/infrastructures/content-message-client.js b/src/background/infrastructures/content-message-client.js deleted file mode 100644 index 7e7e602..0000000 --- a/src/background/infrastructures/content-message-client.js +++ /dev/null @@ -1,33 +0,0 @@ -import messages from '../../shared/messages'; - -export default class ContentMessageClient { - async broadcastSettingsChanged() { - let tabs = await browser.tabs.query({}); - for (let tab of tabs) { - browser.tabs.sendMessage(tab.id, { - type: messages.SETTINGS_CHANGED, - }); - } - } - - async getAddonEnabled(tabId) { - let { enabled } = await browser.tabs.sendMessage(tabId, { - type: messages.ADDON_ENABLED_QUERY, - }); - return enabled; - } - - toggleAddonEnabled(tabId) { - return browser.tabs.sendMessage(tabId, { - type: messages.ADDON_TOGGLE_ENABLED, - }); - } - - scrollTo(tabId, x, y) { - return browser.tabs.sendMessage(tabId, { - type: messages.TAB_SCROLL_TO, - x, - y, - }); - } -} diff --git a/src/background/infrastructures/content-message-listener.js b/src/background/infrastructures/content-message-listener.js deleted file mode 100644 index aae07c0..0000000 --- a/src/background/infrastructures/content-message-listener.js +++ /dev/null @@ -1,141 +0,0 @@ -import messages from '../../shared/messages'; -import CommandController from '../controllers/command'; -import SettingController from '../controllers/setting'; -import FindController from '../controllers/find'; -import AddonEnabledController from '../controllers/addon-enabled'; -import LinkController from '../controllers/link'; -import OperationController from '../controllers/operation'; -import MarkController from '../controllers/mark'; - -export default class ContentMessageListener { - constructor() { - this.settingController = new SettingController(); - this.commandController = new CommandController(); - this.findController = new FindController(); - this.addonEnabledController = new AddonEnabledController(); - this.linkController = new LinkController(); - this.backgroundOperationController = new OperationController(); - this.markController = new MarkController(); - - this.consolePorts = {}; - } - - run() { - browser.runtime.onMessage.addListener((message, sender) => { - try { - let ret = this.onMessage(message, sender); - if (!(ret instanceof Promise)) { - return {}; - } - return ret.catch((e) => { - return browser.tabs.sendMessage(sender.tab.id, { - type: messages.CONSOLE_SHOW_ERROR, - text: e.message, - }); - }); - } catch (e) { - return browser.tabs.sendMessage(sender.tab.id, { - type: messages.CONSOLE_SHOW_ERROR, - text: e.message, - }); - } - }); - browser.runtime.onConnect.addListener(this.onConnected.bind(this)); - } - - onMessage(message, sender) { - switch (message.type) { - case messages.CONSOLE_QUERY_COMPLETIONS: - return this.onConsoleQueryCompletions(message.text); - case messages.CONSOLE_ENTER_COMMAND: - return this.onConsoleEnterCommand(message.text); - case messages.SETTINGS_QUERY: - return this.onSettingsQuery(); - case messages.SETTINGS_RELOAD: - return this.onSettingsReload(); - case messages.FIND_GET_KEYWORD: - return this.onFindGetKeyword(); - case messages.FIND_SET_KEYWORD: - return this.onFindSetKeyword(message.keyword); - case messages.ADDON_ENABLED_RESPONSE: - return this.onAddonEnabledResponse(message.enabled); - case messages.OPEN_URL: - return this.onOpenUrl( - message.newTab, message.url, sender.tab.id, message.background); - case messages.BACKGROUND_OPERATION: - return this.onBackgroundOperation(message.operation); - case messages.MARK_SET_GLOBAL: - return this.onMarkSetGlobal(message.key, message.x, message.y); - case messages.MARK_JUMP_GLOBAL: - return this.onMarkJumpGlobal(message.key); - case messages.CONSOLE_FRAME_MESSAGE: - return this.onConsoleFrameMessage(sender.tab.id, message.message); - } - } - - async onConsoleQueryCompletions(line) { - let completions = await this.commandController.getCompletions(line); - return Promise.resolve(completions.serialize()); - } - - onConsoleEnterCommand(text) { - return this.commandController.exec(text); - } - - - onSettingsQuery() { - return this.settingController.getSetting(); - } - - onSettingsReload() { - return this.settingController.reload(); - } - - onFindGetKeyword() { - return this.findController.getKeyword(); - } - - onFindSetKeyword(keyword) { - return this.findController.setKeyword(keyword); - } - - onAddonEnabledResponse(enabled) { - return this.addonEnabledController.indicate(enabled); - } - - onOpenUrl(newTab, url, openerId, background) { - if (newTab) { - return this.linkController.openNewTab(url, openerId, background); - } - return this.linkController.openToTab(url, openerId); - } - - onBackgroundOperation(operation) { - return this.backgroundOperationController.exec(operation); - } - - onMarkSetGlobal(key, x, y) { - return this.markController.setGlobal(key, x, y); - } - - onMarkJumpGlobal(key) { - return this.markController.jumpGlobal(key); - } - - onConsoleFrameMessage(tabId, message) { - let port = this.consolePorts[tabId]; - if (!port) { - return; - } - port.postMessage(message); - } - - onConnected(port) { - if (port.name !== 'vimvixen-console') { - return; - } - - let id = port.sender.tab.id; - this.consolePorts[id] = port; - } -} diff --git a/src/background/infrastructures/memory-storage.js b/src/background/infrastructures/memory-storage.js deleted file mode 100644 index 3a7e4f2..0000000 --- a/src/background/infrastructures/memory-storage.js +++ /dev/null @@ -1,19 +0,0 @@ -const db = {}; - -export default class MemoryStorage { - set(name, value) { - let data = JSON.stringify(value); - if (typeof data === 'undefined') { - throw new Error('value is not serializable'); - } - db[name] = data; - } - - get(name) { - let data = db[name]; - if (!data) { - return undefined; - } - return JSON.parse(data); - } -} diff --git a/src/background/infrastructures/notifier.js b/src/background/infrastructures/notifier.js deleted file mode 100644 index 1eccc47..0000000 --- a/src/background/infrastructures/notifier.js +++ /dev/null @@ -1,23 +0,0 @@ -const NOTIFICATION_ID = 'vimvixen-update'; - -export default class Notifier { - notify(title, message, onclick) { - const listener = (id) => { - if (id !== NOTIFICATION_ID) { - return; - } - - onclick(); - - browser.notifications.onClicked.removeListener(listener); - }; - browser.notifications.onClicked.addListener(listener); - - return browser.notifications.create(NOTIFICATION_ID, { - 'type': 'basic', - 'iconUrl': browser.extension.getURL('resources/icon_48x48.png'), - title, - message, - }); - } -} -- cgit v1.2.3