summaryrefslogtreecommitdiffstats
path: root/devtools/client/aboutdebugging/src/actions/debug-targets.js
blob: fa768c3d22e5d00c3025cf46da386eca2da22b0d (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/* 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 { AddonManager } = ChromeUtils.importESModule(
  "resource://gre/modules/AddonManager.sys.mjs",
  // AddonManager is a singleton, never create two instances of it.
  { global: "shared" }
);
const {
  remoteClientManager,
} = require("resource://devtools/client/shared/remote-debugging/remote-client-manager.js");

const {
  l10n,
} = require("resource://devtools/client/aboutdebugging/src/modules/l10n.js");

const {
  isSupportedDebugTargetPane,
} = require("resource://devtools/client/aboutdebugging/src/modules/debug-target-support.js");

const {
  openTemporaryExtension,
} = require("resource://devtools/client/aboutdebugging/src/modules/extensions-helper.js");

const {
  getCurrentClient,
  getCurrentRuntime,
} = require("resource://devtools/client/aboutdebugging/src/modules/runtimes-state-helper.js");

const {
  gDevTools,
} = require("resource://devtools/client/framework/devtools.js");

const {
  DEBUG_TARGETS,
  DEBUG_TARGET_PANE,
  REQUEST_EXTENSIONS_FAILURE,
  REQUEST_EXTENSIONS_START,
  REQUEST_EXTENSIONS_SUCCESS,
  REQUEST_PROCESSES_FAILURE,
  REQUEST_PROCESSES_START,
  REQUEST_PROCESSES_SUCCESS,
  REQUEST_TABS_FAILURE,
  REQUEST_TABS_START,
  REQUEST_TABS_SUCCESS,
  REQUEST_WORKERS_FAILURE,
  REQUEST_WORKERS_START,
  REQUEST_WORKERS_SUCCESS,
  TEMPORARY_EXTENSION_INSTALL_FAILURE,
  TEMPORARY_EXTENSION_INSTALL_START,
  TEMPORARY_EXTENSION_INSTALL_SUCCESS,
  TEMPORARY_EXTENSION_RELOAD_FAILURE,
  TEMPORARY_EXTENSION_RELOAD_START,
  TEMPORARY_EXTENSION_RELOAD_SUCCESS,
  TERMINATE_EXTENSION_BGSCRIPT_FAILURE,
  TERMINATE_EXTENSION_BGSCRIPT_SUCCESS,
  TERMINATE_EXTENSION_BGSCRIPT_START,
  RUNTIMES,
} = require("resource://devtools/client/aboutdebugging/src/constants.js");

const Actions = require("resource://devtools/client/aboutdebugging/src/actions/index.js");

function getTabForUrl(url) {
  for (const navigator of Services.wm.getEnumerator("navigator:browser")) {
    for (const browser of navigator.gBrowser.browsers) {
      if (
        browser.contentWindow &&
        browser.contentWindow.location.href === url
      ) {
        return navigator.gBrowser.getTabForBrowser(browser);
      }
    }
  }

  return null;
}

function inspectDebugTarget(type, id) {
  return async ({ dispatch, getState }) => {
    const runtime = getCurrentRuntime(getState().runtimes);

    if (
      type == DEBUG_TARGETS.EXTENSION &&
      runtime.id === RUNTIMES.THIS_FIREFOX
    ) {
      // Bug 1780912: To avoid UX issues when debugging local web extensions,
      // we are opening the toolbox in an independant window.
      // Whereas all others are opened in new tabs.
      gDevTools.showToolboxForWebExtension(id);
    } else {
      const urlParams = {
        type,
      };
      // Main process may not provide any ID.
      if (id) {
        urlParams.id = id;
      }

      if (runtime.id !== RUNTIMES.THIS_FIREFOX) {
        urlParams.remoteId = remoteClientManager.getRemoteId(
          runtime.id,
          runtime.type
        );
      }

      const url = `about:devtools-toolbox?${new window.URLSearchParams(
        urlParams
      )}`;

      const existingTab = getTabForUrl(url);
      if (existingTab) {
        const navigator = existingTab.ownerGlobal;
        navigator.gBrowser.selectedTab = existingTab;
        navigator.focus();
      } else {
        window.open(url);
      }
    }

    dispatch(
      Actions.recordTelemetryEvent("inspect", {
        target_type: type.toUpperCase(),
        runtime_type: runtime.type,
      })
    );
  };
}

function installTemporaryExtension() {
  const message = l10n.getString(
    "about-debugging-tmp-extension-install-message"
  );
  return async ({ dispatch }) => {
    dispatch({ type: TEMPORARY_EXTENSION_INSTALL_START });
    const file = await openTemporaryExtension(window, message);
    try {
      await AddonManager.installTemporaryAddon(file);
      dispatch({ type: TEMPORARY_EXTENSION_INSTALL_SUCCESS });
    } catch (e) {
      dispatch({ type: TEMPORARY_EXTENSION_INSTALL_FAILURE, error: e });
    }
  };
}

function pushServiceWorker(id, registrationFront) {
  return async () => {
    try {
      // The push button is only available if canDebugServiceWorkers is true.
      // With this configuration, `push` should always be called on the
      // registration front, and not on the (service) WorkerTargetActor.
      await registrationFront.push();
    } catch (e) {
      console.error(e);
    }
  };
}

function reloadTemporaryExtension(id) {
  return async ({ dispatch, getState }) => {
    dispatch({ type: TEMPORARY_EXTENSION_RELOAD_START, id });
    const clientWrapper = getCurrentClient(getState().runtimes);

    try {
      const addonTargetFront = await clientWrapper.getAddon({ id });
      await addonTargetFront.reload();
      dispatch({ type: TEMPORARY_EXTENSION_RELOAD_SUCCESS, id });
    } catch (e) {
      const error = typeof e === "string" ? new Error(e) : e;
      dispatch({ type: TEMPORARY_EXTENSION_RELOAD_FAILURE, id, error });
    }
  };
}

function removeTemporaryExtension(id) {
  return async ({ getState }) => {
    const clientWrapper = getCurrentClient(getState().runtimes);

    try {
      await clientWrapper.uninstallAddon({ id });
    } catch (e) {
      console.error(e);
    }
  };
}

function terminateExtensionBackgroundScript(id) {
  return async ({ dispatch, getState }) => {
    dispatch({ type: TERMINATE_EXTENSION_BGSCRIPT_START, id });
    const clientWrapper = getCurrentClient(getState().runtimes);

    try {
      const addonTargetFront = await clientWrapper.getAddon({ id });
      await addonTargetFront.terminateBackgroundScript();
      dispatch({ type: TERMINATE_EXTENSION_BGSCRIPT_SUCCESS, id });
    } catch (e) {
      const error = typeof e === "string" ? new Error(e) : e;
      dispatch({ type: TERMINATE_EXTENSION_BGSCRIPT_FAILURE, id, error });
    }
  };
}

function requestTabs() {
  return async ({ dispatch, getState }) => {
    dispatch({ type: REQUEST_TABS_START });

    const runtime = getCurrentRuntime(getState().runtimes);
    const clientWrapper = getCurrentClient(getState().runtimes);

    try {
      const isSupported = isSupportedDebugTargetPane(
        runtime.runtimeDetails.info.type,
        DEBUG_TARGET_PANE.TAB
      );
      const tabs = isSupported ? await clientWrapper.listTabs() : [];

      // Fetch the favicon for all tabs.
      await Promise.all(
        tabs.map(descriptorFront => descriptorFront.retrieveFavicon())
      );

      dispatch({ type: REQUEST_TABS_SUCCESS, tabs });
    } catch (e) {
      dispatch({ type: REQUEST_TABS_FAILURE, error: e });
    }
  };
}

function requestExtensions() {
  return async ({ dispatch, getState }) => {
    dispatch({ type: REQUEST_EXTENSIONS_START });

    const runtime = getCurrentRuntime(getState().runtimes);
    const clientWrapper = getCurrentClient(getState().runtimes);

    try {
      const isIconDataURLRequired = runtime.type !== RUNTIMES.THIS_FIREFOX;
      const addons = await clientWrapper.listAddons({
        iconDataURL: isIconDataURLRequired,
      });

      const showHiddenAddons = getState().ui.showHiddenAddons;

      // Filter out non-debuggable addons as well as hidden ones, unless the dedicated
      // preference is set to true.
      const extensions = addons.filter(
        a => a.debuggable && (!a.hidden || showHiddenAddons)
      );

      const installedExtensions = extensions.filter(
        e => !e.temporarilyInstalled
      );
      const temporaryExtensions = extensions.filter(
        e => e.temporarilyInstalled
      );

      dispatch({
        type: REQUEST_EXTENSIONS_SUCCESS,
        installedExtensions: sortTargetsByName(installedExtensions),
        temporaryExtensions: sortTargetsByName(temporaryExtensions),
      });
    } catch (e) {
      dispatch({ type: REQUEST_EXTENSIONS_FAILURE, error: e });
    }
  };
}

function requestProcesses() {
  return async ({ dispatch, getState }) => {
    dispatch({ type: REQUEST_PROCESSES_START });

    const clientWrapper = getCurrentClient(getState().runtimes);

    try {
      const mainProcessDescriptorFront = await clientWrapper.getMainProcess();
      dispatch({
        type: REQUEST_PROCESSES_SUCCESS,
        mainProcess: {
          id: 0,
          processFront: mainProcessDescriptorFront,
        },
      });
    } catch (e) {
      dispatch({ type: REQUEST_PROCESSES_FAILURE, error: e });
    }
  };
}

function requestWorkers() {
  return async ({ dispatch, getState }) => {
    dispatch({ type: REQUEST_WORKERS_START });

    const clientWrapper = getCurrentClient(getState().runtimes);

    try {
      const { otherWorkers, serviceWorkers, sharedWorkers } =
        await clientWrapper.listWorkers();

      for (const serviceWorker of serviceWorkers) {
        const { registrationFront } = serviceWorker;
        if (!registrationFront) {
          continue;
        }

        const subscription = await registrationFront.getPushSubscription();
        serviceWorker.subscription = subscription;
      }

      dispatch({
        type: REQUEST_WORKERS_SUCCESS,
        otherWorkers: sortTargetsByName(otherWorkers),
        serviceWorkers: sortTargetsByName(serviceWorkers),
        sharedWorkers: sortTargetsByName(sharedWorkers),
      });
    } catch (e) {
      dispatch({ type: REQUEST_WORKERS_FAILURE, error: e });
    }
  };
}

function startServiceWorker(registrationFront) {
  return async () => {
    try {
      await registrationFront.start();
    } catch (e) {
      console.error(e);
    }
  };
}

function sortTargetsByName(targets) {
  return targets.sort((target1, target2) => {
    // Fallback to empty string in case some targets don't have a valid name.
    const name1 = target1.name || "";
    const name2 = target2.name || "";
    return name1.localeCompare(name2);
  });
}

function unregisterServiceWorker(registrationFront) {
  return async () => {
    try {
      await registrationFront.unregister();
    } catch (e) {
      console.error(e);
    }
  };
}

module.exports = {
  inspectDebugTarget,
  installTemporaryExtension,
  pushServiceWorker,
  reloadTemporaryExtension,
  removeTemporaryExtension,
  requestTabs,
  requestExtensions,
  requestProcesses,
  requestWorkers,
  startServiceWorker,
  terminateExtensionBackgroundScript,
  unregisterServiceWorker,
};