summaryrefslogtreecommitdiffstats
path: root/devtools/server/actors/resources/utils/parent-process-storage.js
blob: 760e6e4d3845ff42a60a65fd7dfe840bc4026a89 (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/* 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 EventEmitter = require("resource://devtools/shared/event-emitter.js");
const { isWindowGlobalPartOfContext } = ChromeUtils.importESModule(
  "resource://devtools/server/actors/watcher/browsing-context-helpers.sys.mjs",
  { global: "contextual" }
);

// ms of delay to throttle updates
const BATCH_DELAY = 200;

// Filters "stores-update" response to only include events for
// the storage type we desire
function getFilteredStorageEvents(updates, storageType) {
  const filteredUpdate = Object.create(null);

  // updateType will be "added", "changed", or "deleted"
  for (const updateType in updates) {
    if (updates[updateType][storageType]) {
      if (!filteredUpdate[updateType]) {
        filteredUpdate[updateType] = {};
      }
      filteredUpdate[updateType][storageType] =
        updates[updateType][storageType];
    }
  }

  return Object.keys(filteredUpdate).length ? filteredUpdate : null;
}

class ParentProcessStorage {
  constructor(ActorConstructor, storageKey, storageType) {
    this.ActorConstructor = ActorConstructor;
    this.storageKey = storageKey;
    this.storageType = storageType;

    this.onStoresUpdate = this.onStoresUpdate.bind(this);
    this.onStoresCleared = this.onStoresCleared.bind(this);

    this.observe = this.observe.bind(this);
    // Notifications that help us keep track of newly added windows and windows
    // that got removed
    Services.obs.addObserver(this, "window-global-created");
    Services.obs.addObserver(this, "window-global-destroyed");

    // bfcacheInParent is only enabled when fission is enabled
    // and when Session History In Parent is enabled. (all three modes should now enabled all together)
    loader.lazyGetter(
      this,
      "isBfcacheInParentEnabled",
      () =>
        Services.appinfo.sessionHistoryInParent &&
        Services.prefs.getBoolPref("fission.bfcacheInParent", false)
    );
  }

  async watch(watcherActor, { onAvailable }) {
    this.watcherActor = watcherActor;
    this.onAvailable = onAvailable;

    // When doing a bfcache navigation with Fission disabled or with Fission + bfCacheInParent enabled,
    // we're not getting a the window-global-created events.
    // In such case, the watcher emits specific events that we can use instead.
    this._offPageShow = watcherActor.on(
      "bf-cache-navigation-pageshow",
      ({ windowGlobal }) => this._onNewWindowGlobal(windowGlobal, true)
    );

    if (watcherActor.sessionContext.type == "browser-element") {
      const { browsingContext, innerWindowID: innerWindowId } =
        watcherActor.browserElement;
      await this._spawnActor(browsingContext.id, innerWindowId);
    } else if (watcherActor.sessionContext.type == "webextension") {
      const { addonBrowsingContextID, addonInnerWindowId } =
        watcherActor.sessionContext;
      await this._spawnActor(addonBrowsingContextID, addonInnerWindowId);
    } else if (watcherActor.sessionContext.type == "all") {
      const parentProcessTargetActor =
        this.watcherActor.getTargetActorInParentProcess();
      const { browsingContextID, innerWindowId } =
        parentProcessTargetActor.form();
      await this._spawnActor(browsingContextID, innerWindowId);
    } else {
      throw new Error(
        "Unsupported session context type=" + watcherActor.sessionContext.type
      );
    }
  }

  onStoresUpdate(response) {
    response = getFilteredStorageEvents(response, this.storageKey);
    if (!response) {
      return;
    }
    this.actor.emit("single-store-update", {
      changed: response.changed,
      added: response.added,
      deleted: response.deleted,
    });
  }

  onStoresCleared(response) {
    const cleared = response[this.storageKey];

    if (!cleared) {
      return;
    }

    this.actor.emit("single-store-cleared", {
      clearedHostsOrPaths: cleared,
    });
  }

  destroy() {
    // Remove observers
    Services.obs.removeObserver(this, "window-global-created");
    Services.obs.removeObserver(this, "window-global-destroyed");
    this._offPageShow();
    this._cleanActor();
  }

  async _spawnActor(browsingContextID, innerWindowId) {
    const storageActor = new StorageActorMock(this.watcherActor);
    this.storageActor = storageActor;
    this.actor = new this.ActorConstructor(storageActor);

    // Some storage types require to prelist their stores
    try {
      await this.actor.populateStoresForHosts();
    } catch (e) {
      // It can happen that the actor gets destroyed while populateStoresForHosts is being
      // executed.
      if (this.actor) {
        throw e;
      }
    }

    // If the actor was destroyed, we don't need to go further.
    if (!this.actor) {
      return;
    }

    // We have to manage the actor manually, because ResourceCommand doesn't
    // use the protocol.js specification.
    // resource-available-form is typed as "json"
    // So that we have to manually handle stuff that would normally be
    // automagically done by procotol.js
    // 1) Manage the actor in order to have an actorID on it
    this.watcherActor.manage(this.actor);
    // 2) Convert to JSON "form"
    const storage = this.actor.form();

    // All resources should have a resourceType, resourceId and resourceKey
    // attributes, so available/updated/destroyed callbacks work properly.
    storage.resourceType = this.storageType;
    storage.resourceId = `${this.storageType}-${innerWindowId}`;
    storage.resourceKey = this.storageKey;
    // NOTE: the resource command needs this attribute
    storage.browsingContextID = browsingContextID;

    this.onAvailable([storage]);

    // Maps global events from `storageActor` shared for all storage-types,
    // down to storage-type's specific actor `storage`.
    storageActor.on("stores-update", this.onStoresUpdate);

    // When a store gets cleared
    storageActor.on("stores-cleared", this.onStoresCleared);
  }

  _cleanActor() {
    this.actor?.destroy();
    this.actor = null;
    if (this.storageActor) {
      this.storageActor.off("stores-update", this.onStoresUpdate);
      this.storageActor.off("stores-cleared", this.onStoresCleared);
      this.storageActor.destroy();
      this.storageActor = null;
    }
  }

  /**
   * Event handler for any docshell update. This lets us figure out whenever
   * any new window is added, or an existing window is removed.
   */
  observe(subject, topic) {
    if (topic === "window-global-created") {
      this._onNewWindowGlobal(subject);
    }
  }

  /**
   * Handle WindowGlobal received via:
   * - <window-global-created> (to cover regular navigations, with brand new documents)
   * - <bf-cache-navigation-pageshow> (to cover history navications)
   *
   * @param {WindowGlobal} windowGlobal
   * @param {Boolean} isBfCacheNavigation
   */
  async _onNewWindowGlobal(windowGlobal, isBfCacheNavigation) {
    // Only process WindowGlobals which are related to the debugged scope.
    if (
      !isWindowGlobalPartOfContext(
        windowGlobal,
        this.watcherActor.sessionContext,
        { acceptNoWindowGlobal: true, acceptSameProcessIframes: true }
      )
    ) {
      return;
    }

    // Ignore about:blank
    if (windowGlobal.documentURI.displaySpec === "about:blank") {
      return;
    }

    // Only process top BrowsingContext (ignore same-process iframe ones)
    const isTopContext =
      windowGlobal.browsingContext.top == windowGlobal.browsingContext;
    if (!isTopContext) {
      return;
    }

    // We only want to spawn a new StorageActor if a new target is being created, i.e.
    // - target switching is enabled and we're notified about a new top-level window global,
    //   via window-global-created
    // - target switching is enabled OR bfCacheInParent is enabled, and a bfcache navigation
    //   is performed (See handling of "pageshow" event in DevToolsFrameChild)
    const isNewTargetBeingCreated =
      this.watcherActor.sessionContext.isServerTargetSwitchingEnabled ||
      (isBfCacheNavigation && this.isBfcacheInParentEnabled);

    if (!isNewTargetBeingCreated) {
      return;
    }

    // When server side target switching is enabled, we replace the StorageActor
    // with a new one.
    // On the frontend, the navigation will destroy the previous target, which
    // will destroy the previous storage front, so we must notify about a new one.

    // When we are target switching we keep the storage watcher, so we need
    // to send a new resource to the client.
    // However, we must ensure that we do this when the new target is
    // already available, so we check innerWindowId to do it.
    await new Promise(resolve => {
      const listener = targetActorForm => {
        if (targetActorForm.innerWindowId != windowGlobal.innerWindowId) {
          return;
        }
        this.watcherActor.off("target-available-form", listener);
        resolve();
      };
      this.watcherActor.on("target-available-form", listener);
    });

    this._cleanActor();
    this._spawnActor(
      windowGlobal.browsingContext.id,
      windowGlobal.innerWindowId
    );
  }
}

