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 "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"
27 int kUserDataKey; // Key value is not important.
29 class ServiceWorkerRequestInterceptor
30 : public net::URLRequestInterceptor {
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);
42 return handler->MaybeCreateJob(
43 request, network_delegate, resource_context_);
47 ResourceContext* resource_context_;
48 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRequestInterceptor);
51 // This is work around to avoid hijacking CORS preflight.
52 // TODO(horo): Remove this check when we implement "HTTP fetch" correctly.
53 // http://fetch.spec.whatwg.org/#concept-http-fetch
54 bool IsMethodSupportedForServiceWorker(const std::string& method) {
55 return method != "OPTIONS";
60 void ServiceWorkerRequestHandler::InitializeHandler(
61 net::URLRequest* request,
62 ServiceWorkerContextWrapper* context_wrapper,
63 storage::BlobStorageContext* blob_storage_context,
66 bool skip_service_worker,
67 FetchRequestMode request_mode,
68 FetchCredentialsMode credentials_mode,
69 ResourceType resource_type,
70 RequestContextType request_context_type,
71 RequestContextFrameType frame_type,
72 scoped_refptr<ResourceRequestBody> body) {
73 if (!request->url().SchemeIsHTTPOrHTTPS() ||
74 !IsMethodSupportedForServiceWorker(request->method())) {
78 if (!context_wrapper || !context_wrapper->context() ||
79 provider_id == kInvalidServiceWorkerProviderId) {
83 ServiceWorkerProviderHost* provider_host =
84 context_wrapper->context()->GetProviderHost(process_id, provider_id);
85 if (!provider_host || !provider_host->IsContextAlive())
88 if (skip_service_worker) {
89 if (ServiceWorkerUtils::IsMainResourceType(resource_type)) {
90 provider_host->SetDocumentUrl(net::SimplifyUrlForRequest(request->url()));
91 provider_host->SetTopmostFrameUrl(request->first_party_for_cookies());
96 scoped_ptr<ServiceWorkerRequestHandler> handler(
97 provider_host->CreateRequestHandler(request_mode,
100 request_context_type,
102 blob_storage_context->AsWeakPtr(),
107 request->SetUserData(&kUserDataKey, handler.release());
110 ServiceWorkerRequestHandler* ServiceWorkerRequestHandler::GetHandler(
111 net::URLRequest* request) {
112 return static_cast<ServiceWorkerRequestHandler*>(
113 request->GetUserData(&kUserDataKey));
116 scoped_ptr<net::URLRequestInterceptor>
117 ServiceWorkerRequestHandler::CreateInterceptor(
118 ResourceContext* resource_context) {
119 return scoped_ptr<net::URLRequestInterceptor>(
120 new ServiceWorkerRequestInterceptor(resource_context));
123 bool ServiceWorkerRequestHandler::IsControlledByServiceWorker(
124 net::URLRequest* request) {
125 ServiceWorkerRequestHandler* handler = GetHandler(request);
126 if (!handler || !handler->provider_host_)
128 return handler->provider_host_->associated_registration() ||
129 handler->provider_host_->running_hosted_version();
132 ServiceWorkerRequestHandler::~ServiceWorkerRequestHandler() {
135 ServiceWorkerRequestHandler::ServiceWorkerRequestHandler(
136 base::WeakPtr<ServiceWorkerContextCore> context,
137 base::WeakPtr<ServiceWorkerProviderHost> provider_host,
138 base::WeakPtr<storage::BlobStorageContext> blob_storage_context,
139 ResourceType resource_type)
141 provider_host_(provider_host),
142 blob_storage_context_(blob_storage_context),
143 resource_type_(resource_type) {
146 } // namespace content