aboutsummaryrefslogtreecommitdiff
path: root/src/background/infrastructures/ContentMessageListener.ts
blob: 4a6a3b8af2fbebe3801a699a0b2a130d08366cab (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { injectable } from "tsyringe";
import * as messages from "../../shared/messages";
import * as operations from "../../shared/operations";
import CommandController from "../controllers/CommandController";
import SettingController from "../controllers/SettingController";
import AddonEnabledController from "../controllers/AddonEnabledController";
import LinkController from "../controllers/LinkController";
import OperationController from "../controllers/OperationController";
import MarkController from "../controllers/MarkController";
import CompletionController from "../controllers/CompletionController";
import ConsoleController from "../controllers/ConsoleController";

@injectable()
export default class ContentMessageListener {
  private readonly consolePorts: { [tabId: number]: browser.runtime.Port } = {};

  constructor(
    private readonly settingController: SettingController,
    private readonly commandController: CommandController,
    private readonly completionController: CompletionController,
    private readonly addonEnabledController: AddonEnabledController,
    private readonly linkController: LinkController,
    private readonly operationController: OperationController,
    private readonly markController: MarkController,
    private readonly consoleController: ConsoleController
  ) {}

  run(): void {
    browser.runtime.onMessage.addListener(
      (message: any, sender: browser.runtime.MessageSender) => {
        try {
          const ret = this.onMessage(message, sender.tab as browser.tabs.Tab);
          if (!(ret instanceof Promise)) {
            return {};
          }
          return ret.catch((e) => {
            if (!sender.tab || !sender.tab.id) {
              return;
            }
            return browser.tabs.sendMessage(sender.tab.id, {
              type: messages.CONSOLE_SHOW_ERROR,
              text: e.message,
            });
          });
        } catch (e) {
          if (!sender.tab || !sender.tab.id) {
            return;
          }
          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: messages.Message,
    senderTab: browser.tabs.Tab
  ): Promise<unknown> | unknown {
    switch (message.type) {
      case messages.CONSOLE_GET_COMPLETION_TYPES:
        return this.completionController.getCompletionTypes();
      case messages.CONSOLE_REQUEST_SEARCH_ENGINES_MESSAGE:
        return this.completionController.requestSearchEngines(message.query);
      case messages.CONSOLE_REQUEST_BOOKMARKS:
        return this.completionController.requestBookmarks(message.query);
      case messages.CONSOLE_REQUEST_HISTORY:
        return this.completionController.requestHistory(message.query);
      case messages.CONSOLE_REQUEST_TABS:
        return this.completionController.queryTabs(
          message.query,
          message.excludePinned
        );
      case messages.CONSOLE_GET_PROPERTIES:
        return this.completionController.getProperties();
      case messages.CONSOLE_ENTER_COMMAND:
        return this.onConsoleEnterCommand(message.text);
      case messages.CONSOLE_RESIZE:
        return this.onConsoleResize(
          senderTab.id!,
          message.width,
          message.height
        );
      case messages.SETTINGS_QUERY:
        return this.onSettingsQuery();
      case messages.ADDON_ENABLED_RESPONSE:
        return this.onAddonEnabledResponse(message.enabled);
      case messages.OPEN_URL:
        return this.onOpenUrl(
          message.newTab,
          message.url,
          senderTab.id as number,
          message.background
        );
      case messages.BACKGROUND_OPERATION:
        return this.onBackgroundOperation(message.repeat, 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(
          senderTab.id as number,
          message.message
        );
    }
    throw new Error("unsupported message: " + message.type);
  }

  onConsoleEnterCommand(text: string): Promise<unknown> {
    return this.commandController.exec(text);
  }

  onConsoleResize(
    senderTabId: number,
    width: number,
    height: number
  ): Promise<void> {
    return this.consoleController.resize(senderTabId, width, height);
  }

  async onSettingsQuery(): Promise<unknown> {
    return (await this.settingController.getSetting()).toJSON();
  }

  onAddonEnabledResponse(enabled: boolean): Promise<void> {
    return this.addonEnabledController.indicate(enabled);
  }

  onOpenUrl(
    newTab: boolean,
    url: string,
    openerId: number,
    background: boolean
  ): Promise<void> {
    if (newTab) {
      return this.linkController.openNewTab(url, openerId, background);
    }
    return this.linkController.openToTab(url, openerId);
  }

  onBackgroundOperation(
    count: number,
    op: operations.Operation
  ): Promise<void> {
    return this.operationController.exec(count, op);
  }

  onMarkSetGlobal(key: string, x: number, y: number): Promise<void> {
    return this.markController.setGlobal(key, x, y);
  }

  onMarkJumpGlobal(key: string): Promise<void> {
    return this.markController.jumpGlobal(key);
  }

  onConsoleFrameMessage(tabId: number, message: any): void {
    const port = this.consolePorts[tabId];
    if (!port) {
      return;
    }
    port.postMessage(message);
  }

  onConnected(port: browser.runtime.Port): void {
    if (port.name !== "vimvixen-console") {
      return;
    }

    if (port.sender && port.sender.tab && port.sender.tab.id) {
      const id = port.sender.tab.id;
      this.consolePorts[id] = port;
    }
  }
}