module.exports = ParentProcessStorage;

class StorageActorMock extends EventEmitter {
  constructor(watcherActor) {
    super();

    this.conn = watcherActor.conn;
    this.watcherActor = watcherActor;

    this.boundUpdate = {};

    // Notifications that help us keep track of newly added windows and windows
    // that got removed
    this.observe = this.observe.bind(this);
    Services.obs.addObserver(this, "window-global-created");
    Services.obs.addObserver(this, "window-global-destroyed");

    // When doing a bfcache navigation with Fission disabled or with Fission + bfCacheInParent enabled,
    // we're not getting a the window-global-created/window-global-destroyed events.
    // In such case, the watcher emits specific events that we can use as equivalent to
    // window-global-created/window-global-destroyed.
    // We only need to react to those events here if target switching is not enabled; when
    // it is enabled, ParentProcessStorage will spawn a whole new actor which will allow
    // the client to get the information it needs.
    if (!this.watcherActor.sessionContext.isServerTargetSwitchingEnabled) {
      this._offPageShow = watcherActor.on(
        "bf-cache-navigation-pageshow",
        ({ windowGlobal }) => {
          // if a new target is created in the content process as a result of the bfcache
          // navigation, we don't need to emit window-ready as a new StorageActorMock will
          // be created by ParentProcessStorage.
          // When server targets are disabled, this only happens when bfcache in parent is enabled.
          if (this.isBfcacheInParentEnabled) {
            return;
          }
          const windowMock = { location: windowGlobal.documentURI };
          this.emit("window-ready", windowMock);
        }
      );

      this._offPageHide = watcherActor.on(
        "bf-cache-navigation-pagehide",
        ({ windowGlobal }) => {
          const windowMock = { location: windowGlobal.documentURI };
          // The listener of this events usually check that there are no other windows
          // with the same host before notifying the client that it can remove it from
          // the UI. The windows are retrieved from the `windows` getter, and in this case
          // we still have a reference to the window we're navigating away from.
          // We pass a `dontCheckHost` parameter alongside the window-destroyed event to
          // always notify the client.
          this.emit("window-destroyed", windowMock, { dontCheckHost: true });
        }
      );
    }
  }

