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 static const int kDocumentMainThreadId = 0; |
| 15 |
| 16 ServiceWorkerRegistrationHandle::ServiceWorkerRegistrationHandle( |
| 17 base::WeakPtr<ServiceWorkerContextCore> context, |
| 18 ServiceWorkerDispatcherHost* dispatcher_host, |
| 19 int provider_id, |
| 20 ServiceWorkerRegistration* registration) |
| 21 : context_(context), |
| 22 dispatcher_host_(dispatcher_host), |
| 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 kDocumentMainThreadId, provider_id_, handle_id_, |
| 107 mask.changed(), attributes)); |
| 108 } |
| 109 |
| 110 void ServiceWorkerRegistrationHandle::ClearVersionAttributes() { |
| 111 SetVersionAttributes(NULL, NULL, NULL); |
| 112 } |
| 113 |
| 114 ServiceWorkerObjectInfo |
| 115 ServiceWorkerRegistrationHandle::CreateServiceWorkerHandleAndPass( |
| 116 ServiceWorkerVersion* version) { |
| 117 ServiceWorkerObjectInfo info; |
| 118 if (context_ && version) { |
| 119 scoped_ptr<ServiceWorkerHandle> handle = |
| 120 ServiceWorkerHandle::Create(context_, |
| 121 dispatcher_host_, |
| 122 kDocumentMainThreadId, |
| 123 provider_id_, |
| 124 version); |
| 125 info = handle->GetObjectInfo(); |
| 126 dispatcher_host_->RegisterServiceWorkerHandle(handle.Pass()); |
| 127 } |
| 128 return info; |
| 129 } |
| 130 |
| 131 } // namespace content |
OLD | NEW |