File: ServiceWorkerPrivate.h

package info (click to toggle)
firefox 143.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,617,328 kB
  • sloc: cpp: 7,478,492; javascript: 6,417,157; ansic: 3,720,058; python: 1,396,372; xml: 627,523; asm: 438,677; java: 186,156; sh: 63,477; makefile: 19,171; objc: 13,059; perl: 12,983; yacc: 4,583; cs: 3,846; pascal: 3,405; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (452 lines) | stat: -rw-r--r-- 15,175 bytes parent folder | download | duplicates (4)
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

#ifndef mozilla_dom_serviceworkerprivate_h
#define mozilla_dom_serviceworkerprivate_h

#include <functional>
#include <type_traits>

#include "mozilla/Attributes.h"
#include "mozilla/Maybe.h"
#include "mozilla/MozPromise.h"
#include "mozilla/RefPtr.h"
#include "mozilla/TimeStamp.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/dom/FetchService.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/RemoteWorkerController.h"
#include "mozilla/dom/RemoteWorkerTypes.h"
#include "mozilla/dom/ServiceWorkerLifetimeExtension.h"
#include "mozilla/dom/ServiceWorkerOpArgs.h"
#include "nsCOMPtr.h"
#include "nsISupportsImpl.h"
#include "nsTArray.h"

#define NOTIFICATION_CLICK_EVENT_NAME u"notificationclick"
#define NOTIFICATION_CLOSE_EVENT_NAME u"notificationclose"

class nsIInterceptedChannel;
class nsIPushSubscription;
class nsIWorkerDebugger;

namespace mozilla {

template <typename T>
class Maybe;

class JSObjectHolder;

namespace net {
class CookieStruct;
}

namespace dom {

class PostMessageSource;
class RemoteWorkerControllerChild;
class ServiceWorkerCloneData;
class ServiceWorkerInfo;
class ServiceWorkerPrivate;
class ServiceWorkerRegistrationInfo;
struct CookieListItem;

namespace ipc {
class StructuredCloneData;
}  // namespace ipc

class LifeCycleEventCallback : public Runnable {
 public:
  LifeCycleEventCallback() : Runnable("dom::LifeCycleEventCallback") {}

  // Called on the worker thread.
  virtual void SetResult(bool aResult) = 0;
};

// Used to keep track of pending waitUntil as well as in-flight extendable
// events. When the last token is released, we attempt to terminate the worker.
class KeepAliveToken final : public nsISupports {
 public:
  NS_DECL_ISUPPORTS

  explicit KeepAliveToken(ServiceWorkerPrivate* aPrivate);

 private:
  ~KeepAliveToken();

  RefPtr<ServiceWorkerPrivate> mPrivate;
};

class ServiceWorkerPrivate final : public RemoteWorkerObserver {
  friend class KeepAliveToken;

 public:
  NS_INLINE_DECL_REFCOUNTING(ServiceWorkerPrivate, override);

  using PromiseExtensionWorkerHasListener = MozPromise<bool, nsresult, false>;

 public:
  explicit ServiceWorkerPrivate(ServiceWorkerInfo* aInfo);

  Maybe<ClientInfo> GetClientInfo() { return mClientInfo; }

  nsresult SendMessageEvent(
      RefPtr<ServiceWorkerCloneData>&& aData,
      const ServiceWorkerLifetimeExtension& aLifetimeExtension,
      const PostMessageSource& aSource);

  // This is used to validate the worker script and continue the installation
  // process.
  nsresult CheckScriptEvaluation(
      const ServiceWorkerLifetimeExtension& aLifetimeExtension,
      RefPtr<LifeCycleEventCallback> aCallback);

  nsresult SendLifeCycleEvent(
      const nsAString& aEventType,
      const ServiceWorkerLifetimeExtension& aLifetimeExtension,
      const RefPtr<LifeCycleEventCallback>& aCallback);

  nsresult SendCookieChangeEvent(
      const net::CookieStruct& aCookie, bool aCookieDeleted,
      RefPtr<ServiceWorkerRegistrationInfo> aRegistration);

  nsresult SendPushEvent(const nsAString& aMessageId,
                         const Maybe<nsTArray<uint8_t>>& aData,
                         RefPtr<ServiceWorkerRegistrationInfo> aRegistration);

