summaryrefslogtreecommitdiffstats
path: root/browser/components/asrouter/content-src/asrouter-utils.mjs
blob: 989d864e71acf48b322bd04cfa140a6ee56c022f (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

// eslint-disable-next-line mozilla/reject-import-system-module-from-non-system
import { MESSAGE_TYPE_HASH as msg } from "../modules/ActorConstants.sys.mjs";
// eslint-disable-next-line mozilla/reject-import-system-module-from-non-system
import { actionCreators as ac } from "../../newtab/common/Actions.sys.mjs";

export const ASRouterUtils = {
  addListener(listener) {
    if (globalThis.ASRouterAddParentListener) {
      globalThis.ASRouterAddParentListener(listener);
    }
  },
  removeListener(listener) {
    if (globalThis.ASRouterRemoveParentListener) {
      globalThis.ASRouterRemoveParentListener(listener);
    }
  },
  sendMessage(action) {
    if (globalThis.ASRouterMessage) {
      return globalThis.ASRouterMessage(action);
    }
    throw new Error(`Unexpected call:\n${JSON.stringify(action, null, 3)}`);
  },
  blockById(id, options) {
    return ASRouterUtils.sendMessage({
      type: msg.BLOCK_MESSAGE_BY_ID,
      data: { id, ...options },
    });
  },
  modifyMessageJson(content) {
    return ASRouterUtils.sendMessage({
      type: msg.MODIFY_MESSAGE_JSON,
      data: { content },
    });
  },
  executeAction(button_action) {
    return ASRouterUtils.sendMessage({
      type: msg.USER_ACTION,
      data: button_action,
    });
  },
  unblockById(id) {
    return ASRouterUtils.sendMessage({
      type: msg.UNBLOCK_MESSAGE_BY_ID,
      data: { id },
    });
  },
  blockBundle(bundle) {
    return ASRouterUtils.sendMessage({
      type: msg.BLOCK_BUNDLE,
      data: { bundle },
    });
  },
  unblockBundle(bundle) {
    return ASRouterUtils.sendMessage({
      type: msg.UNBLOCK_BUNDLE,
      data: { bundle },
    });
  },
  overrideMessage(id) {
    return ASRouterUtils.sendMessage({
      type: msg.OVERRIDE_MESSAGE,
      data: { id },
    });
  },
  editState(key, value) {
    return ASRouterUtils.sendMessage({
      type: msg.EDIT_STATE,
      data: { [key]: value },
    });
  },
  sendTelemetry(ping) {
    return ASRouterUtils.sendMessage(ac.ASRouterUserEvent(ping));
  },
  getPreviewEndpoint() {
    return null;
  },
};