Simplify ChildProcessLauncher
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_request_handler.cc
blobc70b3fd9b9e59e40c0f085b255cdc191dc1df0b0
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 <string>
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 "content/public/browser/resource_context.h"
18 #include "net/base/net_util.h"
19 #include "net/url_request/url_request.h"
20 #include "net/url_request/url_request_interceptor.h"
21 #include "storage/browser/blob/blob_storage_context.h"
23 namespace content {
25 namespace {
27 int kUserDataKey; // Key value is not important.
29 class ServiceWorkerRequestInterceptor
30 : public net::URLRequestInterceptor {
31 public:
32 explicit ServiceWorkerRequestInterceptor(ResourceContext* resource_context)
33 : resource_context_(resource_context) {}
34 ~ServiceWorkerRequestInterceptor() override {}
35 net::URLRequestJob* MaybeInterceptRequest(
36 net::URLRequest* request,
37 net::NetworkDelegate* network_delegate) const override {
38 ServiceWorkerRequestHandler* handler =
39 ServiceWorkerRequestHandler::GetHandler(request);
40 if (!handler)
41 return NULL;
42 return handler->MaybeCreateJob(
43 request, network_delegate, resource_context_);
46 private:
47 ResourceContext* resource_context_;
48 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRequestInterceptor);
51 } // namespace
53 void ServiceWorkerRequestHandler::InitializeHandler(
54 net::URLRequest* request,
55 ServiceWorkerContextWrapper* context_wrapper,
56 storage::BlobStorageContext* blob_storage_context,
57 int process_id,
58 int provider_id,
59 bool skip_service_worker,
60 FetchRequestMode request_mode,
61 FetchCredentialsMode credentials_mode,
62 ResourceType resource_type,
63 RequestContextType request_context_type,
64 RequestContextFrameType frame_type,
65 scoped_refptr<ResourceRequestBody> body) {
66 if (!request->url().SchemeIsHTTPOrHTTPS())
67 return;
69 if (!context_wrapper || !context_wrapper->context() ||
70 provider_id == kInvalidServiceWorkerProviderId) {
71 return;
74 ServiceWorkerProviderHost* provider_host =
75 context_wrapper->context()->GetProviderHost(process_id, provider_id);
76 if (!provider_host || !provider_host->IsContextAlive())
77 return;
79 if (skip_service_worker) {
80 if (ServiceWorkerUtils::IsMainResourceType(resource_type)) {
81 provider_host->SetDocumentUrl(net::SimplifyUrlForRequest(request->url()));
82 provider_host->SetTopmostFrameUrl(request->first_party_for_cookies());
84 return;
87 scoped_ptr<ServiceWorkerRequestHandler> handler(
88 provider_host->CreateRequestHandler(request_mode,
89 credentials_mode,
90 resource_type,
91 request_context_type,
92 frame_type,
93 blob_storage_context->AsWeakPtr(),
94 body));
95 if (!handler)
96 return;
98 request->SetUserData(&kUserDataKey, handler.release());
101 ServiceWorkerRequestHandler* ServiceWorkerRequestHandler::GetHandler(
102 net::URLRequest* request) {
103 return static_cast<ServiceWorkerRequestHandler*>(
104 request->GetUserData(&kUserDataKey));
107 scoped_ptr<net::URLRequestInterceptor>
108 ServiceWorkerRequestHandler::CreateInterceptor(
109 ResourceContext* resource_context) {
110 return scoped_ptr<net::URLRequestInterceptor>(
111 new ServiceWorkerRequestInterceptor(resource_context));
114 bool ServiceWorkerRequestHandler::IsControlledByServiceWorker(
115 net::URLRequest* request) {
116 ServiceWorkerRequestHandler* handler = GetHandler(request);
117 if (!handler || !handler->provider_host_)
118 return false;
119 return handler->provider_host_->associated_registration() ||
120 handler->provider_host_->running_hosted_version();
123 void ServiceWorkerRequestHandler::PrepareForCrossSiteTransfer(
124 int old_process_id) {
125 if (!provider_host_ || !context_)
126 return;
127 old_process_id_ = old_process_id;
128 old_provider_id_ = provider_host_->provider_id();
129 host_for_cross_site_transfer_ =
130 context_->TransferProviderHostOut(old_process_id,
131 provider_host_->provider_id());
132 DCHECK_EQ(provider_host_.get(), host_for_cross_site_transfer_.get());
135 void ServiceWorkerRequestHandler::CompleteCrossSiteTransfer(
136 int new_process_id, int new_provider_id) {
137 if (!host_for_cross_site_transfer_.get() || !context_)
138 return;
139 DCHECK_EQ(provider_host_.get(), host_for_cross_site_transfer_.get());
140 context_->TransferProviderHostIn(
141 new_process_id,
142 new_provider_id,
143 host_for_cross_site_transfer_.Pass());
144 DCHECK_EQ(provider_host_->provider_id(), new_provider_id);
147 void ServiceWorkerRequestHandler::MaybeCompleteCrossSiteTransferInOldProcess(
148 int old_process_id) {
149 if (!host_for_cross_site_transfer_.get() || !context_ ||
150 old_process_id_ != old_process_id) {
151 return;
153 CompleteCrossSiteTransfer(old_process_id_, old_provider_id_);
156 ServiceWorkerRequestHandler::~ServiceWorkerRequestHandler() {
159 ServiceWorkerRequestHandler::ServiceWorkerRequestHandler(
160 base::WeakPtr<ServiceWorkerContextCore> context,
161 base::WeakPtr<ServiceWorkerProviderHost> provider_host,
162 base::WeakPtr<storage::BlobStorageContext> blob_storage_context,
163 ResourceType resource_type)
164 : context_(context),
165 provider_host_(provider_host),
166 blob_storage_context_(blob_storage_context),
167 resource_type_(resource_type),
168 old_process_id_(0),
169 old_provider_id_(kInvalidServiceWorkerProviderId) {
172 } // namespace content