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_URL_REQUEST_JOB_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_URL_REQUEST_JOB_H_
8 #include "base/memory/weak_ptr.h"
9 #include "content/common/content_export.h"
10 #include "content/common/service_worker/service_worker_status_code.h"
11 #include "content/common/service_worker/service_worker_types.h"
12 #include "net/http/http_byte_range.h"
13 #include "net/url_request/url_request_job.h"
17 class ServiceWorkerContextCore
;
18 class ServiceWorkerFetchDispatcher
;
19 class ServiceWorkerProviderHost
;
21 class CONTENT_EXPORT ServiceWorkerURLRequestJob
22 : public net::URLRequestJob
{
24 ServiceWorkerURLRequestJob(
25 net::URLRequest
* request
,
26 net::NetworkDelegate
* network_delegate
,
27 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
);
29 // Sets the response type.
30 void FallbackToNetwork();
31 void ForwardToServiceWorker();
33 bool ShouldFallbackToNetwork() const {
34 return response_type_
== FALLBACK_TO_NETWORK
;
36 bool ShouldForwardToServiceWorker() const {
37 return response_type_
== FORWARD_TO_SERVICE_WORKER
;
40 // net::URLRequestJob overrides:
41 virtual void Start() OVERRIDE
;
42 virtual void Kill() OVERRIDE
;
43 virtual net::LoadState
GetLoadState() const OVERRIDE
;
44 virtual bool GetCharset(std::string
* charset
) OVERRIDE
;
45 virtual bool GetMimeType(std::string
* mime_type
) const OVERRIDE
;
46 virtual void GetResponseInfo(net::HttpResponseInfo
* info
) OVERRIDE
;
47 virtual int GetResponseCode() const OVERRIDE
;
48 virtual void SetExtraRequestHeaders(
49 const net::HttpRequestHeaders
& headers
) OVERRIDE
;
50 virtual bool ReadRawData(net::IOBuffer
* buf
,
52 int *bytes_read
) OVERRIDE
;
54 const net::HttpResponseInfo
* http_info() const;
57 virtual ~ServiceWorkerURLRequestJob();
63 FORWARD_TO_SERVICE_WORKER
,
66 // We start processing the request if Start() is called AND response_type_
68 void MaybeStartRequest();
71 // For FORWARD_TO_SERVICE_WORKER case.
72 void DidDispatchFetchEvent(ServiceWorkerStatusCode status
,
73 ServiceWorkerFetchEventResult fetch_result
,
74 const ServiceWorkerResponse
& response
);
76 void CreateResponseHeader(const ServiceWorkerResponse
& response
);
78 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host_
;
80 ResponseType response_type_
;
83 net::HttpByteRange byte_range_
;
84 scoped_ptr
<net::HttpResponseInfo
> range_response_info_
;
85 scoped_ptr
<net::HttpResponseInfo
> http_response_info_
;
87 // Used when response type is FORWARD_TO_SERVICE_WORKER.
88 scoped_ptr
<ServiceWorkerFetchDispatcher
> fetch_dispatcher_
;
90 base::WeakPtrFactory
<ServiceWorkerURLRequestJob
> weak_factory_
;
92 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerURLRequestJob
);
95 } // namespace content
97 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_URL_REQUEST_JOB_H_