aboutsummaryrefslogtreecommitdiff
path: root/src/background/infrastructures/content-message-listener.js
blob: 2fa5f54845b30928fc2c6abb32f33bbe7ac62d74 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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) => {
      return this.onMessage(message, sender).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);
  }
}