1 // Copyright (c) 2012 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_APPCACHE_APPCACHE_URL_REQUEST_JOB_H_
6 #define CONTENT_BROWSER_APPCACHE_APPCACHE_URL_REQUEST_JOB_H_
10 #include "base/memory/weak_ptr.h"
11 #include "content/browser/appcache/appcache_entry.h"
12 #include "content/browser/appcache/appcache_executable_handler.h"
13 #include "content/browser/appcache/appcache_response.h"
14 #include "content/browser/appcache/appcache_storage.h"
15 #include "content/common/content_export.h"
16 #include "net/http/http_byte_range.h"
17 #include "net/url_request/url_request_job.h"
20 class GrowableIOBuffer
;
25 class AppCacheRequestHandlerTest
;
26 class AppCacheURLRequestJobTest
;
28 // A net::URLRequestJob derivative that knows how to return a response stored
30 class CONTENT_EXPORT AppCacheURLRequestJob
31 : public net::URLRequestJob
,
32 public AppCacheStorage::Delegate
{
34 AppCacheURLRequestJob(net::URLRequest
* request
,
35 net::NetworkDelegate
* network_delegate
,
36 AppCacheStorage
* storage
,
38 bool is_main_resource
);
40 // Informs the job of what response it should deliver. Only one of these
41 // methods should be called, and only once per job. A job will sit idle and
42 // wait indefinitely until one of the deliver methods is called.
43 void DeliverAppCachedResponse(const GURL
& manifest_url
, int64 group_id
,
44 int64 cache_id
, const AppCacheEntry
& entry
,
46 void DeliverNetworkResponse();
47 void DeliverErrorResponse();
49 bool is_waiting() const {
50 return delivery_type_
== AWAITING_DELIVERY_ORDERS
;
53 bool is_delivering_appcache_response() const {
54 return delivery_type_
== APPCACHED_DELIVERY
;
57 bool is_delivering_network_response() const {
58 return delivery_type_
== NETWORK_DELIVERY
;
61 bool is_delivering_error_response() const {
62 return delivery_type_
== ERROR_DELIVERY
;
65 // Accessors for the info about the appcached response, if any,
66 // that this job has been instructed to deliver. These are only
67 // valid to call if is_delivering_appcache_response.
68 const GURL
& manifest_url() const { return manifest_url_
; }
69 int64
group_id() const { return group_id_
; }
70 int64
cache_id() const { return cache_id_
; }
71 const AppCacheEntry
& entry() const { return entry_
; }
73 // net::URLRequestJob's Kill method is made public so the users of this
74 // class in the appcache namespace can call it.
77 // Returns true if the job has been started by the net library.
78 bool has_been_started() const {
79 return has_been_started_
;
82 // Returns true if the job has been killed.
83 bool has_been_killed() const {
84 return has_been_killed_
;
87 // Returns true if the cache entry was not found in the disk cache.
88 bool cache_entry_not_found() const {
89 return cache_entry_not_found_
;
93 ~AppCacheURLRequestJob() override
;
96 friend class content::AppCacheRequestHandlerTest
;
97 friend class content::AppCacheURLRequestJobTest
;
100 AWAITING_DELIVERY_ORDERS
,
106 // Returns true if one of the Deliver methods has been called.
107 bool has_delivery_orders() const {
108 return !is_waiting();
111 void MaybeBeginDelivery();
112 void BeginDelivery();
114 // For executable response handling.
115 void BeginExecutableHandlerDelivery();
116 void OnExecutableSourceLoaded(int result
);
117 void InvokeExecutableHandler(AppCacheExecutableHandler
* handler
);
118 void OnExecutableResponseCallback(
119 const AppCacheExecutableHandler::Response
& response
);
120 void BeginErrorDelivery(const char* message
);
122 // AppCacheStorage::Delegate methods
123 void OnResponseInfoLoaded(AppCacheResponseInfo
* response_info
,
124 int64 response_id
) override
;
125 void OnCacheLoaded(AppCache
* cache
, int64 cache_id
) override
;
127 const net::HttpResponseInfo
* http_info() const;
128 bool is_range_request() const { return range_requested_
.IsValid(); }
129 void SetupRangeResponse();
131 // AppCacheResponseReader completion callback
132 void OnReadComplete(int result
);
134 // net::URLRequestJob methods, see url_request_job.h for doc comments
135 void Start() override
;
136 net::LoadState
GetLoadState() const override
;
137 bool GetCharset(std::string
* charset
) override
;
138 void GetResponseInfo(net::HttpResponseInfo
* info
) override
;
139 bool ReadRawData(net::IOBuffer
* buf
, int buf_size
, int* bytes_read
) override
;
141 // Sets extra request headers for Job types that support request headers.
142 // This is how we get informed of range-requests.
143 void SetExtraRequestHeaders(const net::HttpRequestHeaders
& headers
) override
;
145 // FilterContext methods
146 bool GetMimeType(std::string
* mime_type
) const override
;
147 int GetResponseCode() const override
;
150 AppCacheStorage
* storage_
;
151 base::TimeTicks start_time_tick_
;
152 bool has_been_started_
;
153 bool has_been_killed_
;
154 DeliveryType delivery_type_
;
158 AppCacheEntry entry_
;
160 bool is_main_resource_
; // Used for histogram logging.
161 bool cache_entry_not_found_
;
162 scoped_refptr
<AppCacheResponseInfo
> info_
;
163 scoped_refptr
<net::GrowableIOBuffer
> handler_source_buffer_
;
164 scoped_ptr
<AppCacheResponseReader
> handler_source_reader_
;
165 net::HttpByteRange range_requested_
;
166 scoped_ptr
<net::HttpResponseInfo
> range_response_info_
;
167 scoped_ptr
<AppCacheResponseReader
> reader_
;
168 scoped_refptr
<AppCache
> cache_
;
169 scoped_refptr
<AppCacheGroup
> group_
;
170 base::WeakPtrFactory
<AppCacheURLRequestJob
> weak_factory_
;
173 } // namespace content
175 #endif // CONTENT_BROWSER_APPCACHE_APPCACHE_REQUEST_HANDLER_H_