Skip to content

Commit

Permalink
Moving service worker job related code to its own file. (#628)
Browse files Browse the repository at this point in the history
b/286597506

Change-Id: I0ca3ac56d3f1e1acdc6fd2cd8faac34a463b8d61
  • Loading branch information
aee-google authored Jun 23, 2023
1 parent 48939ab commit 6e280c8
Show file tree
Hide file tree
Showing 26 changed files with 2,147 additions and 2,037 deletions.
4 changes: 2 additions & 2 deletions cobalt/browser/browser_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -629,8 +629,8 @@ void BrowserModule::Navigate(const GURL& url_reference) {

options.web_options.web_settings = &web_settings_;
options.web_options.network_module = network_module_;
options.web_options.service_worker_jobs =
service_worker_registry_->service_worker_jobs();
options.web_options.service_worker_context =
service_worker_registry_->service_worker_context();
options.web_options.platform_info = platform_info_.get();
web_module_.reset(new WebModule("MainWebModule"));
// Wait for service worker to start if one exists.
Expand Down
15 changes: 7 additions & 8 deletions cobalt/browser/service_worker_registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "base/threading/thread.h"
#include "base/trace_event/trace_event.h"
#include "cobalt/network/network_module.h"
#include "cobalt/worker/service_worker_jobs.h"

namespace cobalt {
namespace browser {
Expand All @@ -34,7 +33,7 @@ void SignalWaitableEvent(base::WaitableEvent* event) { event->Signal(); }

void ServiceWorkerRegistry::WillDestroyCurrentMessageLoop() {
// Clear all member variables allocated from the thread.
service_worker_jobs_.reset();
service_worker_context_.reset();
}

ServiceWorkerRegistry::ServiceWorkerRegistry(
Expand Down Expand Up @@ -75,28 +74,28 @@ ServiceWorkerRegistry::~ServiceWorkerRegistry() {
destruction_observer_added_.Wait();
DCHECK_NE(thread_.message_loop(), base::MessageLoop::current());
thread_.Stop();
DCHECK(!service_worker_jobs_);
DCHECK(!service_worker_context_);
}

void ServiceWorkerRegistry::EnsureServiceWorkerStarted(
const url::Origin& storage_key, const GURL& client_url,
base::WaitableEvent* done_event) {
service_worker_jobs()->EnsureServiceWorkerStarted(storage_key, client_url,
done_event);
service_worker_context()->EnsureServiceWorkerStarted(storage_key, client_url,
done_event);
}

worker::ServiceWorkerJobs* ServiceWorkerRegistry::service_worker_jobs() {
worker::ServiceWorkerContext* ServiceWorkerRegistry::service_worker_context() {
// Ensure that the thread had a chance to allocate the object.
destruction_observer_added_.Wait();
return service_worker_jobs_.get();
return service_worker_context_.get();
}

void ServiceWorkerRegistry::Initialize(
web::WebSettings* web_settings, network::NetworkModule* network_module,
web::UserAgentPlatformInfo* platform_info, const GURL& url) {
TRACE_EVENT0("cobalt::browser", "ServiceWorkerRegistry::Initialize()");
DCHECK_EQ(base::MessageLoop::current(), message_loop());
service_worker_jobs_.reset(new worker::ServiceWorkerJobs(
service_worker_context_.reset(new worker::ServiceWorkerContext(
web_settings, network_module, platform_info, message_loop(), url));
}

Expand Down
6 changes: 3 additions & 3 deletions cobalt/browser/service_worker_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "base/threading/thread.h"
#include "cobalt/network/network_module.h"
#include "cobalt/web/web_settings.h"
#include "cobalt/worker/service_worker_jobs.h"
#include "cobalt/worker/service_worker_context.h"

namespace cobalt {
namespace browser {
Expand All @@ -48,7 +48,7 @@ class ServiceWorkerRegistry : public base::MessageLoop::DestructionObserver {
const GURL& client_url,
base::WaitableEvent* done_event);

worker::ServiceWorkerJobs* service_worker_jobs();
worker::ServiceWorkerContext* service_worker_context();

private:
// Called by the constructor to perform any other initialization required on
Expand All @@ -69,7 +69,7 @@ class ServiceWorkerRegistry : public base::MessageLoop::DestructionObserver {
base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED};

std::unique_ptr<worker::ServiceWorkerJobs> service_worker_jobs_;
std::unique_ptr<worker::ServiceWorkerContext> service_worker_context_;
};

} // namespace browser
Expand Down
4 changes: 2 additions & 2 deletions cobalt/layout_tests/web_platform_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ std::string RunWebPlatformTest(const GURL& url, bool* got_results) {

web_module_options.web_options.web_settings = &web_settings;
web_module_options.web_options.network_module = &network_module;
web_module_options.web_options.service_worker_jobs =
service_worker_registry->service_worker_jobs();
web_module_options.web_options.service_worker_context =
service_worker_registry->service_worker_context();
web_module_options.web_options.platform_info = platform_info.get();

// Prepare a slot for our results to be placed when ready.
Expand Down
22 changes: 10 additions & 12 deletions cobalt/web/agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#include "cobalt/web/url.h"
#include "cobalt/web/window_or_worker_global_scope.h"
#include "cobalt/worker/service_worker.h"
#include "cobalt/worker/service_worker_jobs.h"
#include "cobalt/worker/service_worker_context.h"
#include "cobalt/worker/service_worker_object.h"
#include "cobalt/worker/service_worker_registration.h"
#include "cobalt/worker/service_worker_registration_object.h"
Expand Down Expand Up @@ -91,8 +91,8 @@ class Impl : public Context {
DCHECK(fetcher_factory_);
return fetcher_factory_->network_module();
}
worker::ServiceWorkerJobs* service_worker_jobs() const final {
return service_worker_jobs_;
worker::ServiceWorkerContext* service_worker_context() const final {
return service_worker_context_;
}

const std::string& name() const final { return name_; };
Expand Down Expand Up @@ -217,7 +217,7 @@ class Impl : public Context {
std::map<worker::ServiceWorkerObject*, scoped_refptr<worker::ServiceWorker>>
service_worker_object_map_;

worker::ServiceWorkerJobs* service_worker_jobs_;
worker::ServiceWorkerContext* service_worker_context_;
const web::UserAgentPlatformInfo* platform_info_;

// https://html.spec.whatwg.org/multipage/webappapis.html#concept-environment-active-service-worker
Expand Down Expand Up @@ -250,7 +250,7 @@ void LogScriptError(const base::SourceLocation& source_location,
Impl::Impl(const std::string& name, const Agent::Options& options)
: name_(name), web_settings_(options.web_settings) {
TRACE_EVENT0("cobalt::web", "Agent::Impl::Impl()");
service_worker_jobs_ = options.service_worker_jobs;
service_worker_context_ = options.service_worker_context;
platform_info_ = options.platform_info;
blob_registry_.reset(new Blob::Registry);

Expand Down Expand Up @@ -352,11 +352,9 @@ void Impl::SetupFinished() {
}
#endif

if (service_worker_jobs_) {
service_worker_jobs_->RegisterWebContext(this);
}
if (service_worker_jobs_) {
service_worker_jobs_->SetActiveWorker(environment_settings_.get());
if (service_worker_context_) {
service_worker_context_->RegisterWebContext(this);
service_worker_context_->SetActiveWorker(environment_settings_.get());
}
}

Expand Down Expand Up @@ -533,8 +531,8 @@ void Agent::Stop() {
DCHECK(message_loop());
DCHECK(thread_.IsRunning());

if (context() && context()->service_worker_jobs()) {
context()->service_worker_jobs()->UnregisterWebContext(context());
if (context() && context()->service_worker_context()) {
context()->service_worker_context()->UnregisterWebContext(context());
}

// Ensure that the destruction observer got added before stopping the thread.
Expand Down
4 changes: 2 additions & 2 deletions cobalt/web/agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

namespace cobalt {
namespace worker {
class ServiceWorkerJobs;
class ServiceWorkerContext;
}
namespace web {

Expand Down Expand Up @@ -69,7 +69,7 @@ class Agent : public base::MessageLoop::DestructionObserver {
base::Callback<int(const std::string&, std::unique_ptr<char[]>*)>
read_cache_callback;

worker::ServiceWorkerJobs* service_worker_jobs = nullptr;
worker::ServiceWorkerContext* service_worker_context = nullptr;

const UserAgentPlatformInfo* platform_info = nullptr;
};
Expand Down
4 changes: 2 additions & 2 deletions cobalt/web/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace worker {
class ServiceWorkerRegistration;
class ServiceWorkerRegistrationObject;
class ServiceWorker;
class ServiceWorkerJobs;
class ServiceWorkerContext;
class ServiceWorkerObject;
} // namespace worker
namespace web {
Expand Down Expand Up @@ -65,7 +65,7 @@ class Context {
virtual Blob::Registry* blob_registry() const = 0;
virtual web::WebSettings* web_settings() const = 0;
virtual network::NetworkModule* network_module() const = 0;
virtual worker::ServiceWorkerJobs* service_worker_jobs() const = 0;
virtual worker::ServiceWorkerContext* service_worker_context() const = 0;

virtual const std::string& name() const = 0;
virtual void SetupEnvironmentSettings(EnvironmentSettings* settings) = 0;
Expand Down
2 changes: 1 addition & 1 deletion cobalt/web/testing/stub_web_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class StubWebContext final : public Context {
return network_module_.get();
}

worker::ServiceWorkerJobs* service_worker_jobs() const final {
worker::ServiceWorkerContext* service_worker_context() const final {
NOTREACHED();
return nullptr;
}
Expand Down
2 changes: 2 additions & 0 deletions cobalt/worker/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ static_library("worker") {
"service_worker_consts.h",
"service_worker_container.cc",
"service_worker_container.h",
"service_worker_context.cc",
"service_worker_context.h",
"service_worker_global_scope.cc",
"service_worker_global_scope.h",
"service_worker_jobs.cc",
Expand Down
34 changes: 19 additions & 15 deletions cobalt/worker/clients.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
#include "cobalt/web/dom_exception.h"
#include "cobalt/web/environment_settings.h"
#include "cobalt/worker/client.h"
#include "cobalt/worker/service_worker_context.h"
#include "cobalt/worker/service_worker_global_scope.h"
#include "cobalt/worker/service_worker_jobs.h"
#include "cobalt/worker/service_worker_object.h"

namespace cobalt {
Expand Down Expand Up @@ -72,12 +72,13 @@ script::HandlePromiseWrappable Clients::Get(const std::string& id) {
settings_->context()->GetWindowOrWorkerGlobalScope(), promise));

// 2. Run these substeps in parallel:
ServiceWorkerJobs* jobs = settings_->context()->service_worker_jobs();
DCHECK(jobs);
jobs->message_loop()->task_runner()->PostTask(
ServiceWorkerContext* context =
settings_->context()->service_worker_context();
DCHECK(context);
context->message_loop()->task_runner()->PostTask(
FROM_HERE,
base::BindOnce(&ServiceWorkerJobs::ClientsGetSubSteps,
base::Unretained(jobs),
base::BindOnce(&ServiceWorkerContext::ClientsGetSubSteps,
base::Unretained(context),
base::Unretained(settings_->context()),
base::Unretained(GetAssociatedServiceWorker(settings_)),
std::move(promise_reference), id));
Expand All @@ -101,12 +102,13 @@ script::HandlePromiseSequenceWrappable Clients::MatchAll(
promise_reference(new script::ValuePromiseSequenceWrappable::Reference(
settings_->context()->GetWindowOrWorkerGlobalScope(), promise));
// 2. Run the following steps in parallel:
ServiceWorkerJobs* jobs = settings_->context()->service_worker_jobs();
DCHECK(jobs);
jobs->message_loop()->task_runner()->PostTask(
ServiceWorkerContext* context =
settings_->context()->service_worker_context();
DCHECK(context);
context->message_loop()->task_runner()->PostTask(
FROM_HERE,
base::BindOnce(&ServiceWorkerJobs::ClientsMatchAllSubSteps,
base::Unretained(jobs),
base::BindOnce(&ServiceWorkerContext::ClientsMatchAllSubSteps,
base::Unretained(context),
base::Unretained(settings_->context()),
base::Unretained(GetAssociatedServiceWorker(settings_)),
std::move(promise_reference),
Expand Down Expand Up @@ -156,11 +158,13 @@ script::HandlePromiseVoid Clients::Claim() {
service_worker->state() == kServiceWorkerStateActivating);

// 3. Run the following substeps in parallel:
ServiceWorkerJobs* jobs = settings_->context()->service_worker_jobs();
DCHECK(jobs);
jobs->message_loop()->task_runner()->PostTask(
ServiceWorkerContext* context =
settings_->context()->service_worker_context();
DCHECK(context);
context->message_loop()->task_runner()->PostTask(
FROM_HERE,
base::BindOnce(&ServiceWorkerJobs::ClaimSubSteps, base::Unretained(jobs),
base::BindOnce(&ServiceWorkerContext::ClaimSubSteps,
base::Unretained(context),
base::Unretained(settings_->context()),
base::Unretained(GetAssociatedServiceWorker(settings_)),
std::move(promise_reference)));
Expand Down
4 changes: 2 additions & 2 deletions cobalt/worker/dedicated_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ void DedicatedWorker::Initialize(script::ExceptionState* exception_state) {
options.outside_event_target = this;
options.outside_port = outside_port_.get();
options.options = worker_options_;
options.web_options.service_worker_jobs =
options.outside_context->service_worker_jobs();
options.web_options.service_worker_context =
options.outside_context->service_worker_context();
// Store the current source location as the construction location, to be used
// in the error event if worker loading of initialization fails.
auto stack_trace =
Expand Down
9 changes: 5 additions & 4 deletions cobalt/worker/extendable_event.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@ void ExtendableEvent::StateChange(
web::Context* context =
base::polymorphic_downcast<web::EnvironmentSettings*>(settings)
->context();
ServiceWorkerJobs* jobs = context->service_worker_jobs();
DCHECK(jobs);
ServiceWorkerContext* worker_context = context->service_worker_context();
DCHECK(worker_context);
// 5.2.1. Let registration be the current global object's associated
// service worker's containing service worker registration.
jobs->message_loop()->task_runner()->PostTask(
worker_context->message_loop()->task_runner()->PostTask(
FROM_HERE,
base::BindOnce(
&ServiceWorkerJobs::WaitUntilSubSteps, base::Unretained(jobs),
&ServiceWorkerContext::WaitUntilSubSteps,
base::Unretained(worker_context),
base::Unretained(context->GetWindowOrWorkerGlobalScope()
->AsServiceWorker()
->service_worker_object()
Expand Down
2 changes: 1 addition & 1 deletion cobalt/worker/extendable_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
#include "cobalt/web/event.h"
#include "cobalt/web/window_or_worker_global_scope.h"
#include "cobalt/worker/extendable_event_init.h"
#include "cobalt/worker/service_worker_context.h"
#include "cobalt/worker/service_worker_global_scope.h"
#include "cobalt/worker/service_worker_jobs.h"
#include "cobalt/worker/service_worker_object.h"
#include "cobalt/worker/service_worker_registration_object.h"
#include "v8/include/v8.h"
Expand Down
15 changes: 8 additions & 7 deletions cobalt/worker/service_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
#include "cobalt/web/environment_settings_helper.h"
#include "cobalt/web/event_target.h"
#include "cobalt/web/message_port.h"
#include "cobalt/worker/service_worker_context.h"
#include "cobalt/worker/service_worker_global_scope.h"
#include "cobalt/worker/service_worker_jobs.h"
#include "cobalt/worker/service_worker_object.h"
#include "cobalt/worker/service_worker_state.h"

Expand Down Expand Up @@ -56,13 +56,14 @@ void ServiceWorker::PostMessage(const script::ValueHandleHolder& message) {
// "message" and serviceWorker is true, then return.
if (service_worker->ShouldSkipEvent(base::Tokens::message())) return;
// 6. Run these substeps in parallel:
ServiceWorkerJobs* jobs =
incumbent_settings->context()->service_worker_jobs();
DCHECK(jobs);
jobs->message_loop()->task_runner()->PostTask(
ServiceWorkerContext* worker_context =
incumbent_settings->context()->service_worker_context();
DCHECK(worker_context);
worker_context->message_loop()->task_runner()->PostTask(
FROM_HERE,
base::BindOnce(&ServiceWorkerJobs::ServiceWorkerPostMessageSubSteps,
base::Unretained(jobs), base::Unretained(service_worker),
base::BindOnce(&ServiceWorkerContext::ServiceWorkerPostMessageSubSteps,
base::Unretained(worker_context),
base::Unretained(service_worker),
base::Unretained(incumbent_settings->context()),
std::move(structured_clone)));
}
Expand Down
Loading

0 comments on commit 6e280c8

Please sign in to comment.