| 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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 class BackendFactory { | 79 class BackendFactory { |
| 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(NetLog* net_log, |
| 90 disk_cache::Backend** backend, |
| 90 CompletionCallback* callback) = 0; | 91 CompletionCallback* callback) = 0; |
| 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(NetLog* net_log, |
| 109 disk_cache::Backend** backend, |
| 108 CompletionCallback* callback); | 110 CompletionCallback* callback); |
| 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. |
| (...skipping 13 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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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; |
| 215 | 218 |
| 216 explicit ActiveEntry(disk_cache::Entry*); | 219 explicit ActiveEntry(disk_cache::Entry* entry); |
| 217 ~ActiveEntry(); | 220 ~ActiveEntry(); |
| 218 }; | 221 }; |
| 219 | 222 |
| 220 typedef base::hash_map<std::string, ActiveEntry*> ActiveEntriesMap; | 223 typedef base::hash_map<std::string, ActiveEntry*> ActiveEntriesMap; |
| 221 typedef base::hash_map<std::string, PendingOp*> PendingOpsMap; | 224 typedef base::hash_map<std::string, PendingOp*> PendingOpsMap; |
| 222 typedef std::set<ActiveEntry*> ActiveEntriesSet; | 225 typedef std::set<ActiveEntry*> ActiveEntriesSet; |
| 223 | 226 |
| 224 // Methods ------------------------------------------------------------------ | 227 // Methods ------------------------------------------------------------------ |
| 225 | 228 |
| 226 // Creates the |backend| object and notifies the |callback| when the operation | 229 // 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). | 289 // Destroys an ActiveEntry (active or doomed). |
| 287 void DestroyEntry(ActiveEntry* entry); | 290 void DestroyEntry(ActiveEntry* entry); |
| 288 | 291 |
| 289 // Adds a transaction to an ActiveEntry. If this method returns ERR_IO_PENDING | 292 // 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 | 293 // 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 | 294 // 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 | 295 // added to the provided entry, and it should retry the process with another |
| 293 // one (in this case, the entry is no longer valid). | 296 // one (in this case, the entry is no longer valid). |
| 294 int AddTransactionToEntry(ActiveEntry* entry, Transaction* trans); | 297 int AddTransactionToEntry(ActiveEntry* entry, Transaction* trans); |
| 295 | 298 |
| 299 // Same as AddTransactionToEntry, except it does create a NetLog event for |
| 300 // queuing the event. Used by OnProcessPendingQueue to prevent redundant |
| 301 // logging. |
| 302 int AddTransactionToEntryInternal(ActiveEntry* entry, Transaction* trans); |
| 303 |
| 296 // Called when the transaction has finished working with this entry. |cancel| | 304 // 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 | 305 // is true if the operation was cancelled by the caller instead of running |
| 298 // to completion. | 306 // to completion. |
| 299 void DoneWithEntry(ActiveEntry* entry, Transaction* trans, bool cancel); | 307 void DoneWithEntry(ActiveEntry* entry, Transaction* trans, bool cancel); |
| 300 | 308 |
| 301 // Called when the transaction has finished writting to this entry. |success| | 309 // Called when the transaction has finished writting to this entry. |success| |
| 302 // is false if the cache entry should be deleted. | 310 // is false if the cache entry should be deleted. |
| 303 void DoneWritingToEntry(ActiveEntry* entry, bool success); | 311 void DoneWritingToEntry(ActiveEntry* entry, bool success); |
| 304 | 312 |
| 305 // Called when the transaction has finished reading from this entry. | 313 // Called when the transaction has finished reading from this entry. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 335 | 343 |
| 336 // Processes BackendCallback notifications. | 344 // Processes BackendCallback notifications. |
| 337 void OnIOComplete(int result, PendingOp* entry); | 345 void OnIOComplete(int result, PendingOp* entry); |
| 338 | 346 |
| 339 // Processes the backend creation notification. | 347 // Processes the backend creation notification. |
| 340 void OnBackendCreated(int result, PendingOp* pending_op); | 348 void OnBackendCreated(int result, PendingOp* pending_op); |
| 341 | 349 |
| 342 | 350 |
| 343 // Variables ---------------------------------------------------------------- | 351 // Variables ---------------------------------------------------------------- |
| 344 | 352 |
| 353 NetLog* net_log_; |
| 354 |
| 345 // Used when lazily constructing the disk_cache_. | 355 // Used when lazily constructing the disk_cache_. |
| 346 scoped_ptr<BackendFactory> backend_factory_; | 356 scoped_ptr<BackendFactory> backend_factory_; |
| 347 bool building_backend_; | 357 bool building_backend_; |
| 348 | 358 |
| 349 Mode mode_; | 359 Mode mode_; |
| 350 | 360 |
| 351 scoped_ptr<SSLHostInfoFactoryAdaptor> ssl_host_info_factory_; | 361 scoped_ptr<SSLHostInfoFactoryAdaptor> ssl_host_info_factory_; |
| 352 | 362 |
| 353 scoped_ptr<HttpTransactionFactory> network_layer_; | 363 scoped_ptr<HttpTransactionFactory> network_layer_; |
| 354 scoped_ptr<disk_cache::Backend> disk_cache_; | 364 scoped_ptr<disk_cache::Backend> disk_cache_; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 366 | 376 |
| 367 typedef base::hash_map<std::string, int> PlaybackCacheMap; | 377 typedef base::hash_map<std::string, int> PlaybackCacheMap; |
| 368 scoped_ptr<PlaybackCacheMap> playback_cache_map_; | 378 scoped_ptr<PlaybackCacheMap> playback_cache_map_; |
| 369 | 379 |
| 370 DISALLOW_COPY_AND_ASSIGN(HttpCache); | 380 DISALLOW_COPY_AND_ASSIGN(HttpCache); |
| 371 }; | 381 }; |
| 372 | 382 |
| 373 } // namespace net | 383 } // namespace net |
| 374 | 384 |
| 375 #endif // NET_HTTP_HTTP_CACHE_H_ | 385 #endif // NET_HTTP_HTTP_CACHE_H_ |
| OLD | NEW |