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 17 matching lines...) Expand all Loading... |
28 : process_id_(process_id), | 28 : process_id_(process_id), |
29 provider_id_(provider_id), | 29 provider_id_(provider_id), |
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_) | 38 if (controlling_version_.get()) |
39 controlling_version_->RemoveControllee(this); | 39 controlling_version_->RemoveControllee(this); |
40 if (active_version_) | 40 if (active_version_.get()) |
41 active_version_->RemovePotentialControllee(this); | 41 active_version_->RemovePotentialControllee(this); |
42 if (waiting_version_) | 42 if (waiting_version_.get()) |
43 waiting_version_->RemovePotentialControllee(this); | 43 waiting_version_->RemovePotentialControllee(this); |
44 if (installing_version_) | 44 if (installing_version_.get()) |
45 installing_version_->RemovePotentialControllee(this); | 45 installing_version_->RemovePotentialControllee(this); |
46 if (associated_registration_) | 46 if (associated_registration_.get()) |
47 associated_registration_->RemoveListener(this); | 47 associated_registration_->RemoveListener(this); |
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_, registration); | 54 DCHECK_EQ(associated_registration_.get(), registration); |
55 SetVersionAttributes(registration->installing_version(), | 55 SetVersionAttributes(registration->installing_version(), |
56 registration->waiting_version(), | 56 registration->waiting_version(), |
57 registration->active_version()); | 57 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_, 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::SetVersionAttributes( | 71 void ServiceWorkerProviderHost::SetVersionAttributes( |
72 ServiceWorkerVersion* installing_version, | 72 ServiceWorkerVersion* installing_version, |
73 ServiceWorkerVersion* waiting_version, | 73 ServiceWorkerVersion* waiting_version, |
74 ServiceWorkerVersion* active_version) { | 74 ServiceWorkerVersion* active_version) { |
75 ChangedVersionAttributesMask mask; | 75 ChangedVersionAttributesMask mask; |
76 | 76 |
77 if (installing_version != installing_version_) { | 77 if (installing_version != installing_version_.get()) { |
78 SetVersionAttributesInternal(installing_version, &installing_version_); | 78 SetVersionAttributesInternal(installing_version, &installing_version_); |
79 mask.add(ChangedVersionAttributesMask::INSTALLING_VERSION); | 79 mask.add(ChangedVersionAttributesMask::INSTALLING_VERSION); |
80 } | 80 } |
81 if (waiting_version != waiting_version_) { | 81 if (waiting_version != waiting_version_.get()) { |
82 SetVersionAttributesInternal(waiting_version, &waiting_version_); | 82 SetVersionAttributesInternal(waiting_version, &waiting_version_); |
83 mask.add(ChangedVersionAttributesMask::WAITING_VERSION); | 83 mask.add(ChangedVersionAttributesMask::WAITING_VERSION); |
84 } | 84 } |
85 if (active_version != active_version_) { | 85 if (active_version != active_version_.get()) { |
86 SetVersionAttributesInternal(active_version, &active_version_); | 86 SetVersionAttributesInternal(active_version, &active_version_); |
87 mask.add(ChangedVersionAttributesMask::ACTIVE_VERSION); | 87 mask.add(ChangedVersionAttributesMask::ACTIVE_VERSION); |
88 } | 88 } |
89 | 89 |
90 if (!dispatcher_host_) | 90 if (!dispatcher_host_) |
91 return; // Could be NULL in some tests. | 91 return; // Could be NULL in some tests. |
92 if (!mask.changed()) | 92 if (!mask.changed()) |
93 return; | 93 return; |
94 | 94 |
95 ServiceWorkerVersionAttributes attributes; | 95 ServiceWorkerVersionAttributes attributes; |
(...skipping 12 matching lines...) Expand all Loading... |
108 attributes)); | 108 attributes)); |
109 } | 109 } |
110 | 110 |
111 void ServiceWorkerProviderHost::SetVersionAttributesInternal( | 111 void ServiceWorkerProviderHost::SetVersionAttributesInternal( |
112 ServiceWorkerVersion* version, | 112 ServiceWorkerVersion* version, |
113 scoped_refptr<ServiceWorkerVersion>* data_member) { | 113 scoped_refptr<ServiceWorkerVersion>* data_member) { |
114 scoped_refptr<ServiceWorkerVersion> previous_version = *data_member; | 114 scoped_refptr<ServiceWorkerVersion> previous_version = *data_member; |
115 *data_member = version; | 115 *data_member = version; |
116 if (version) | 116 if (version) |
117 version->AddPotentialControllee(this); | 117 version->AddPotentialControllee(this); |
118 if (previous_version) | 118 if (previous_version.get()) |
119 previous_version->RemovePotentialControllee(this); | 119 previous_version->RemovePotentialControllee(this); |
120 } | 120 } |
121 | 121 |
122 void ServiceWorkerProviderHost::SetControllerVersionAttribute( | 122 void ServiceWorkerProviderHost::SetControllerVersionAttribute( |
123 ServiceWorkerVersion* version) { | 123 ServiceWorkerVersion* version) { |
124 if (version == controlling_version_) | 124 if (version == controlling_version_.get()) |
125 return; | 125 return; |
126 | 126 |
127 scoped_refptr<ServiceWorkerVersion> previous_version = controlling_version_; | 127 scoped_refptr<ServiceWorkerVersion> previous_version = controlling_version_; |
128 controlling_version_ = version; | 128 controlling_version_ = version; |
129 if (version) | 129 if (version) |
130 version->AddControllee(this); | 130 version->AddControllee(this); |
131 if (previous_version) | 131 if (previous_version.get()) |
132 previous_version->RemoveControllee(this); | 132 previous_version->RemoveControllee(this); |
133 | 133 |
134 if (!dispatcher_host_) | 134 if (!dispatcher_host_) |
135 return; // Could be NULL in some tests. | 135 return; // Could be NULL in some tests. |
136 | 136 |
137 dispatcher_host_->Send(new ServiceWorkerMsg_SetControllerServiceWorker( | 137 dispatcher_host_->Send(new ServiceWorkerMsg_SetControllerServiceWorker( |
138 kDocumentMainThreadId, provider_id(), CreateHandleAndPass(version))); | 138 kDocumentMainThreadId, provider_id(), CreateHandleAndPass(version))); |
139 } | 139 } |
140 | 140 |
141 void ServiceWorkerProviderHost::ClearVersionAttributes() { | 141 void ServiceWorkerProviderHost::ClearVersionAttributes() { |
142 SetVersionAttributes(NULL, NULL, NULL); | 142 SetVersionAttributes(NULL, NULL, NULL); |
143 SetControllerVersionAttribute(NULL); | 143 SetControllerVersionAttribute(NULL); |
144 } | 144 } |
145 | 145 |
146 bool ServiceWorkerProviderHost::SetHostedVersionId(int64 version_id) { | 146 bool ServiceWorkerProviderHost::SetHostedVersionId(int64 version_id) { |
147 if (!context_) | 147 if (!context_) |
148 return true; // System is shutting down. | 148 return true; // System is shutting down. |
149 if (active_version_) | 149 if (active_version_.get()) |
150 return false; // Unexpected bad message. | 150 return false; // Unexpected bad message. |
151 | 151 |
152 ServiceWorkerVersion* live_version = context_->GetLiveVersion(version_id); | 152 ServiceWorkerVersion* live_version = context_->GetLiveVersion(version_id); |
153 if (!live_version) | 153 if (!live_version) |
154 return true; // Was deleted before it got started. | 154 return true; // Was deleted before it got started. |
155 | 155 |
156 ServiceWorkerVersionInfo info = live_version->GetInfo(); | 156 ServiceWorkerVersionInfo info = live_version->GetInfo(); |
157 if (info.running_status != ServiceWorkerVersion::STARTING || | 157 if (info.running_status != ServiceWorkerVersion::STARTING || |
158 info.process_id != process_id_) { | 158 info.process_id != process_id_) { |
159 // If we aren't trying to start this version in our process | 159 // If we aren't trying to start this version in our process |
(...skipping 10 matching lines...) Expand all Loading... |
170 DCHECK(CanAssociateRegistration(registration)); | 170 DCHECK(CanAssociateRegistration(registration)); |
171 associated_registration_ = registration; | 171 associated_registration_ = registration; |
172 registration->AddListener(this); | 172 registration->AddListener(this); |
173 SetVersionAttributes(registration->installing_version(), | 173 SetVersionAttributes(registration->installing_version(), |
174 registration->waiting_version(), | 174 registration->waiting_version(), |
175 registration->active_version()); | 175 registration->active_version()); |
176 SetControllerVersionAttribute(registration->active_version()); | 176 SetControllerVersionAttribute(registration->active_version()); |
177 } | 177 } |
178 | 178 |
179 void ServiceWorkerProviderHost::UnassociateRegistration() { | 179 void ServiceWorkerProviderHost::UnassociateRegistration() { |
180 if (!associated_registration_) | 180 if (!associated_registration_.get()) |
181 return; | 181 return; |
182 associated_registration_->RemoveListener(this); | 182 associated_registration_->RemoveListener(this); |
183 associated_registration_ = NULL; | 183 associated_registration_ = NULL; |
184 ClearVersionAttributes(); | 184 ClearVersionAttributes(); |
185 } | 185 } |
186 | 186 |
187 scoped_ptr<ServiceWorkerRequestHandler> | 187 scoped_ptr<ServiceWorkerRequestHandler> |
188 ServiceWorkerProviderHost::CreateRequestHandler( | 188 ServiceWorkerProviderHost::CreateRequestHandler( |
189 ResourceType resource_type, | 189 ResourceType resource_type, |
190 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, | 190 base::WeakPtr<storage::BlobStorageContext> blob_storage_context, |
191 scoped_refptr<ResourceRequestBody> body) { | 191 scoped_refptr<ResourceRequestBody> body) { |
192 if (IsHostToRunningServiceWorker()) { | 192 if (IsHostToRunningServiceWorker()) { |
193 return scoped_ptr<ServiceWorkerRequestHandler>( | 193 return scoped_ptr<ServiceWorkerRequestHandler>( |
194 new ServiceWorkerContextRequestHandler( | 194 new ServiceWorkerContextRequestHandler( |
195 context_, AsWeakPtr(), blob_storage_context, resource_type)); | 195 context_, AsWeakPtr(), blob_storage_context, resource_type)); |
196 } | 196 } |
197 if (ServiceWorkerUtils::IsMainResourceType(resource_type) || | 197 if (ServiceWorkerUtils::IsMainResourceType(resource_type) || |
198 active_version()) { | 198 active_version()) { |
199 return scoped_ptr<ServiceWorkerRequestHandler>( | 199 return scoped_ptr<ServiceWorkerRequestHandler>( |
200 new ServiceWorkerControlleeRequestHandler( | 200 new ServiceWorkerControlleeRequestHandler( |
201 context_, AsWeakPtr(), blob_storage_context, resource_type, body)); | 201 context_, AsWeakPtr(), blob_storage_context, resource_type, body)); |
202 } | 202 } |
203 return scoped_ptr<ServiceWorkerRequestHandler>(); | 203 return scoped_ptr<ServiceWorkerRequestHandler>(); |
204 } | 204 } |
205 | 205 |
206 bool ServiceWorkerProviderHost::CanAssociateRegistration( | 206 bool ServiceWorkerProviderHost::CanAssociateRegistration( |
207 ServiceWorkerRegistration* registration) { | 207 ServiceWorkerRegistration* registration) { |
208 if (!context_) | 208 if (!context_) |
209 return false; | 209 return false; |
210 if (running_hosted_version_) | 210 if (running_hosted_version_.get()) |
211 return false; | 211 return false; |
212 if (!registration || associated_registration_) | 212 if (!registration || associated_registration_.get()) |
213 return false; | 213 return false; |
214 return true; | 214 return true; |
215 } | 215 } |
216 | 216 |
217 void ServiceWorkerProviderHost::PostMessage( | 217 void ServiceWorkerProviderHost::PostMessage( |
218 const base::string16& message, | 218 const base::string16& message, |
219 const std::vector<int>& sent_message_port_ids) { | 219 const std::vector<int>& sent_message_port_ids) { |
220 if (!dispatcher_host_) | 220 if (!dispatcher_host_) |
221 return; // Could be NULL in some tests. | 221 return; // Could be NULL in some tests. |
222 | 222 |
(...skipping 24 matching lines...) Expand all Loading... |
247 dispatcher_host_->RegisterServiceWorkerHandle(handle.Pass()); | 247 dispatcher_host_->RegisterServiceWorkerHandle(handle.Pass()); |
248 } | 248 } |
249 return info; | 249 return info; |
250 } | 250 } |
251 | 251 |
252 bool ServiceWorkerProviderHost::IsContextAlive() { | 252 bool ServiceWorkerProviderHost::IsContextAlive() { |
253 return context_ != NULL; | 253 return context_ != NULL; |
254 } | 254 } |
255 | 255 |
256 } // namespace content | 256 } // namespace content |
OLD | NEW |