  nsresult SendPushSubscriptionChangeEvent(
      const RefPtr<nsIPushSubscription>& aOldSubscription);

  nsresult SendNotificationClickEvent(const IPCNotification& aNotification,
                                      const nsAString& aAction);

  nsresult SendNotificationCloseEvent(const IPCNotification& aNotification);

  nsresult SendFetchEvent(nsCOMPtr<nsIInterceptedChannel> aChannel,
                          nsILoadGroup* aLoadGroup, const nsAString& aClientId,
                          const nsAString& aResultingClientId);

  Result<RefPtr<PromiseExtensionWorkerHasListener>, nsresult>
  WakeForExtensionAPIEvent(const nsAString& aExtensionAPINamespace,
                           const nsAString& aEXtensionAPIEventName);

  // This will terminate the current running worker thread and drop the
  // workerPrivate reference.
  // Called by ServiceWorkerInfo when [[Clear Registration]] is invoked
  // or whenever the spec mandates that we terminate the worker.
  // This is a no-op if the worker has already been stopped.
  //
  // Now takes an optional promise that will be resolved when the worker is
  // dead, including if the worker was not running at all.
  void TerminateWorker(Maybe<RefPtr<Promise>> aMaybePromise = Nothing());

  void NoteDeadServiceWorkerInfo();

  void NoteStoppedControllingDocuments();

  void UpdateState(ServiceWorkerState aState);

  void UpdateIsOnContentBlockingAllowList(bool aOnContentBlockingAllowList);

  nsresult GetDebugger(nsIWorkerDebugger** aResult);

  nsresult AttachDebugger();

  nsresult DetachDebugger();

  // Return the current lifetime deadline for this ServiceWorker; this value may
  // be null or in the past.
  //
  // This value always only reflects the explicit lifetime extensions
  // resulting from functional events and will never reflect the extra "grace
  // period".
  TimeStamp GetLifetimeDeadline() { return mIdleDeadline; }

  uint32_t GetLaunchCount() { return mLaunchCount; }

  bool IsIdle() const;

  // This promise is used schedule clearing of the owning registrations and its
  // associated Service Workers if that registration becomes "unreachable" by
  // the ServiceWorkerManager. This occurs under two conditions, which are the
  // preconditions to calling this method:
  // - The owning registration must be unregistered.
  // - The associated Service Worker must *not* be controlling clients.
  //
  // Additionally, perhaps stating the obvious, the associated Service Worker
  // must *not* be idle (whatever must be done "when idle" can just be done
  // immediately).
  RefPtr<GenericPromise> GetIdlePromise();

  void SetHandlesFetch(bool aValue);

  RefPtr<GenericPromise> SetSkipWaitingFlag();

  static void RunningShutdown() {
    // Force a final update of the number of running ServiceWorkers
    UpdateRunning(0, 0);
    MOZ_ASSERT(sRunningServiceWorkers == 0);
    MOZ_ASSERT(sRunningServiceWorkersFetch == 0);
  }

  /**
   * Update Telemetry for # of running ServiceWorkers
   */
  static void UpdateRunning(int32_t aDelta, int32_t aFetchDelta);

 private:
  // Timer callbacks
  void NoteIdleWorkerCallback(nsITimer* aTimer);

  void TerminateWorkerCallback(nsITimer* aTimer);

  void RenewKeepAliveToken(
      const ServiceWorkerLifetimeExtension& aLifetimeExtension);

  void ResetIdleTimeout(
      const ServiceWorkerLifetimeExtension& aLifetimeExtension);

  void AddToken();

  void ReleaseToken();

  already_AddRefed<KeepAliveToken> CreateEventKeepAliveToken();

  nsresult SpawnWorkerIfNeeded(
      const ServiceWorkerLifetimeExtension& aLifetimeExtension);

  ~ServiceWorkerPrivate();

  nsresult Initialize();

  void RegenerateClientInfo();

  /**
   * RemoteWorkerObserver
   */
  void CreationFailed() override;

  void CreationSucceeded() override;

  void ErrorReceived(const ErrorValue& aError) override;

  void LockNotified(bool aCreated) final {
    // no-op for service workers
  }

  void WebTransportNotified(bool aCreated) final {
    // no-op for service workers
  }

  void Terminated() override;

  // Refreshes only the parts of mRemoteWorkerData that may change over time.
  void RefreshRemoteWorkerData(
      const RefPtr<ServiceWorkerRegistrationInfo>& aRegistration);

