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
  582
  583
  584
  585
  586
  587
  588
  589
  590
  591
  592
  593
  594
  595
  596
  597
  598
  599
  600
  601
  602
  603
  604
  605
  606
  607
  608
  609
  610
  611
  612
  613
  614
  615
  616
  617
  618
  619
  620
  621
  622
  623
  624

content / browser / service_worker / service_worker_context_wrapper.h [blame]

// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_WRAPPER_H_
#define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_WRAPPER_H_

#include <stdint.h>

#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>

#include "base/containers/flat_map.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/observer_list.h"
#include "base/observer_list_threadsafe.h"
#include "content/browser/service_worker/service_worker_context_core.h"
#include "content/browser/service_worker/service_worker_context_core_observer.h"
#include "content/browser/service_worker/service_worker_identifiability_metrics.h"
#include "content/common/content_export.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/browser/service_worker_context.h"
#include "content/public/browser/service_worker_running_info.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "services/network/public/mojom/client_security_state.mojom-forward.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"

namespace base {
class FilePath;
}  // namespace base

namespace storage {
class QuotaManagerProxy;
class ServiceWorkerStorageControlImpl;
class SpecialStoragePolicy;
}  // namespace storage

namespace url {
class Origin;
}  // namespace url

namespace content {

class BrowserContext;
class ChromeBlobStorageContext;
class ServiceWorkerContextObserver;
class StoragePartitionImpl;

// A ref-counted wrapper struct around an ObserverList. This is needed because
// the ObserverList is shared implicitly between the ServiceWorkerContextCore
// and the ServiceWorkerContextWrapper.
struct ServiceWorkerContextSynchronousObserverList
    : public base::RefCounted<ServiceWorkerContextSynchronousObserverList> {
 public:
  REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE();

  ServiceWorkerContextSynchronousObserverList();

  base::ObserverList<ServiceWorkerContextObserverSynchronous> observers;

 private:
  friend class base::RefCounted<ServiceWorkerContextSynchronousObserverList>;
  ~ServiceWorkerContextSynchronousObserverList();
};

// A refcounted wrapper class for ServiceWorkerContextCore. Higher level content
// lib classes keep references to this class on multiple threads. The inner core
// instance is strictly single threaded (on the UI thread) and is not
// refcounted. The core object is what is used internally by service worker
// classes.
//
// All the methods called on the UI thread.
// TODO(crbug.com/40738640): Require all references to be on the UI
// thread and remove RefCountedThreadSafe.
class CONTENT_EXPORT ServiceWorkerContextWrapper
    : public ServiceWorkerContext,
      public ServiceWorkerContextCoreObserver,
      public base::RefCountedThreadSafe<ServiceWorkerContextWrapper,
                                        BrowserThread::DeleteOnUIThread> {
 public:
  using StatusCallback =
      base::OnceCallback<void(blink::ServiceWorkerStatusCode)>;
  using BoolCallback = base::OnceCallback<void(bool)>;
  using FindRegistrationCallback =
      ServiceWorkerRegistry::FindRegistrationCallback;
  using GetRegistrationsCallback =
      ServiceWorkerRegistry::GetRegistrationsCallback;
  using GetRegistrationsInfosCallback =
      ServiceWorkerRegistry::GetRegistrationsInfosCallback;
  using GetUserDataCallback = ServiceWorkerRegistry::GetUserDataCallback;
  using GetUserKeysAndDataCallback =
      ServiceWorkerRegistry::GetUserKeysAndDataCallback;
  using GetUserDataForAllRegistrationsCallback =
      ServiceWorkerRegistry::GetUserDataForAllRegistrationsCallback;
  using GetInstalledRegistrationOriginsCallback =
      base::OnceCallback<void(const std::vector<url::Origin>& origins)>;

  explicit ServiceWorkerContextWrapper(BrowserContext* browser_context);

  ServiceWorkerContextWrapper(const ServiceWorkerContextWrapper&) = delete;
  ServiceWorkerContextWrapper& operator=(const ServiceWorkerContextWrapper&) =
      delete;

  // Init and Shutdown called when the StoragePartition is being setup and torn
  // down.
  void Init(const base::FilePath& user_data_directory,
            storage::QuotaManagerProxy* quota_manager_proxy,
            storage::SpecialStoragePolicy* special_storage_policy,
            ChromeBlobStorageContext* blob_context);
  void Shutdown();

  // Deletes all files on disk and restarts the system asynchronously. This
  // leaves the system in a disabled state until it's done.
  void DeleteAndStartOver();

  // Can be null before/during init and during/after shutdown (and in tests).
  StoragePartitionImpl* storage_partition() const;

  void set_storage_partition(StoragePartitionImpl* storage_partition);

  BrowserContext* browser_context();

  ServiceWorkerProcessManager* process_manager() {
    return process_manager_.get();
  }

  // ServiceWorkerContextCoreObserver implementation:
  void OnRegistrationCompleted(int64_t registration_id,
                               const GURL& scope,
                               const blink::StorageKey& key) override;
  void OnRegistrationStored(int64_t registration_id,
                            const GURL& scope,
                            const blink::StorageKey& key) override;
  void OnAllRegistrationsDeletedForStorageKey(
      const blink::StorageKey& key) override;
  void OnErrorReported(
      int64_t version_id,
      const GURL& scope,
      const blink::StorageKey& key,
      const ServiceWorkerContextObserver::ErrorInfo& info) override;
  void OnReportConsoleMessage(int64_t version_id,
                              const GURL& scope,
                              const blink::StorageKey& key,
                              const ConsoleMessage& message) override;
  void OnControlleeAdded(int64_t version_id,
                         const std::string& uuid,
                         const ServiceWorkerClientInfo& info) override;
  void OnControlleeRemoved(int64_t version_id,
                           const std::string& uuid) override;
  void OnNoControllees(int64_t version_id,
                       const GURL& scope,
                       const blink::StorageKey& key) override;
  void OnControlleeNavigationCommitted(
      int64_t version_id,
      const std::string& uuid,
      GlobalRenderFrameHostId render_frame_host_id) override;
  void OnStarting(int64_t version_id) override;
  void OnStarted(int64_t version_id,
                 const GURL& scope,
                 int process_id,
                 const GURL& script_url,
                 const blink::ServiceWorkerToken& token,
                 const blink::StorageKey& key) override;
  void OnStopping(int64_t version_id) override;
  void OnStopped(int64_t version_id) override;
  void OnDeleteAndStartOver() override;
  void OnVersionStateChanged(int64_t version_id,
                             const GURL& scope,
                             const blink::StorageKey& key,
                             ServiceWorkerVersion::Status status) override;
  void OnWindowOpened(const GURL& script_url, const GURL& url) override;
  void OnClientNavigated(const GURL& script_url, const GURL& url) override;

  // ServiceWorkerContext implementation:
  void AddObserver(ServiceWorkerContextObserver* observer) override;
  void RemoveObserver(ServiceWorkerContextObserver* observer) override;
  void AddSyncObserver(
      ServiceWorkerContextObserverSynchronous* observer) override;
  void RemoveSyncObserver(
      ServiceWorkerContextObserverSynchronous* observer) override;
  // TODO (crbug.com/1335059) RegisterServiceWorker passes an invalid frame id.
  // Currently it's okay because it is used only by PaymentAppInstaller and
  // Extensions, but ideally we should add some guard to avoid the method is
  // called from other places.
  void RegisterServiceWorker(
      const GURL& script_url,
      const blink::StorageKey& key,
      const blink::mojom::ServiceWorkerRegistrationOptions& options,
      StatusCodeCallback callback) override;
  void UnregisterServiceWorker(const GURL& scope,
                               const blink::StorageKey& key,
                               StatusCodeCallback callback) override;
  void UnregisterServiceWorkerImmediately(const GURL& scope,
                                          const blink::StorageKey& key,
                                          StatusCodeCallback callback) override;
  ServiceWorkerExternalRequestResult StartingExternalRequest(
      int64_t service_worker_version_id,
      ServiceWorkerExternalRequestTimeoutType timeout_type,
      const base::Uuid& request_uuid) override;
  ServiceWorkerExternalRequestResult FinishedExternalRequest(
      int64_t service_worker_version_id,
      const base::Uuid& request_uuid) override;
  size_t CountExternalRequestsForTest(const blink::StorageKey& key) override;
  bool ExecuteScriptForTest(
      const std::string& script,
      int64_t service_worker_version_id,
      ServiceWorkerScriptExecutionCallback callback) override;
  bool MaybeHasRegistrationForStorageKey(const blink::StorageKey& key) override;
  void GetAllStorageKeysInfo(GetUsageInfoCallback callback) override;
  void DeleteForStorageKey(const blink::StorageKey& key,
                           ResultCallback callback) override;
  void CheckHasServiceWorker(const GURL& url,
                             const blink::StorageKey& key,
                             CheckHasServiceWorkerCallback callback) override;

  void ClearAllServiceWorkersForTest(base::OnceClosure callback) override;
  void StartWorkerForScope(const GURL& scope,
                           const blink::StorageKey& key,
                           StartWorkerCallback info_callback,
                           StatusCodeCallback failure_callback) override;
  void StartServiceWorkerAndDispatchMessage(
      const GURL& scope,
      const blink::StorageKey& key,
      blink::TransferableMessage message,
      ResultCallback result_callback) override;
  void StartServiceWorkerForNavigationHint(
      const GURL& document_url,
      const blink::StorageKey& key,
      StartServiceWorkerForNavigationHintCallback callback) override;
  void WarmUpServiceWorker(const GURL& document_url,
                           const blink::StorageKey& key,
                           WarmUpServiceWorkerCallback callback) override;
  void StopAllServiceWorkersForStorageKey(
      const blink::StorageKey& key) override;
  void StopAllServiceWorkers(base::OnceClosure callback) override;
  const base::flat_map<int64_t, ServiceWorkerRunningInfo>&
  GetRunningServiceWorkerInfos() override;
  bool IsLiveStartingServiceWorker(int64_t service_worker_version_id) override;
  bool IsLiveRunningServiceWorker(int64_t service_worker_version_id) override;
  service_manager::InterfaceProvider& GetRemoteInterfaces(
      int64_t service_worker_version_id) override;
  blink::AssociatedInterfaceProvider& GetRemoteAssociatedInterfaces(
      int64_t service_worker_version_id) override;

  // Returns the running info for a worker with `version_id`, if found.
  std::optional<ServiceWorkerRunningInfo> GetRunningServiceWorkerInfo(
      int64_t version_id);

  scoped_refptr<ServiceWorkerRegistration> GetLiveRegistration(
      int64_t registration_id);
  ServiceWorkerVersion* GetLiveVersion(int64_t version_id);
  std::vector<ServiceWorkerRegistrationInfo> GetAllLiveRegistrationInfo();
  std::vector<ServiceWorkerVersionInfo> GetAllLiveVersionInfo();

  void HasMainFrameWindowClient(const blink::StorageKey& key,
                                BoolCallback callback) const;

  // Returns all frame routing ids for the given `key`.
  std::unique_ptr<std::vector<GlobalRenderFrameHostId>>
  GetWindowClientFrameRoutingIds(const blink::StorageKey& key) const;

  // Returns the registration whose scope longest matches `client_url` with the
  // associated `key`. It is guaranteed that the returned registration has the
  // activated worker.
  //
  //  - If the registration is not found, returns ERROR_NOT_FOUND.
  //  - If the registration has neither the waiting version nor the active
  //    version, returns ERROR_NOT_FOUND.
  //  - If the registration does not have the active version but has the waiting
  //    version, activates the waiting version and runs `callback` when it is
  //    activated.
  //
  // There is no guarantee for whether the callback is called synchronously or
  // asynchronously.
  void FindReadyRegistrationForClientUrl(const GURL& client_url,
                                         const blink::StorageKey& key,
                                         FindRegistrationCallback callback);

  // Returns the registration for `scope` with the associated `key`. It is
  // guaranteed that the returned registration has the activated worker.
  //
  //  - If the registration is not found, returns ERROR_NOT_FOUND.
  //  - If the registration has neither the waiting version nor the active
  //    version, returns ERROR_NOT_FOUND.
  //  - If the registration does not have the active version but has the waiting
  //    version, activates the waiting version and runs `callback` when it is
  //    activated.
  //
  // There is no guarantee for whether the callback is called synchronously or
  // asynchronously.
  void FindReadyRegistrationForScope(const GURL& scope,
                                     const blink::StorageKey& key,
                                     FindRegistrationCallback callback);

  // Similar to FindReadyRegistrationForScope, but in the case no waiting or
  // active worker is found (i.e., there is only an installing worker),
  // `callback` is called without waiting for the worker to reach active.
  //
  // Not guaranteed to call the callback asynchronously.
  // TODO(falken): Should it?
  void FindRegistrationForScope(const GURL& scope,
                                const blink::StorageKey& key,
                                FindRegistrationCallback callback);

  // Returns the registration for `registration_id`. It is guaranteed that the
  // returned registration has the activated worker.
  //
  //  - If the registration is not found, returns ERROR_NOT_FOUND.
  //  - If the registration has neither the waiting version nor the active
  //    version, returns ERROR_NOT_FOUND.
  //  - If the registration does not have the active version but has the waiting
  //    version, activates the waiting version and runs `callback` when it is
  //    activated.
  //
  // There is no guarantee about whether the callback is called asynchronously
  // or synchronously.
  void FindReadyRegistrationForId(int64_t registration_id,
                                  const blink::StorageKey& key,
                                  FindRegistrationCallback callback);

  // Returns the registration for |registration_id|. It is guaranteed that the
  // returned registration has the activated worker.
  //
  // Generally |FindReadyRegistrationForId| should be used to look up a
  // registration by |registration_id| since it's more efficient. But if a
  // |registration_id| is all that is available this method can be used instead.
  //
  //  - If the registration is not found, returns ERROR_NOT_FOUND.
  //  - If the registration has neither the waiting version nor the active
  //    version, returns ERROR_NOT_FOUND.
  //  - If the registration does not have the active version but has the waiting
  //    version, activates the waiting version and runs |callback| when it is
  //    activated.
  //
  // There is no guarantee about whether the callback is called synchronously or
  // asynchronously.
  void FindReadyRegistrationForIdOnly(int64_t registration_id,
                                      FindRegistrationCallback callback);

  void GetAllRegistrations(GetRegistrationsInfosCallback callback);

  void GetRegistrationUserData(int64_t registration_id,
                               const std::vector<std::string>& keys,
                               GetUserDataCallback callback);
  void GetRegistrationUserDataByKeyPrefix(int64_t registration_id,
                                          const std::string& key_prefix,
                                          GetUserDataCallback callback);
  void GetRegistrationUserKeysAndDataByKeyPrefix(
      int64_t registration_id,
      const std::string& key_prefix,
      GetUserKeysAndDataCallback callback);
  void StoreRegistrationUserData(
      int64_t registration_id,
      const blink::StorageKey& key,
      const std::vector<std::pair<std::string, std::string>>& key_value_pairs,
      StatusCallback callback);
  void ClearRegistrationUserData(int64_t registration_id,
                                 const std::vector<std::string>& keys,
                                 StatusCallback callback);
  void ClearRegistrationUserDataByKeyPrefixes(
      int64_t registration_id,
      const std::vector<std::string>& key_prefixes,
      StatusCallback callback);
  void GetUserDataForAllRegistrations(
      const std::string& key,
      GetUserDataForAllRegistrationsCallback callback);
  void GetUserDataForAllRegistrationsByKeyPrefix(
      const std::string& key_prefix,
      GetUserDataForAllRegistrationsCallback callback);
  void ClearUserDataForAllRegistrationsByKeyPrefix(
      const std::string& key_prefix,
      StatusCallback callback);

  // Returns a list of ServiceWorkerRegistration for `key`. The list includes
  // stored registrations and installing (not stored yet) registrations.
  void GetRegistrationsForStorageKey(const blink::StorageKey& key,
                                     GetRegistrationsCallback callback);

  // Fails with kErrorNotFound if there is no active registration for the given
  // `scope` and `key`. It means that there is no registration at all or that
  // the registration doesn't have an active version yet (which is the case for
  // installing service workers).
  void StartActiveServiceWorker(const GURL& scope,
                                const blink::StorageKey& key,
                                StatusCallback callback);

  void SkipWaitingWorker(const GURL& scope, const blink::StorageKey& key);
  void UpdateRegistration(const GURL& scope, const blink::StorageKey& key);
  void SetForceUpdateOnPageLoad(bool force_update_on_page_load);

  // Different from AddObserver/RemoveObserver(ServiceWorkerContextObserver*).
  // But we must keep the same name, or else base::ScopedObservation breaks.
  void AddObserver(ServiceWorkerContextCoreObserver* observer);
  void RemoveObserver(ServiceWorkerContextCoreObserver* observer);

  // Notifies only synchronous observer
  // `ServiceWorkerContextObserverSynchronous` of all running workers stopped.
  void NotifyRunningServiceWorkerStoppedToSynchronousObserver();

  bool is_incognito() const { return is_incognito_; }

  // Can be null before/during init, during/after shutdown, and after
  // DeleteAndStartOver fails.
  ServiceWorkerContextCore* context();

  void SetLoaderFactoryForUpdateCheckForTest(
      scoped_refptr<network::SharedURLLoaderFactory> loader_factory);
  // Returns nullptr on failure.
  scoped_refptr<network::SharedURLLoaderFactory> GetLoaderFactoryForUpdateCheck(
      const GURL& scope,
      network::mojom::ClientSecurityStatePtr client_security_state);

  // Returns nullptr on failure.
  // Note: This is currently only used for plzServiceWorker.
  scoped_refptr<network::SharedURLLoaderFactory>
  GetLoaderFactoryForMainScriptFetch(
      const GURL& scope,
      int64_t version_id,
      network::mojom::ClientSecurityStatePtr client_security_state);

  // Binds a ServiceWorkerStorageControl.
  void BindStorageControl(
      mojo::PendingReceiver<storage::mojom::ServiceWorkerStorageControl>
          receiver);

  using StorageControlBinder = base::RepeatingCallback<void(
      mojo::PendingReceiver<storage::mojom::ServiceWorkerStorageControl>)>;
  // Sets a callback to bind ServiceWorkerStorageControl for testing.
  void SetStorageControlBinderForTest(StorageControlBinder binder);

  ServiceWorkerContextCore* GetContextCoreForTest() {
    return context_core_.get();
  }

  void SetForceUpdateOnPageLoadForTesting(
      bool force_update_on_page_load) override;

 private:
  friend class BackgroundSyncManagerTest;
  friend class base::DeleteHelper<ServiceWorkerContextWrapper>;
  friend class EmbeddedWorkerBrowserTest;
  friend class EmbeddedWorkerTestHelper;
  friend class FakeServiceWorkerContextWrapper;
  friend class ServiceWorkerClientsApiBrowserTest;
  friend class ServiceWorkerInternalsUI;
  friend class ServiceWorkerMainResourceHandle;
  friend class ServiceWorkerProcessManager;
  friend class ServiceWorkerVersionBrowserTest;
  friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>;

  ~ServiceWorkerContextWrapper() override;

  // Init() with a custom database task runner and BrowserContext. Explicitly
  // called from EmbeddedWorkerTestHelper.
  void InitInternal(
      storage::QuotaManagerProxy* quota_manager_proxy,
      storage::SpecialStoragePolicy* special_storage_policy,
      ChromeBlobStorageContext* blob_context,
      BrowserContext* browser_context);

  // If `include_installing_version` is true, `callback` is called if there is
  // an installing version with no waiting or active version.
  //
  // Not guaranteed to call the callback asynchronously.
  // TODO(falken): Should it?
  void FindRegistrationForScopeImpl(const GURL& scope,
                                    const blink::StorageKey& key,
                                    bool include_installing_version,
                                    FindRegistrationCallback callback);

  // Helper methods for `UnregisterServiceWorker()` and
  // `UnregisterServiceWorkerImmediately()`. `callback` provides the status that
  // was encountered. `blink::ServiceWorkerStatusCode::kOk` means the request to
  // unregister was sent. It does not mean the worker has been fully
  // unregistered though.
  void UnregisterServiceWorkerImpl(const GURL& scope,
                                   const blink::StorageKey& key,
                                   StatusCodeCallback callback);
  void UnregisterServiceWorkerImmediatelyImpl(const GURL& scope,
                                              const blink::StorageKey& key,
                                              StatusCodeCallback callback);

  void MaybeProcessPendingWarmUpRequest();

  void DidFindRegistrationForFindImpl(
      bool include_installing_version,
      FindRegistrationCallback callback,
      blink::ServiceWorkerStatusCode status,
      scoped_refptr<ServiceWorkerRegistration> registration);
  void OnStatusChangedForFindReadyRegistration(
      FindRegistrationCallback callback,
      scoped_refptr<ServiceWorkerRegistration> registration);

  void DidDeleteAndStartOver(blink::ServiceWorkerStatusCode status);

  void DidGetAllRegistrationsForGetAllStorageKeys(
      GetUsageInfoCallback callback,
      blink::ServiceWorkerStatusCode status,
      const std::vector<ServiceWorkerRegistrationInfo>& registrations);

  void DidFindRegistrationForUpdate(
      blink::ServiceWorkerStatusCode status,
      scoped_refptr<content::ServiceWorkerRegistration> registration);

  void DidFindRegistrationForNavigationHint(
      StartServiceWorkerForNavigationHintCallback callback,
      blink::ServiceWorkerStatusCode status,
      scoped_refptr<ServiceWorkerRegistration> registration);

  void DidFindRegistrationForWarmUp(
      WarmUpServiceWorkerCallback callback,
      blink::ServiceWorkerStatusCode status,
      scoped_refptr<ServiceWorkerRegistration> registration);

  void DidStartServiceWorkerForNavigationHint(
      const GURL& scope,
      StartServiceWorkerForNavigationHintCallback callback,
      blink::ServiceWorkerStatusCode code);

  void DidWarmUpServiceWorker(const GURL& scope,
                              WarmUpServiceWorkerCallback callback,
                              blink::ServiceWorkerStatusCode code);

  void DidFindRegistrationForMessageDispatch(
      blink::TransferableMessage message,
      const GURL& source_origin,
      ResultCallback result_callback,
      blink::ServiceWorkerStatusCode service_worker_status,
      scoped_refptr<ServiceWorkerRegistration> registration);

  void DidStartServiceWorkerForMessageDispatch(
      blink::TransferableMessage message,
      const GURL& source_origin,
      scoped_refptr<ServiceWorkerRegistration> registration,
      ServiceWorkerContext::ResultCallback result_callback,
      blink::ServiceWorkerStatusCode service_worker_status);

  std::unique_ptr<blink::PendingURLLoaderFactoryBundle>
  CreateNonNetworkPendingURLLoaderFactoryBundleForUpdateCheck(
      BrowserContext* browser_context);

  // TODO(crbug.com/40820909): Remove. Temporary workaround.
  void StartServiceWorkerAndDispatchMessageOnUIThread(
      const GURL& scope,
      const blink::StorageKey& key,
      blink::TransferableMessage message,
      ResultCallback callback);

  // Clears running workers and notifies `ServiceWorkerContextObservers` of
  // worker stop.
  void ClearRunningServiceWorkers();

  scoped_refptr<network::SharedURLLoaderFactory>
  GetLoaderFactoryForBrowserInitiatedRequest(
      const GURL& scope,
      std::optional<int64_t> version_id,
      network::mojom::ClientSecurityStatePtr client_security_state);

  // Observers of `context_core_` which live within content's implementation
  // boundary. Shared with `context_core_`.
  using ServiceWorkerContextObserverList =
      base::ObserverListThreadSafe<ServiceWorkerContextCoreObserver>;
  const scoped_refptr<ServiceWorkerContextObserverList> core_observer_list_;
  // Observers of `context_core_`, but actually a subset of
  // `ServiceWorkerContextObserver`. Shared with `context_core_`.
  const scoped_refptr<ServiceWorkerContextSynchronousObserverList>
      core_sync_observer_list_;

  // Observers which live outside content's implementation boundary.
  base::ObserverList<ServiceWorkerContextObserver, true>::Unchecked
      observer_list_;

  // `browser_context_` is maintained to be valid within the lifetime of the
  // browser context.
  raw_ptr<BrowserContext, DanglingUntriaged> browser_context_;

  const std::unique_ptr<ServiceWorkerProcessManager> process_manager_;
  std::unique_ptr<ServiceWorkerContextCore> context_core_;

  // Initialized in Init(); true if the user data directory is empty.
  bool is_incognito_ = false;

  // Indicates if we are in the middle of deleting the `context_core_` in
  // order to start over.
  bool is_deleting_and_starting_over_ = false;

  // Raw pointer to the StoragePartitionImpl owning |this|.
  raw_ptr<StoragePartitionImpl, DanglingUntriaged> storage_partition_ = nullptr;

  // Map that contains all service workers that are considered "running". Used
  // to dispatch OnVersionStartedRunning()/OnVersionStoppedRunning() events.
  base::flat_map<int64_t /* version_id */, ServiceWorkerRunningInfo>
      running_service_workers_;

  std::unique_ptr<ServiceWorkerIdentifiabilityMetrics> identifiability_metrics_;

  // TODO(crbug.com/40120038): Remove `storage_control_` when
  // ServiceWorkerStorage is sandboxed. An instance of this impl should live in
  // the storage service, not here.
  std::unique_ptr<storage::ServiceWorkerStorageControlImpl> storage_control_;
  // These fields are used to (re)create `storage_control_`.
  base::FilePath user_data_directory_;
  scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_;

  // A callback to bind ServiceWorkerStorageControl. Used for tests.
  StorageControlBinder storage_control_binder_for_test_;

  // A loader factory used to register a service worker. Used for tests.
  scoped_refptr<network::SharedURLLoaderFactory> loader_factory_for_test_;

 private:
  // Returns a version if the worker is live, otherwise nullptr.
  ServiceWorkerVersion* GetLiveServiceWorker(int64_t service_worker_version_id);
};

}  // namespace content

#endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_WRAPPER_H_