Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 12 matching lines...) Expand all Loading... | |
| 23 #include "base/file_path.h" | 23 #include "base/file_path.h" |
| 24 #include "base/hash_tables.h" | 24 #include "base/hash_tables.h" |
| 25 #include "base/message_loop_proxy.h" | 25 #include "base/message_loop_proxy.h" |
| 26 #include "base/non_thread_safe.h" | 26 #include "base/non_thread_safe.h" |
| 27 #include "base/scoped_ptr.h" | 27 #include "base/scoped_ptr.h" |
| 28 #include "base/task.h" | 28 #include "base/task.h" |
| 29 #include "base/weak_ptr.h" | 29 #include "base/weak_ptr.h" |
| 30 #include "net/base/cache_type.h" | 30 #include "net/base/cache_type.h" |
| 31 #include "net/base/completion_callback.h" | 31 #include "net/base/completion_callback.h" |
| 32 #include "net/base/load_states.h" | 32 #include "net/base/load_states.h" |
| 33 #include "net/base/net_log.h" | |
| 33 #include "net/http/http_transaction_factory.h" | 34 #include "net/http/http_transaction_factory.h" |
| 34 | 35 |
| 35 class GURL; | 36 class GURL; |
| 36 | 37 |
| 37 namespace disk_cache { | 38 namespace disk_cache { |
| 38 class Backend; | 39 class Backend; |
| 39 class Entry; | 40 class Entry; |
| 40 } | 41 } |
| 41 | 42 |
| 42 namespace net { | 43 namespace net { |
| 43 | 44 |
| 44 class DnsCertProvenanceChecker; | 45 class DnsCertProvenanceChecker; |
| 45 class DnsRRResolver; | 46 class DnsRRResolver; |
| 46 class HostResolver; | 47 class HostResolver; |
| 47 class HttpAuthHandlerFactory; | 48 class HttpAuthHandlerFactory; |
| 48 class HttpNetworkDelegate; | 49 class HttpNetworkDelegate; |
| 49 class HttpNetworkSession; | 50 class HttpNetworkSession; |
| 50 struct HttpRequestInfo; | 51 struct HttpRequestInfo; |
| 51 class HttpResponseInfo; | 52 class HttpResponseInfo; |
| 52 class IOBuffer; | 53 class IOBuffer; |
| 53 class NetLog; | |
| 54 class ProxyService; | 54 class ProxyService; |
| 55 class SSLConfigService; | 55 class SSLConfigService; |
| 56 class ViewCacheHelper; | 56 class ViewCacheHelper; |
| 57 | 57 |
| 58 class HttpCache : public HttpTransactionFactory, | 58 class HttpCache : public HttpTransactionFactory, |
| 59 public base::SupportsWeakPtr<HttpCache>, | 59 public base::SupportsWeakPtr<HttpCache>, |
| 60 public NonThreadSafe { | 60 public NonThreadSafe { |
| 61 public: | 61 public: |
| 62 ~HttpCache(); | 62 ~HttpCache(); |
| 63 | 63 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 80 public: | 80 public: |
| 81 virtual ~BackendFactory() {} | 81 virtual ~BackendFactory() {} |
| 82 | 82 |
| 83 // The actual method to build the backend. Returns a net error code. If | 83 // The actual method to build the backend. Returns a net error code. If |
| 84 // ERR_IO_PENDING is returned, the |callback| will be notified when the | 84 // ERR_IO_PENDING is returned, the |callback| will be notified when the |
| 85 // operation completes, and |backend| must remain valid until the | 85 // operation completes, and |backend| must remain valid until the |
| 86 // notification arrives. | 86 // notification arrives. |
| 87 // The implementation must not access the factory object after invoking the | 87 // The implementation must not access the factory object after invoking the |
| 88 // |callback| because the object can be deleted from within the callback. | 88 // |callback| because the object can be deleted from within the callback. |
| 89 virtual int CreateBackend(disk_cache::Backend** backend, | 89 virtual int CreateBackend(disk_cache::Backend** backend, |
| 90 CompletionCallback* callback) = 0; | 90 CompletionCallback* callback, |
| 91 NetLog* net_log) = 0; | |
|
rvargas (doing something else)
2010/12/11 00:29:01
nit: this should be the first argument.
| |
| 91 }; | 92 }; |
| 92 | 93 |
| 93 // A default backend factory for the common use cases. | 94 // A default backend factory for the common use cases. |
| 94 class DefaultBackend : public BackendFactory { | 95 class DefaultBackend : public BackendFactory { |
| 95 public: | 96 public: |
| 96 // |path| is the destination for any files used by the backend, and | 97 // |path| is the destination for any files used by the backend, and |
| 97 // |cache_thread| is the thread where disk operations should take place. If | 98 // |cache_thread| is the thread where disk operations should take place. If |
| 98 // |max_bytes| is zero, a default value will be calculated automatically. | 99 // |max_bytes| is zero, a default value will be calculated automatically. |
| 99 DefaultBackend(CacheType type, const FilePath& path, int max_bytes, | 100 DefaultBackend(CacheType type, const FilePath& path, int max_bytes, |
| 100 base::MessageLoopProxy* thread); | 101 base::MessageLoopProxy* thread); |
| 101 virtual ~DefaultBackend(); | 102 virtual ~DefaultBackend(); |
| 102 | 103 |
| 103 // Returns a factory for an in-memory cache. | 104 // Returns a factory for an in-memory cache. |
| 104 static BackendFactory* InMemory(int max_bytes); | 105 static BackendFactory* InMemory(int max_bytes); |
| 105 | 106 |
| 106 // BackendFactory implementation. | 107 // BackendFactory implementation. |
| 107 virtual int CreateBackend(disk_cache::Backend** backend, | 108 virtual int CreateBackend(disk_cache::Backend** backend, |
| 108 CompletionCallback* callback); | 109 CompletionCallback* callback, |
| 110 NetLog* net_log); | |
| 109 | 111 |
| 110 private: | 112 private: |
| 111 CacheType type_; | 113 CacheType type_; |
| 112 const FilePath path_; | 114 const FilePath path_; |
| 113 int max_bytes_; | 115 int max_bytes_; |
| 114 scoped_refptr<base::MessageLoopProxy> thread_; | 116 scoped_refptr<base::MessageLoopProxy> thread_; |
| 115 }; | 117 }; |
| 116 | 118 |
| 117 // The disk cache is initialized lazily (by CreateTransaction) in this case. | 119 // The disk cache is initialized lazily (by CreateTransaction) in this case. |
| 118 // The HttpCache takes ownership of the |backend_factory|. | 120 // The HttpCache takes ownership of the |backend_factory|. |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 131 // network layer with a shared HttpNetworkSession in order for multiple | 133 // network layer with a shared HttpNetworkSession in order for multiple |
| 132 // network layers to share information (e.g. authenication data). The | 134 // network layers to share information (e.g. authenication data). The |
| 133 // HttpCache takes ownership of the |backend_factory|. | 135 // HttpCache takes ownership of the |backend_factory|. |
| 134 HttpCache(HttpNetworkSession* session, BackendFactory* backend_factory); | 136 HttpCache(HttpNetworkSession* session, BackendFactory* backend_factory); |
| 135 | 137 |
| 136 // Initialize the cache from its component parts, which is useful for | 138 // Initialize the cache from its component parts, which is useful for |
| 137 // testing. The lifetime of the network_layer and backend_factory are managed | 139 // testing. The lifetime of the network_layer and backend_factory are managed |
| 138 // by the HttpCache and will be destroyed using |delete| when the HttpCache is | 140 // by the HttpCache and will be destroyed using |delete| when the HttpCache is |
| 139 // destroyed. | 141 // destroyed. |
| 140 HttpCache(HttpTransactionFactory* network_layer, | 142 HttpCache(HttpTransactionFactory* network_layer, |
| 141 BackendFactory* backend_factory); | 143 BackendFactory* backend_factory, |
| 144 NetLog* net_log); | |
| 142 | 145 |
| 143 HttpTransactionFactory* network_layer() { return network_layer_.get(); } | 146 HttpTransactionFactory* network_layer() { return network_layer_.get(); } |
| 144 | 147 |
| 145 // Retrieves the cache backend for this HttpCache instance. If the backend | 148 // Retrieves the cache backend for this HttpCache instance. If the backend |
| 146 // is not initialized yet, this method will initialize it. The return value is | 149 // is not initialized yet, this method will initialize it. The return value is |
| 147 // a network error code, and it could be ERR_IO_PENDING, in which case the | 150 // a network error code, and it could be ERR_IO_PENDING, in which case the |
| 148 // |callback| will be notified when the operation completes. The pointer that | 151 // |callback| will be notified when the operation completes. The pointer that |
| 149 // receives the |backend| must remain valid until the operation completes. | 152 // receives the |backend| must remain valid until the operation completes. |
| 150 int GetBackend(disk_cache::Backend** backend, CompletionCallback* callback); | 153 int GetBackend(disk_cache::Backend** backend, CompletionCallback* callback); |
| 151 | 154 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 205 typedef std::list<Transaction*> TransactionList; | 208 typedef std::list<Transaction*> TransactionList; |
| 206 typedef std::list<WorkItem*> WorkItemList; | 209 typedef std::list<WorkItem*> WorkItemList; |
| 207 | 210 |
| 208 struct ActiveEntry { | 211 struct ActiveEntry { |
| 209 disk_cache::Entry* disk_entry; | 212 disk_cache::Entry* disk_entry; |
| 210 Transaction* writer; | 213 Transaction* writer; |
| 211 TransactionList readers; | 214 TransactionList readers; |
| 212 TransactionList pending_queue; | 215 TransactionList pending_queue; |
| 213 bool will_process_pending_queue; | 216 bool will_process_pending_queue; |
| 214 bool doomed; | 217 bool doomed; |
| 218 BoundNetLog net_log; | |
|
rvargas (doing something else)
2010/12/11 00:29:01
I'm not convinced that making ActiveEntry a source
mmenke
2010/12/11 01:51:05
I wasn't sure if they were worth logging or not.
| |
| 215 | 219 |
| 216 explicit ActiveEntry(disk_cache::Entry*); | 220 explicit ActiveEntry(disk_cache::Entry* entry, NetLog* net_log); |
| 217 ~ActiveEntry(); | 221 ~ActiveEntry(); |
| 218 }; | 222 }; |
| 219 | 223 |
| 220 typedef base::hash_map<std::string, ActiveEntry*> ActiveEntriesMap; | 224 typedef base::hash_map<std::string, ActiveEntry*> ActiveEntriesMap; |
| 221 typedef base::hash_map<std::string, PendingOp*> PendingOpsMap; | 225 typedef base::hash_map<std::string, PendingOp*> PendingOpsMap; |
| 222 typedef std::set<ActiveEntry*> ActiveEntriesSet; | 226 typedef std::set<ActiveEntry*> ActiveEntriesSet; |
| 223 | 227 |
| 224 // Methods ------------------------------------------------------------------ | 228 // Methods ------------------------------------------------------------------ |
| 225 | 229 |
| 226 // Creates the |backend| object and notifies the |callback| when the operation | 230 // Creates the |backend| object and notifies the |callback| when the operation |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 286 // Destroys an ActiveEntry (active or doomed). | 290 // Destroys an ActiveEntry (active or doomed). |
| 287 void DestroyEntry(ActiveEntry* entry); | 291 void DestroyEntry(ActiveEntry* entry); |
| 288 | 292 |
| 289 // Adds a transaction to an ActiveEntry. If this method returns ERR_IO_PENDING | 293 // Adds a transaction to an ActiveEntry. If this method returns ERR_IO_PENDING |
| 290 // the transaction will be notified about completion via its IO callback. This | 294 // the transaction will be notified about completion via its IO callback. This |
| 291 // method returns ERR_CACHE_RACE to signal the transaction that it cannot be | 295 // method returns ERR_CACHE_RACE to signal the transaction that it cannot be |
| 292 // added to the provided entry, and it should retry the process with another | 296 // added to the provided entry, and it should retry the process with another |
| 293 // one (in this case, the entry is no longer valid). | 297 // one (in this case, the entry is no longer valid). |
| 294 int AddTransactionToEntry(ActiveEntry* entry, Transaction* trans); | 298 int AddTransactionToEntry(ActiveEntry* entry, Transaction* trans); |
| 295 | 299 |
| 300 // Same as AddTransactionToEntry, except it does create a NetLog event for | |
| 301 // queuing the event. Used by OnProcessPendingQueue to prevent redundant | |
| 302 // logging. | |
| 303 int AddTransactionToEntryInternal(ActiveEntry* entry, Transaction* trans); | |
| 304 | |
| 296 // Called when the transaction has finished working with this entry. |cancel| | 305 // Called when the transaction has finished working with this entry. |cancel| |
| 297 // is true if the operation was cancelled by the caller instead of running | 306 // is true if the operation was cancelled by the caller instead of running |
| 298 // to completion. | 307 // to completion. |
| 299 void DoneWithEntry(ActiveEntry* entry, Transaction* trans, bool cancel); | 308 void DoneWithEntry(ActiveEntry* entry, Transaction* trans, bool cancel); |
| 300 | 309 |
| 301 // Called when the transaction has finished writting to this entry. |success| | 310 // Called when the transaction has finished writting to this entry. |success| |
| 302 // is false if the cache entry should be deleted. | 311 // is false if the cache entry should be deleted. |
| 303 void DoneWritingToEntry(ActiveEntry* entry, bool success); | 312 void DoneWritingToEntry(ActiveEntry* entry, bool success); |
| 304 | 313 |
| 305 // Called when the transaction has finished reading from this entry. | 314 // Called when the transaction has finished reading from this entry. |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 335 | 344 |
| 336 // Processes BackendCallback notifications. | 345 // Processes BackendCallback notifications. |
| 337 void OnIOComplete(int result, PendingOp* entry); | 346 void OnIOComplete(int result, PendingOp* entry); |
| 338 | 347 |
| 339 // Processes the backend creation notification. | 348 // Processes the backend creation notification. |
| 340 void OnBackendCreated(int result, PendingOp* pending_op); | 349 void OnBackendCreated(int result, PendingOp* pending_op); |
| 341 | 350 |
| 342 | 351 |
| 343 // Variables ---------------------------------------------------------------- | 352 // Variables ---------------------------------------------------------------- |
| 344 | 353 |
| 354 NetLog* net_log_; | |
| 355 | |
| 345 // Used when lazily constructing the disk_cache_. | 356 // Used when lazily constructing the disk_cache_. |
| 346 scoped_ptr<BackendFactory> backend_factory_; | 357 scoped_ptr<BackendFactory> backend_factory_; |
| 347 bool building_backend_; | 358 bool building_backend_; |
| 348 | 359 |
| 349 Mode mode_; | 360 Mode mode_; |
| 350 | 361 |
| 351 scoped_ptr<SSLHostInfoFactoryAdaptor> ssl_host_info_factory_; | 362 scoped_ptr<SSLHostInfoFactoryAdaptor> ssl_host_info_factory_; |
| 352 | 363 |
| 353 scoped_ptr<HttpTransactionFactory> network_layer_; | 364 scoped_ptr<HttpTransactionFactory> network_layer_; |
| 354 scoped_ptr<disk_cache::Backend> disk_cache_; | 365 scoped_ptr<disk_cache::Backend> disk_cache_; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 366 | 377 |
| 367 typedef base::hash_map<std::string, int> PlaybackCacheMap; | 378 typedef base::hash_map<std::string, int> PlaybackCacheMap; |
| 368 scoped_ptr<PlaybackCacheMap> playback_cache_map_; | 379 scoped_ptr<PlaybackCacheMap> playback_cache_map_; |
| 369 | 380 |
| 370 DISALLOW_COPY_AND_ASSIGN(HttpCache); | 381 DISALLOW_COPY_AND_ASSIGN(HttpCache); |
| 371 }; | 382 }; |
| 372 | 383 |
| 373 } // namespace net | 384 } // namespace net |
| 374 | 385 |
| 375 #endif // NET_HTTP_HTTP_CACHE_H_ | 386 #endif // NET_HTTP_HTTP_CACHE_H_ |
| OLD | NEW |