  destroy() {
    // clear update throttle timeout
    clearTimeout(this.batchTimer);
    this.batchTimer = null;
    // Remove observers
    Services.obs.removeObserver(this, "window-global-created");
    Services.obs.removeObserver(this, "window-global-destroyed");
    if (this._offPageShow) {
      this._offPageShow();
    }
    if (this._offPageHide) {
      this._offPageHide();
    }
  }

  get windows() {
    return (
      this.watcherActor
        .getAllBrowsingContexts({
          acceptSameProcessIframes: true,
        })
        .map(x => {
          const uri = x.currentWindowGlobal.documentURI;
          return { location: uri };
        })
        // NOTE: we are removing about:blank because we might get them for iframes
        // whose src attribute has not been set yet.
        .filter(x => x.location.displaySpec !== "about:blank")
    );
  }

  // NOTE: this uri argument is not a real window.Location, but the
  // `currentWindowGlobal.documentURI` object passed from `windows` getter.
  getHostName(uri) {
    switch (uri.scheme) {
      case "about":
      case "file":
      case "javascript":
      case "resource":
        return uri.displaySpec;
      case "moz-extension":
      case "http":
      case "https":
        return uri.prePath;
      default:
        // chrome: and data: do not support storage
        return null;
    }
  }

  getWindowFromHost(host) {
    const hostBrowsingContext = this.watcherActor
      .getAllBrowsingContexts({ acceptSameProcessIframes: true })
      .find(x => {
        const hostName = this.getHostName(x.currentWindowGlobal.documentURI);
        return hostName === host;
      });
    // In case of WebExtension or BrowserToolbox, we may pass privileged hosts
    // which don't relate to any particular window.
    // Like "indexeddb+++fx-devtools" or "chrome".
    // (callsites of this method are used to handle null returned values)
    if (!hostBrowsingContext) {
      return null;
    }

    const principal =
      hostBrowsingContext.currentWindowGlobal.documentStoragePrincipal;

    return { document: { effectiveStoragePrincipal: principal } };
  }

  get parentActor() {
    return {
      isRootActor: this.watcherActor.sessionContext.type == "all",
      addonId: this.watcherActor.sessionContext.addonId,
    };
  }

  /**
   * Event handler for any docshell update. This lets us figure out whenever
   * any new window is added, or an existing window is removed.
   */
  async observe(windowGlobal, topic) {
    // Only process WindowGlobals which are related to the debugged scope.
    if (
      !isWindowGlobalPartOfContext(
        windowGlobal,
        this.watcherActor.sessionContext,
        { acceptNoWindowGlobal: true, acceptSameProcessIframes: true }
      )
    ) {
      return;
    }

    // Ignore about:blank
    if (windowGlobal.documentURI.displaySpec === "about:blank") {
      return;
    }

    // Only notify about remote iframe windows when JSWindowActor based targets are enabled
    // We will create a new StorageActor for the top level tab documents when server side target
    // switching is enabled
    const isTopContext =
      windowGlobal.browsingContext.top == windowGlobal.browsingContext;
    if (
      isTopContext &&
      this.watcherActor.sessionContext.isServerTargetSwitchingEnabled
    ) {
      return;
    }

    // emit window-wready and window-destroyed events when needed
    const windowMock = { location: windowGlobal.documentURI };
    if (topic === "window-global-created") {
      this.emit("window-ready", windowMock);
    } else if (topic === "window-global-destroyed") {
      this.emit("window-destroyed", windowMock);
    }
  }