  nsresult SendCookieChangeEventInternal(
      RefPtr<ServiceWorkerRegistrationInfo>&& aRegistration,
      ServiceWorkerCookieChangeEventOpArgs&& aArgs);

  nsresult SendPushEventInternal(
      RefPtr<ServiceWorkerRegistrationInfo>&& aRegistration,
      ServiceWorkerPushEventOpArgs&& aArgs);

  // Setup the navigation preload by the intercepted channel and the
  // RegistrationInfo.
  RefPtr<FetchServicePromises> SetupNavigationPreload(
      nsCOMPtr<nsIInterceptedChannel>& aChannel,
      const RefPtr<ServiceWorkerRegistrationInfo>& aRegistration);

  nsresult SendFetchEventInternal(
      RefPtr<ServiceWorkerRegistrationInfo>&& aRegistration,
      ParentToParentServiceWorkerFetchEventOpArgs&& aArgs,
      nsCOMPtr<nsIInterceptedChannel>&& aChannel,
      RefPtr<FetchServicePromises>&& aPreloadResponseReadyPromises);

  void Shutdown(Maybe<RefPtr<Promise>>&& aMaybePromise = Nothing());

  RefPtr<GenericNonExclusivePromise> ShutdownInternal(
      uint32_t aShutdownStateId);

  nsresult ExecServiceWorkerOp(
      ServiceWorkerOpArgs&& aArgs,
      const ServiceWorkerLifetimeExtension& aLifetimeExtension,
      std::function<void(ServiceWorkerOpResult&&)>&& aSuccessCallback,
      std::function<void()>&& aFailureCallback = [] {});

  class PendingFunctionalEvent {
   public:
    PendingFunctionalEvent(
        ServiceWorkerPrivate* aOwner,
        RefPtr<ServiceWorkerRegistrationInfo>&& aRegistration);

    virtual ~PendingFunctionalEvent();

    virtual nsresult Send() = 0;

   protected:
    ServiceWorkerPrivate* const MOZ_NON_OWNING_REF mOwner;
    RefPtr<ServiceWorkerRegistrationInfo> mRegistration;
  };

  class PendingCookieChangeEvent final : public PendingFunctionalEvent {
   public:
    PendingCookieChangeEvent(
        ServiceWorkerPrivate* aOwner,
        RefPtr<ServiceWorkerRegistrationInfo>&& aRegistration,
        ServiceWorkerCookieChangeEventOpArgs&& aArgs);

    nsresult Send() override;

   private:
    ServiceWorkerCookieChangeEventOpArgs mArgs;
  };

  class PendingPushEvent final : public PendingFunctionalEvent {
   public:
    PendingPushEvent(ServiceWorkerPrivate* aOwner,
                     RefPtr<ServiceWorkerRegistrationInfo>&& aRegistration,
                     ServiceWorkerPushEventOpArgs&& aArgs);

    nsresult Send() override;

   private:
    ServiceWorkerPushEventOpArgs mArgs;
  };

  class PendingFetchEvent final : public PendingFunctionalEvent {
   public:
    PendingFetchEvent(
        ServiceWorkerPrivate* aOwner,
        RefPtr<ServiceWorkerRegistrationInfo>&& aRegistration,
        ParentToParentServiceWorkerFetchEventOpArgs&& aArgs,
        nsCOMPtr<nsIInterceptedChannel>&& aChannel,
        RefPtr<FetchServicePromises>&& aPreloadResponseReadyPromises);

    nsresult Send() override;

    ~PendingFetchEvent();

   private:
    ParentToParentServiceWorkerFetchEventOpArgs mArgs;
    nsCOMPtr<nsIInterceptedChannel> mChannel;
    // The promises from FetchService. It indicates if the preload response is
    // ready or not. The promise's resolve/reject value should be handled in
    // FetchEventOpChild, such that the preload result can be propagated to the
    // ServiceWorker through IPC. However, FetchEventOpChild creation could be
    // pending here, so this member is needed. And it will be forwarded to
    // FetchEventOpChild when crearting the FetchEventOpChild.
    RefPtr<FetchServicePromises> mPreloadResponseReadyPromises;
  };

  nsTArray<UniquePtr<PendingFunctionalEvent>> mPendingFunctionalEvents;

