Commit bc69651f authored by Kenichi Ishibashi's avatar Kenichi Ishibashi Committed by Commit Bot

service worker: Add ServiceWorkerRegistry::CreateNewRegistration()

The purpose of this change is to have a single point of creating
ServiceWorkerRegistration. This way,
* We can hide NewRegistrationId() inside ServiceWorkerRegistry.
  This is useful because we plan to move id management into the
  Storage Service and we don't want to expose it other than
  ServiceWorkerRegistry.
* ServiceWorkerRegistry can track all live registrations. In
  subsequent CLs we plan to hook ServiceWorkerRegistration creation
  so that ServiceWorkerRegistry can detect if the registration is
  stored or not.

Bug: 1039200
Change-Id: Ieeeb4c3b156488de8fe6a527515ec23998c5ea3e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2010599
Commit-Queue: Kenichi Ishibashi <bashi@chromium.org>
Reviewed-by: default avatarHiroki Nakagawa <nhiroki@chromium.org>
Cr-Commit-Position: refs/heads/master@{#733542}
parent 8c919071
...@@ -118,9 +118,7 @@ class EmbeddedWorkerInstanceTest : public testing::Test, ...@@ -118,9 +118,7 @@ class EmbeddedWorkerInstanceTest : public testing::Test,
RegistrationAndVersionPair pair; RegistrationAndVersionPair pair;
blink::mojom::ServiceWorkerRegistrationOptions options; blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = scope; options.scope = scope;
pair.first = base::MakeRefCounted<ServiceWorkerRegistration>( pair.first = context()->registry()->CreateNewRegistration(options);
options, context()->storage()->NewRegistrationId(),
context()->AsWeakPtr());
pair.second = base::MakeRefCounted<ServiceWorkerVersion>( pair.second = base::MakeRefCounted<ServiceWorkerVersion>(
pair.first.get(), script_url, blink::mojom::ScriptType::kClassic, pair.first.get(), script_url, blink::mojom::ScriptType::kClassic,
context()->storage()->NewVersionId(), context()->AsWeakPtr()); context()->storage()->NewVersionId(), context()->AsWeakPtr());
......
...@@ -342,8 +342,7 @@ class ServiceWorkerNavigationLoaderTest : public testing::Test { ...@@ -342,8 +342,7 @@ class ServiceWorkerNavigationLoaderTest : public testing::Test {
blink::mojom::ServiceWorkerRegistrationOptions options; blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = GURL("https://example.com/"); options.scope = GURL("https://example.com/");
registration_ = registration_ =
new ServiceWorkerRegistration(options, storage()->NewRegistrationId(), helper_->context()->registry()->CreateNewRegistration(options);
helper_->context()->AsWeakPtr());
version_ = new ServiceWorkerVersion( version_ = new ServiceWorkerVersion(
registration_.get(), GURL("https://example.com/service_worker.js"), registration_.get(), GURL("https://example.com/service_worker.js"),
blink::mojom::ScriptType::kClassic, storage()->NewVersionId(), blink::mojom::ScriptType::kClassic, storage()->NewVersionId(),
......
...@@ -180,9 +180,7 @@ class ServiceWorkerNewScriptLoaderTest : public testing::Test { ...@@ -180,9 +180,7 @@ class ServiceWorkerNewScriptLoaderTest : public testing::Test {
void SetUpRegistrationWithOptions( void SetUpRegistrationWithOptions(
const GURL& script_url, const GURL& script_url,
blink::mojom::ServiceWorkerRegistrationOptions options) { blink::mojom::ServiceWorkerRegistrationOptions options) {
registration_ = base::MakeRefCounted<ServiceWorkerRegistration>( registration_ = context()->registry()->CreateNewRegistration(options);
options, context()->storage()->NewRegistrationId(),
context()->AsWeakPtr());
SetUpVersion(script_url); SetUpVersion(script_url);
} }
......
...@@ -116,9 +116,8 @@ class ServiceWorkerObjectHostTest : public testing::Test { ...@@ -116,9 +116,8 @@ class ServiceWorkerObjectHostTest : public testing::Test {
blink::mojom::ServiceWorkerRegistrationOptions options; blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = scope; options.scope = scope;
registration_ = new ServiceWorkerRegistration( registration_ =
options, helper_->context()->storage()->NewRegistrationId(), helper_->context()->registry()->CreateNewRegistration(options);
helper_->context()->AsWeakPtr());
version_ = new ServiceWorkerVersion( version_ = new ServiceWorkerVersion(
registration_.get(), script_url, blink::mojom::ScriptType::kClassic, registration_.get(), script_url, blink::mojom::ScriptType::kClassic,
helper_->context()->storage()->NewVersionId(), helper_->context()->storage()->NewVersionId(),
......
...@@ -422,16 +422,16 @@ void ServiceWorkerRegisterJob::OnUpdateCheckFinished( ...@@ -422,16 +422,16 @@ void ServiceWorkerRegisterJob::OnUpdateCheckFinished(
void ServiceWorkerRegisterJob::RegisterAndContinue() { void ServiceWorkerRegisterJob::RegisterAndContinue() {
SetPhase(REGISTER); SetPhase(REGISTER);
int64_t registration_id = context_->storage()->NewRegistrationId(); blink::mojom::ServiceWorkerRegistrationOptions options(
if (registration_id == blink::mojom::kInvalidServiceWorkerRegistrationId) { scope_, worker_script_type_, update_via_cache_);
scoped_refptr<ServiceWorkerRegistration> new_registration =
context_->registry()->CreateNewRegistration(options);
if (!new_registration) {
Complete(blink::ServiceWorkerStatusCode::kErrorAbort); Complete(blink::ServiceWorkerStatusCode::kErrorAbort);
return; return;
} }
blink::mojom::ServiceWorkerRegistrationOptions options( set_registration(std::move(new_registration));
scope_, worker_script_type_, update_via_cache_);
set_registration(new ServiceWorkerRegistration(options, registration_id,
context_->AsWeakPtr()));
AddRegistrationToMatchingProviderHosts(registration()); AddRegistrationToMatchingProviderHosts(registration());
UpdateAndContinue(); UpdateAndContinue();
} }
......
...@@ -71,6 +71,8 @@ class CONTENT_EXPORT ServiceWorkerRegistration ...@@ -71,6 +71,8 @@ class CONTENT_EXPORT ServiceWorkerRegistration
kUninstalled, kUninstalled,
}; };
// The constructor should be called only from ServiceWorkerRegistry other than
// tests.
ServiceWorkerRegistration( ServiceWorkerRegistration(
const blink::mojom::ServiceWorkerRegistrationOptions& options, const blink::mojom::ServiceWorkerRegistrationOptions& options,
int64_t registration_id, int64_t registration_id,
......
...@@ -345,8 +345,7 @@ TEST_F(ServiceWorkerRegistrationTest, NavigationPreload) { ...@@ -345,8 +345,7 @@ TEST_F(ServiceWorkerRegistrationTest, NavigationPreload) {
blink::mojom::ServiceWorkerRegistrationOptions options; blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = kScope; options.scope = kScope;
scoped_refptr<ServiceWorkerRegistration> registration = scoped_refptr<ServiceWorkerRegistration> registration =
base::MakeRefCounted<ServiceWorkerRegistration>( context()->registry()->CreateNewRegistration(options);
options, storage()->NewRegistrationId(), context()->AsWeakPtr());
scoped_refptr<ServiceWorkerVersion> version_1 = scoped_refptr<ServiceWorkerVersion> version_1 =
base::MakeRefCounted<ServiceWorkerVersion>( base::MakeRefCounted<ServiceWorkerVersion>(
registration.get(), kScript, blink::mojom::ScriptType::kClassic, registration.get(), kScript, blink::mojom::ScriptType::kClassic,
...@@ -394,8 +393,7 @@ class ServiceWorkerActivationTest : public ServiceWorkerRegistrationTest, ...@@ -394,8 +393,7 @@ class ServiceWorkerActivationTest : public ServiceWorkerRegistrationTest,
blink::mojom::ServiceWorkerRegistrationOptions options; blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = kScope; options.scope = kScope;
registration_ = base::MakeRefCounted<ServiceWorkerRegistration>( registration_ = context()->registry()->CreateNewRegistration(options);
options, storage()->NewRegistrationId(), context()->AsWeakPtr());
// Create an active version. // Create an active version.
scoped_refptr<ServiceWorkerVersion> version_1 = scoped_refptr<ServiceWorkerVersion> version_1 =
...@@ -881,12 +879,11 @@ class ServiceWorkerRegistrationObjectHostTest ...@@ -881,12 +879,11 @@ class ServiceWorkerRegistrationObjectHostTest
return status.value(); return status.value();
} }
scoped_refptr<ServiceWorkerRegistration> CreateRegistration( scoped_refptr<ServiceWorkerRegistration> CreateNewRegistration(
const GURL& scope) { const GURL& scope) {
blink::mojom::ServiceWorkerRegistrationOptions options; blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = scope; options.scope = scope;
return base::MakeRefCounted<ServiceWorkerRegistration>( return context()->registry()->CreateNewRegistration(options);
options, storage()->NewRegistrationId(), context()->AsWeakPtr());
} }
scoped_refptr<ServiceWorkerVersion> CreateVersion( scoped_refptr<ServiceWorkerVersion> CreateVersion(
...@@ -914,7 +911,7 @@ class ServiceWorkerRegistrationObjectHostTest ...@@ -914,7 +911,7 @@ class ServiceWorkerRegistrationObjectHostTest
// Prepare ServiceWorkerRegistration and ServiceWorkerVersion. // Prepare ServiceWorkerRegistration and ServiceWorkerVersion.
scoped_refptr<ServiceWorkerRegistration> registration = scoped_refptr<ServiceWorkerRegistration> registration =
CreateRegistration(scope); CreateNewRegistration(scope);
scoped_refptr<ServiceWorkerVersion> version = scoped_refptr<ServiceWorkerVersion> version =
CreateVersion(registration.get(), script_url); CreateVersion(registration.get(), script_url);
...@@ -1113,7 +1110,7 @@ TEST_P(ServiceWorkerRegistrationObjectHostUpdateTest, ...@@ -1113,7 +1110,7 @@ TEST_P(ServiceWorkerRegistrationObjectHostUpdateTest,
const GURL kScope("https://www.example.com/"); const GURL kScope("https://www.example.com/");
const GURL kScriptUrl("https://www.example.com/sw.js"); const GURL kScriptUrl("https://www.example.com/sw.js");
scoped_refptr<ServiceWorkerRegistration> registration = scoped_refptr<ServiceWorkerRegistration> registration =
CreateRegistration(kScope); CreateNewRegistration(kScope);
scoped_refptr<ServiceWorkerVersion> version = scoped_refptr<ServiceWorkerVersion> version =
CreateVersion(registration.get(), kScriptUrl); CreateVersion(registration.get(), kScriptUrl);
...@@ -1144,7 +1141,7 @@ TEST_P(ServiceWorkerRegistrationObjectHostUpdateTest, ...@@ -1144,7 +1141,7 @@ TEST_P(ServiceWorkerRegistrationObjectHostUpdateTest,
const GURL kScope("https://www.example.com/"); const GURL kScope("https://www.example.com/");
const GURL kScriptUrl("https://www.example.com/sw.js"); const GURL kScriptUrl("https://www.example.com/sw.js");
scoped_refptr<ServiceWorkerRegistration> registration = scoped_refptr<ServiceWorkerRegistration> registration =
CreateRegistration(kScope); CreateNewRegistration(kScope);
scoped_refptr<ServiceWorkerVersion> version = scoped_refptr<ServiceWorkerVersion> version =
CreateVersion(registration.get(), kScriptUrl); CreateVersion(registration.get(), kScriptUrl);
......
...@@ -64,6 +64,16 @@ ServiceWorkerRegistry::ServiceWorkerRegistry( ...@@ -64,6 +64,16 @@ ServiceWorkerRegistry::ServiceWorkerRegistry(
ServiceWorkerRegistry::~ServiceWorkerRegistry() = default; ServiceWorkerRegistry::~ServiceWorkerRegistry() = default;
scoped_refptr<ServiceWorkerRegistration>
ServiceWorkerRegistry::CreateNewRegistration(
blink::mojom::ServiceWorkerRegistrationOptions options) {
int64_t registration_id = storage()->NewRegistrationId();
if (registration_id == blink::mojom::kInvalidServiceWorkerRegistrationId)
return nullptr;
return base::MakeRefCounted<ServiceWorkerRegistration>(
std::move(options), registration_id, context_->AsWeakPtr());
}
void ServiceWorkerRegistry::FindRegistrationForClientUrl( void ServiceWorkerRegistry::FindRegistrationForClientUrl(
const GURL& client_url, const GURL& client_url,
FindRegistrationCallback callback) { FindRegistrationCallback callback) {
......
...@@ -68,6 +68,12 @@ class CONTENT_EXPORT ServiceWorkerRegistry { ...@@ -68,6 +68,12 @@ class CONTENT_EXPORT ServiceWorkerRegistry {
ServiceWorkerStorage* storage() const { return storage_.get(); } ServiceWorkerStorage* storage() const { return storage_.get(); }
// Creates a new in-memory representation of registration. Can be null when
// storage is disabled. This method must be called after storage is
// initialized.
scoped_refptr<ServiceWorkerRegistration> CreateNewRegistration(
blink::mojom::ServiceWorkerRegistrationOptions options);
// TODO(crbug.com/1039200): Move corresponding comments from // TODO(crbug.com/1039200): Move corresponding comments from
// ServiceWorkerStorage. // ServiceWorkerStorage.
void FindRegistrationForClientUrl(const GURL& client_url, void FindRegistrationForClientUrl(const GURL& client_url,
......
...@@ -355,8 +355,7 @@ CreateServiceWorkerRegistrationAndVersion(ServiceWorkerContextCore* context, ...@@ -355,8 +355,7 @@ CreateServiceWorkerRegistrationAndVersion(ServiceWorkerContextCore* context,
blink::mojom::ServiceWorkerRegistrationOptions options; blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = scope; options.scope = scope;
auto registration = base::MakeRefCounted<ServiceWorkerRegistration>( auto registration = context->registry()->CreateNewRegistration(options);
options, storage->NewRegistrationId(), context->AsWeakPtr());
auto version = base::MakeRefCounted<ServiceWorkerVersion>( auto version = base::MakeRefCounted<ServiceWorkerVersion>(
registration.get(), script, blink::mojom::ScriptType::kClassic, registration.get(), script, blink::mojom::ScriptType::kClassic,
storage->NewVersionId(), context->AsWeakPtr()); storage->NewVersionId(), context->AsWeakPtr());
......
...@@ -98,9 +98,7 @@ class ServiceWorkerUpdatedScriptLoaderTest : public testing::Test { ...@@ -98,9 +98,7 @@ class ServiceWorkerUpdatedScriptLoaderTest : public testing::Test {
void SetUpRegistrationWithOptions( void SetUpRegistrationWithOptions(
const GURL& script_url, const GURL& script_url,
blink::mojom::ServiceWorkerRegistrationOptions options) { blink::mojom::ServiceWorkerRegistrationOptions options) {
registration_ = base::MakeRefCounted<ServiceWorkerRegistration>( registration_ = context()->registry()->CreateNewRegistration(options);
options, context()->storage()->NewRegistrationId(),
context()->AsWeakPtr());
SetUpVersion(script_url); SetUpVersion(script_url);
} }
......
...@@ -433,9 +433,8 @@ class ServiceWorkerVersionBrowserTest : public ContentBrowserTest { ...@@ -433,9 +433,8 @@ class ServiceWorkerVersionBrowserTest : public ContentBrowserTest {
blink::mojom::ServiceWorkerRegistrationOptions options; blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = scope; options.scope = scope;
options.type = script_type; options.type = script_type;
registration_ = new ServiceWorkerRegistration( registration_ =
options, wrapper()->context()->storage()->NewRegistrationId(), wrapper()->context()->registry()->CreateNewRegistration(options);
wrapper()->context()->AsWeakPtr());
// Set the update check time to avoid triggering updates in the middle of // Set the update check time to avoid triggering updates in the middle of
// tests. // tests.
registration_->set_last_update_check(base::Time::Now()); registration_->set_last_update_check(base::Time::Now());
......
...@@ -90,9 +90,8 @@ class ServiceWorkerVersionTest : public testing::Test { ...@@ -90,9 +90,8 @@ class ServiceWorkerVersionTest : public testing::Test {
scope_ = GURL("https://www.example.com/test/"); scope_ = GURL("https://www.example.com/test/");
blink::mojom::ServiceWorkerRegistrationOptions options; blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = scope_; options.scope = scope_;
registration_ = new ServiceWorkerRegistration( registration_ =
options, helper_->context()->storage()->NewRegistrationId(), helper_->context()->registry()->CreateNewRegistration(options);
helper_->context()->AsWeakPtr());
version_ = new ServiceWorkerVersion( version_ = new ServiceWorkerVersion(
registration_.get(), registration_.get(),
GURL("https://www.example.com/test/service_worker.js"), GURL("https://www.example.com/test/service_worker.js"),
...@@ -1115,9 +1114,8 @@ TEST_F(ServiceWorkerVersionTest, BadOrigin) { ...@@ -1115,9 +1114,8 @@ TEST_F(ServiceWorkerVersionTest, BadOrigin) {
const GURL scope("bad-origin://www.example.com/test/"); const GURL scope("bad-origin://www.example.com/test/");
blink::mojom::ServiceWorkerRegistrationOptions options; blink::mojom::ServiceWorkerRegistrationOptions options;
options.scope = scope; options.scope = scope;
auto registration = base::MakeRefCounted<ServiceWorkerRegistration>( auto registration =
options, helper_->context()->storage()->NewRegistrationId(), helper_->context()->registry()->CreateNewRegistration(options);
helper_->context()->AsWeakPtr());
auto version = base::MakeRefCounted<ServiceWorkerVersion>( auto version = base::MakeRefCounted<ServiceWorkerVersion>(
registration_.get(), registration_.get(),
GURL("bad-origin://www.example.com/test/service_worker.js"), GURL("bad-origin://www.example.com/test/service_worker.js"),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment