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 10 matching lines...) Expand all Loading... |
21 #include "base/file_path.h" | 21 #include "base/file_path.h" |
22 #include "base/hash_tables.h" | 22 #include "base/hash_tables.h" |
23 #include "base/scoped_ptr.h" | 23 #include "base/scoped_ptr.h" |
24 #include "base/task.h" | 24 #include "base/task.h" |
25 #include "base/weak_ptr.h" | 25 #include "base/weak_ptr.h" |
26 #include "net/base/cache_type.h" | 26 #include "net/base/cache_type.h" |
27 #include "net/base/completion_callback.h" | 27 #include "net/base/completion_callback.h" |
28 #include "net/http/http_transaction_factory.h" | 28 #include "net/http/http_transaction_factory.h" |
29 | 29 |
30 class GURL; | 30 class GURL; |
| 31 class MessageLoop; |
31 class ViewCacheHelper; | 32 class ViewCacheHelper; |
32 | 33 |
33 namespace disk_cache { | 34 namespace disk_cache { |
34 class Backend; | 35 class Backend; |
35 class Entry; | 36 class Entry; |
36 } | 37 } |
37 | 38 |
38 namespace net { | 39 namespace net { |
39 | 40 |
40 class HostResolver; | 41 class HostResolver; |
(...skipping 21 matching lines...) Expand all Loading... |
62 // standard invalidations. | 63 // standard invalidations. |
63 PLAYBACK, | 64 PLAYBACK, |
64 // Disables reads and writes from the cache. | 65 // Disables reads and writes from the cache. |
65 // Equivalent to setting LOAD_DISABLE_CACHE on every request. | 66 // Equivalent to setting LOAD_DISABLE_CACHE on every request. |
66 DISABLE | 67 DISABLE |
67 }; | 68 }; |
68 | 69 |
69 // Initialize the cache from the directory where its data is stored. The | 70 // Initialize the cache from the directory where its data is stored. The |
70 // disk cache is initialized lazily (by CreateTransaction) in this case. If | 71 // disk cache is initialized lazily (by CreateTransaction) in this case. If |
71 // |cache_size| is zero, a default value will be calculated automatically. | 72 // |cache_size| is zero, a default value will be calculated automatically. |
| 73 // The |cache_thread| is the thread where disk operations should take place. |
72 HttpCache(NetworkChangeNotifier* network_change_notifier, | 74 HttpCache(NetworkChangeNotifier* network_change_notifier, |
73 HostResolver* host_resolver, | 75 HostResolver* host_resolver, |
74 ProxyService* proxy_service, | 76 ProxyService* proxy_service, |
75 SSLConfigService* ssl_config_service, | 77 SSLConfigService* ssl_config_service, |
76 HttpAuthHandlerFactory* http_auth_handler_factory, | 78 HttpAuthHandlerFactory* http_auth_handler_factory, |
77 const FilePath& cache_dir, | 79 const FilePath& cache_dir, |
| 80 MessageLoop* cache_thread, |
78 int cache_size); | 81 int cache_size); |
79 | 82 |
80 // Initialize the cache from the directory where its data is stored. The | 83 // Initialize the cache from the directory where its data is stored. The |
81 // disk cache is initialized lazily (by CreateTransaction) in this case. If | 84 // disk cache is initialized lazily (by CreateTransaction) in this case. If |
82 // |cache_size| is zero, a default value will be calculated automatically. | 85 // |cache_size| is zero, a default value will be calculated automatically. |
83 // Provide an existing HttpNetworkSession, the cache can construct a | 86 // Provide an existing HttpNetworkSession, the cache can construct a |
84 // network layer with a shared HttpNetworkSession in order for multiple | 87 // network layer with a shared HttpNetworkSession in order for multiple |
85 // network layers to share information (e.g. authenication data). | 88 // network layers to share information (e.g. authenication data). |
| 89 // The |cache_thread| is the thread where disk operations should take place. |
86 HttpCache(HttpNetworkSession* session, const FilePath& cache_dir, | 90 HttpCache(HttpNetworkSession* session, const FilePath& cache_dir, |
87 int cache_size); | 91 MessageLoop* cache_thread, int cache_size); |
88 | 92 |
89 // Initialize using an in-memory cache. The cache is initialized lazily | 93 // Initialize using an in-memory cache. The cache is initialized lazily |
90 // (by CreateTransaction) in this case. If |cache_size| is zero, a default | 94 // (by CreateTransaction) in this case. If |cache_size| is zero, a default |
91 // value will be calculated automatically. | 95 // value will be calculated automatically. |
92 HttpCache(NetworkChangeNotifier* network_change_notifier, | 96 HttpCache(NetworkChangeNotifier* network_change_notifier, |
93 HostResolver* host_resolver, | 97 HostResolver* host_resolver, |
94 ProxyService* proxy_service, | 98 ProxyService* proxy_service, |
95 SSLConfigService* ssl_config_service, | 99 SSLConfigService* ssl_config_service, |
96 HttpAuthHandlerFactory* http_auth_handler_factory, | 100 HttpAuthHandlerFactory* http_auth_handler_factory, |
97 int cache_size); | 101 int cache_size); |
98 | 102 |
99 // Initialize the cache from its component parts, which is useful for | 103 // Initialize the cache from its component parts, which is useful for |
100 // testing. The lifetime of the network_layer and disk_cache are managed by | 104 // testing. The lifetime of the network_layer and disk_cache are managed by |
101 // the HttpCache and will be destroyed using |delete| when the HttpCache is | 105 // the HttpCache and will be destroyed using |delete| when the HttpCache is |
102 // destroyed. | 106 // destroyed. |
103 HttpCache(HttpTransactionFactory* network_layer, | 107 HttpCache(HttpTransactionFactory* network_layer, |
104 disk_cache::Backend* disk_cache); | 108 disk_cache::Backend* disk_cache); |
105 | 109 |
106 HttpTransactionFactory* network_layer() { return network_layer_.get(); } | 110 HttpTransactionFactory* network_layer() { return network_layer_.get(); } |
107 | 111 |
108 // Returns the cache backend for this HttpCache instance. If the backend | 112 // Returns the cache backend for this HttpCache instance. If the backend |
109 // is not initialized yet, this method will initialize it. If the return | 113 // is not initialized yet, this method will initialize it. If the return |
110 // value is NULL then the backend cannot be initialized. | 114 // value is NULL then the backend cannot be initialized. |
| 115 // This method is deprecated. |
111 disk_cache::Backend* GetBackend(); | 116 disk_cache::Backend* GetBackend(); |
112 | 117 |
| 118 // Retrieves the cache backend for this HttpCache instance. If the backend |
| 119 // is not initialized yet, this method will initialize it. The return value is |
| 120 // a network error code, and it could be ERR_IO_PENDING, in which case the |
| 121 // |callback| will be notified when the operation completes. The pointer that |
| 122 // receives the |backend| must remain valid until the operation completes. |
| 123 int GetBackend(disk_cache::Backend** backend, CompletionCallback* callback); |
| 124 |
113 // HttpTransactionFactory implementation: | 125 // HttpTransactionFactory implementation: |
114 virtual int CreateTransaction(scoped_ptr<HttpTransaction>* trans); | 126 virtual int CreateTransaction(scoped_ptr<HttpTransaction>* trans); |
115 virtual HttpCache* GetCache(); | 127 virtual HttpCache* GetCache(); |
116 virtual HttpNetworkSession* GetSession(); | 128 virtual HttpNetworkSession* GetSession(); |
117 virtual void Suspend(bool suspend); | 129 virtual void Suspend(bool suspend); |
118 | 130 |
119 // Helper function for reading response info from the disk cache. If the | 131 // Helper function for reading response info from the disk cache. If the |
120 // cache doesn't have the whole resource *|request_truncated| is set to true. | 132 // cache doesn't have the whole resource *|request_truncated| is set to true. |
121 // Avoid this function for performance critical paths as it uses blocking IO. | 133 // Avoid this function for performance critical paths as it uses blocking IO. |
122 static bool ReadResponseInfo(disk_cache::Entry* disk_entry, | 134 static bool ReadResponseInfo(disk_cache::Entry* disk_entry, |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 bool doomed; | 206 bool doomed; |
195 | 207 |
196 explicit ActiveEntry(disk_cache::Entry*); | 208 explicit ActiveEntry(disk_cache::Entry*); |
197 ~ActiveEntry(); | 209 ~ActiveEntry(); |
198 }; | 210 }; |
199 | 211 |
200 typedef base::hash_map<std::string, ActiveEntry*> ActiveEntriesMap; | 212 typedef base::hash_map<std::string, ActiveEntry*> ActiveEntriesMap; |
201 typedef base::hash_map<std::string, NewEntry*> NewEntriesMap; | 213 typedef base::hash_map<std::string, NewEntry*> NewEntriesMap; |
202 typedef std::set<ActiveEntry*> ActiveEntriesSet; | 214 typedef std::set<ActiveEntry*> ActiveEntriesSet; |
203 | 215 |
204 | |
205 // Methods ------------------------------------------------------------------ | 216 // Methods ------------------------------------------------------------------ |
206 | 217 |
207 // Generates the cache key for this request. | 218 // Generates the cache key for this request. |
208 std::string GenerateCacheKey(const HttpRequestInfo*); | 219 std::string GenerateCacheKey(const HttpRequestInfo*); |
209 | 220 |
210 // Dooms the entry selected by |key|. |trans| will be notified via its IO | 221 // Dooms the entry selected by |key|. |trans| will be notified via its IO |
211 // callback if this method returns ERR_IO_PENDING. The entry can be | 222 // callback if this method returns ERR_IO_PENDING. The entry can be |
212 // currently in use or not. | 223 // currently in use or not. |
213 int DoomEntry(const std::string& key, Transaction* trans); | 224 int DoomEntry(const std::string& key, Transaction* trans); |
214 | 225 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 // Callbacks ---------------------------------------------------------------- | 313 // Callbacks ---------------------------------------------------------------- |
303 | 314 |
304 // Processes BackendCallback notifications. | 315 // Processes BackendCallback notifications. |
305 void OnIOComplete(int result, NewEntry* entry); | 316 void OnIOComplete(int result, NewEntry* entry); |
306 | 317 |
307 | 318 |
308 // Variables ---------------------------------------------------------------- | 319 // Variables ---------------------------------------------------------------- |
309 | 320 |
310 // Used when lazily constructing the disk_cache_. | 321 // Used when lazily constructing the disk_cache_. |
311 FilePath disk_cache_dir_; | 322 FilePath disk_cache_dir_; |
| 323 MessageLoop* cache_thread_; |
312 | 324 |
313 Mode mode_; | 325 Mode mode_; |
314 CacheType type_; | 326 CacheType type_; |
315 | 327 |
316 scoped_ptr<HttpTransactionFactory> network_layer_; | 328 scoped_ptr<HttpTransactionFactory> network_layer_; |
317 scoped_ptr<disk_cache::Backend> disk_cache_; | 329 scoped_ptr<disk_cache::Backend> disk_cache_; |
318 | 330 |
319 // The set of active entries indexed by cache key. | 331 // The set of active entries indexed by cache key. |
320 ActiveEntriesMap active_entries_; | 332 ActiveEntriesMap active_entries_; |
321 | 333 |
(...skipping 10 matching lines...) Expand all Loading... |
332 | 344 |
333 typedef base::hash_map<std::string, int> PlaybackCacheMap; | 345 typedef base::hash_map<std::string, int> PlaybackCacheMap; |
334 scoped_ptr<PlaybackCacheMap> playback_cache_map_; | 346 scoped_ptr<PlaybackCacheMap> playback_cache_map_; |
335 | 347 |
336 DISALLOW_COPY_AND_ASSIGN(HttpCache); | 348 DISALLOW_COPY_AND_ASSIGN(HttpCache); |
337 }; | 349 }; |
338 | 350 |
339 } // namespace net | 351 } // namespace net |
340 | 352 |
341 #endif // NET_HTTP_HTTP_CACHE_H_ | 353 #endif // NET_HTTP_HTTP_CACHE_H_ |
OLD | NEW |