  /**
   * It's possible that there are still in-progress operations when a
   * a termination operation is issued. In this case, it's important to keep
   * the RemoteWorkerControllerChild actor alive until all pending operations
   * have completed before destroying it with Send__delete__().
   *
   * RAIIActorPtrHolder holds a singular, owning reference to a
   * RemoteWorkerControllerChild actor and is responsible for destroying the
   * actor in its (i.e. the holder's) destructor. This implies that all
   * in-progress operations must maintain a strong reference to their
   * corresponding holders and release the reference once completed/canceled.
   *
   * Additionally a RAIIActorPtrHolder must be initialized with a non-null actor
   * and cannot be moved or copied. Therefore, the identities of two held
   * actors can be compared by simply comparing their holders' addresses.
   */
  class RAIIActorPtrHolder final {
   public:
    NS_INLINE_DECL_REFCOUNTING(RAIIActorPtrHolder)

    explicit RAIIActorPtrHolder(
        already_AddRefed<RemoteWorkerControllerChild> aActor);

    RAIIActorPtrHolder(const RAIIActorPtrHolder& aOther) = delete;
    RAIIActorPtrHolder& operator=(const RAIIActorPtrHolder& aOther) = delete;

    RAIIActorPtrHolder(RAIIActorPtrHolder&& aOther) = delete;
    RAIIActorPtrHolder& operator=(RAIIActorPtrHolder&& aOther) = delete;

    RemoteWorkerControllerChild* operator->() const
        MOZ_NO_ADDREF_RELEASE_ON_RETURN;

    RemoteWorkerControllerChild* get() const;

    RefPtr<GenericPromise> OnDestructor();

   private:
    ~RAIIActorPtrHolder();

    MozPromiseHolder<GenericPromise> mDestructorPromiseHolder;

    const RefPtr<RemoteWorkerControllerChild> mActor;
  };

  RefPtr<RAIIActorPtrHolder> mControllerChild;

  RemoteWorkerData mRemoteWorkerData;
  Maybe<ClientInfo> mClientInfo;

  TimeStamp mServiceWorkerLaunchTimeStart;

  // Counters for Telemetry - totals running simultaneously, and those that
  // handle Fetch, plus Max values for each
  static uint32_t sRunningServiceWorkers;
  static uint32_t sRunningServiceWorkersFetch;
  static uint32_t sRunningServiceWorkersMax;
  static uint32_t sRunningServiceWorkersFetchMax;

  // We know the state after we've evaluated the worker, and we then store
  // it in the registration.  The only valid state transition should be
  // from Unknown to Enabled or Disabled.
  enum { Unknown, Enabled, Disabled } mHandlesFetch{Unknown};

  // The info object owns us. It is possible to outlive it for a brief period
  // of time if there are pending waitUntil promises, in which case it
  // will be null and |SpawnWorkerIfNeeded| will always fail.
  ServiceWorkerInfo* MOZ_NON_OWNING_REF mInfo;

  nsCOMPtr<nsITimer> mIdleWorkerTimer;

  ServiceWorkerLifetimeExtension mPendingSpawnLifetime;

  // This is the current time in the future that the idle timer is set to expire
  // for keepalive purposes.  This will not be updated for the
  // "dom.serviceWorkers.idle_extended_timeout" grace period after the time
  // first expires.
  TimeStamp mIdleDeadline;

  // We keep a token for |dom.serviceWorkers.idle_timeout| seconds to give the
  // worker a grace period after each event.
  RefPtr<KeepAliveToken> mIdleKeepAliveToken;

  uint64_t mDebuggerCount;

  uint64_t mTokenCount;

  uint32_t mLaunchCount;

  // Used by the owning `ServiceWorkerRegistrationInfo` when it wants to call
  // `Clear` after being unregistered and isn't controlling any clients but this
  // worker (i.e. the registration's active worker) isn't idle yet. Note that
  // such an event should happen at most once in a
  // `ServiceWorkerRegistrationInfo`s lifetime, so this promise should also only
  // be obtained at most once.
  MozPromiseHolder<GenericPromise> mIdlePromiseHolder;

#ifdef DEBUG
  bool mIdlePromiseObtained = false;
#endif
};

}  // namespace dom
}  // namespace mozilla

#endif  // mozilla_dom_serviceworkerprivate_h