chromium/content/browser/service_worker/service_worker_context_core.h

// 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_CORE_H_
#define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_CORE_H_

#include <stdint.h>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <vector>

#include "base/containers/id_map.h"
#include "base/functional/callback.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list_threadsafe.h"
#include "base/observer_list_types.h"
#include "components/services/storage/public/mojom/quota_client.mojom.h"
#include "components/services/storage/public/mojom/service_worker_storage_control.mojom.h"
#include "content/browser/service_worker/service_worker_info.h"
#include "content/browser/service_worker/service_worker_process_manager.h"
#include "content/browser/service_worker/service_worker_registration_status.h"
#include "content/browser/service_worker/service_worker_registry.h"
#include "content/common/content_export.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/browser/service_worker_context.h"
#include "mojo/public/cpp/bindings/associated_receiver_set.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration.mojom-forward.h"

class GURL;

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

namespace content {
class ServiceWorkerContainerHostForClient;
class ServiceWorkerContextCoreObserver;
class ServiceWorkerContextWrapper;
class ServiceWorkerJobCoordinator;
class ServiceWorkerQuotaClient;
class ServiceWorkerRegistration;
struct ServiceWorkerContextSynchronousObserverList;

#if !BUILDFLAG(IS_ANDROID)
class ServiceWorkerHidDelegateObserver;
class ServiceWorkerUsbDelegateObserver;
#endif  // !BUILDFLAG(IS_ANDROID)

// A smart pointer of `ServiceWorkerClient`.
//
// - If `CommitResponseAndRelease()` is not called, this works as a
//   semi-strong reference:
//   - Keeps the underlying `ServiceWorkerClient` alive unless its
//     `ServiceWorkerClientOwner` is destroyed.
//   - Destroys the `ServiceWorkerClient` synchronously in the
//     `ScopedServiceWorkerClient` destructor.
//   - Actually the `ServiceWorkerClient` is owned by `ServiceWorkerClientOwner`
//     and `ServiceWorkerClientOwner::OnContainerHostReceiverDisconnected()` is
//     never called until `ServiceWorkerClientOwner::BindHost()` is called (i.e.
//     `ScopedServiceWorkerClient::CommitResponseAndRelease` is called),
//     and thus there is nothing explicitly to do to keep-alive it by
//     `ScopedServiceWorkerClient`, and the destructor of
//     `ScopedServiceWorkerClient` explicitly destroys the client when
//     `CommitResponseAndRelease()` hasn't been called.
// - After `CommitResponseAndRelease()` is called, this works as a weak
//   reference:
//   - No longer keeps alive nor destroys the `ServiceWorkerClient`. Instead,
//     the returned object from `CommitResponseAndRelease()` keeps it alive
//     (i.e. until
//     `ServiceWorkerClientOwner::OnContainerHostReceiverDisconnected()` is
//     called)
//   - `service_worker_client_` is NOT cleared and still can be used.
class CONTENT_EXPORT ScopedServiceWorkerClient final {};

// A class responsible for `ServiceWorkerClient` management, including its
// ownership, lifetime, and client ID updates.
// This is always owned by and associated with a `ServiceWorkerContextCore`.
// This is split from `ServiceWorkerContextCore` to allow `ServiceWorkerClient`
// to access `ServiceWorkerClientOwner` throughout the lifetime of
// `ServiceWorkerClient` while disallow access to other parts of
// `ServiceWorkerContextCore` after `DeleteAndStartOver()`.
// Callers other than `ServiceWorkerClient` /
// `ServiceWorkerContainerHostForClient` should access this through
// `ServiceWorkerContextCore::service_worker_client_owner()`.
class CONTENT_EXPORT ServiceWorkerClientOwner final {};

// This class manages data associated with service workers.
// The class is single threaded and should only be used on the UI thread.
// In chromium, there is one instance per storagepartition. This class
// is the root of the containment hierarchy for service worker data
// associated with a particular partition.
class CONTENT_EXPORT ServiceWorkerContextCore
    : public ServiceWorkerVersion::Observer {};

}  // namespace content

#endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_CORE_H_