summaryrefslogtreecommitdiffstats
path: root/devtools/client/framework/toolbox-host-manager.js
blob: 6c1a0e645d8abfa339c0e041402d53ac37e64e16 (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
/* 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 { LocalizationHelper } = require("resource://devtools/shared/l10n.js");
const L10N = new LocalizationHelper(
  "devtools/client/locales/toolbox.properties"
);
const DevToolsUtils = require("resource://devtools/shared/DevToolsUtils.js");
const { DOMHelpers } = require("resource://devtools/shared/dom-helpers.js");

// The min-width of toolbox and browser toolbox.
const WIDTH_CHEVRON_AND_MEATBALL = 50;
const WIDTH_CHEVRON_AND_MEATBALL_AND_CLOSE = 74;
const ZOOM_VALUE_PREF = "devtools.toolbox.zoomValue";

loader.lazyRequireGetter(
  this,
  "Toolbox",
  "resource://devtools/client/framework/toolbox.js",
  true
);
loader.lazyRequireGetter(
  this,
  "Hosts",
  "resource://devtools/client/framework/toolbox-hosts.js",
  true
);

/**
 * Implement a wrapper on the chrome side to setup a Toolbox within Firefox UI.
 *
 * This component handles iframe creation within Firefox, in which we are loading
 * the toolbox document. Then both the chrome and the toolbox document communicate
 * via "message" events.
 *
 * Messages sent by the toolbox to the chrome:
 * - switch-host:
 *   Order to display the toolbox in another host (side, bottom, window, or the
 *   previously used one)
 * - raise-host:
 *   Focus the tools
 * - set-host-title:
 *   When using the window host, update the window title
 *
 * Messages sent by the chrome to the toolbox:
 * - switched-host:
 *   The `switch-host` command sent by the toolbox is done
 */

const LAST_HOST = "devtools.toolbox.host";
const PREVIOUS_HOST = "devtools.toolbox.previousHost";
let ID_COUNTER = 1;

function ToolboxHostManager(commands, hostType, hostOptions) {
  this.commands = commands;

  // When debugging a local tab, we keep a reference of the current tab into which the toolbox is displayed.
  // This will only change from the descriptor's localTab when we start debugging popups (i.e. window.open).
  this.currentTab = this.commands.descriptorFront.localTab;

  // Keep the previously instantiated Host for all tabs where we displayed the Toolbox.
  // This will only be useful when we start debugging popups (i.e. window.open).
  // This is used to re-use the previous host instance when we re-select the original tab
  // we were debugging before the popup opened.
  this.hostPerTab = new Map();

  this.frameId = ID_COUNTER++;

  if (!hostType) {
    hostType = Services.prefs.getCharPref(LAST_HOST);
    if (!Hosts[hostType]) {
      // If the preference value is unexpected, restore to the default value.
      Services.prefs.clearUserPref(LAST_HOST);
      hostType = Services.prefs.getCharPref(LAST_HOST);
    }
  }
  this.eventController = new AbortController();
  this.host = this.createHost(hostType, hostOptions);
  this.hostType = hostType;
  this.setMinWidthWithZoom = this.setMinWidthWithZoom.bind(this);
  this._onMessage = this._onMessage.bind(this);
  Services.prefs.addObserver(ZOOM_VALUE_PREF, this.setMinWidthWithZoom);
}

ToolboxHostManager.prototype = {
  async create(toolId) {
    await this.host.create();
    if (this.currentTab) {
      this.hostPerTab.set(this.currentTab, this.host);
    }

    this.host.frame.setAttribute("aria-label", L10N.getStr("toolbox.label"));
    this.host.frame.ownerDocument.defaultView.addEventListener(
      "message",
      this._onMessage,
      { signal: this.eventController.signal }
    );

    const toolbox = new Toolbox(
      this.commands,
      toolId,
      this.host.type,
      this.host.frame.contentWindow,
      this.frameId
    );
    toolbox.once("destroyed", this._onToolboxDestroyed.bind(this));

    // Prevent reloading the toolbox when loading the tools in a tab
    // (e.g. from about:debugging)
    const location = this.host.frame.contentWindow.location;
    if (!location.href.startsWith("about:devtools-toolbox")) {
      this.host.frame.setAttribute("src", "about:devtools-toolbox");
    }

    this.setMinWidthWithZoom();
    return toolbox;
  },

  setMinWidthWithZoom() {
    const zoomValue = parseFloat(Services.prefs.getCharPref(ZOOM_VALUE_PREF));

    if (isNaN(zoomValue)) {
      return;
    }

    if (
      this.hostType === Toolbox.HostType.LEFT ||
      this.hostType === Toolbox.HostType.RIGHT
    ) {
      this.host.frame.style.minWidth =
        WIDTH_CHEVRON_AND_MEATBALL_AND_CLOSE * zoomValue + "px";
    } else if (
      this.hostType === Toolbox.HostType.WINDOW ||
      this.hostType === Toolbox.HostType.PAGE ||
      this.hostType === Toolbox.HostType.BROWSERTOOLBOX
    ) {
      this.host.frame.style.minWidth =
        WIDTH_CHEVRON_AND_MEATBALL * zoomValue + "px";
    }
  },

  _onToolboxDestroyed() {
    // Delay self-destruction to let the debugger complete async destruction.
    // Otherwise it throws when running browser_dbg-breakpoints-in-evaled-sources.js
    // because the promise middleware delay each promise action using setTimeout...
    DevToolsUtils.executeSoon(() => {
      this.destroy();
    });
  },

  _onMessage(event) {
    if (!event.data) {
      return;
    }
    const msg = event.data;
    // Toolbox document is still chrome and disallow identifying message
    // origin via event.source as it is null. So use a custom id.
    if (msg.frameId != this.frameId) {
      return;
    }
    switch (msg.name) {
      case "switch-host":
        this.switchHost(msg.hostType);
        break;
      case "switch-host-to-tab":
        this.switchHostToTab(msg.tabBrowsingContextID);
        break;
      case "raise-host":
        this.host.raise();
        this.postMessage({
          name: "host-raised",
        });
        break;
      case "set-host-title":
        this.host.setTitle(msg.title);
        break;
    }
  },

  postMessage(data) {
    const window = this.host.frame.contentWindow;
    window.postMessage(data, "*");
  },

  destroy() {
    Services.prefs.removeObserver(ZOOM_VALUE_PREF, this.setMinWidthWithZoom);
    this.eventController.abort();
    this.eventController = null;
    this.destroyHost();
    // When we are debugging popup, we created host for each popup opened
    // in some other tabs. Ensure destroying them here.
    for (const host of this.hostPerTab.values()) {
      host.destroy();
    }
    this.hostPerTab.clear();
    this.host = null;
    this.hostType = null;
    this.commands = null;
  },

  /**
   * Create a host object based on the given host type.
   *
   * Warning: bottom and sidebar hosts require that the toolbox target provides
   * a reference to the attached tab. Not all Targets have a tab property -
   * make sure you correctly mix and match hosts and targets.
   *
   * @param {string} hostType
   *        The host type of the new host object
   *
   * @return {Host} host
   *        The created host object
   */
  createHost(hostType, options) {
    if (!Hosts[hostType]) {
      throw new Error("Unknown hostType: " + hostType);
    }
    const newHost = new Hosts[hostType](this.currentTab, options);
    return newHost;
  },

  /**
   * Migrate the toolbox to a new host, while keeping it fully functional.
   * The toolbox's iframe will be moved as-is to the new host.
   *
   * @param {String} hostType
   *        The new type of host to spawn
   * @param {Boolean} destroyPreviousHost
   *        Defaults to true. If false is passed, we will avoid destroying
   *        the previous host. This is helpful for popup debugging,
   *        where we migrate the toolbox between two tabs. In this scenario
   *        we are reusing previously instantiated hosts. This is especially
   *        useful when we close the current tab and have to have an
   *        already instantiated host to migrate to. If we don't have one,
   *        the toolbox iframe will already be destroyed before we have a chance
   *        to migrate it.
   */
  async switchHost(hostType, destroyPreviousHost = true) {
    if (hostType == "previous") {
      // Switch to the last used host for the toolbox UI.
      // This is determined by the devtools.toolbox.previousHost pref.
      hostType = Services.prefs.getCharPref(PREVIOUS_HOST);

      // Handle the case where the previous host happens to match the current
      // host. If so, switch to bottom if it's not already used, and right side if not.
      if (hostType === this.hostType) {
        if (hostType === Toolbox.HostType.BOTTOM) {
          hostType = Toolbox.HostType.RIGHT;
        } else {
          hostType = Toolbox.HostType.BOTTOM;
        }
      }
    }
    const iframe = this.host.frame;
    const newHost = this.createHost(hostType);
    const newIframe = await newHost.create();

    // Load a blank document in the host frame. The new iframe must have a valid
    // document before using swapFrameLoaders().
    await new Promise(resolve => {
      newIframe.setAttribute("src", "about:blank");
      DOMHelpers.onceDOMReady(newIframe.contentWindow, resolve);
    });

    // change toolbox document's parent to the new host
    newIframe.swapFrameLoaders(iframe);
    if (destroyPreviousHost) {
      this.destroyHost();
    }

    if (
      this.hostType !== Toolbox.HostType.BROWSERTOOLBOX &&
      this.hostType !== Toolbox.HostType.PAGE
    ) {
      Services.prefs.setCharPref(PREVIOUS_HOST, this.hostType);
    }

    this.host = newHost;
    if (this.currentTab) {
      this.hostPerTab.set(this.currentTab, newHost);
    }
    this.hostType = hostType;
    this.host.setTitle(this.host.frame.contentWindow.document.title);
    this.host.frame.ownerDocument.defaultView.addEventListener(
      "message",
      this._onMessage,
      { signal: this.eventController.signal }
    );

    this.setMinWidthWithZoom();

    if (
      hostType !== Toolbox.HostType.BROWSERTOOLBOX &&
      hostType !== Toolbox.HostType.PAGE
    ) {
      Services.prefs.setCharPref(LAST_HOST, hostType);
    }

    // Tell the toolbox the host changed
    this.postMessage({
      name: "switched-host",
      hostType,
    });
  },

  /**
   * When we are debugging popup, we are moving around the toolbox between original tab
   * and popup tabs. This method will only move the host to a new tab, while
   * keeping the same host type.
   *
   * @param {String} tabBrowsingContextID
   *        The ID of the browsing context of the tab we want to move to.
   */
  async switchHostToTab(tabBrowsingContextID) {
    const { gBrowser } = this.host.frame.ownerDocument.defaultView;

    const previousTab = this.currentTab;
    const newTab = gBrowser.tabs.find(
      tab => tab.linkedBrowser.browsingContext.id == tabBrowsingContextID
    );
    // Note that newTab will be undefined when the popup opens in a new top level window.
    if (newTab && newTab != previousTab) {
      this.currentTab = newTab;
      const newHost = this.hostPerTab.get(this.currentTab);
      if (newHost) {
        newHost.frame.swapFrameLoaders(this.host.frame);
        this.host = newHost;
      } else {
        await this.switchHost(this.hostType, false);
      }
      previousTab.addEventListener(
        "TabSelect",
        event => {
          this.switchHostToTab(event.target.linkedBrowser.browsingContext.id);
        },
        { once: true, signal: this.eventController.signal }
      );
    }

    this.postMessage({
      name: "switched-host-to-tab",
      browsingContextID: tabBrowsingContextID,
    });
  },

  /**
   * Destroy the current host, and remove event listeners from its frame.
   *
   * @return {promise} to be resolved when the host is destroyed.
   */
  destroyHost() {
    return this.host.destroy();
  },
};
exports.ToolboxHostManager = ToolboxHostManager;