aboutsummaryrefslogtreecommitdiff
path: root/src/background/infrastructures/ContentMessageListener.js
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2019-02-24 20:54:35 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2019-02-24 21:58:12 +0900
commita26d8a8a1bed48a77e062914c120a23ace7bb8cf (patch)
tree29ce84b9b03e87e818197b63d77e20e5a897cf0b /src/background/infrastructures/ContentMessageListener.js
parent21788740c1d4ee28ce05d6bd09d37901d1d88b68 (diff)
Capitalize background scripts
Diffstat (limited to 'src/background/infrastructures/ContentMessageListener.js')
-rw-r--r--src/background/infrastructures/ContentMessageListener.js141
1 files changed, 141 insertions, 0 deletions
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;
+ }
+}