Home | History | Annotate | Download | only in service_worker
      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 #include "net/url_request/url_request_interceptor.h"
     16 #include "webkit/browser/blob/blob_storage_context.h"
     17 
     18 namespace content {
     19 
     20 namespace {
     21 
     22 int kUserDataKey;  // Key value is not important.
     23 
     24 class ServiceWorkerRequestInterceptor
     25     : public net::URLRequestInterceptor {
     26  public:
     27   ServiceWorkerRequestInterceptor() {}
     28   virtual ~ServiceWorkerRequestInterceptor() {}
     29   virtual net::URLRequestJob* MaybeInterceptRequest(
     30       net::URLRequest* request,
     31       net::NetworkDelegate* network_delegate) const OVERRIDE {
     32     ServiceWorkerRequestHandler* handler =
     33         ServiceWorkerRequestHandler::GetHandler(request);
     34     if (!handler)
     35       return NULL;
     36     return handler->MaybeCreateJob(request, network_delegate);
     37   }
     38 
     39  private:
     40   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRequestInterceptor);
     41 };
     42 
     43 bool IsMethodSupported(const std::string& method) {
     44   return (method == "GET") || (method == "HEAD");
     45 }
     46 
     47 bool IsSchemeAndMethodSupported(const net::URLRequest* request) {
     48   return request->url().SchemeIsHTTPOrHTTPS() &&
     49          IsMethodSupported(request->method());
     50 }
     51 
     52 }  // namespace
     53 
     54 void ServiceWorkerRequestHandler::InitializeHandler(
     55     net::URLRequest* request,
     56     ServiceWorkerContextWrapper* context_wrapper,
     57     webkit_blob::BlobStorageContext* blob_storage_context,
     58     int process_id,
     59     int provider_id,
     60     ResourceType::Type resource_type) {
     61   if (!ServiceWorkerUtils::IsFeatureEnabled() ||
     62       !IsSchemeAndMethodSupported(request)) {
     63     return;
     64   }
     65 
     66   if (!context_wrapper || !context_wrapper->context() ||
     67       provider_id == kInvalidServiceWorkerProviderId) {
     68     return;
     69   }
     70 
     71   ServiceWorkerProviderHost* provider_host =
     72       context_wrapper->context()->GetProviderHost(process_id, provider_id);
     73   if (!provider_host || !provider_host->IsContextAlive())
     74     return;
     75 
     76   scoped_ptr<ServiceWorkerRequestHandler> handler(
     77       provider_host->CreateRequestHandler(resource_type,
     78                                           blob_storage_context->AsWeakPtr()));
     79   if (!handler)
     80     return;
     81 
     82   request->SetUserData(&kUserDataKey, handler.release());
     83 }
     84 
     85 ServiceWorkerRequestHandler* ServiceWorkerRequestHandler::GetHandler(
     86     net::URLRequest* request) {
     87   return reinterpret_cast<ServiceWorkerRequestHandler*>(
     88       request->GetUserData(&kUserDataKey));
     89 }
     90 
     91 scoped_ptr<net::URLRequestInterceptor>
     92 ServiceWorkerRequestHandler::CreateInterceptor() {
     93   return scoped_ptr<net::URLRequestInterceptor>(
     94       new ServiceWorkerRequestInterceptor);
     95 }
     96 
     97 ServiceWorkerRequestHandler::~ServiceWorkerRequestHandler() {
     98 }
     99 
    100 ServiceWorkerRequestHandler::ServiceWorkerRequestHandler(
    101     base::WeakPtr<ServiceWorkerContextCore> context,
    102     base::WeakPtr<ServiceWorkerProviderHost> provider_host,
    103     base::WeakPtr<webkit_blob::BlobStorageContext> blob_storage_context,
    104     ResourceType::Type resource_type)
    105     : context_(context),
    106       provider_host_(provider_host),
    107       blob_storage_context_(blob_storage_context),
    108       resource_type_(resource_type) {
    109 }
    110 
    111 }  // namespace content
    112