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/browser/render_frame_host.h" | 20 #include "content/public/browser/render_frame_host.h" |
21 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
22 #include "content/public/browser/web_contents_delegate.h" | 22 #include "content/public/browser/web_contents_delegate.h" |
| 23 #include "content/public/common/child_process_host.h" |
23 | 24 |
24 namespace content { | 25 namespace content { |
25 | 26 |
26 static const int kDocumentMainThreadId = 0; | 27 static const int kDocumentMainThreadId = 0; |
27 | 28 |
28 namespace { | 29 namespace { |
29 | 30 |
30 void FocusOnUIThread(int render_process_id, | 31 void FocusOnUIThread(int render_process_id, |
31 int render_frame_id, | 32 int render_frame_id, |
32 const ServiceWorkerProviderHost::FocusCallback& callback) { | 33 const ServiceWorkerProviderHost::FocusCallback& callback) { |
(...skipping 16 matching lines...) Expand all Loading... |
49 ServiceWorkerProviderHost::ServiceWorkerProviderHost( | 50 ServiceWorkerProviderHost::ServiceWorkerProviderHost( |
50 int render_process_id, int render_frame_id, int provider_id, | 51 int render_process_id, int render_frame_id, int provider_id, |
51 base::WeakPtr<ServiceWorkerContextCore> context, | 52 base::WeakPtr<ServiceWorkerContextCore> context, |
52 ServiceWorkerDispatcherHost* dispatcher_host) | 53 ServiceWorkerDispatcherHost* dispatcher_host) |
53 : render_process_id_(render_process_id), | 54 : render_process_id_(render_process_id), |
54 render_frame_id_(render_frame_id), | 55 render_frame_id_(render_frame_id), |
55 provider_id_(provider_id), | 56 provider_id_(provider_id), |
56 context_(context), | 57 context_(context), |
57 dispatcher_host_(dispatcher_host), | 58 dispatcher_host_(dispatcher_host), |
58 allow_association_(true) { | 59 allow_association_(true) { |
| 60 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, render_process_id_); |
59 } | 61 } |
60 | 62 |
61 ServiceWorkerProviderHost::~ServiceWorkerProviderHost() { | 63 ServiceWorkerProviderHost::~ServiceWorkerProviderHost() { |
62 // Clear docurl so the deferred activation of a waiting worker | 64 // Clear docurl so the deferred activation of a waiting worker |
63 // won't associate the new version with a provider being destroyed. | 65 // won't associate the new version with a provider being destroyed. |
64 document_url_ = GURL(); | 66 document_url_ = GURL(); |
65 if (controlling_version_.get()) | 67 if (controlling_version_.get()) |
66 controlling_version_->RemoveControllee(this); | 68 controlling_version_->RemoveControllee(this); |
67 if (associated_registration_.get()) { | 69 if (associated_registration_.get()) { |
68 DecreaseProcessReference(associated_registration_->pattern()); | 70 DecreaseProcessReference(associated_registration_->pattern()); |
69 associated_registration_->RemoveListener(this); | 71 associated_registration_->RemoveListener(this); |
70 } | 72 } |
71 for (std::vector<GURL>::iterator it = associated_patterns_.begin(); | 73 |
72 it != associated_patterns_.end(); ++it) { | 74 for (const GURL& pattern : associated_patterns_) |
73 DecreaseProcessReference(*it); | 75 DecreaseProcessReference(pattern); |
74 } | |
75 } | 76 } |
76 | 77 |
77 void ServiceWorkerProviderHost::OnRegistrationFailed( | 78 void ServiceWorkerProviderHost::OnRegistrationFailed( |
78 ServiceWorkerRegistration* registration) { | 79 ServiceWorkerRegistration* registration) { |
79 DCHECK_EQ(associated_registration_.get(), registration); | 80 DCHECK_EQ(associated_registration_.get(), registration); |
80 DisassociateRegistration(); | 81 DisassociateRegistration(); |
81 } | 82 } |
82 | 83 |
83 void ServiceWorkerProviderHost::SetDocumentUrl(const GURL& url) { | 84 void ServiceWorkerProviderHost::SetDocumentUrl(const GURL& url) { |
84 DCHECK(!url.has_ref()); | 85 DCHECK(!url.has_ref()); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 return true; | 131 return true; |
131 } | 132 } |
132 | 133 |
133 void ServiceWorkerProviderHost::AssociateRegistration( | 134 void ServiceWorkerProviderHost::AssociateRegistration( |
134 ServiceWorkerRegistration* registration) { | 135 ServiceWorkerRegistration* registration) { |
135 DCHECK(CanAssociateRegistration(registration)); | 136 DCHECK(CanAssociateRegistration(registration)); |
136 if (associated_registration_.get()) | 137 if (associated_registration_.get()) |
137 DecreaseProcessReference(associated_registration_->pattern()); | 138 DecreaseProcessReference(associated_registration_->pattern()); |
138 IncreaseProcessReference(registration->pattern()); | 139 IncreaseProcessReference(registration->pattern()); |
139 | 140 |
140 if (dispatcher_host_) { | |
141 ServiceWorkerRegistrationHandle* handle = | |
142 dispatcher_host_->GetOrCreateRegistrationHandle( | |
143 provider_id(), registration); | |
144 | |
145 ServiceWorkerVersionAttributes attrs; | |
146 attrs.installing = handle->CreateServiceWorkerHandleAndPass( | |
147 registration->installing_version()); | |
148 attrs.waiting = handle->CreateServiceWorkerHandleAndPass( | |
149 registration->waiting_version()); | |
150 attrs.active = handle->CreateServiceWorkerHandleAndPass( | |
151 registration->active_version()); | |
152 | |
153 dispatcher_host_->Send(new ServiceWorkerMsg_AssociateRegistration( | |
154 kDocumentMainThreadId, provider_id(), handle->GetObjectInfo(), attrs)); | |
155 } | |
156 | |
157 associated_registration_ = registration; | 141 associated_registration_ = registration; |
158 associated_registration_->AddListener(this); | 142 associated_registration_->AddListener(this); |
| 143 SendAssociateRegistrationMessage(); |
159 SetControllerVersionAttribute(registration->active_version()); | 144 SetControllerVersionAttribute(registration->active_version()); |
160 } | 145 } |
161 | 146 |
162 void ServiceWorkerProviderHost::DisassociateRegistration() { | 147 void ServiceWorkerProviderHost::DisassociateRegistration() { |
163 if (!associated_registration_.get()) | 148 if (!associated_registration_.get()) |
164 return; | 149 return; |
165 DecreaseProcessReference(associated_registration_->pattern()); | 150 DecreaseProcessReference(associated_registration_->pattern()); |
166 associated_registration_->RemoveListener(this); | 151 associated_registration_->RemoveListener(this); |
167 associated_registration_ = NULL; | 152 associated_registration_ = NULL; |
168 SetControllerVersionAttribute(NULL); | 153 SetControllerVersionAttribute(NULL); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 render_frame_id_, | 226 render_frame_id_, |
242 callback)); | 227 callback)); |
243 } | 228 } |
244 | 229 |
245 void ServiceWorkerProviderHost::AddScopedProcessReferenceToPattern( | 230 void ServiceWorkerProviderHost::AddScopedProcessReferenceToPattern( |
246 const GURL& pattern) { | 231 const GURL& pattern) { |
247 associated_patterns_.push_back(pattern); | 232 associated_patterns_.push_back(pattern); |
248 IncreaseProcessReference(pattern); | 233 IncreaseProcessReference(pattern); |
249 } | 234 } |
250 | 235 |
| 236 void ServiceWorkerProviderHost::PrepareForCrossSiteTransfer() { |
| 237 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, render_process_id_); |
| 238 |
| 239 for (const GURL& pattern : associated_patterns_) |
| 240 DecreaseProcessReference(pattern); |
| 241 |
| 242 if (associated_registration_.get()) |
| 243 DecreaseProcessReference(associated_registration_->pattern()); |
| 244 |
| 245 render_process_id_ = ChildProcessHost::kInvalidUniqueID; |
| 246 render_frame_id_ = MSG_ROUTING_NONE; |
| 247 provider_id_ = kInvalidServiceWorkerProviderId; |
| 248 dispatcher_host_ = nullptr; |
| 249 } |
| 250 |
| 251 void ServiceWorkerProviderHost::CompleteCrossSiteTransfer( |
| 252 int new_process_id, |
| 253 int new_frame_id, |
| 254 int new_provider_id, |
| 255 ServiceWorkerDispatcherHost* new_dispatcher_host) { |
| 256 DCHECK_EQ(ChildProcessHost::kInvalidUniqueID, render_process_id_); |
| 257 DCHECK_NE(ChildProcessHost::kInvalidUniqueID, new_process_id); |
| 258 |
| 259 render_process_id_ = new_process_id; |
| 260 render_frame_id_ = new_frame_id; |
| 261 provider_id_ = new_provider_id; |
| 262 dispatcher_host_ = new_dispatcher_host; |
| 263 |
| 264 for (const GURL& pattern : associated_patterns_) |
| 265 IncreaseProcessReference(pattern); |
| 266 |
| 267 if (associated_registration_.get()) { |
| 268 IncreaseProcessReference(associated_registration_->pattern()); |
| 269 SendAssociateRegistrationMessage(); |
| 270 if (dispatcher_host_ && associated_registration_->active_version()) { |
| 271 dispatcher_host_->Send(new ServiceWorkerMsg_SetControllerServiceWorker( |
| 272 kDocumentMainThreadId, provider_id(), |
| 273 CreateHandleAndPass(associated_registration_->active_version()))); |
| 274 } |
| 275 } |
| 276 } |
| 277 |
| 278 void ServiceWorkerProviderHost::SendAssociateRegistrationMessage() { |
| 279 if (!dispatcher_host_) |
| 280 return; |
| 281 |
| 282 ServiceWorkerRegistrationHandle* handle = |
| 283 dispatcher_host_->GetOrCreateRegistrationHandle( |
| 284 provider_id(), associated_registration_.get()); |
| 285 |
| 286 ServiceWorkerVersionAttributes attrs; |
| 287 attrs.installing = handle->CreateServiceWorkerHandleAndPass( |
| 288 associated_registration_->installing_version()); |
| 289 attrs.waiting = handle->CreateServiceWorkerHandleAndPass( |
| 290 associated_registration_->waiting_version()); |
| 291 attrs.active = handle->CreateServiceWorkerHandleAndPass( |
| 292 associated_registration_->active_version()); |
| 293 |
| 294 dispatcher_host_->Send(new ServiceWorkerMsg_AssociateRegistration( |
| 295 kDocumentMainThreadId, provider_id(), handle->GetObjectInfo(), attrs)); |
| 296 } |
| 297 |
251 ServiceWorkerObjectInfo ServiceWorkerProviderHost::CreateHandleAndPass( | 298 ServiceWorkerObjectInfo ServiceWorkerProviderHost::CreateHandleAndPass( |
252 ServiceWorkerVersion* version) { | 299 ServiceWorkerVersion* version) { |
253 ServiceWorkerObjectInfo info; | 300 ServiceWorkerObjectInfo info; |
254 if (context_ && version) { | 301 if (context_ && version) { |
255 scoped_ptr<ServiceWorkerHandle> handle = | 302 scoped_ptr<ServiceWorkerHandle> handle = |
256 ServiceWorkerHandle::Create(context_, | 303 ServiceWorkerHandle::Create(context_, |
257 dispatcher_host_, | 304 dispatcher_host_, |
258 kDocumentMainThreadId, | 305 kDocumentMainThreadId, |
259 provider_id_, | 306 provider_id_, |
260 version); | 307 version); |
(...skipping 17 matching lines...) Expand all Loading... |
278 context_->process_manager()->RemoveProcessReferenceFromPattern( | 325 context_->process_manager()->RemoveProcessReferenceFromPattern( |
279 pattern, render_process_id_); | 326 pattern, render_process_id_); |
280 } | 327 } |
281 } | 328 } |
282 | 329 |
283 bool ServiceWorkerProviderHost::IsContextAlive() { | 330 bool ServiceWorkerProviderHost::IsContextAlive() { |
284 return context_ != NULL; | 331 return context_ != NULL; |
285 } | 332 } |
286 | 333 |
287 } // namespace content | 334 } // namespace content |
OLD | NEW |