aboutsummaryrefslogtreecommitdiff
path: root/src/background/infrastructures
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2018-07-28 20:05:06 +0900
committerGitHub <noreply@github.com>2018-07-28 20:05:06 +0900
commited2bd7d75ee1e7aa1db7d03c3f908c740ded1983 (patch)
tree6ac3f5ac5126e1a07c958549c782aedd586c6534 /src/background/infrastructures
parent84a9655bb39e5902b417e124a0eb23d80808a6a7 (diff)
parent4bd2084ba7b23327c26a2d8b24dc4169c14bfa17 (diff)
Merge pull request #440 from ueokande/background-clean-architecture
Background clean architecture
Diffstat (limited to 'src/background/infrastructures')
-rw-r--r--src/background/infrastructures/content-message-client.js25
-rw-r--r--src/background/infrastructures/content-message-listener.js101
-rw-r--r--src/background/infrastructures/memory-storage.js19
-rw-r--r--src/background/infrastructures/notifier.js23
4 files changed, 168 insertions, 0 deletions
diff --git a/src/background/infrastructures/content-message-client.js b/src/background/infrastructures/content-message-client.js
new file mode 100644
index 0000000..d659560
--- /dev/null
+++ b/src/background/infrastructures/content-message-client.js
@@ -0,0 +1,25 @@
+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,
+ });
+ }
+}
diff --git a/src/background/infrastructures/content-message-listener.js b/src/background/infrastructures/content-message-listener.js
new file mode 100644
index 0000000..58716fb
--- /dev/null
+++ b/src/background/infrastructures/content-message-listener.js
@@ -0,0 +1,101 @@
+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';
+
+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();
+ }
+
+ run() {
+ browser.runtime.onMessage.addListener((message, sender) => {
+ try {
+ return this.onMessage(message, sender).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,
+ });
+ }
+ });
+ }
+
+ 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);
+ }
+ }
+
+ 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);
+ }
+}
diff --git a/src/background/infrastructures/memory-storage.js b/src/background/infrastructures/memory-storage.js
new file mode 100644
index 0000000..3a7e4f2
--- /dev/null
+++ b/src/background/infrastructures/memory-storage.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,
+ });
+ }
+}