| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/service_worker/service_worker_provider_host.h" | 5 #include "content/browser/service_worker/service_worker_provider_host.h" |
| 6 | 6 |
| 7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
| 8 #include "content/browser/message_port_message_filter.h" | 8 #include "content/browser/message_port_message_filter.h" |
| 9 #include "content/browser/service_worker/service_worker_context_core.h" | 9 #include "content/browser/service_worker/service_worker_context_core.h" |
| 10 #include "content/browser/service_worker/service_worker_context_request_handler.
h" | 10 #include "content/browser/service_worker/service_worker_context_request_handler.
h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 context_(context), | 30 context_(context), |
| 31 dispatcher_host_(dispatcher_host) { | 31 dispatcher_host_(dispatcher_host) { |
| 32 } | 32 } |
| 33 | 33 |
| 34 ServiceWorkerProviderHost::~ServiceWorkerProviderHost() { | 34 ServiceWorkerProviderHost::~ServiceWorkerProviderHost() { |
| 35 // Clear docurl so the deferred activation of a waiting worker | 35 // Clear docurl so the deferred activation of a waiting worker |
| 36 // won't associate the new version with a provider being destroyed. | 36 // won't associate the new version with a provider being destroyed. |
| 37 document_url_ = GURL(); | 37 document_url_ = GURL(); |
| 38 if (controlling_version_.get()) | 38 if (controlling_version_.get()) |
| 39 controlling_version_->RemoveControllee(this); | 39 controlling_version_->RemoveControllee(this); |
| 40 if (active_version_.get()) | 40 if (associated_registration_.get()) { |
| 41 active_version_->RemovePotentialControllee(this); | 41 DecreaseProcessReference(associated_registration_->pattern()); |
| 42 if (waiting_version_.get()) | |
| 43 waiting_version_->RemovePotentialControllee(this); | |
| 44 if (installing_version_.get()) | |
| 45 installing_version_->RemovePotentialControllee(this); | |
| 46 if (associated_registration_.get()) | |
| 47 associated_registration_->RemoveListener(this); | 42 associated_registration_->RemoveListener(this); |
| 43 } |
| 44 for (std::vector<GURL>::iterator it = associated_patterns_.begin(); |
| 45 it != associated_patterns_.end(); ++it) { |
| 46 DecreaseProcessReference(*it); |
| 47 } |
| 48 } | 48 } |
| 49 | 49 |
| 50 void ServiceWorkerProviderHost::OnVersionAttributesChanged( | 50 void ServiceWorkerProviderHost::OnVersionAttributesChanged( |
| 51 ServiceWorkerRegistration* registration, | 51 ServiceWorkerRegistration* registration, |
| 52 ChangedVersionAttributesMask changed_mask, | 52 ChangedVersionAttributesMask changed_mask, |
| 53 const ServiceWorkerRegistrationInfo& info) { | 53 const ServiceWorkerRegistrationInfo& info) { |
| 54 DCHECK_EQ(associated_registration_.get(), registration); | 54 DCHECK_EQ(associated_registration_.get(), registration); |
| 55 UpdatePotentialControllees(registration->installing_version(), | 55 installing_version_ = registration->installing_version(); |
| 56 registration->waiting_version(), | 56 waiting_version_ = registration->waiting_version(); |
| 57 registration->active_version()); | 57 active_version_ = registration->active_version(); |
| 58 } | 58 } |
| 59 | 59 |
| 60 void ServiceWorkerProviderHost::OnRegistrationFailed( | 60 void ServiceWorkerProviderHost::OnRegistrationFailed( |
| 61 ServiceWorkerRegistration* registration) { | 61 ServiceWorkerRegistration* registration) { |
| 62 DCHECK_EQ(associated_registration_.get(), registration); | 62 DCHECK_EQ(associated_registration_.get(), registration); |
| 63 UnassociateRegistration(); | 63 UnassociateRegistration(); |
| 64 } | 64 } |
| 65 | 65 |
| 66 void ServiceWorkerProviderHost::SetDocumentUrl(const GURL& url) { | 66 void ServiceWorkerProviderHost::SetDocumentUrl(const GURL& url) { |
| 67 DCHECK(!url.has_ref()); | 67 DCHECK(!url.has_ref()); |
| 68 document_url_ = url; | 68 document_url_ = url; |
| 69 } | 69 } |
| 70 | 70 |
| 71 void ServiceWorkerProviderHost::UpdatePotentialControllees( | |
| 72 ServiceWorkerVersion* installing_version, | |
| 73 ServiceWorkerVersion* waiting_version, | |
| 74 ServiceWorkerVersion* active_version) { | |
| 75 if (installing_version != installing_version_.get()) { | |
| 76 scoped_refptr<ServiceWorkerVersion> previous_version = installing_version_; | |
| 77 if (previous_version.get()) | |
| 78 previous_version->RemovePotentialControllee(this); | |
| 79 if (installing_version) | |
| 80 installing_version->AddPotentialControllee(this); | |
| 81 installing_version_ = installing_version; | |
| 82 } | |
| 83 | |
| 84 if (waiting_version != waiting_version_.get()) { | |
| 85 scoped_refptr<ServiceWorkerVersion> previous_version = waiting_version_; | |
| 86 if (previous_version.get()) | |
| 87 previous_version->RemovePotentialControllee(this); | |
| 88 if (waiting_version) | |
| 89 waiting_version->AddPotentialControllee(this); | |
| 90 waiting_version_ = waiting_version; | |
| 91 } | |
| 92 | |
| 93 if (active_version != active_version_.get()) { | |
| 94 scoped_refptr<ServiceWorkerVersion> previous_version = active_version_; | |
| 95 if (previous_version.get()) | |
| 96 previous_version->RemovePotentialControllee(this); | |
| 97 if (active_version) | |
| 98 active_version->AddPotentialControllee(this); | |
| 99 active_version_ = active_version; | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 void ServiceWorkerProviderHost::SetControllerVersionAttribute( | 71 void ServiceWorkerProviderHost::SetControllerVersionAttribute( |
| 104 ServiceWorkerVersion* version) { | 72 ServiceWorkerVersion* version) { |
| 105 if (version == controlling_version_.get()) | 73 if (version == controlling_version_.get()) |
| 106 return; | 74 return; |
| 107 | 75 |
| 108 scoped_refptr<ServiceWorkerVersion> previous_version = controlling_version_; | 76 scoped_refptr<ServiceWorkerVersion> previous_version = controlling_version_; |
| 109 controlling_version_ = version; | 77 controlling_version_ = version; |
| 110 if (version) | 78 if (version) |
| 111 version->AddControllee(this); | 79 version->AddControllee(this); |
| 112 if (previous_version.get()) | 80 if (previous_version.get()) |
| (...skipping 24 matching lines...) Expand all Loading... |
| 137 return false; | 105 return false; |
| 138 } | 106 } |
| 139 | 107 |
| 140 running_hosted_version_ = live_version; | 108 running_hosted_version_ = live_version; |
| 141 return true; | 109 return true; |
| 142 } | 110 } |
| 143 | 111 |
| 144 void ServiceWorkerProviderHost::AssociateRegistration( | 112 void ServiceWorkerProviderHost::AssociateRegistration( |
| 145 ServiceWorkerRegistration* registration) { | 113 ServiceWorkerRegistration* registration) { |
| 146 DCHECK(CanAssociateRegistration(registration)); | 114 DCHECK(CanAssociateRegistration(registration)); |
| 115 if (associated_registration_.get()) |
| 116 DecreaseProcessReference(associated_registration_->pattern()); |
| 117 IncreaseProcessReference(registration->pattern()); |
| 118 |
| 147 associated_registration_ = registration; | 119 associated_registration_ = registration; |
| 148 registration->AddListener(this); | 120 registration->AddListener(this); |
| 149 UpdatePotentialControllees(registration->installing_version(), | 121 installing_version_ = registration->installing_version(); |
| 150 registration->waiting_version(), | 122 waiting_version_ = registration->waiting_version(); |
| 151 registration->active_version()); | 123 active_version_ = registration->active_version(); |
| 152 SetControllerVersionAttribute(registration->active_version()); | 124 SetControllerVersionAttribute(registration->active_version()); |
| 153 } | 125 } |
| 154 | 126 |
| 155 void ServiceWorkerProviderHost::UnassociateRegistration() { | 127 void ServiceWorkerProviderHost::UnassociateRegistration() { |
| 156 if (!associated_registration_.get()) | 128 if (!associated_registration_.get()) |
| 157 return; | 129 return; |
| 130 DecreaseProcessReference(associated_registration_->pattern()); |
| 158 associated_registration_->RemoveListener(this); | 131 associated_registration_->RemoveListener(this); |
| 159 associated_registration_ = NULL; | 132 associated_registration_ = NULL; |
| 160 UpdatePotentialControllees(NULL, NULL, NULL); | 133 installing_version_ = NULL; |
| 134 waiting_version_ = NULL; |
| 135 active_version_ = NULL; |
| 161 SetControllerVersionAttribute(NULL); | 136 SetControllerVersionAttribute(NULL); |
| 162 } | 137 } |
| 163 | 138 |
| 164 scoped_ptr<ServiceWorkerRequestHandler> | 139 scoped_ptr<ServiceWorkerRequestHandler> |
| 165 ServiceWorkerProviderHost::CreateRequestHandler( | 140 ServiceWorkerProviderHost::CreateRequestHandler( |
| 166 ResourceType resource_type, | 141 ResourceType resource_type, |
| 167 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, | 142 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, |
| 168 scoped_refptr<ResourceRequestBody> body) { | 143 scoped_refptr<ResourceRequestBody> body) { |
| 169 if (IsHostToRunningServiceWorker()) { | 144 if (IsHostToRunningServiceWorker()) { |
| 170 return scoped_ptr<ServiceWorkerRequestHandler>( | 145 return scoped_ptr<ServiceWorkerRequestHandler>( |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 &new_routing_ids); | 178 &new_routing_ids); |
| 204 | 179 |
| 205 dispatcher_host_->Send( | 180 dispatcher_host_->Send( |
| 206 new ServiceWorkerMsg_MessageToDocument( | 181 new ServiceWorkerMsg_MessageToDocument( |
| 207 kDocumentMainThreadId, provider_id(), | 182 kDocumentMainThreadId, provider_id(), |
| 208 message, | 183 message, |
| 209 sent_message_port_ids, | 184 sent_message_port_ids, |
| 210 new_routing_ids)); | 185 new_routing_ids)); |
| 211 } | 186 } |
| 212 | 187 |
| 188 void ServiceWorkerProviderHost::AddScopedProcessReferenceToPattern( |
| 189 const GURL& pattern) { |
| 190 associated_patterns_.push_back(pattern); |
| 191 IncreaseProcessReference(pattern); |
| 192 } |
| 193 |
| 213 ServiceWorkerObjectInfo ServiceWorkerProviderHost::CreateHandleAndPass( | 194 ServiceWorkerObjectInfo ServiceWorkerProviderHost::CreateHandleAndPass( |
| 214 ServiceWorkerVersion* version) { | 195 ServiceWorkerVersion* version) { |
| 215 ServiceWorkerObjectInfo info; | 196 ServiceWorkerObjectInfo info; |
| 216 if (context_ && version) { | 197 if (context_ && version) { |
| 217 scoped_ptr<ServiceWorkerHandle> handle = | 198 scoped_ptr<ServiceWorkerHandle> handle = |
| 218 ServiceWorkerHandle::Create(context_, | 199 ServiceWorkerHandle::Create(context_, |
| 219 dispatcher_host_, | 200 dispatcher_host_, |
| 220 kDocumentMainThreadId, | 201 kDocumentMainThreadId, |
| 221 provider_id_, | 202 provider_id_, |
| 222 version); | 203 version); |
| 223 info = handle->GetObjectInfo(); | 204 info = handle->GetObjectInfo(); |
| 224 dispatcher_host_->RegisterServiceWorkerHandle(handle.Pass()); | 205 dispatcher_host_->RegisterServiceWorkerHandle(handle.Pass()); |
| 225 } | 206 } |
| 226 return info; | 207 return info; |
| 227 } | 208 } |
| 228 | 209 |
| 210 void ServiceWorkerProviderHost::IncreaseProcessReference( |
| 211 const GURL& pattern) { |
| 212 if (context_ && context_->process_manager()) { |
| 213 context_->process_manager()->AddProcessReferenceToPattern( |
| 214 pattern, process_id_); |
| 215 } |
| 216 } |
| 217 |
| 218 void ServiceWorkerProviderHost::DecreaseProcessReference( |
| 219 const GURL& pattern) { |
| 220 if (context_ && context_->process_manager()) { |
| 221 context_->process_manager()->RemoveProcessReferenceFromPattern( |
| 222 pattern, process_id_); |
| 223 } |
| 224 } |
| 225 |
| 229 bool ServiceWorkerProviderHost::IsContextAlive() { | 226 bool ServiceWorkerProviderHost::IsContextAlive() { |
| 230 return context_ != NULL; | 227 return context_ != NULL; |
| 231 } | 228 } |
| 232 | 229 |
| 233 } // namespace content | 230 } // namespace content |
| OLD | NEW |