summaryrefslogtreecommitdiffstats
path: root/devtools/client/aboutdebugging/src/modules/client-wrapper.js
blob: 6d537b60a4288e464c1c3f83d365b18920c61788 (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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* 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/. */

"use strict";

const {
  checkVersionCompatibility,
} = require("resource://devtools/client/shared/remote-debugging/version-checker.js");

const {
  RUNTIME_PREFERENCE,
} = require("resource://devtools/client/aboutdebugging/src/constants.js");
const {
  WorkersListener,
} = require("resource://devtools/client/shared/workers-listener.js");
const RootResourceCommand = require("resource://devtools/shared/commands/root-resource/root-resource-command.js");

const PREF_TYPES = {
  BOOL: "BOOL",
};

// Map of preference to preference type.
const PREF_TO_TYPE = {
  [RUNTIME_PREFERENCE.CHROME_DEBUG_ENABLED]: PREF_TYPES.BOOL,
  [RUNTIME_PREFERENCE.CONNECTION_PROMPT]: PREF_TYPES.BOOL,
  [RUNTIME_PREFERENCE.PERMANENT_PRIVATE_BROWSING]: PREF_TYPES.BOOL,
  [RUNTIME_PREFERENCE.REMOTE_DEBUG_ENABLED]: PREF_TYPES.BOOL,
  [RUNTIME_PREFERENCE.SERVICE_WORKERS_ENABLED]: PREF_TYPES.BOOL,
};

// Some events are fired by mainRoot rather than client.
const MAIN_ROOT_EVENTS = ["addonListChanged", "tabListChanged"];

/**
 * The ClientWrapper class is used to isolate aboutdebugging from the DevTools client API
 * The modules of about:debugging should never call DevTools client APIs directly.
 */
class ClientWrapper {
  constructor(client) {
    this.client = client;
    this.workersListener = new WorkersListener(client.mainRoot);
  }

  once(evt, listener) {
    if (MAIN_ROOT_EVENTS.includes(evt)) {
      this.client.mainRoot.once(evt, listener);
    } else {
      this.client.once(evt, listener);
    }
  }

  on(evt, listener) {
    if (evt === "workersUpdated") {
      this.workersListener.addListener(listener);
    } else if (MAIN_ROOT_EVENTS.includes(evt)) {
      this.client.mainRoot.on(evt, listener);
    } else {
      this.client.on(evt, listener);
    }
  }

  off(evt, listener) {
    if (evt === "workersUpdated") {
      this.workersListener.removeListener(listener);
    } else if (MAIN_ROOT_EVENTS.includes(evt)) {
      this.client.mainRoot.off(evt, listener);
    } else {
      this.client.off(evt, listener);
    }
  }

  async getFront(typeName) {
    return this.client.mainRoot.getFront(typeName);
  }

  async getDeviceDescription() {
    const deviceFront = await this.getFront("device");
    const description = await deviceFront.getDescription();

    // Only expose a specific set of properties.
    return {
      canDebugServiceWorkers: description.canDebugServiceWorkers,
      channel: description.channel,
      deviceName: description.deviceName,
      name: description.brandName,
      os: description.os,
      version: description.version,
    };
  }

  createRootResourceCommand() {
    return new RootResourceCommand({ rootFront: this.client.mainRoot });
  }

  async checkVersionCompatibility() {
    return checkVersionCompatibility(this.client);
  }

  async setPreference(prefName, value) {
    const prefType = PREF_TO_TYPE[prefName];
    const preferenceFront = await this.client.mainRoot.getFront("preference");
    switch (prefType) {
      case PREF_TYPES.BOOL:
        return preferenceFront.setBoolPref(prefName, value);
      default:
        throw new Error("Unsupported preference" + prefName);
    }
  }

  async getPreference(prefName, defaultValue) {
    if (typeof defaultValue === "undefined") {
      throw new Error(
        "Default value is mandatory for getPreference, the actor will " +
          "throw if the preference is not set on the target runtime"
      );
    }

    const prefType = PREF_TO_TYPE[prefName];
    const preferenceFront = await this.client.mainRoot.getFront("preference");
    switch (prefType) {
      case PREF_TYPES.BOOL:
        // TODO: Add server-side trait and methods to pass a default value to getBoolPref.
        // See Bug 1522588.
        let prefValue;
        try {
          prefValue = await preferenceFront.getBoolPref(prefName);
        } catch (e) {
          prefValue = defaultValue;
        }
        return prefValue;
      default:
        throw new Error("Unsupported preference:" + prefName);
    }
  }

  async listTabs() {
    return this.client.mainRoot.listTabs();
  }

  async listAddons(options) {
    return this.client.mainRoot.listAddons(options);
  }

  async getAddon({ id }) {
    return this.client.mainRoot.getAddon({ id });
  }

  async uninstallAddon({ id }) {
    const addonsFront = await this.getFront("addons");
    return addonsFront.uninstallAddon(id);
  }

  async getMainProcess() {
    return this.client.mainRoot.getMainProcess();
  }

  async getServiceWorkerFront({ id }) {
    return this.client.mainRoot.getWorker(id);
  }

  async listWorkers() {
    const { other, service, shared } =
      await this.client.mainRoot.listAllWorkers();

    return {
      otherWorkers: other,
      serviceWorkers: service,
      sharedWorkers: shared,
    };
  }

  async close() {
    return this.client.close();
  }

  isClosed() {
    return this.client._transportClosed;
  }

  // This method will be mocked to return a dummy URL during mochitests
  getPerformancePanelUrl() {
    return "chrome://devtools/content/performance-new/panel/index.xhtml";
  }

  /**
   * @param {Window} win - The window of the dialog window.
   * @param {Function} openAboutProfiling
   */
  async loadPerformanceProfiler(win, openAboutProfiling) {
    const perfFront = await this.getFront("perf");
    const { traits } = this.client;
    await win.gInit(perfFront, traits, "devtools-remote", openAboutProfiling);
  }

  /**
   * @param {Window} win - The window of the dialog window.
   * @param {Function} openRemoteDevTools
   */
  async loadAboutProfiling(win, openRemoteDevTools) {
    const perfFront = await this.getFront("perf");
    const isSupportedPlatform = await perfFront.isSupportedPlatform();
    const supportedFeatures = await perfFront.getSupportedFeatures();
    await win.gInit(
      "aboutprofiling-remote",
      isSupportedPlatform,
      supportedFeatures,
      openRemoteDevTools
    );
  }

  get traits() {
    return { ...this.client.mainRoot.traits };
  }
}

exports.ClientWrapper = ClientWrapper;