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"
19 #include "third_party/WebKit/public/platform/WebServiceWorkerResponseType.h"
22 class NetworkDelegate
;
24 class URLRequestInterceptor
;
28 class BlobStorageContext
;
33 class ResourceContext
;
34 class ResourceRequestBody
;
35 class ServiceWorkerContextCore
;
36 class ServiceWorkerContextWrapper
;
37 class ServiceWorkerProviderHost
;
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 bool* was_fetched_via_service_worker
,
89 bool* was_fallback_required_by_service_worker
,
90 GURL
* original_url_via_service_worker
,
91 blink::WebServiceWorkerResponseType
* response_type_via_service_worker
,
92 base::TimeTicks
* fetch_start_time
,
93 base::TimeTicks
* fetch_ready_time
,
94 base::TimeTicks
* fetch_end_time
) const = 0;
96 // Methods to support cross site navigations.
97 void PrepareForCrossSiteTransfer(int old_process_id
);
98 void CompleteCrossSiteTransfer(int new_process_id
,
100 void MaybeCompleteCrossSiteTransferInOldProcess(
103 ServiceWorkerContextCore
* context() const { return context_
.get(); }
106 ServiceWorkerRequestHandler(
107 base::WeakPtr
<ServiceWorkerContextCore
> context
,
108 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
,
109 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context
,
110 ResourceType resource_type
);
112 base::WeakPtr
<ServiceWorkerContextCore
> context_
;
113 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host_
;
114 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context_
;
115 ResourceType resource_type_
;
118 scoped_ptr
<ServiceWorkerProviderHost
> host_for_cross_site_transfer_
;
120 int old_provider_id_
;
122 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerRequestHandler
);
125 } // namespace content
127 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_REQUEST_HANDLER_H_