#include "extensions/browser/service_worker/service_worker_task_queue.h"
#include <memory>
#include <optional>
#include <type_traits>
#include <utility>
#include <vector>
#include "base/check.h"
#include "base/containers/contains.h"
#include "base/containers/map_util.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/syslog_logging.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/child_process_security_policy.h"
#include "content/public/browser/console_message.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/service_worker_context.h"
#include "content/public/browser/service_worker_running_info.h"
#include "content/public/browser/storage_partition.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_error.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_util.h"
#include "extensions/browser/extensions_browser_client.h"
#include "extensions/browser/process_manager.h"
#include "extensions/browser/renderer_startup_helper.h"
#include "extensions/browser/service_worker/service_worker_task_queue_factory.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension_features.h"
#include "extensions/common/extension_id.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/manifest_handlers/background_info.h"
#include "extensions/common/manifest_handlers/incognito_info.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/common/tokens/tokens.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration_options.mojom.h"
#include "url/origin.h"
BrowserContext;
namespace extensions {
namespace {
const char kPrefServiceWorkerRegistrationInfo[] = …;
const char kServiceWorkerVersion[] = …;
ServiceWorkerTaskQueue::TestObserver* g_test_observer = …;
bool g_allow_multiple_workers_per_extension = …;
}
ServiceWorkerTaskQueue::ServiceWorkerTaskQueue(BrowserContext* browser_context)
: … { … }
ServiceWorkerTaskQueue::~ServiceWorkerTaskQueue() { … }
ServiceWorkerTaskQueue::WorkerState::WorkerState() = default;
ServiceWorkerTaskQueue::WorkerState::~WorkerState() = default;
void ServiceWorkerTaskQueue::WorkerState::SetWorkerId(
const WorkerId& worker_id,
ProcessManager* process_manager) { … }
bool ServiceWorkerTaskQueue::WorkerState::ready() const { … }
ServiceWorkerTaskQueue::TestObserver::TestObserver() = default;
ServiceWorkerTaskQueue::TestObserver::~TestObserver() = default;
ServiceWorkerTaskQueue* ServiceWorkerTaskQueue::Get(BrowserContext* context) { … }
void ServiceWorkerTaskQueue::DidStartWorkerForScope(
const SequencedContextId& context_id,
base::Time start_time,
int64_t version_id,
int process_id,
int thread_id) { … }
void ServiceWorkerTaskQueue::DidStartWorkerFail(
const SequencedContextId& context_id,
base::Time start_time,
blink::ServiceWorkerStatusCode status_code) { … }
void ServiceWorkerTaskQueue::DidInitializeServiceWorkerContext(
int render_process_id,
const ExtensionId& extension_id,
int64_t service_worker_version_id,
int thread_id,
const blink::ServiceWorkerToken& service_worker_token) { … }
void ServiceWorkerTaskQueue::DidStartServiceWorkerContext(
int render_process_id,
const ExtensionId& extension_id,
const base::UnguessableToken& activation_token,
const GURL& service_worker_scope,
int64_t service_worker_version_id,
int thread_id) { … }
void ServiceWorkerTaskQueue::DidStopServiceWorkerContext(
int render_process_id,
const ExtensionId& extension_id,
const base::UnguessableToken& activation_token,
const GURL& service_worker_scope,
int64_t service_worker_version_id,
int thread_id) { … }
void ServiceWorkerTaskQueue::StopObservingContextForTest(
content::ServiceWorkerContext* service_worker_context) { … }
void ServiceWorkerTaskQueue::SetObserverForTest(TestObserver* observer) { … }
bool ServiceWorkerTaskQueue::ShouldEnqueueTask(
BrowserContext* context,
const Extension* extension) const { … }
bool ServiceWorkerTaskQueue::IsReadyToRunTasks(
content::BrowserContext* context,
const Extension* extension) const { … }
void ServiceWorkerTaskQueue::AddPendingTask(
const LazyContextId& lazy_context_id,
PendingTask task) { … }
void ServiceWorkerTaskQueue::ActivateExtension(const Extension* extension) { … }
void ServiceWorkerTaskQueue::VerifyRegistration(
content::ServiceWorkerContext* service_worker_context,
const SequencedContextId& context_id,
const GURL& scope) { … }
void ServiceWorkerTaskQueue::RegisterServiceWorker(
RegistrationReason reason,
const SequencedContextId& context_id,
const Extension& extension) { … }
void ServiceWorkerTaskQueue::DeactivateExtension(const Extension* extension) { … }
void ServiceWorkerTaskQueue::RunTasksAfterStartWorker(
const SequencedContextId& context_id) { … }
std::vector<ServiceWorkerTaskQueue::PendingTask>*
ServiceWorkerTaskQueue::pending_tasks(const SequencedContextId& context_id) { … }
std::vector<ServiceWorkerTaskQueue::PendingTask>&
ServiceWorkerTaskQueue::GetOrAddPendingTasks(
const SequencedContextId& context_id) { … }
void ServiceWorkerTaskQueue::AddPendingTaskForContext(
PendingTask&& pending_task,
const SequencedContextId& context_id) { … }
void ServiceWorkerTaskQueue::DeleteAllPendingTasks(
const SequencedContextId& context_id) { … }
bool ServiceWorkerTaskQueue::HasPendingTasks(
const SequencedContextId& context_id) { … }
bool ServiceWorkerTaskQueue::ShouldRetryRegistrationRequest(
base::UnguessableToken activation_token) { … }
void ServiceWorkerTaskQueue::DidRegisterServiceWorker(
const SequencedContextId& context_id,
RegistrationReason reason,
base::Time start_time,
blink::ServiceWorkerStatusCode status_code) { … }
void ServiceWorkerTaskQueue::DidUnregisterServiceWorker(
const ExtensionId& extension_id,
const base::UnguessableToken& activation_token,
bool worker_previously_registered,
blink::ServiceWorkerStatusCode status) { … }
base::Version ServiceWorkerTaskQueue::RetrieveRegisteredServiceWorkerVersion(
const ExtensionId& extension_id) { … }
void ServiceWorkerTaskQueue::SetRegisteredServiceWorkerInfo(
const ExtensionId& extension_id,
const base::Version& version) { … }
void ServiceWorkerTaskQueue::RemoveRegisteredServiceWorkerInfo(
const ExtensionId& extension_id) { … }
void ServiceWorkerTaskQueue::RunPendingTasksIfWorkerReady(
const SequencedContextId& context_id) { … }
bool ServiceWorkerTaskQueue::IsCurrentActivation(
const ExtensionId& extension_id,
const base::UnguessableToken& activation_token) const { … }
std::optional<base::UnguessableToken>
ServiceWorkerTaskQueue::GetCurrentActivationToken(
const ExtensionId& extension_id) const { … }
void ServiceWorkerTaskQueue::OnRegistrationStored(int64_t registration_id,
const GURL& scope) { … }
void ServiceWorkerTaskQueue::OnReportConsoleMessage(
int64_t version_id,
const GURL& scope,
const content::ConsoleMessage& message) { … }
void ServiceWorkerTaskQueue::OnDestruct(
content::ServiceWorkerContext* context) { … }
void ServiceWorkerTaskQueue::OnStopped(
int64_t version_id,
const content::ServiceWorkerRunningInfo& worker_info) { … }
size_t ServiceWorkerTaskQueue::GetNumPendingTasksForTest(
const LazyContextId& lazy_context_id) { … }
base::AutoReset<bool>
ServiceWorkerTaskQueue::AllowMultipleWorkersPerExtensionForTesting() { … }
const ServiceWorkerTaskQueue::WorkerState*
ServiceWorkerTaskQueue::GetWorkerState(
const SequencedContextId& context_id) const { … }
ServiceWorkerTaskQueue::WorkerState* ServiceWorkerTaskQueue::GetWorkerState(
const SequencedContextId& context_id) { … }
content::ServiceWorkerContext* ServiceWorkerTaskQueue::GetServiceWorkerContext(
const ExtensionId& extension_id) { … }
void ServiceWorkerTaskQueue::StartObserving(
content::ServiceWorkerContext* service_worker_context) { … }
void ServiceWorkerTaskQueue::StopObserving(
content::ServiceWorkerContext* service_worker_context) { … }
void ServiceWorkerTaskQueue::DidVerifyRegistration(
const SequencedContextId& context_id,
content::ServiceWorkerCapability capability) { … }
void ServiceWorkerTaskQueue::EmitWorkerWillBeStartedHistograms(
const ExtensionId& extension_id) { … }
void ServiceWorkerTaskQueue::ActivateIncognitoSplitModeExtensions(
ServiceWorkerTaskQueue* other) { … }
}