| 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, |
| 143 NetLog* net_log, |
| 141 BackendFactory* backend_factory); | 144 BackendFactory* backend_factory); |
| 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); |
| (...skipping 55 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 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 | 338 |
| 336 // Processes BackendCallback notifications. | 339 // Processes BackendCallback notifications. |
| 337 void OnIOComplete(int result, PendingOp* entry); | 340 void OnIOComplete(int result, PendingOp* entry); |
| 338 | 341 |
| 339 // Processes the backend creation notification. | 342 // Processes the backend creation notification. |
| 340 void OnBackendCreated(int result, PendingOp* pending_op); | 343 void OnBackendCreated(int result, PendingOp* pending_op); |
| 341 | 344 |
| 342 | 345 |
| 343 // Variables ---------------------------------------------------------------- | 346 // Variables ---------------------------------------------------------------- |
| 344 | 347 |
| 348 NetLog* net_log_; |
| 349 |
| 345 // Used when lazily constructing the disk_cache_. | 350 // Used when lazily constructing the disk_cache_. |
| 346 scoped_ptr<BackendFactory> backend_factory_; | 351 scoped_ptr<BackendFactory> backend_factory_; |
| 347 bool building_backend_; | 352 bool building_backend_; |
| 348 | 353 |
| 349 Mode mode_; | 354 Mode mode_; |
| 350 | 355 |
| 351 scoped_ptr<SSLHostInfoFactoryAdaptor> ssl_host_info_factory_; | 356 scoped_ptr<SSLHostInfoFactoryAdaptor> ssl_host_info_factory_; |
| 352 | 357 |
| 353 scoped_ptr<HttpTransactionFactory> network_layer_; | 358 scoped_ptr<HttpTransactionFactory> network_layer_; |
| 354 scoped_ptr<disk_cache::Backend> disk_cache_; | 359 scoped_ptr<disk_cache::Backend> disk_cache_; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 366 | 371 |
| 367 typedef base::hash_map<std::string, int> PlaybackCacheMap; | 372 typedef base::hash_map<std::string, int> PlaybackCacheMap; |
| 368 scoped_ptr<PlaybackCacheMap> playback_cache_map_; | 373 scoped_ptr<PlaybackCacheMap> playback_cache_map_; |
| 369 | 374 |
| 370 DISALLOW_COPY_AND_ASSIGN(HttpCache); | 375 DISALLOW_COPY_AND_ASSIGN(HttpCache); |
| 371 }; | 376 }; |
| 372 | 377 |
| 373 } // namespace net | 378 } // namespace net |
| 374 | 379 |
| 375 #endif // NET_HTTP_HTTP_CACHE_H_ | 380 #endif // NET_HTTP_HTTP_CACHE_H_ |
| OLD | NEW |