1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/service_worker/service_worker_request_handler.h"
7 #include "content/browser/service_worker/service_worker_context_core.h"
8 #include "content/browser/service_worker/service_worker_context_wrapper.h"
9 #include "content/browser/service_worker/service_worker_provider_host.h"
10 #include "content/browser/service_worker/service_worker_registration.h"
11 #include "content/browser/service_worker/service_worker_url_request_job.h"
12 #include "content/browser/service_worker/service_worker_utils.h"
13 #include "content/common/service_worker/service_worker_types.h"
14 #include "net/url_request/url_request.h"
20 int kUserDataKey
; // Key value is not important.
22 class ServiceWorkerRequestInterceptor
23 : public net::URLRequestJobFactory::ProtocolHandler
{
25 ServiceWorkerRequestInterceptor() {}
26 virtual ~ServiceWorkerRequestInterceptor() {}
27 virtual net::URLRequestJob
* MaybeCreateJob(
28 net::URLRequest
* request
,
29 net::NetworkDelegate
* network_delegate
) const OVERRIDE
{
30 ServiceWorkerRequestHandler
* handler
=
31 ServiceWorkerRequestHandler::GetHandler(request
);
34 return handler
->MaybeCreateJob(request
, network_delegate
);
38 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRequestInterceptor
);
43 void ServiceWorkerRequestHandler::InitializeHandler(
44 net::URLRequest
* request
,
45 ServiceWorkerContextWrapper
* context_wrapper
,
48 ResourceType::Type resource_type
) {
49 if (!ServiceWorkerUtils::IsFeatureEnabled())
52 if (!context_wrapper
|| !context_wrapper
->context() ||
53 provider_id
== kInvalidServiceWorkerProviderId
) {
57 ServiceWorkerProviderHost
* provider_host
=
58 context_wrapper
->context()->GetProviderHost(process_id
, provider_id
);
62 scoped_ptr
<ServiceWorkerRequestHandler
> handler(
63 provider_host
->CreateRequestHandler(resource_type
));
67 request
->SetUserData(&kUserDataKey
, handler
.release());
70 ServiceWorkerRequestHandler
* ServiceWorkerRequestHandler::GetHandler(
71 net::URLRequest
* request
) {
72 return reinterpret_cast<ServiceWorkerRequestHandler
*>(
73 request
->GetUserData(&kUserDataKey
));
76 scoped_ptr
<net::URLRequestJobFactory::ProtocolHandler
>
77 ServiceWorkerRequestHandler::CreateInterceptor() {
78 return make_scoped_ptr
<net::URLRequestJobFactory::ProtocolHandler
>(
79 new ServiceWorkerRequestInterceptor
);
82 ServiceWorkerRequestHandler::~ServiceWorkerRequestHandler() {
85 ServiceWorkerRequestHandler::ServiceWorkerRequestHandler(
86 base::WeakPtr
<ServiceWorkerContextCore
> context
,
87 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
,
88 ResourceType::Type resource_type
)
90 provider_host_(provider_host
),
91 resource_type_(resource_type
) {
94 } // namespace content