  /**
   * This method is called by the registered storage types so as to tell the
   * Storage Actor that there are some changes in the stores. Storage Actor then
   * notifies the client front about these changes at regular (BATCH_DELAY)
   * interval.
   *
   * @param {string} action
   *        The type of change. One of "added", "changed" or "deleted"
   * @param {string} storeType
   *        The storage actor in which this change has occurred.
   * @param {object} data
   *        The update object. This object is of the following format:
   *         - {
   *             <host1>: [<store_names1>, <store_name2>...],
   *             <host2>: [<store_names34>...],
   *           }
   *           Where host1, host2 are the host in which this change happened and
   *           [<store_namesX] is an array of the names of the changed store objects.
   *           Pass an empty array if the host itself was affected: either completely
   *           removed or cleared.
   */
  // eslint-disable-next-line complexity
  update(action, storeType, data) {
    if (action == "cleared") {
      this.emit("stores-cleared", { [storeType]: data });
      return null;
    }

    if (this.batchTimer) {
      clearTimeout(this.batchTimer);
    }
    if (!this.boundUpdate[action]) {
      this.boundUpdate[action] = {};
    }
    if (!this.boundUpdate[action][storeType]) {
      this.boundUpdate[action][storeType] = {};
    }
    for (const host in data) {
      if (!this.boundUpdate[action][storeType][host]) {
        this.boundUpdate[action][storeType][host] = [];
      }
      for (const name of data[host]) {
        if (!this.boundUpdate[action][storeType][host].includes(name)) {
          this.boundUpdate[action][storeType][host].push(name);
        }
      }
    }
    if (action == "added") {
      // If the same store name was previously deleted or changed, but now is
      // added somehow, dont send the deleted or changed update.
      this.removeNamesFromUpdateList("deleted", storeType, data);
      this.removeNamesFromUpdateList("changed", storeType, data);
    } else if (
      action == "changed" &&
      this.boundUpdate.added &&
      this.boundUpdate.added[storeType]
    ) {
      // If something got added and changed at the same time, then remove those
      // items from changed instead.
      this.removeNamesFromUpdateList(
        "changed",
        storeType,
        this.boundUpdate.added[storeType]
      );
    } else if (action == "deleted") {
      // If any item got delete, or a host got delete, no point in sending
      // added or changed update
      this.removeNamesFromUpdateList("added", storeType, data);
      this.removeNamesFromUpdateList("changed", storeType, data);

      for (const host in data) {
        if (
          !data[host].length &&
          this.boundUpdate.added &&
          this.boundUpdate.added[storeType] &&
          this.boundUpdate.added[storeType][host]
        ) {
          delete this.boundUpdate.added[storeType][host];
        }
        if (
          !data[host].length &&
          this.boundUpdate.changed &&
          this.boundUpdate.changed[storeType] &&
          this.boundUpdate.changed[storeType][host]
        ) {
          delete this.boundUpdate.changed[storeType][host];
        }
      }
    }

    this.batchTimer = setTimeout(() => {
      clearTimeout(this.batchTimer);
      this.emit("stores-update", this.boundUpdate);
      this.boundUpdate = {};
    }, BATCH_DELAY);

    return null;
  }

  /**
   * This method removes data from the this.boundUpdate object in the same
   * manner like this.update() adds data to it.
   *
   * @param {string} action
   *        The type of change. One of "added", "changed" or "deleted"
   * @param {string} storeType
   *        The storage actor for which you want to remove the updates data.
   * @param {object} data
   *        The update object. This object is of the following format:
   *         - {
   *             <host1>: [<store_names1>, <store_name2>...],
   *             <host2>: [<store_names34>...],
   *           }
   *           Where host1, host2 are the hosts which you want to remove and
   *           [<store_namesX] is an array of the names of the store objects.
   */
  removeNamesFromUpdateList(action, storeType, data) {
    for (const host in data) {
      if (
        this.boundUpdate[action] &&
        this.boundUpdate[action][storeType] &&
        this.boundUpdate[action][storeType][host]
      ) {
        for (const name of data[host]) {
          const index = this.boundUpdate[action][storeType][host].indexOf(name);
          if (index > -1) {
            this.boundUpdate[action][storeType][host].splice(index, 1);
          }
        }
        if (!this.boundUpdate[action][storeType][host].length) {
          delete this.boundUpdate[action][storeType][host];
        }
      }
    }
    return null;
  }
}