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"
9 #include "content/browser/service_worker/service_worker_context_core.h"
10 #include "content/browser/service_worker/service_worker_context_wrapper.h"
11 #include "content/browser/service_worker/service_worker_provider_host.h"
12 #include "content/browser/service_worker/service_worker_registration.h"
13 #include "content/browser/service_worker/service_worker_url_request_job.h"
14 #include "content/browser/service_worker/service_worker_utils.h"
15 #include "content/common/resource_request_body.h"
16 #include "content/common/service_worker/service_worker_types.h"
17 #include "net/url_request/url_request.h"
18 #include "net/url_request/url_request_interceptor.h"
19 #include "webkit/browser/blob/blob_storage_context.h"
25 int kUserDataKey
; // Key value is not important.
27 class ServiceWorkerRequestInterceptor
28 : public net::URLRequestInterceptor
{
30 ServiceWorkerRequestInterceptor() {}
31 virtual ~ServiceWorkerRequestInterceptor() {}
32 virtual net::URLRequestJob
* MaybeInterceptRequest(
33 net::URLRequest
* request
,
34 net::NetworkDelegate
* network_delegate
) const OVERRIDE
{
35 ServiceWorkerRequestHandler
* handler
=
36 ServiceWorkerRequestHandler::GetHandler(request
);
39 return handler
->MaybeCreateJob(request
, network_delegate
);
43 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRequestInterceptor
);
48 void ServiceWorkerRequestHandler::InitializeHandler(
49 net::URLRequest
* request
,
50 ServiceWorkerContextWrapper
* context_wrapper
,
51 storage::BlobStorageContext
* blob_storage_context
,
54 ResourceType resource_type
,
55 scoped_refptr
<ResourceRequestBody
> body
) {
56 if (!request
->url().SchemeIsHTTPOrHTTPS())
59 if (!context_wrapper
|| !context_wrapper
->context() ||
60 provider_id
== kInvalidServiceWorkerProviderId
) {
64 ServiceWorkerProviderHost
* provider_host
=
65 context_wrapper
->context()->GetProviderHost(process_id
, provider_id
);
66 if (!provider_host
|| !provider_host
->IsContextAlive())
69 scoped_ptr
<ServiceWorkerRequestHandler
> handler(
70 provider_host
->CreateRequestHandler(
71 resource_type
, blob_storage_context
->AsWeakPtr(), body
));
75 request
->SetUserData(&kUserDataKey
, handler
.release());
78 ServiceWorkerRequestHandler
* ServiceWorkerRequestHandler::GetHandler(
79 net::URLRequest
* request
) {
80 return reinterpret_cast<ServiceWorkerRequestHandler
*>(
81 request
->GetUserData(&kUserDataKey
));
84 scoped_ptr
<net::URLRequestInterceptor
>
85 ServiceWorkerRequestHandler::CreateInterceptor() {
86 return scoped_ptr
<net::URLRequestInterceptor
>(
87 new ServiceWorkerRequestInterceptor
);
90 ServiceWorkerRequestHandler::~ServiceWorkerRequestHandler() {
93 ServiceWorkerRequestHandler::ServiceWorkerRequestHandler(
94 base::WeakPtr
<ServiceWorkerContextCore
> context
,
95 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
,
96 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context
,
97 ResourceType resource_type
)
99 provider_host_(provider_host
),
100 blob_storage_context_(blob_storage_context
),
101 resource_type_(resource_type
) {
104 } // namespace content