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 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REQUEST_HANDLER_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REQUEST_HANDLER_H_
8 #include "base/basictypes.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/supports_user_data.h"
11 #include "base/time/time.h"
12 #include "content/common/content_export.h"
13 #include "content/common/service_worker/service_worker_status_code.h"
14 #include "content/common/service_worker/service_worker_types.h"
15 #include "content/public/common/request_context_frame_type.h"
16 #include "content/public/common/request_context_type.h"
17 #include "content/public/common/resource_type.h"
18 #include "net/url_request/url_request_job_factory.h"
21 class NetworkDelegate
;
23 class URLRequestInterceptor
;
27 class BlobStorageContext
;
32 class ResourceContext
;
33 class ResourceRequestBody
;
34 class ServiceWorkerContextCore
;
35 class ServiceWorkerContextWrapper
;
36 class ServiceWorkerProviderHost
;
37 struct ResourceResponseInfo
;
39 // Abstract base class for routing network requests to ServiceWorkers.
40 // Created one per URLRequest and attached to each request.
41 class CONTENT_EXPORT ServiceWorkerRequestHandler
42 : public base::SupportsUserData::Data
{
44 // Attaches a newly created handler if the given |request| needs to
45 // be handled by ServiceWorker.
46 // TODO(kinuko): While utilizing UserData to attach data to URLRequest
47 // has some precedence, it might be better to attach this handler in a more
48 // explicit way within content layer, e.g. have ResourceRequestInfoImpl
50 static void InitializeHandler(
51 net::URLRequest
* request
,
52 ServiceWorkerContextWrapper
* context_wrapper
,
53 storage::BlobStorageContext
* blob_storage_context
,
56 bool skip_service_worker
,
57 FetchRequestMode request_mode
,
58 FetchCredentialsMode credentials_mode
,
59 FetchRedirectMode redirect_mode
,
60 ResourceType resource_type
,
61 RequestContextType request_context_type
,
62 RequestContextFrameType frame_type
,
63 scoped_refptr
<ResourceRequestBody
> body
);
65 // Returns the handler attached to |request|. This may return NULL
66 // if no handler is attached.
67 static ServiceWorkerRequestHandler
* GetHandler(
68 net::URLRequest
* request
);
70 // Creates a protocol interceptor for ServiceWorker.
71 static scoped_ptr
<net::URLRequestInterceptor
> CreateInterceptor(
72 ResourceContext
* resource_context
);
74 // Returns true if the request falls into the scope of a ServiceWorker.
75 // It's only reliable after the ServiceWorkerRequestHandler MaybeCreateJob
76 // method runs to completion for this request. The AppCache handler uses
77 // this to avoid colliding with ServiceWorkers.
78 static bool IsControlledByServiceWorker(net::URLRequest
* request
);
80 ~ServiceWorkerRequestHandler() override
;
82 // Called via custom URLRequestJobFactory.
83 virtual net::URLRequestJob
* MaybeCreateJob(
84 net::URLRequest
* request
,
85 net::NetworkDelegate
* network_delegate
,
86 ResourceContext
* context
) = 0;
88 virtual void GetExtraResponseInfo(
89 ResourceResponseInfo
* response_info
) const = 0;
91 // Methods to support cross site navigations.
92 void PrepareForCrossSiteTransfer(int old_process_id
);
93 void CompleteCrossSiteTransfer(int new_process_id
,
95 void MaybeCompleteCrossSiteTransferInOldProcess(
98 ServiceWorkerContextCore
* context() const { return context_
.get(); }
101 ServiceWorkerRequestHandler(
102 base::WeakPtr
<ServiceWorkerContextCore
> context
,
103 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
,
104 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context
,
105 ResourceType resource_type
);
107 base::WeakPtr
<ServiceWorkerContextCore
> context_
;
108 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host_
;
109 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context_
;
110 ResourceType resource_type_
;
113 scoped_ptr
<ServiceWorkerProviderHost
> host_for_cross_site_transfer_
;
115 int old_provider_id_
;
117 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRequestHandler
);
120 } // namespace content
122 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REQUEST_HANDLER_H_