summaryrefslogtreecommitdiffstats
path: root/devtools/shared/commands/target/legacy-target-watchers/legacy-serviceworkers-watcher.js
blob: 259eaea4822e2939cf23a93a6fd61e9c3217e7a0 (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
/* 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 {
  WorkersListener,
  // eslint-disable-next-line mozilla/reject-some-requires
} = require("resource://devtools/client/shared/workers-listener.js");

const LegacyWorkersWatcher = require("resource://devtools/shared/commands/target/legacy-target-watchers/legacy-workers-watcher.js");

class LegacyServiceWorkersWatcher extends LegacyWorkersWatcher {
  // Holds the current target URL object
  #currentTargetURL;

  constructor(targetCommand, onTargetAvailable, onTargetDestroyed, commands) {
    super(targetCommand, onTargetAvailable, onTargetDestroyed);
    this._registrations = [];
    this._processTargets = new Set();
    this.commands = commands;

    // We need to listen for registration changes at least in order to properly
    // filter service workers by domain when debugging a local tab.
    //
    // A WorkerTarget instance has a url property, but it points to the url of
    // the script, whereas the url property of the ServiceWorkerRegistration
    // points to the URL controlled by the service worker.
    //
    // Historically we have been matching the service worker registration URL
    // to match service workers for local tab tools (app panel & debugger).
    // Maybe here we could have some more info on the actual worker.
    this._workersListener = new WorkersListener(this.rootFront, {
      registrationsOnly: true,
    });

    // Note that this is called much more often than when a registration
    // is created or destroyed. WorkersListener notifies of anything that
    // potentially impacted workers.
    // I use it as a shortcut in this first patch. Listening to rootFront's
    // "serviceWorkerRegistrationListChanged" should be enough to be notified
    // about registrations. And if we need to also update the
    // "debuggerServiceWorkerStatus" from here, then we would have to
    // also listen to "registration-changed" one each registration.
    this._onRegistrationListChanged =
      this._onRegistrationListChanged.bind(this);
    this._onDocumentEvent = this._onDocumentEvent.bind(this);

    // Flag used from the parent class to listen to process targets.
    // Decision tree is complicated, keep all logic in the parent methods.
    this._isServiceWorkerWatcher = true;
  }

  /**
   * Override from LegacyWorkersWatcher.
   *
   * We record all valid service worker targets (ie workers that match a service
   * worker registration), but we will only notify about the ones which match
   * the current domain.
   */
  _recordWorkerTarget(workerTarget) {
    return !!this._getRegistrationForWorkerTarget(workerTarget);
  }

  // Override from LegacyWorkersWatcher.
  _supportWorkerTarget(workerTarget) {
    if (!workerTarget.isServiceWorker) {
      return false;
    }

    const registration = this._getRegistrationForWorkerTarget(workerTarget);
    return registration && this._isRegistrationValidForTarget(registration);
  }

  // Override from LegacyWorkersWatcher.
  async listen() {
    // Listen to the current target front.
    this.target = this.targetCommand.targetFront;

    if (this.targetCommand.descriptorFront.isTabDescriptor) {
      this.#currentTargetURL = new URL(this.targetCommand.targetFront.url);
    }

    this._workersListener.addListener(this._onRegistrationListChanged);

    // Fetch the registrations before calling listen, since service workers
    // might already be available and will need to be compared with the existing
    // registrations.
    await this._onRegistrationListChanged();

    if (this.targetCommand.descriptorFront.isTabDescriptor) {
      await this.commands.resourceCommand.watchResources(
        [this.commands.resourceCommand.TYPES.DOCUMENT_EVENT],
        {
          onAvailable: this._onDocumentEvent,
          ignoreExistingResources: true,
        }
      );
    }

    await super.listen();
  }

  // Override from LegacyWorkersWatcher.
  unlisten(...args) {
    this._workersListener.removeListener(this._onRegistrationListChanged);

    if (this.targetCommand.descriptorFront.isTabDescriptor) {
      this.commands.resourceCommand.unwatchResources(
        [this.commands.resourceCommand.TYPES.DOCUMENT_EVENT],
        {
          onAvailable: this._onDocumentEvent,
        }
      );
    }

    super.unlisten(...args);
  }

  // Override from LegacyWorkersWatcher.
  async _onProcessAvailable({ targetFront }) {
    if (this.targetCommand.descriptorFront.isTabDescriptor) {
      // XXX: This has been ported straight from the current debugger
      // implementation. Since pauseMatchingServiceWorkers expects an origin
      // to filter matching workers, it only makes sense when we are debugging
      // a tab. However in theory, parent process debugging could pause all
      // service workers without matching anything.
      try {
        // To support early breakpoint we need to setup the
        // `pauseMatchingServiceWorkers` mechanism in each process.
        await targetFront.pauseMatchingServiceWorkers({
          origin: this.#currentTargetURL.origin,
        });
      } catch (e) {
        if (targetFront.actorID) {
          throw e;
        } else {
          console.warn(
            "Process target destroyed while calling pauseMatchingServiceWorkers"
          );
        }
      }
    }

    this._processTargets.add(targetFront);
    return super._onProcessAvailable({ targetFront });
  }

  _onProcessDestroyed({ targetFront }) {
    this._processTargets.delete(targetFront);
    return super._onProcessDestroyed({ targetFront });
  }

  _onDocumentEvent(resources) {
    for (const resource of resources) {
      if (
        resource.resourceType !==
        this.commands.resourceCommand.TYPES.DOCUMENT_EVENT
      ) {
        continue;
      }

      if (resource.name === "will-navigate") {
        // We rely on will-navigate as the onTargetAvailable for the top-level frame can
        // happen after the onTargetAvailable for processes (handled in _onProcessAvailable),
        // where we need the origin we navigate to.
        this.#currentTargetURL = new URL(resource.newURI);
        continue;
      }

      // Note that we rely on "dom-loading" rather than "will-navigate" because the
      // destroyed/available callbacks should be triggered after the Debugger
      // has cleaned up its reducers, which happens on "will-navigate".
      // On the other end, "dom-complete", which is a better mapping of "navigate", is
      // happening too late (because of resources being throttled), and would cause failures
      // in test (like browser_target_command_service_workers_navigation.js), as the new worker
      // target would already be registered at this point, and seen as something that would
      // need to be destroyed.
      if (resource.name === "dom-loading") {
        const allServiceWorkerTargets = this._getAllServiceWorkerTargets();

        for (const target of allServiceWorkerTargets) {
          // Note: we call isTargetRegistered again because calls to
          // onTargetDestroyed might have modified the list of registered targets.
          const isRegisteredAfter =
            this.targetCommand.isTargetRegistered(target);
          const isValidTarget = this._supportWorkerTarget(target);
          if (isValidTarget && !isRegisteredAfter) {
            // If the target is still valid for the current top target, call
            // onTargetAvailable as well.
            this.onTargetAvailable(target);
          }
        }
      }
    }
  }

  async _onRegistrationListChanged() {
    if (this.targetCommand.isDestroyed()) {
      return;
    }

    await this._updateRegistrations();

    // Everything after this point is not strictly necessary for sw support
    // in the target list, but it makes the behavior closer to the previous
    // listAllWorkers/WorkersListener pair.
    const allServiceWorkerTargets = this._getAllServiceWorkerTargets();
    for (const target of allServiceWorkerTargets) {
      const hasRegistration = this._getRegistrationForWorkerTarget(target);
      if (!hasRegistration) {
        // XXX: At this point the worker target is not really destroyed, but
        // historically, listAllWorkers* APIs stopped returning worker targets
        // if worker registrations are no longer available.
        if (this.targetCommand.isTargetRegistered(target)) {
          // Only emit onTargetDestroyed if it wasn't already done by
          // onNavigate (ie the target is still tracked by TargetCommand)
          this.onTargetDestroyed(target);
        }
        // Here we only care about service workers which no longer match *any*
        // registration. The worker will be completely destroyed soon, remove
        // it from the legacy worker watcher internal targetsByProcess Maps.
        this._removeTargetReferences(target);
      }
    }
  }

  // Delete the provided worker target from the internal targetsByProcess Maps.
  _removeTargetReferences(target) {
    const allProcessTargets = this._getProcessTargets().filter(t =>
      this.targetsByProcess.get(t)
    );

    for (const processTarget of allProcessTargets) {
      this.targetsByProcess.get(processTarget).delete(target);
    }
  }

  async _updateRegistrations() {
    const { registrations } =
      await this.rootFront.listServiceWorkerRegistrations();

    this._registrations = registrations;
  }

  _getRegistrationForWorkerTarget(workerTarget) {
    return this._registrations.find(r => {
      return (
        r.evaluatingWorker?.id === workerTarget.id ||
        r.activeWorker?.id === workerTarget.id ||
        r.installingWorker?.id === workerTarget.id ||
        r.waitingWorker?.id === workerTarget.id
      );
    });
  }

  _getProcessTargets() {
    return [...this._processTargets];
  }

  // Flatten all service worker targets in all processes.
  _getAllServiceWorkerTargets() {
    const allProcessTargets = this._getProcessTargets().filter(target =>
      this.targetsByProcess.get(target)
    );

    const serviceWorkerTargets = [];
    for (const target of allProcessTargets) {
      serviceWorkerTargets.push(...this.targetsByProcess.get(target));
    }
    return serviceWorkerTargets;
  }

  // Check if the registration is relevant for the current target, ie
  // corresponds to the same domain.
  _isRegistrationValidForTarget(registration) {
    if (this.targetCommand.descriptorFront.isBrowserProcessDescriptor) {
      // All registrations are valid for main process debugging.
      return true;
    }

    if (!this.targetCommand.descriptorFront.isTabDescriptor) {
      // No support for service worker targets outside of main process &
      // tab debugging.
      return false;
    }

    // For local tabs, we match ServiceWorkerRegistrations and the target
    // if they share the same hostname for their "url" properties.
    const targetDomain = this.#currentTargetURL.hostname;
    try {
      const registrationDomain = new URL(registration.url).hostname;
      return registrationDomain === targetDomain;
    } catch (e) {
      // XXX: Some registrations have an empty URL.
      return false;
    }
  }
}

module.exports = LegacyServiceWorkersWatcher;