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 ResourceType resource_type
,
60 RequestContextType request_context_type
,
61 RequestContextFrameType frame_type
,
62 scoped_refptr
<ResourceRequestBody
> body
);
64 // Returns the handler attached to |request|. This may return NULL
65 // if no handler is attached.
66 static ServiceWorkerRequestHandler
* GetHandler(
67 net::URLRequest
* request
);
69 // Creates a protocol interceptor for ServiceWorker.
70 static scoped_ptr
<net::URLRequestInterceptor
> CreateInterceptor(
71 ResourceContext
* resource_context
);
73 // Returns true if the request falls into the scope of a ServiceWorker.
74 // It's only reliable after the ServiceWorkerRequestHandler MaybeCreateJob
75 // method runs to completion for this request. The AppCache handler uses
76 // this to avoid colliding with ServiceWorkers.
77 static bool IsControlledByServiceWorker(net::URLRequest
* request
);
79 ~ServiceWorkerRequestHandler() override
;
81 // Called via custom URLRequestJobFactory.
82 virtual net::URLRequestJob
* MaybeCreateJob(
83 net::URLRequest
* request
,
84 net::NetworkDelegate
* network_delegate
,
85 ResourceContext
* context
) = 0;
87 virtual void GetExtraResponseInfo(
88 ResourceResponseInfo
* response_info
) const = 0;
90 // Methods to support cross site navigations.
91 void PrepareForCrossSiteTransfer(int old_process_id
);
92 void CompleteCrossSiteTransfer(int new_process_id
,
94 void MaybeCompleteCrossSiteTransferInOldProcess(
97 ServiceWorkerContextCore
* context() const { return context_
.get(); }
100 ServiceWorkerRequestHandler(
101 base::WeakPtr
<ServiceWorkerContextCore
> context
,
102 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
,
103 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context
,
104 ResourceType resource_type
);
106 base::WeakPtr
<ServiceWorkerContextCore
> context_
;
107 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host_
;
108 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context_
;
109 ResourceType resource_type_
;
112 scoped_ptr
<ServiceWorkerProviderHost
> host_for_cross_site_transfer_
;
114 int old_provider_id_
;
116 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRequestHandler
);
119 } // namespace content
121 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REQUEST_HANDLER_H_