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 // This file declares a HttpTransactionFactory implementation that can be | 5 // This file declares a HttpTransactionFactory implementation that can be |
6 // layered on top of another HttpTransactionFactory to add HTTP caching. The | 6 // layered on top of another HttpTransactionFactory to add HTTP caching. The |
7 // caching logic follows RFC 2616 (any exceptions are called out in the code). | 7 // caching logic follows RFC 2616 (any exceptions are called out in the code). |
8 // | 8 // |
9 // The HttpCache takes a disk_cache::Backend as a parameter, and uses that for | 9 // The HttpCache takes a disk_cache::Backend as a parameter, and uses that for |
10 // the cache storage. | 10 // the cache storage. |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 scoped_ptr<disk_cache::Backend>* backend, | 92 scoped_ptr<disk_cache::Backend>* backend, |
93 const CompletionCallback& callback) = 0; | 93 const CompletionCallback& callback) = 0; |
94 }; | 94 }; |
95 | 95 |
96 // A default backend factory for the common use cases. | 96 // A default backend factory for the common use cases. |
97 class NET_EXPORT DefaultBackend : public BackendFactory { | 97 class NET_EXPORT DefaultBackend : public BackendFactory { |
98 public: | 98 public: |
99 // |path| is the destination for any files used by the backend, and | 99 // |path| is the destination for any files used by the backend, and |
100 // |cache_thread| is the thread where disk operations should take place. If | 100 // |cache_thread| is the thread where disk operations should take place. If |
101 // |max_bytes| is zero, a default value will be calculated automatically. | 101 // |max_bytes| is zero, a default value will be calculated automatically. |
102 DefaultBackend(CacheType type, BackendType backend_type, | 102 DefaultBackend(CacheType type, |
103 const base::FilePath& path, int max_bytes, | 103 BackendType backend_type, |
| 104 const base::FilePath& path, |
| 105 int max_bytes, |
104 base::MessageLoopProxy* thread); | 106 base::MessageLoopProxy* thread); |
105 virtual ~DefaultBackend(); | 107 virtual ~DefaultBackend(); |
106 | 108 |
107 // Returns a factory for an in-memory cache. | 109 // Returns a factory for an in-memory cache. |
108 static BackendFactory* InMemory(int max_bytes); | 110 static BackendFactory* InMemory(int max_bytes); |
109 | 111 |
110 // BackendFactory implementation. | 112 // BackendFactory implementation. |
111 virtual int CreateBackend(NetLog* net_log, | 113 virtual int CreateBackend(NetLog* net_log, |
112 scoped_ptr<disk_cache::Backend>* backend, | 114 scoped_ptr<disk_cache::Backend>* backend, |
113 const CompletionCallback& callback) OVERRIDE; | 115 const CompletionCallback& callback) OVERRIDE; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 // a network error code, and it could be ERR_IO_PENDING, in which case the | 151 // a network error code, and it could be ERR_IO_PENDING, in which case the |
150 // |callback| will be notified when the operation completes. The pointer that | 152 // |callback| will be notified when the operation completes. The pointer that |
151 // receives the |backend| must remain valid until the operation completes. | 153 // receives the |backend| must remain valid until the operation completes. |
152 int GetBackend(disk_cache::Backend** backend, | 154 int GetBackend(disk_cache::Backend** backend, |
153 const net::CompletionCallback& callback); | 155 const net::CompletionCallback& callback); |
154 | 156 |
155 // Returns the current backend (can be NULL). | 157 // Returns the current backend (can be NULL). |
156 disk_cache::Backend* GetCurrentBackend() const; | 158 disk_cache::Backend* GetCurrentBackend() const; |
157 | 159 |
158 // Given a header data blob, convert it to a response info object. | 160 // Given a header data blob, convert it to a response info object. |
159 static bool ParseResponseInfo(const char* data, int len, | 161 static bool ParseResponseInfo(const char* data, |
| 162 int len, |
160 HttpResponseInfo* response_info, | 163 HttpResponseInfo* response_info, |
161 bool* response_truncated); | 164 bool* response_truncated); |
162 | 165 |
163 // Writes |buf_len| bytes of metadata stored in |buf| to the cache entry | 166 // Writes |buf_len| bytes of metadata stored in |buf| to the cache entry |
164 // referenced by |url|, as long as the entry's |expected_response_time| has | 167 // referenced by |url|, as long as the entry's |expected_response_time| has |
165 // not changed. This method returns without blocking, and the operation will | 168 // not changed. This method returns without blocking, and the operation will |
166 // be performed asynchronously without any completion notification. | 169 // be performed asynchronously without any completion notification. |
167 void WriteMetadata(const GURL& url, | 170 void WriteMetadata(const GURL& url, |
168 RequestPriority priority, | 171 RequestPriority priority, |
169 base::Time expected_response_time, | 172 base::Time expected_response_time, |
(...skipping 22 matching lines...) Expand all Loading... |
192 // HttpTransactionFactory implementation: | 195 // HttpTransactionFactory implementation: |
193 virtual int CreateTransaction(RequestPriority priority, | 196 virtual int CreateTransaction(RequestPriority priority, |
194 scoped_ptr<HttpTransaction>* trans) OVERRIDE; | 197 scoped_ptr<HttpTransaction>* trans) OVERRIDE; |
195 virtual HttpCache* GetCache() OVERRIDE; | 198 virtual HttpCache* GetCache() OVERRIDE; |
196 virtual HttpNetworkSession* GetSession() OVERRIDE; | 199 virtual HttpNetworkSession* GetSession() OVERRIDE; |
197 | 200 |
198 // Resets the network layer to allow for tests that probe | 201 // Resets the network layer to allow for tests that probe |
199 // network changes (e.g. host unreachable). The old network layer is | 202 // network changes (e.g. host unreachable). The old network layer is |
200 // returned to allow for filter patterns that only intercept | 203 // returned to allow for filter patterns that only intercept |
201 // some creation requests. Note ownership exchange. | 204 // some creation requests. Note ownership exchange. |
202 scoped_ptr<HttpTransactionFactory> | 205 scoped_ptr<HttpTransactionFactory> SetHttpNetworkTransactionFactoryForTesting( |
203 SetHttpNetworkTransactionFactoryForTesting( | 206 scoped_ptr<HttpTransactionFactory> new_network_layer); |
204 scoped_ptr<HttpTransactionFactory> new_network_layer); | |
205 | 207 |
206 private: | 208 private: |
207 // Types -------------------------------------------------------------------- | 209 // Types -------------------------------------------------------------------- |
208 | 210 |
209 // Disk cache entry data indices. | 211 // Disk cache entry data indices. |
210 enum { | 212 enum { |
211 kResponseInfoIndex = 0, | 213 kResponseInfoIndex = 0, |
212 kResponseContentIndex, | 214 kResponseContentIndex, |
213 kMetadataIndex, | 215 kMetadataIndex, |
214 | 216 |
(...skipping 10 matching lines...) Expand all Loading... |
225 struct PendingOp; // Info for an entry under construction. | 227 struct PendingOp; // Info for an entry under construction. |
226 | 228 |
227 typedef std::list<Transaction*> TransactionList; | 229 typedef std::list<Transaction*> TransactionList; |
228 typedef std::list<WorkItem*> WorkItemList; | 230 typedef std::list<WorkItem*> WorkItemList; |
229 | 231 |
230 struct ActiveEntry { | 232 struct ActiveEntry { |
231 explicit ActiveEntry(disk_cache::Entry* entry); | 233 explicit ActiveEntry(disk_cache::Entry* entry); |
232 ~ActiveEntry(); | 234 ~ActiveEntry(); |
233 | 235 |
234 disk_cache::Entry* disk_entry; | 236 disk_cache::Entry* disk_entry; |
235 Transaction* writer; | 237 Transaction* writer; |
236 TransactionList readers; | 238 TransactionList readers; |
237 TransactionList pending_queue; | 239 TransactionList pending_queue; |
238 bool will_process_pending_queue; | 240 bool will_process_pending_queue; |
239 bool doomed; | 241 bool doomed; |
240 }; | 242 }; |
241 | 243 |
242 typedef base::hash_map<std::string, ActiveEntry*> ActiveEntriesMap; | 244 typedef base::hash_map<std::string, ActiveEntry*> ActiveEntriesMap; |
243 typedef base::hash_map<std::string, PendingOp*> PendingOpsMap; | 245 typedef base::hash_map<std::string, PendingOp*> PendingOpsMap; |
244 typedef std::set<ActiveEntry*> ActiveEntriesSet; | 246 typedef std::set<ActiveEntry*> ActiveEntriesSet; |
245 typedef base::hash_map<std::string, int> PlaybackCacheMap; | 247 typedef base::hash_map<std::string, int> PlaybackCacheMap; |
246 | 248 |
247 // Methods ------------------------------------------------------------------ | 249 // Methods ------------------------------------------------------------------ |
248 | 250 |
249 // Creates the |backend| object and notifies the |callback| when the operation | 251 // Creates the |backend| object and notifies the |callback| when the operation |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 // Returns the PendingOp for the desired |key|. If an entry is not under | 299 // Returns the PendingOp for the desired |key|. If an entry is not under |
298 // construction already, a new PendingOp structure is created. | 300 // construction already, a new PendingOp structure is created. |
299 PendingOp* GetPendingOp(const std::string& key); | 301 PendingOp* GetPendingOp(const std::string& key); |
300 | 302 |
301 // Deletes a PendingOp. | 303 // Deletes a PendingOp. |
302 void DeletePendingOp(PendingOp* pending_op); | 304 void DeletePendingOp(PendingOp* pending_op); |
303 | 305 |
304 // Opens the disk cache entry associated with |key|, returning an ActiveEntry | 306 // Opens the disk cache entry associated with |key|, returning an ActiveEntry |
305 // in |*entry|. |trans| will be notified via its IO callback if this method | 307 // in |*entry|. |trans| will be notified via its IO callback if this method |
306 // returns ERR_IO_PENDING. | 308 // returns ERR_IO_PENDING. |
307 int OpenEntry(const std::string& key, ActiveEntry** entry, | 309 int OpenEntry(const std::string& key, |
| 310 ActiveEntry** entry, |
308 Transaction* trans); | 311 Transaction* trans); |
309 | 312 |
310 // Creates the disk cache entry associated with |key|, returning an | 313 // Creates the disk cache entry associated with |key|, returning an |
311 // ActiveEntry in |*entry|. |trans| will be notified via its IO callback if | 314 // ActiveEntry in |*entry|. |trans| will be notified via its IO callback if |
312 // this method returns ERR_IO_PENDING. | 315 // this method returns ERR_IO_PENDING. |
313 int CreateEntry(const std::string& key, ActiveEntry** entry, | 316 int CreateEntry(const std::string& key, |
| 317 ActiveEntry** entry, |
314 Transaction* trans); | 318 Transaction* trans); |
315 | 319 |
316 // Destroys an ActiveEntry (active or doomed). | 320 // Destroys an ActiveEntry (active or doomed). |
317 void DestroyEntry(ActiveEntry* entry); | 321 void DestroyEntry(ActiveEntry* entry); |
318 | 322 |
319 // Adds a transaction to an ActiveEntry. If this method returns ERR_IO_PENDING | 323 // Adds a transaction to an ActiveEntry. If this method returns ERR_IO_PENDING |
320 // the transaction will be notified about completion via its IO callback. This | 324 // the transaction will be notified about completion via its IO callback. This |
321 // method returns ERR_CACHE_RACE to signal the transaction that it cannot be | 325 // method returns ERR_CACHE_RACE to signal the transaction that it cannot be |
322 // added to the provided entry, and it should retry the process with another | 326 // added to the provided entry, and it should retry the process with another |
323 // one (in this case, the entry is no longer valid). | 327 // one (in this case, the entry is no longer valid). |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 PendingOpsMap pending_ops_; | 409 PendingOpsMap pending_ops_; |
406 | 410 |
407 scoped_ptr<PlaybackCacheMap> playback_cache_map_; | 411 scoped_ptr<PlaybackCacheMap> playback_cache_map_; |
408 | 412 |
409 DISALLOW_COPY_AND_ASSIGN(HttpCache); | 413 DISALLOW_COPY_AND_ASSIGN(HttpCache); |
410 }; | 414 }; |
411 | 415 |
412 } // namespace net | 416 } // namespace net |
413 | 417 |
414 #endif // NET_HTTP_HTTP_CACHE_H_ | 418 #endif // NET_HTTP_HTTP_CACHE_H_ |
OLD | NEW |