| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/guid.h" | 9 #include "base/guid.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "content/browser/message_port_message_filter.h" | 12 #include "content/browser/message_port_message_filter.h" |
| 13 #include "content/browser/service_worker/service_worker_context_core.h" | 13 #include "content/browser/service_worker/service_worker_context_core.h" |
| 14 #include "content/browser/service_worker/service_worker_context_request_handler.
h" | 14 #include "content/browser/service_worker/service_worker_context_request_handler.
h" |
| 15 #include "content/browser/service_worker/service_worker_controllee_request_handl
er.h" | 15 #include "content/browser/service_worker/service_worker_controllee_request_handl
er.h" |
| 16 #include "content/browser/service_worker/service_worker_dispatcher_host.h" | 16 #include "content/browser/service_worker/service_worker_dispatcher_host.h" |
| 17 #include "content/browser/service_worker/service_worker_handle.h" | 17 #include "content/browser/service_worker/service_worker_handle.h" |
| 18 #include "content/browser/service_worker/service_worker_registration_handle.h" | 18 #include "content/browser/service_worker/service_worker_registration_handle.h" |
| 19 #include "content/browser/service_worker/service_worker_version.h" | 19 #include "content/browser/service_worker/service_worker_version.h" |
| 20 #include "content/common/resource_request_body.h" | 20 #include "content/common/resource_request_body.h" |
| 21 #include "content/common/service_worker/service_worker_messages.h" | 21 #include "content/common/service_worker/service_worker_messages.h" |
| 22 #include "content/common/service_worker/service_worker_types.h" | 22 #include "content/common/service_worker/service_worker_types.h" |
| 23 #include "content/common/service_worker/service_worker_utils.h" | 23 #include "content/common/service_worker/service_worker_utils.h" |
| 24 #include "content/public/browser/content_browser_client.h" |
| 24 #include "content/public/common/browser_side_navigation_policy.h" | 25 #include "content/public/common/browser_side_navigation_policy.h" |
| 25 #include "content/public/common/child_process_host.h" | 26 #include "content/public/common/child_process_host.h" |
| 27 #include "content/public/common/content_client.h" |
| 28 #include "content/public/common/origin_util.h" |
| 26 | 29 |
| 27 namespace content { | 30 namespace content { |
| 28 | 31 |
| 29 namespace { | 32 namespace { |
| 30 | 33 |
| 31 // PlzNavigate | 34 // PlzNavigate |
| 32 // Next ServiceWorkerProviderHost ID for navigations, starts at -2 and keeps | 35 // Next ServiceWorkerProviderHost ID for navigations, starts at -2 and keeps |
| 33 // going down. | 36 // going down. |
| 34 int g_next_navigation_provider_id = -2; | 37 int g_next_navigation_provider_id = -2; |
| 35 | 38 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 47 // static | 50 // static |
| 48 std::unique_ptr<ServiceWorkerProviderHost> | 51 std::unique_ptr<ServiceWorkerProviderHost> |
| 49 ServiceWorkerProviderHost::PreCreateNavigationHost( | 52 ServiceWorkerProviderHost::PreCreateNavigationHost( |
| 50 base::WeakPtr<ServiceWorkerContextCore> context) { | 53 base::WeakPtr<ServiceWorkerContextCore> context) { |
| 51 CHECK(IsBrowserSideNavigationEnabled()); | 54 CHECK(IsBrowserSideNavigationEnabled()); |
| 52 // Generate a new browser-assigned id for the host. | 55 // Generate a new browser-assigned id for the host. |
| 53 int provider_id = g_next_navigation_provider_id--; | 56 int provider_id = g_next_navigation_provider_id--; |
| 54 return std::unique_ptr<ServiceWorkerProviderHost>( | 57 return std::unique_ptr<ServiceWorkerProviderHost>( |
| 55 new ServiceWorkerProviderHost( | 58 new ServiceWorkerProviderHost( |
| 56 ChildProcessHost::kInvalidUniqueID, MSG_ROUTING_NONE, provider_id, | 59 ChildProcessHost::kInvalidUniqueID, MSG_ROUTING_NONE, provider_id, |
| 57 SERVICE_WORKER_PROVIDER_FOR_WINDOW, context, nullptr)); | 60 SERVICE_WORKER_PROVIDER_FOR_WINDOW, FrameSecurityLevel::UNINITIALIZED, |
| 61 context, nullptr)); |
| 58 } | 62 } |
| 59 | 63 |
| 60 ServiceWorkerProviderHost::ServiceWorkerProviderHost( | 64 ServiceWorkerProviderHost::ServiceWorkerProviderHost( |
| 61 int render_process_id, | 65 int render_process_id, |
| 62 int route_id, | 66 int route_id, |
| 63 int provider_id, | 67 int provider_id, |
| 64 ServiceWorkerProviderType provider_type, | 68 ServiceWorkerProviderType provider_type, |
| 69 FrameSecurityLevel parent_frame_security_level, |
| 65 base::WeakPtr<ServiceWorkerContextCore> context, | 70 base::WeakPtr<ServiceWorkerContextCore> context, |
| 66 ServiceWorkerDispatcherHost* dispatcher_host) | 71 ServiceWorkerDispatcherHost* dispatcher_host) |
| 67 : client_uuid_(base::GenerateGUID()), | 72 : client_uuid_(base::GenerateGUID()), |
| 68 render_process_id_(render_process_id), | 73 render_process_id_(render_process_id), |
| 69 route_id_(route_id), | 74 route_id_(route_id), |
| 70 render_thread_id_(kDocumentMainThreadId), | 75 render_thread_id_(kDocumentMainThreadId), |
| 71 provider_id_(provider_id), | 76 provider_id_(provider_id), |
| 72 provider_type_(provider_type), | 77 provider_type_(provider_type), |
| 78 parent_frame_security_level_(parent_frame_security_level), |
| 73 context_(context), | 79 context_(context), |
| 74 dispatcher_host_(dispatcher_host), | 80 dispatcher_host_(dispatcher_host), |
| 75 allow_association_(true) { | 81 allow_association_(true) { |
| 76 DCHECK_NE(SERVICE_WORKER_PROVIDER_UNKNOWN, provider_type_); | 82 DCHECK_NE(SERVICE_WORKER_PROVIDER_UNKNOWN, provider_type_); |
| 77 DCHECK_NE(SERVICE_WORKER_PROVIDER_FOR_SANDBOXED_FRAME, provider_type_); | |
| 78 | 83 |
| 79 // PlzNavigate | 84 // PlzNavigate |
| 80 CHECK(render_process_id != ChildProcessHost::kInvalidUniqueID || | 85 CHECK(render_process_id != ChildProcessHost::kInvalidUniqueID || |
| 81 IsBrowserSideNavigationEnabled()); | 86 IsBrowserSideNavigationEnabled()); |
| 82 | 87 |
| 83 if (provider_type_ == SERVICE_WORKER_PROVIDER_FOR_CONTROLLER) { | 88 if (provider_type_ == SERVICE_WORKER_PROVIDER_FOR_CONTROLLER) { |
| 84 // Actual thread id is set when the service worker context gets started. | 89 // Actual thread id is set when the service worker context gets started. |
| 85 render_thread_id_ = kInvalidEmbeddedWorkerThreadId; | 90 render_thread_id_ = kInvalidEmbeddedWorkerThreadId; |
| 86 } | 91 } |
| 87 context_->RegisterProviderHostByClientID(client_uuid_, this); | 92 context_->RegisterProviderHostByClientID(client_uuid_, this); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 105 for (const GURL& pattern : associated_patterns_) | 110 for (const GURL& pattern : associated_patterns_) |
| 106 DecreaseProcessReference(pattern); | 111 DecreaseProcessReference(pattern); |
| 107 } | 112 } |
| 108 | 113 |
| 109 int ServiceWorkerProviderHost::frame_id() const { | 114 int ServiceWorkerProviderHost::frame_id() const { |
| 110 if (provider_type_ == SERVICE_WORKER_PROVIDER_FOR_WINDOW) | 115 if (provider_type_ == SERVICE_WORKER_PROVIDER_FOR_WINDOW) |
| 111 return route_id_; | 116 return route_id_; |
| 112 return MSG_ROUTING_NONE; | 117 return MSG_ROUTING_NONE; |
| 113 } | 118 } |
| 114 | 119 |
| 120 bool ServiceWorkerProviderHost::IsContextSecureForServiceWorker() const { |
| 121 // |document_url_| may be empty if loading has not begun, or |
| 122 // ServiceWorkerRequestHandler didn't handle the load (because e.g. another |
| 123 // handler did first, or the initial request URL was such that |
| 124 // OriginCanAccessServiceWorkers returned false). |
| 125 if (!document_url_.is_valid()) |
| 126 return false; |
| 127 if (!OriginCanAccessServiceWorkers(document_url_)) |
| 128 return false; |
| 129 |
| 130 if (is_parent_frame_secure()) |
| 131 return true; |
| 132 |
| 133 std::set<std::string> schemes; |
| 134 GetContentClient()->browser()->GetSchemesBypassingSecureContextCheckWhitelist( |
| 135 &schemes); |
| 136 return schemes.find(document_url().scheme()) != schemes.end(); |
| 137 } |
| 138 |
| 115 void ServiceWorkerProviderHost::OnVersionAttributesChanged( | 139 void ServiceWorkerProviderHost::OnVersionAttributesChanged( |
| 116 ServiceWorkerRegistration* registration, | 140 ServiceWorkerRegistration* registration, |
| 117 ChangedVersionAttributesMask changed_mask, | 141 ChangedVersionAttributesMask changed_mask, |
| 118 const ServiceWorkerRegistrationInfo& info) { | 142 const ServiceWorkerRegistrationInfo& info) { |
| 119 if (!get_ready_callback_ || get_ready_callback_->called) | 143 if (!get_ready_callback_ || get_ready_callback_->called) |
| 120 return; | 144 return; |
| 121 if (changed_mask.active_changed() && registration->active_version()) { | 145 if (changed_mask.active_changed() && registration->active_version()) { |
| 122 // Wait until the state change so we don't send the get for ready | 146 // Wait until the state change so we don't send the get for ready |
| 123 // registration complete message before set version attributes message. | 147 // registration complete message before set version attributes message. |
| 124 registration->active_version()->RegisterStatusChangeCallback(base::Bind( | 148 registration->active_version()->RegisterStatusChangeCallback(base::Bind( |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 document_url_ = url; | 183 document_url_ = url; |
| 160 } | 184 } |
| 161 | 185 |
| 162 void ServiceWorkerProviderHost::SetTopmostFrameUrl(const GURL& url) { | 186 void ServiceWorkerProviderHost::SetTopmostFrameUrl(const GURL& url) { |
| 163 topmost_frame_url_ = url; | 187 topmost_frame_url_ = url; |
| 164 } | 188 } |
| 165 | 189 |
| 166 void ServiceWorkerProviderHost::SetControllerVersionAttribute( | 190 void ServiceWorkerProviderHost::SetControllerVersionAttribute( |
| 167 ServiceWorkerVersion* version, | 191 ServiceWorkerVersion* version, |
| 168 bool notify_controllerchange) { | 192 bool notify_controllerchange) { |
| 193 CHECK(!version || IsContextSecureForServiceWorker()); |
| 169 if (version == controlling_version_.get()) | 194 if (version == controlling_version_.get()) |
| 170 return; | 195 return; |
| 171 | 196 |
| 172 scoped_refptr<ServiceWorkerVersion> previous_version = controlling_version_; | 197 scoped_refptr<ServiceWorkerVersion> previous_version = controlling_version_; |
| 173 controlling_version_ = version; | 198 controlling_version_ = version; |
| 174 if (version) | 199 if (version) |
| 175 version->AddControllee(this); | 200 version->AddControllee(this); |
| 176 if (previous_version.get()) | 201 if (previous_version.get()) |
| 177 previous_version->RemoveControllee(this); | 202 previous_version->RemoveControllee(this); |
| 178 | 203 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 205 } | 230 } |
| 206 | 231 |
| 207 bool ServiceWorkerProviderHost::IsProviderForClient() const { | 232 bool ServiceWorkerProviderHost::IsProviderForClient() const { |
| 208 switch (provider_type_) { | 233 switch (provider_type_) { |
| 209 case SERVICE_WORKER_PROVIDER_FOR_WINDOW: | 234 case SERVICE_WORKER_PROVIDER_FOR_WINDOW: |
| 210 case SERVICE_WORKER_PROVIDER_FOR_WORKER: | 235 case SERVICE_WORKER_PROVIDER_FOR_WORKER: |
| 211 case SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER: | 236 case SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER: |
| 212 return true; | 237 return true; |
| 213 case SERVICE_WORKER_PROVIDER_FOR_CONTROLLER: | 238 case SERVICE_WORKER_PROVIDER_FOR_CONTROLLER: |
| 214 return false; | 239 return false; |
| 215 case SERVICE_WORKER_PROVIDER_FOR_SANDBOXED_FRAME: | |
| 216 case SERVICE_WORKER_PROVIDER_UNKNOWN: | 240 case SERVICE_WORKER_PROVIDER_UNKNOWN: |
| 217 NOTREACHED() << provider_type_; | 241 NOTREACHED() << provider_type_; |
| 218 } | 242 } |
| 219 NOTREACHED() << provider_type_; | 243 NOTREACHED() << provider_type_; |
| 220 return false; | 244 return false; |
| 221 } | 245 } |
| 222 | 246 |
| 223 blink::WebServiceWorkerClientType ServiceWorkerProviderHost::client_type() | 247 blink::WebServiceWorkerClientType ServiceWorkerProviderHost::client_type() |
| 224 const { | 248 const { |
| 225 switch (provider_type_) { | 249 switch (provider_type_) { |
| 226 case SERVICE_WORKER_PROVIDER_FOR_WINDOW: | 250 case SERVICE_WORKER_PROVIDER_FOR_WINDOW: |
| 227 return blink::WebServiceWorkerClientTypeWindow; | 251 return blink::WebServiceWorkerClientTypeWindow; |
| 228 case SERVICE_WORKER_PROVIDER_FOR_WORKER: | 252 case SERVICE_WORKER_PROVIDER_FOR_WORKER: |
| 229 return blink::WebServiceWorkerClientTypeWorker; | 253 return blink::WebServiceWorkerClientTypeWorker; |
| 230 case SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER: | 254 case SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER: |
| 231 return blink::WebServiceWorkerClientTypeSharedWorker; | 255 return blink::WebServiceWorkerClientTypeSharedWorker; |
| 232 case SERVICE_WORKER_PROVIDER_FOR_CONTROLLER: | 256 case SERVICE_WORKER_PROVIDER_FOR_CONTROLLER: |
| 233 case SERVICE_WORKER_PROVIDER_FOR_SANDBOXED_FRAME: | |
| 234 case SERVICE_WORKER_PROVIDER_UNKNOWN: | 257 case SERVICE_WORKER_PROVIDER_UNKNOWN: |
| 235 NOTREACHED() << provider_type_; | 258 NOTREACHED() << provider_type_; |
| 236 } | 259 } |
| 237 NOTREACHED() << provider_type_; | 260 NOTREACHED() << provider_type_; |
| 238 return blink::WebServiceWorkerClientTypeWindow; | 261 return blink::WebServiceWorkerClientTypeWindow; |
| 239 } | 262 } |
| 240 | 263 |
| 241 void ServiceWorkerProviderHost::AssociateRegistration( | 264 void ServiceWorkerProviderHost::AssociateRegistration( |
| 242 ServiceWorkerRegistration* registration, | 265 ServiceWorkerRegistration* registration, |
| 243 bool notify_controllerchange) { | 266 bool notify_controllerchange) { |
| 267 CHECK(IsContextSecureForServiceWorker()); |
| 244 DCHECK(CanAssociateRegistration(registration)); | 268 DCHECK(CanAssociateRegistration(registration)); |
| 245 associated_registration_ = registration; | 269 associated_registration_ = registration; |
| 246 AddMatchingRegistration(registration); | 270 AddMatchingRegistration(registration); |
| 247 SendAssociateRegistrationMessage(); | 271 SendAssociateRegistrationMessage(); |
| 248 SetControllerVersionAttribute(registration->active_version(), | 272 SetControllerVersionAttribute(registration->active_version(), |
| 249 notify_controllerchange); | 273 notify_controllerchange); |
| 250 } | 274 } |
| 251 | 275 |
| 252 void ServiceWorkerProviderHost::DisassociateRegistration() { | 276 void ServiceWorkerProviderHost::DisassociateRegistration() { |
| 253 queued_events_.clear(); | 277 queued_events_.clear(); |
| 254 if (!associated_registration_.get()) | 278 if (!associated_registration_.get()) |
| 255 return; | 279 return; |
| 256 associated_registration_ = NULL; | 280 associated_registration_ = NULL; |
| 257 SetControllerVersionAttribute(NULL, false /* notify_controllerchange */); | 281 SetControllerVersionAttribute(NULL, false /* notify_controllerchange */); |
| 258 | 282 |
| 259 if (!dispatcher_host_) | 283 if (!dispatcher_host_) |
| 260 return; | 284 return; |
| 261 | 285 |
| 262 // Disassociation message should be sent only for controllees. | 286 // Disassociation message should be sent only for controllees. |
| 263 DCHECK(IsProviderForClient()); | 287 DCHECK(IsProviderForClient()); |
| 264 Send(new ServiceWorkerMsg_DisassociateRegistration( | 288 Send(new ServiceWorkerMsg_DisassociateRegistration( |
| 265 render_thread_id_, provider_id())); | 289 render_thread_id_, provider_id())); |
| 266 } | 290 } |
| 267 | 291 |
| 268 void ServiceWorkerProviderHost::AddMatchingRegistration( | 292 void ServiceWorkerProviderHost::AddMatchingRegistration( |
| 269 ServiceWorkerRegistration* registration) { | 293 ServiceWorkerRegistration* registration) { |
| 270 DCHECK(ServiceWorkerUtils::ScopeMatches( | 294 DCHECK(ServiceWorkerUtils::ScopeMatches( |
| 271 registration->pattern(), document_url_)); | 295 registration->pattern(), document_url_)); |
| 296 if (!IsContextSecureForServiceWorker()) |
| 297 return; |
| 272 size_t key = registration->pattern().spec().size(); | 298 size_t key = registration->pattern().spec().size(); |
| 273 if (ContainsKey(matching_registrations_, key)) | 299 if (ContainsKey(matching_registrations_, key)) |
| 274 return; | 300 return; |
| 275 IncreaseProcessReference(registration->pattern()); | 301 IncreaseProcessReference(registration->pattern()); |
| 276 registration->AddListener(this); | 302 registration->AddListener(this); |
| 277 matching_registrations_[key] = registration; | 303 matching_registrations_[key] = registration; |
| 278 ReturnRegistrationForReadyIfNeeded(); | 304 ReturnRegistrationForReadyIfNeeded(); |
| 279 } | 305 } |
| 280 | 306 |
| 281 void ServiceWorkerProviderHost::RemoveMatchingRegistration( | 307 void ServiceWorkerProviderHost::RemoveMatchingRegistration( |
| (...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 646 Send(new ServiceWorkerMsg_SetControllerServiceWorker( | 672 Send(new ServiceWorkerMsg_SetControllerServiceWorker( |
| 647 render_thread_id_, provider_id(), | 673 render_thread_id_, provider_id(), |
| 648 GetOrCreateServiceWorkerHandle( | 674 GetOrCreateServiceWorkerHandle( |
| 649 associated_registration_->active_version()), | 675 associated_registration_->active_version()), |
| 650 false /* shouldNotifyControllerChange */)); | 676 false /* shouldNotifyControllerChange */)); |
| 651 } | 677 } |
| 652 } | 678 } |
| 653 } | 679 } |
| 654 | 680 |
| 655 } // namespace content | 681 } // namespace content |
| OLD | NEW |