Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / service_worker_request_handler.cc
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.
4
5 #include "content/browser/service_worker/service_worker_request_handler.h"
6
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"
15
16 namespace content {
17
18 namespace {
19
20 int kUserDataKey;  // Key value is not important.
21
22 class ServiceWorkerRequestInterceptor
23     : public net::URLRequestJobFactory::ProtocolHandler {
24  public:
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);
32     if (!handler)
33       return NULL;
34     return handler->MaybeCreateJob(request, network_delegate);
35   }
36
37  private:
38   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRequestInterceptor);
39 };
40
41 }  // namespace
42
43 void ServiceWorkerRequestHandler::InitializeHandler(
44     net::URLRequest* request,
45     ServiceWorkerContextWrapper* context_wrapper,
46     int process_id,
47     int provider_id,
48     ResourceType::Type resource_type) {
49   if (!ServiceWorkerUtils::IsFeatureEnabled())
50     return;
51
52   if (!context_wrapper || !context_wrapper->context() ||
53       provider_id == kInvalidServiceWorkerProviderId) {
54     return;
55   }
56
57   ServiceWorkerProviderHost* provider_host =
58       context_wrapper->context()->GetProviderHost(process_id, provider_id);
59   if (!provider_host)
60     return;
61
62   scoped_ptr<ServiceWorkerRequestHandler> handler(
63       provider_host->CreateRequestHandler(resource_type));
64   if (!handler)
65     return;
66
67   request->SetUserData(&kUserDataKey, handler.release());
68 }
69
70 ServiceWorkerRequestHandler* ServiceWorkerRequestHandler::GetHandler(
71     net::URLRequest* request) {
72   return reinterpret_cast<ServiceWorkerRequestHandler*>(
73       request->GetUserData(&kUserDataKey));
74 }
75
76 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
77 ServiceWorkerRequestHandler::CreateInterceptor() {
78   return make_scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>(
79       new ServiceWorkerRequestInterceptor);
80 }
81
82 ServiceWorkerRequestHandler::~ServiceWorkerRequestHandler() {
83 }
84
85 ServiceWorkerRequestHandler::ServiceWorkerRequestHandler(
86     base::WeakPtr<ServiceWorkerContextCore> context,
87     base::WeakPtr<ServiceWorkerProviderHost> provider_host,
88     ResourceType::Type resource_type)
89     : context_(context),
90       provider_host_(provider_host),
91       resource_type_(resource_type) {
92 }
93
94 }  // namespace content