Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/service_worker/service_worker_registration_handle.h" | |
| 6 | |
| 7 #include "content/browser/service_worker/service_worker_context_core.h" | |
| 8 #include "content/browser/service_worker/service_worker_dispatcher_host.h" | |
| 9 #include "content/browser/service_worker/service_worker_handle.h" | |
| 10 #include "content/common/service_worker/service_worker_messages.h" | |
| 11 | |
| 12 namespace content { | |
| 13 | |
| 14 ServiceWorkerRegistrationHandle::ServiceWorkerRegistrationHandle( | |
| 15 base::WeakPtr<ServiceWorkerContextCore> context, | |
| 16 ServiceWorkerDispatcherHost* dispatcher_host, | |
| 17 int thread_id, | |
| 18 int provider_id, | |
| 19 ServiceWorkerRegistration* registration) | |
| 20 : context_(context), | |
| 21 dispatcher_host_(dispatcher_host), | |
| 22 thread_id_(thread_id), | |
| 23 provider_id_(provider_id), | |
| 24 handle_id_(context ? context->GetNewRegistrationHandleId() | |
| 25 : kInvalidServiceWorkerRegistrationHandleId), | |
| 26 ref_count_(1), | |
| 27 registration_(registration) { | |
| 28 DCHECK(registration_); | |
| 29 SetVersionAttributes(registration->installing_version(), | |
| 30 registration->waiting_version(), | |
| 31 registration->active_version()); | |
| 32 registration_->AddListener(this); | |
| 33 } | |
| 34 | |
| 35 ServiceWorkerRegistrationHandle::~ServiceWorkerRegistrationHandle() { | |
| 36 registration_->RemoveListener(this); | |
| 37 } | |
| 38 | |
| 39 void ServiceWorkerRegistrationHandle::IncrementRefCount() { | |
| 40 DCHECK_GT(ref_count_, 0); | |
| 41 ++ref_count_; | |
| 42 } | |
| 43 | |
| 44 void ServiceWorkerRegistrationHandle::DecrementRefCount() { | |
| 45 DCHECK_GT(ref_count_, 0); | |
| 46 --ref_count_; | |
| 47 } | |
| 48 | |
| 49 void ServiceWorkerRegistrationHandle::OnVersionAttributesChanged( | |
| 50 ServiceWorkerRegistration* registration, | |
| 51 ChangedVersionAttributesMask changed_mask, | |
| 52 const ServiceWorkerRegistrationInfo& info) { | |
| 53 DCHECK_EQ(registration->id(), registration_->id()); | |
| 54 SetVersionAttributes(registration->installing_version(), | |
| 55 registration->waiting_version(), | |
| 56 registration->active_version()); | |
| 57 } | |
| 58 | |
| 59 void ServiceWorkerRegistrationHandle::OnRegistrationFailed( | |
| 60 ServiceWorkerRegistration* registration) { | |
| 61 DCHECK_EQ(registration->id(), registration_->id()); | |
| 62 ClearVersionAttributes(); | |
| 63 registration_->RemoveListener(this); | |
| 64 registration_ = NULL; | |
| 65 } | |
| 66 | |
| 67 void ServiceWorkerRegistrationHandle::SetVersionAttributes( | |
| 68 ServiceWorkerVersion* installing_version, | |
| 69 ServiceWorkerVersion* waiting_version, | |
| 70 ServiceWorkerVersion* active_version) { | |
| 71 ChangedVersionAttributesMask mask; | |
| 72 | |
| 73 if (installing_version != installing_version_) { | |
| 74 installing_version_ = installing_version; | |
| 75 mask.add(ChangedVersionAttributesMask::INSTALLING_VERSION); | |
| 76 } | |
| 77 if (waiting_version != waiting_version_) { | |
| 78 waiting_version_ = waiting_version; | |
| 79 mask.add(ChangedVersionAttributesMask::WAITING_VERSION); | |
| 80 } | |
| 81 if (active_version != active_version_) { | |
| 82 active_version_ = active_version; | |
| 83 mask.add(ChangedVersionAttributesMask::ACTIVE_VERSION); | |
| 84 } | |
| 85 | |
| 86 if (!dispatcher_host_) | |
| 87 return; // Could be NULL in some tests. | |
| 88 if (!mask.changed()) | |
| 89 return; | |
| 90 | |
| 91 ServiceWorkerVersionAttributes attributes; | |
| 92 if (mask.installing_changed()) { | |
| 93 attributes.installing = | |
| 94 CreateServiceWorkerHandleAndPass(installing_version); | |
| 95 } | |
| 96 if (mask.waiting_changed()) { | |
| 97 attributes.waiting = | |
| 98 CreateServiceWorkerHandleAndPass(waiting_version); | |
| 99 } | |
| 100 if (mask.active_changed()) { | |
| 101 attributes.active = | |
| 102 CreateServiceWorkerHandleAndPass(active_version); | |
| 103 } | |
| 104 | |
| 105 dispatcher_host_->Send(new ServiceWorkerMsg_SetVersionAttributes( | |
| 106 thread_id_, provider_id_, handle_id_, mask.changed(), attributes)); | |
| 107 } | |
| 108 | |
| 109 void ServiceWorkerRegistrationHandle::ClearVersionAttributes() { | |
| 110 SetVersionAttributes(NULL, NULL, NULL); | |
| 111 } | |
| 112 | |
| 113 ServiceWorkerObjectInfo | |
| 114 ServiceWorkerRegistrationHandle::CreateServiceWorkerHandleAndPass( | |
| 115 ServiceWorkerVersion* version) { | |
| 116 ServiceWorkerObjectInfo info; | |
| 117 if (context_ && version) { | |
| 118 scoped_ptr<ServiceWorkerHandle> handle = | |
| 119 ServiceWorkerHandle::Create(context_, | |
|
michaeln
2014/08/13 04:18:16
does it matter that we create a new handle instanc
falken
2014/08/13 08:27:06
I don't think they get dedupped. This is https://c
| |
| 120 dispatcher_host_, | |
| 121 thread_id_, | |
| 122 provider_id_, | |
| 123 version); | |
| 124 info = handle->GetObjectInfo(); | |
| 125 dispatcher_host_->RegisterServiceWorkerHandle(handle.Pass()); | |
| 126 } | |
| 127 return info; | |
| 128 } | |
| 129 | |
| 130 } // namespace content | |
| OLD | NEW |