Commit d1f1bb12 authored by nhiroki's avatar nhiroki Committed by Commit bot

ServiceWorker: Merge FindSWRegistration into FindOrCreateRegistration for cleanup

BUG=452403
TEST=content_unittests --gtest_filter=ServiceWorkerDispatcherTest.*

Review URL: https://codereview.chromium.org/1017453006

Cr-Commit-Position: refs/heads/master@{#320902}
parent 62164aaf
...@@ -273,27 +273,11 @@ WebServiceWorkerImpl* ServiceWorkerDispatcher::GetServiceWorker( ...@@ -273,27 +273,11 @@ WebServiceWorkerImpl* ServiceWorkerDispatcher::GetServiceWorker(
return new WebServiceWorkerImpl(handle_ref.Pass(), sender_.get()); return new WebServiceWorkerImpl(handle_ref.Pass(), sender_.get());
} }
WebServiceWorkerRegistrationImpl*
ServiceWorkerDispatcher::FindServiceWorkerRegistration(
const ServiceWorkerRegistrationObjectInfo& info,
bool adopt_handle) {
RegistrationObjectMap::iterator registration =
registrations_.find(info.handle_id);
if (registration == registrations_.end())
return NULL;
if (adopt_handle) {
// We are instructed to adopt a handle but we already have one, so
// adopt and destroy a handle ref.
ServiceWorkerRegistrationHandleReference::Adopt(info, sender_.get());
}
return registration->second;
}
WebServiceWorkerRegistrationImpl* WebServiceWorkerRegistrationImpl*
ServiceWorkerDispatcher::CreateServiceWorkerRegistration( ServiceWorkerDispatcher::CreateServiceWorkerRegistration(
const ServiceWorkerRegistrationObjectInfo& info, const ServiceWorkerRegistrationObjectInfo& info,
bool adopt_handle) { bool adopt_handle) {
DCHECK(!FindServiceWorkerRegistration(info, adopt_handle)); DCHECK(!ContainsKey(registrations_, info.handle_id));
if (info.handle_id == kInvalidServiceWorkerRegistrationHandleId) if (info.handle_id == kInvalidServiceWorkerRegistrationHandleId)
return NULL; return NULL;
...@@ -683,20 +667,21 @@ WebServiceWorkerRegistrationImpl* ...@@ -683,20 +667,21 @@ WebServiceWorkerRegistrationImpl*
ServiceWorkerDispatcher::FindOrCreateRegistration( ServiceWorkerDispatcher::FindOrCreateRegistration(
const ServiceWorkerRegistrationObjectInfo& info, const ServiceWorkerRegistrationObjectInfo& info,
const ServiceWorkerVersionAttributes& attrs) { const ServiceWorkerVersionAttributes& attrs) {
WebServiceWorkerRegistrationImpl* registration = RegistrationObjectMap::iterator found = registrations_.find(info.handle_id);
FindServiceWorkerRegistration(info, true); if (found != registrations_.end()) {
if (!registration) { ServiceWorkerRegistrationHandleReference::Adopt(info, sender_.get());
registration = CreateServiceWorkerRegistration(info, true);
registration->SetInstalling(GetServiceWorker(attrs.installing, true));
registration->SetWaiting(GetServiceWorker(attrs.waiting, true));
registration->SetActive(GetServiceWorker(attrs.active, true));
} else {
// |registration| must already have version attributes, so adopt and destroy
// handle refs for them.
ServiceWorkerHandleReference::Adopt(attrs.installing, sender_.get()); ServiceWorkerHandleReference::Adopt(attrs.installing, sender_.get());
ServiceWorkerHandleReference::Adopt(attrs.waiting, sender_.get()); ServiceWorkerHandleReference::Adopt(attrs.waiting, sender_.get());
ServiceWorkerHandleReference::Adopt(attrs.active, sender_.get()); ServiceWorkerHandleReference::Adopt(attrs.active, sender_.get());
return found->second;
} }
bool adopt_handle = true;
WebServiceWorkerRegistrationImpl* registration =
CreateServiceWorkerRegistration(info, adopt_handle);
registration->SetInstalling(GetServiceWorker(attrs.installing, adopt_handle));
registration->SetWaiting(GetServiceWorker(attrs.waiting, adopt_handle));
registration->SetActive(GetServiceWorker(attrs.active, adopt_handle));
return registration; return registration;
} }
......
...@@ -112,14 +112,6 @@ class CONTENT_EXPORT ServiceWorkerDispatcher ...@@ -112,14 +112,6 @@ class CONTENT_EXPORT ServiceWorkerDispatcher
const ServiceWorkerObjectInfo& info, const ServiceWorkerObjectInfo& info,
bool adopt_handle); bool adopt_handle);
// Finds a WebServiceWorkerRegistrationImpl for the specified registration.
// If it's not found, returns NULL. If |adopt_handle| is true,
// a ServiceWorkerRegistrationHandleReference will be adopted for the
// registration.
WebServiceWorkerRegistrationImpl* FindServiceWorkerRegistration(
const ServiceWorkerRegistrationObjectInfo& info,
bool adopt_handle);
// Creates a WebServiceWorkerRegistrationImpl for the specified registration // Creates a WebServiceWorkerRegistrationImpl for the specified registration
// and transfers its ownership to the caller. If |adopt_handle| is true, a // and transfers its ownership to the caller. If |adopt_handle| is true, a
// ServiceWorkerRegistrationHandleReference will be adopted for the // ServiceWorkerRegistrationHandleReference will be adopted for the
...@@ -234,6 +226,11 @@ class CONTENT_EXPORT ServiceWorkerDispatcher ...@@ -234,6 +226,11 @@ class CONTENT_EXPORT ServiceWorkerDispatcher
void RemoveServiceWorkerRegistration( void RemoveServiceWorkerRegistration(
int registration_handle_id); int registration_handle_id);
// Returns an existing registration or new one filled in with version
// attributes. This function assumes given |info| and |attrs| retain handle
// references and always adopts them.
// TODO(nhiroki): This assumption seems to impair readability. We could
// explictly pass ServiceWorker(Registration)HandleReference instead.
WebServiceWorkerRegistrationImpl* FindOrCreateRegistration( WebServiceWorkerRegistrationImpl* FindOrCreateRegistration(
const ServiceWorkerRegistrationObjectInfo& info, const ServiceWorkerRegistrationObjectInfo& info,
const ServiceWorkerVersionAttributes& attrs); const ServiceWorkerVersionAttributes& attrs);
......
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