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" |
11 #include "content/browser/service_worker/service_worker_controllee_request_handl
er.h" | 11 #include "content/browser/service_worker/service_worker_controllee_request_handl
er.h" |
12 #include "content/browser/service_worker/service_worker_dispatcher_host.h" | 12 #include "content/browser/service_worker/service_worker_dispatcher_host.h" |
13 #include "content/browser/service_worker/service_worker_handle.h" | 13 #include "content/browser/service_worker/service_worker_handle.h" |
14 #include "content/browser/service_worker/service_worker_registration_handle.h" | 14 #include "content/browser/service_worker/service_worker_registration_handle.h" |
15 #include "content/browser/service_worker/service_worker_utils.h" | 15 #include "content/browser/service_worker/service_worker_utils.h" |
16 #include "content/browser/service_worker/service_worker_version.h" | 16 #include "content/browser/service_worker/service_worker_version.h" |
17 #include "content/common/resource_request_body.h" | 17 #include "content/common/resource_request_body.h" |
18 #include "content/common/service_worker/service_worker_messages.h" | 18 #include "content/common/service_worker/service_worker_messages.h" |
19 #include "content/common/service_worker/service_worker_types.h" | 19 #include "content/common/service_worker/service_worker_types.h" |
| 20 #include "content/public/common/child_process_host.h" |
20 | 21 |
21 namespace content { | 22 namespace content { |
22 | 23 |
23 static const int kDocumentMainThreadId = 0; | 24 static const int kDocumentMainThreadId = 0; |
24 | 25 |
25 ServiceWorkerProviderHost::ServiceWorkerProviderHost( | 26 ServiceWorkerProviderHost::ServiceWorkerProviderHost( |
26 int process_id, int provider_id, | 27 int process_id, int provider_id, |
27 base::WeakPtr<ServiceWorkerContextCore> context, | 28 base::WeakPtr<ServiceWorkerContextCore> context, |
28 ServiceWorkerDispatcherHost* dispatcher_host) | 29 ServiceWorkerDispatcherHost* dispatcher_host) |
29 : process_id_(process_id), | 30 : process_id_(process_id), |
30 provider_id_(provider_id), | 31 provider_id_(provider_id), |
31 context_(context), | 32 context_(context), |
32 dispatcher_host_(dispatcher_host), | 33 dispatcher_host_(dispatcher_host), |
33 allow_association_(true) { | 34 allow_association_(true) { |
| 35 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, process_id_); |
34 } | 36 } |
35 | 37 |
36 ServiceWorkerProviderHost::~ServiceWorkerProviderHost() { | 38 ServiceWorkerProviderHost::~ServiceWorkerProviderHost() { |
37 // Clear docurl so the deferred activation of a waiting worker | 39 // Clear docurl so the deferred activation of a waiting worker |
38 // won't associate the new version with a provider being destroyed. | 40 // won't associate the new version with a provider being destroyed. |
39 document_url_ = GURL(); | 41 document_url_ = GURL(); |
40 if (controlling_version_.get()) | 42 if (controlling_version_.get()) |
41 controlling_version_->RemoveControllee(this); | 43 controlling_version_->RemoveControllee(this); |
42 if (associated_registration_.get()) { | 44 if (associated_registration_.get()) { |
43 DecreaseProcessReference(associated_registration_->pattern()); | 45 DecreaseProcessReference(associated_registration_->pattern()); |
44 associated_registration_->RemoveListener(this); | 46 associated_registration_->RemoveListener(this); |
45 } | 47 } |
46 for (std::vector<GURL>::iterator it = associated_patterns_.begin(); | 48 |
47 it != associated_patterns_.end(); ++it) { | 49 for (const GURL& pattern : associated_patterns_) |
48 DecreaseProcessReference(*it); | 50 DecreaseProcessReference(pattern); |
49 } | |
50 } | 51 } |
51 | 52 |
52 void ServiceWorkerProviderHost::OnRegistrationFailed( | 53 void ServiceWorkerProviderHost::OnRegistrationFailed( |
53 ServiceWorkerRegistration* registration) { | 54 ServiceWorkerRegistration* registration) { |
54 DCHECK_EQ(associated_registration_.get(), registration); | 55 DCHECK_EQ(associated_registration_.get(), registration); |
55 DisassociateRegistration(); | 56 DisassociateRegistration(); |
56 } | 57 } |
57 | 58 |
58 void ServiceWorkerProviderHost::SetDocumentUrl(const GURL& url) { | 59 void ServiceWorkerProviderHost::SetDocumentUrl(const GURL& url) { |
59 DCHECK(!url.has_ref()); | 60 DCHECK(!url.has_ref()); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 return true; | 106 return true; |
106 } | 107 } |
107 | 108 |
108 void ServiceWorkerProviderHost::AssociateRegistration( | 109 void ServiceWorkerProviderHost::AssociateRegistration( |
109 ServiceWorkerRegistration* registration) { | 110 ServiceWorkerRegistration* registration) { |
110 DCHECK(CanAssociateRegistration(registration)); | 111 DCHECK(CanAssociateRegistration(registration)); |
111 if (associated_registration_.get()) | 112 if (associated_registration_.get()) |
112 DecreaseProcessReference(associated_registration_->pattern()); | 113 DecreaseProcessReference(associated_registration_->pattern()); |
113 IncreaseProcessReference(registration->pattern()); | 114 IncreaseProcessReference(registration->pattern()); |
114 | 115 |
115 if (dispatcher_host_) { | |
116 ServiceWorkerRegistrationHandle* handle = | |
117 dispatcher_host_->GetOrCreateRegistrationHandle( | |
118 provider_id(), registration); | |
119 | |
120 ServiceWorkerVersionAttributes attrs; | |
121 attrs.installing = handle->CreateServiceWorkerHandleAndPass( | |
122 registration->installing_version()); | |
123 attrs.waiting = handle->CreateServiceWorkerHandleAndPass( | |
124 registration->waiting_version()); | |
125 attrs.active = handle->CreateServiceWorkerHandleAndPass( | |
126 registration->active_version()); | |
127 | |
128 dispatcher_host_->Send(new ServiceWorkerMsg_AssociateRegistration( | |
129 kDocumentMainThreadId, provider_id(), handle->GetObjectInfo(), attrs)); | |
130 } | |
131 | |
132 associated_registration_ = registration; | 116 associated_registration_ = registration; |
133 associated_registration_->AddListener(this); | 117 associated_registration_->AddListener(this); |
| 118 SendAssociateRegistrationMessage(); |
134 SetControllerVersionAttribute(registration->active_version()); | 119 SetControllerVersionAttribute(registration->active_version()); |
135 } | 120 } |
136 | 121 |
137 void ServiceWorkerProviderHost::DisassociateRegistration() { | 122 void ServiceWorkerProviderHost::DisassociateRegistration() { |
138 if (!associated_registration_.get()) | 123 if (!associated_registration_.get()) |
139 return; | 124 return; |
140 DecreaseProcessReference(associated_registration_->pattern()); | 125 DecreaseProcessReference(associated_registration_->pattern()); |
141 associated_registration_->RemoveListener(this); | 126 associated_registration_->RemoveListener(this); |
142 associated_registration_ = NULL; | 127 associated_registration_ = NULL; |
143 SetControllerVersionAttribute(NULL); | 128 SetControllerVersionAttribute(NULL); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 sent_message_port_ids, | 192 sent_message_port_ids, |
208 new_routing_ids)); | 193 new_routing_ids)); |
209 } | 194 } |
210 | 195 |
211 void ServiceWorkerProviderHost::AddScopedProcessReferenceToPattern( | 196 void ServiceWorkerProviderHost::AddScopedProcessReferenceToPattern( |
212 const GURL& pattern) { | 197 const GURL& pattern) { |
213 associated_patterns_.push_back(pattern); | 198 associated_patterns_.push_back(pattern); |
214 IncreaseProcessReference(pattern); | 199 IncreaseProcessReference(pattern); |
215 } | 200 } |
216 | 201 |
| 202 void ServiceWorkerProviderHost::PrepareForCrossSiteTransfer() { |
| 203 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, process_id_); |
| 204 |
| 205 for (const GURL& pattern : associated_patterns_) |
| 206 DecreaseProcessReference(pattern); |
| 207 |
| 208 if (associated_registration_.get()) |
| 209 DecreaseProcessReference(associated_registration_->pattern()); |
| 210 |
| 211 process_id_ = ChildProcessHost::kInvalidUniqueID; |
| 212 provider_id_ = kInvalidServiceWorkerProviderId; |
| 213 dispatcher_host_ = nullptr; |
| 214 } |
| 215 |
| 216 void ServiceWorkerProviderHost::CompleteCrossSiteTransfer( |
| 217 int new_process_id, |
| 218 int new_provider_id, |
| 219 ServiceWorkerDispatcherHost* new_dispatcher_host) { |
| 220 DCHECK_EQ(ChildProcessHost::kInvalidUniqueID, process_id_); |
| 221 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, new_process_id); |
| 222 |
| 223 process_id_ = new_process_id; |
| 224 provider_id_ = new_provider_id; |
| 225 dispatcher_host_ = new_dispatcher_host; |
| 226 |
| 227 for (const GURL& pattern : associated_patterns_) |
| 228 IncreaseProcessReference(pattern); |
| 229 |
| 230 if (associated_registration_.get()) { |
| 231 IncreaseProcessReference(associated_registration_->pattern()); |
| 232 SendAssociateRegistrationMessage(); |
| 233 if (dispatcher_host_ && associated_registration_->active_version()) { |
| 234 dispatcher_host_->Send(new ServiceWorkerMsg_SetControllerServiceWorker( |
| 235 kDocumentMainThreadId, provider_id(), |
| 236 CreateHandleAndPass(associated_registration_->active_version()))); |
| 237 } |
| 238 } |
| 239 } |
| 240 |
| 241 void ServiceWorkerProviderHost::SendAssociateRegistrationMessage() { |
| 242 if (!dispatcher_host_) |
| 243 return; |
| 244 |
| 245 ServiceWorkerRegistrationHandle* handle = |
| 246 dispatcher_host_->GetOrCreateRegistrationHandle( |
| 247 provider_id(), associated_registration_.get()); |
| 248 |
| 249 ServiceWorkerVersionAttributes attrs; |
| 250 attrs.installing = handle->CreateServiceWorkerHandleAndPass( |
| 251 associated_registration_->installing_version()); |
| 252 attrs.waiting = handle->CreateServiceWorkerHandleAndPass( |
| 253 associated_registration_->waiting_version()); |
| 254 attrs.active = handle->CreateServiceWorkerHandleAndPass( |
| 255 associated_registration_->active_version()); |
| 256 |
| 257 dispatcher_host_->Send(new ServiceWorkerMsg_AssociateRegistration( |
| 258 kDocumentMainThreadId, provider_id(), handle->GetObjectInfo(), attrs)); |
| 259 } |
| 260 |
217 ServiceWorkerObjectInfo ServiceWorkerProviderHost::CreateHandleAndPass( | 261 ServiceWorkerObjectInfo ServiceWorkerProviderHost::CreateHandleAndPass( |
218 ServiceWorkerVersion* version) { | 262 ServiceWorkerVersion* version) { |
219 ServiceWorkerObjectInfo info; | 263 ServiceWorkerObjectInfo info; |
220 if (context_ && version) { | 264 if (context_ && version) { |
221 scoped_ptr<ServiceWorkerHandle> handle = | 265 scoped_ptr<ServiceWorkerHandle> handle = |
222 ServiceWorkerHandle::Create(context_, | 266 ServiceWorkerHandle::Create(context_, |
223 dispatcher_host_, | 267 dispatcher_host_, |
224 kDocumentMainThreadId, | 268 kDocumentMainThreadId, |
225 provider_id_, | 269 provider_id_, |
226 version); | 270 version); |
(...skipping 17 matching lines...) Expand all Loading... |
244 context_->process_manager()->RemoveProcessReferenceFromPattern( | 288 context_->process_manager()->RemoveProcessReferenceFromPattern( |
245 pattern, process_id_); | 289 pattern, process_id_); |
246 } | 290 } |
247 } | 291 } |
248 | 292 |
249 bool ServiceWorkerProviderHost::IsContextAlive() { | 293 bool ServiceWorkerProviderHost::IsContextAlive() { |
250 return context_ != NULL; | 294 return context_ != NULL; |
251 } | 295 } |
252 | 296 |
253 } // namespace content | 297 } // namespace content |
OLD | NEW |