Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_request_handler.cc
blobc0b739494a5b8f3a3ed10fd864a42329ca88eb35
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());
83 // A page load with skip_service_worker should be triggered by
84 // shift-reload, so retain all live matching registrations.
85 provider_host->AddAllMatchingRegistrations();
87 return;
90 scoped_ptr<ServiceWorkerRequestHandler> handler(
91 provider_host->CreateRequestHandler(request_mode,
92 credentials_mode,
93 resource_type,
94 request_context_type,
95 frame_type,
96 blob_storage_context->AsWeakPtr(),
97 body));
98 if (!handler)
99 return;
101 request->SetUserData(&kUserDataKey, handler.release());
104 ServiceWorkerRequestHandler* ServiceWorkerRequestHandler::GetHandler(
105 net::URLRequest* request) {
106 return static_cast<ServiceWorkerRequestHandler*>(
107 request->GetUserData(&kUserDataKey));
110 scoped_ptr<net::URLRequestInterceptor>
111 ServiceWorkerRequestHandler::CreateInterceptor(
112 ResourceContext* resource_context) {
113 return scoped_ptr<net::URLRequestInterceptor>(
114 new ServiceWorkerRequestInterceptor(resource_context));
117 bool ServiceWorkerRequestHandler::IsControlledByServiceWorker(
118 net::URLRequest* request) {
119 ServiceWorkerRequestHandler* handler = GetHandler(request);
120 if (!handler || !handler->provider_host_)
121 return false;
122 return handler->provider_host_->associated_registration() ||
123 handler->provider_host_->running_hosted_version();
126 void ServiceWorkerRequestHandler::PrepareForCrossSiteTransfer(
127 int old_process_id) {
128 if (!provider_host_ || !context_)
129 return;
130 old_process_id_ = old_process_id;
131 old_provider_id_ = provider_host_->provider_id();
132 host_for_cross_site_transfer_ =
133 context_->TransferProviderHostOut(old_process_id,
134 provider_host_->provider_id());
135 DCHECK_EQ(provider_host_.get(), host_for_cross_site_transfer_.get());
138 void ServiceWorkerRequestHandler::CompleteCrossSiteTransfer(
139 int new_process_id, int new_provider_id) {
140 if (!host_for_cross_site_transfer_.get() || !context_)
141 return;
142 DCHECK_EQ(provider_host_.get(), host_for_cross_site_transfer_.get());
143 context_->TransferProviderHostIn(
144 new_process_id,
145 new_provider_id,
146 host_for_cross_site_transfer_.Pass());
147 DCHECK_EQ(provider_host_->provider_id(), new_provider_id);
150 void ServiceWorkerRequestHandler::MaybeCompleteCrossSiteTransferInOldProcess(
151 int old_process_id) {
152 if (!host_for_cross_site_transfer_.get() || !context_ ||
153 old_process_id_ != old_process_id) {
154 return;
156 CompleteCrossSiteTransfer(old_process_id_, old_provider_id_);
159 ServiceWorkerRequestHandler::~ServiceWorkerRequestHandler() {
162 ServiceWorkerRequestHandler::ServiceWorkerRequestHandler(
163 base::WeakPtr<ServiceWorkerContextCore> context,
164 base::WeakPtr<ServiceWorkerProviderHost> provider_host,
165 base::WeakPtr<storage::BlobStorageContext> blob_storage_context,
166 ResourceType resource_type)
167 : context_(context),
168 provider_host_(provider_host),
169 blob_storage_context_(blob_storage_context),
170 resource_type_(resource_type),
171 old_process_id_(0),
172 old_provider_id_(kInvalidServiceWorkerProviderId) {
175 } // namespace content