OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_APPCACHE_APPCACHE_URL_REQUEST_JOB_H_ | 5 #ifndef CONTENT_BROWSER_APPCACHE_APPCACHE_URL_REQUEST_JOB_H_ |
6 #define CONTENT_BROWSER_APPCACHE_APPCACHE_URL_REQUEST_JOB_H_ | 6 #define CONTENT_BROWSER_APPCACHE_APPCACHE_URL_REQUEST_JOB_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/memory/weak_ptr.h" | |
14 #include "content/browser/appcache/appcache_entry.h" | 13 #include "content/browser/appcache/appcache_entry.h" |
15 #include "content/browser/appcache/appcache_executable_handler.h" | 14 #include "content/browser/appcache/appcache_executable_handler.h" |
15 #include "content/browser/appcache/appcache_job.h" | |
16 #include "content/browser/appcache/appcache_response.h" | 16 #include "content/browser/appcache/appcache_response.h" |
17 #include "content/browser/appcache/appcache_storage.h" | 17 #include "content/browser/appcache/appcache_storage.h" |
18 #include "content/common/content_export.h" | 18 #include "content/common/content_export.h" |
19 #include "net/http/http_byte_range.h" | 19 #include "net/http/http_byte_range.h" |
20 #include "net/url_request/url_request_job.h" | 20 #include "net/url_request/url_request_job.h" |
21 | 21 |
22 namespace net { | 22 namespace net { |
23 class GrowableIOBuffer; | 23 class GrowableIOBuffer; |
24 }; | 24 }; |
25 | 25 |
26 namespace content { | 26 namespace content { |
27 class AppCacheHost; | 27 class AppCacheHost; |
28 class AppCacheRequestHandlerTest; | 28 class AppCacheRequestHandlerTest; |
29 class AppCacheURLRequestJobTest; | 29 class AppCacheURLRequestJobTest; |
30 | 30 |
31 // A net::URLRequestJob derivative that knows how to return a response stored | 31 // A net::URLRequestJob derivative that knows how to return a response stored |
32 // in the appcache. | 32 // in the appcache. |
33 class CONTENT_EXPORT AppCacheURLRequestJob | 33 class CONTENT_EXPORT AppCacheURLRequestJob : public net::URLRequestJob, |
34 : public net::URLRequestJob, | 34 public AppCacheStorage::Delegate, |
35 public AppCacheStorage::Delegate { | 35 public AppCacheJob { |
36 public: | 36 public: |
37 // Callback that will be invoked before the request is restarted. The caller | 37 // Callback that will be invoked before the request is restarted. The caller |
38 // can use this opportunity to grab state from the AppCacheURLRequestJob to | 38 // can use this opportunity to grab state from the AppCacheURLRequestJob to |
39 // determine how it should behave when the request is restarted. | 39 // determine how it should behave when the request is restarted. |
40 using OnPrepareToRestartCallback = base::Closure; | 40 using OnPrepareToRestartCallback = base::Closure; |
41 | 41 |
42 AppCacheURLRequestJob(net::URLRequest* request, | |
43 net::NetworkDelegate* network_delegate, | |
44 AppCacheStorage* storage, | |
45 AppCacheHost* host, | |
46 bool is_main_resource, | |
47 const OnPrepareToRestartCallback& restart_callback_); | |
48 | |
49 ~AppCacheURLRequestJob() override; | 42 ~AppCacheURLRequestJob() override; |
50 | 43 |
51 // Informs the job of what response it should deliver. Only one of these | 44 // AppCacheJob overrides. |
52 // methods should be called, and only once per job. A job will sit idle and | 45 void Kill() override; |
53 // wait indefinitely until one of the deliver methods is called. | 46 bool IsStarted() const override; |
47 bool IsWaiting() const override; | |
48 bool IsDeliveringAppCacheResponse() const override; | |
49 bool IsDeliveringNetworkResponse() const override; | |
50 bool IsDeliveringErrorResponse() const override; | |
51 bool IsCacheEntryNotFound() const override; | |
54 void DeliverAppCachedResponse(const GURL& manifest_url, | 52 void DeliverAppCachedResponse(const GURL& manifest_url, |
55 int64_t cache_id, | 53 int64_t cache_id, |
56 const AppCacheEntry& entry, | 54 const AppCacheEntry& entry, |
57 bool is_fallback); | 55 bool is_fallback) override; |
58 void DeliverNetworkResponse(); | 56 void DeliverNetworkResponse() override; |
59 void DeliverErrorResponse(); | 57 void DeliverErrorResponse() override; |
60 | 58 const GURL& GetURL() const override; |
61 bool is_waiting() const { | 59 net::URLRequestJob* AsURLRequestJob() override; |
62 return delivery_type_ == AWAITING_DELIVERY_ORDERS; | |
63 } | |
64 | |
65 bool is_delivering_appcache_response() const { | |
66 return delivery_type_ == APPCACHED_DELIVERY; | |
67 } | |
68 | |
69 bool is_delivering_network_response() const { | |
70 return delivery_type_ == NETWORK_DELIVERY; | |
71 } | |
72 | |
73 bool is_delivering_error_response() const { | |
74 return delivery_type_ == ERROR_DELIVERY; | |
75 } | |
76 | 60 |
77 // Accessors for the info about the appcached response, if any, | 61 // Accessors for the info about the appcached response, if any, |
78 // that this job has been instructed to deliver. These are only | 62 // that this job has been instructed to deliver. These are only |
79 // valid to call if is_delivering_appcache_response. | 63 // valid to call if is_delivering_appcache_response. |
80 const GURL& manifest_url() const { return manifest_url_; } | 64 const GURL& manifest_url() const { return manifest_url_; } |
81 int64_t cache_id() const { return cache_id_; } | 65 int64_t cache_id() const { return cache_id_; } |
82 const AppCacheEntry& entry() const { return entry_; } | 66 const AppCacheEntry& entry() const { return entry_; } |
83 | 67 |
84 // net::URLRequestJob's Kill method is made public so the users of this | |
85 // class in the appcache namespace can call it. | |
86 void Kill() override; | |
87 | |
88 // Returns true if the job has been started by the net library. | |
89 bool has_been_started() const { | |
90 return has_been_started_; | |
91 } | |
92 | |
93 // Returns true if the job has been killed. | 68 // Returns true if the job has been killed. |
94 bool has_been_killed() const { | 69 bool has_been_killed() const { |
95 return has_been_killed_; | 70 return has_been_killed_; |
96 } | 71 } |
97 | 72 |
98 // Returns true if the cache entry was not found in the disk cache. | |
99 bool cache_entry_not_found() const { | |
100 return cache_entry_not_found_; | |
101 } | |
102 | |
103 private: | 73 private: |
104 friend class AppCacheRequestHandlerTest; | 74 friend class AppCacheRequestHandlerTest; |
105 friend class AppCacheURLRequestJobTest; | 75 friend class AppCacheURLRequestJobTest; |
76 // AppCacheJob::Create() creates this instance. | |
jam
2017/05/05 23:15:25
do we gain much by making the constructor private?
ananta
2017/05/06 03:01:35
It seemed better to have create done by the base A
| |
77 friend class AppCacheJob; | |
106 | 78 |
107 // Friend so it can get a weak pointer. | 79 AppCacheURLRequestJob(net::URLRequest* request, |
108 friend class AppCacheRequestHandler; | 80 net::NetworkDelegate* network_delegate, |
81 AppCacheStorage* storage, | |
82 AppCacheHost* host, | |
83 bool is_main_resource, | |
84 const OnPrepareToRestartCallback& restart_callback_); | |
109 | 85 |
110 enum DeliveryType { | 86 enum DeliveryType { |
111 AWAITING_DELIVERY_ORDERS, | 87 AWAITING_DELIVERY_ORDERS, |
112 APPCACHED_DELIVERY, | 88 APPCACHED_DELIVERY, |
113 NETWORK_DELIVERY, | 89 NETWORK_DELIVERY, |
114 ERROR_DELIVERY | 90 ERROR_DELIVERY |
115 }; | 91 }; |
116 | 92 |
117 base::WeakPtr<AppCacheURLRequestJob> GetWeakPtr(); | |
118 | |
119 // Returns true if one of the Deliver methods has been called. | 93 // Returns true if one of the Deliver methods has been called. |
120 bool has_delivery_orders() const { | 94 bool has_delivery_orders() const { return !IsWaiting(); } |
121 return !is_waiting(); | |
122 } | |
123 | 95 |
124 void MaybeBeginDelivery(); | 96 void MaybeBeginDelivery(); |
125 void BeginDelivery(); | 97 void BeginDelivery(); |
126 | 98 |
127 // For executable response handling. | 99 // For executable response handling. |
128 void BeginExecutableHandlerDelivery(); | 100 void BeginExecutableHandlerDelivery(); |
129 void OnExecutableSourceLoaded(int result); | 101 void OnExecutableSourceLoaded(int result); |
130 void InvokeExecutableHandler(AppCacheExecutableHandler* handler); | 102 void InvokeExecutableHandler(AppCacheExecutableHandler* handler); |
131 void OnExecutableResponseCallback( | 103 void OnExecutableResponseCallback( |
132 const AppCacheExecutableHandler::Response& response); | 104 const AppCacheExecutableHandler::Response& response); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
177 bool cache_entry_not_found_; | 149 bool cache_entry_not_found_; |
178 scoped_refptr<AppCacheResponseInfo> info_; | 150 scoped_refptr<AppCacheResponseInfo> info_; |
179 scoped_refptr<net::GrowableIOBuffer> handler_source_buffer_; | 151 scoped_refptr<net::GrowableIOBuffer> handler_source_buffer_; |
180 std::unique_ptr<AppCacheResponseReader> handler_source_reader_; | 152 std::unique_ptr<AppCacheResponseReader> handler_source_reader_; |
181 net::HttpByteRange range_requested_; | 153 net::HttpByteRange range_requested_; |
182 std::unique_ptr<net::HttpResponseInfo> range_response_info_; | 154 std::unique_ptr<net::HttpResponseInfo> range_response_info_; |
183 std::unique_ptr<AppCacheResponseReader> reader_; | 155 std::unique_ptr<AppCacheResponseReader> reader_; |
184 scoped_refptr<AppCache> cache_; | 156 scoped_refptr<AppCache> cache_; |
185 scoped_refptr<AppCacheGroup> group_; | 157 scoped_refptr<AppCacheGroup> group_; |
186 const OnPrepareToRestartCallback on_prepare_to_restart_callback_; | 158 const OnPrepareToRestartCallback on_prepare_to_restart_callback_; |
187 base::WeakPtrFactory<AppCacheURLRequestJob> weak_factory_; | |
188 }; | 159 }; |
189 | 160 |
190 } // namespace content | 161 } // namespace content |
191 | 162 |
192 #endif // CONTENT_BROWSER_APPCACHE_APPCACHE_URL_REQUEST_JOB_H_ | 163 #endif // CONTENT_BROWSER_APPCACHE_APPCACHE_URL_REQUEST_JOB_H_ |
OLD | NEW |