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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 class BackendFactory { | 80 class BackendFactory { |
81 public: | 81 public: |
82 virtual ~BackendFactory() {} | 82 virtual ~BackendFactory() {} |
83 | 83 |
84 // The actual method to build the backend. Returns a net error code. If | 84 // The actual method to build the backend. Returns a net error code. If |
85 // ERR_IO_PENDING is returned, the |callback| will be notified when the | 85 // ERR_IO_PENDING is returned, the |callback| will be notified when the |
86 // operation completes, and |backend| must remain valid until the | 86 // operation completes, and |backend| must remain valid until the |
87 // notification arrives. | 87 // notification arrives. |
88 // The implementation must not access the factory object after invoking the | 88 // The implementation must not access the factory object after invoking the |
89 // |callback| because the object can be deleted from within the callback. | 89 // |callback| because the object can be deleted from within the callback. |
90 virtual int CreateBackend(disk_cache::Backend** backend, | 90 virtual int CreateBackend(NetLog* net_log, |
| 91 disk_cache::Backend** backend, |
91 CompletionCallback* callback) = 0; | 92 CompletionCallback* callback) = 0; |
92 }; | 93 }; |
93 | 94 |
94 // A default backend factory for the common use cases. | 95 // A default backend factory for the common use cases. |
95 class DefaultBackend : public BackendFactory { | 96 class DefaultBackend : public BackendFactory { |
96 public: | 97 public: |
97 // |path| is the destination for any files used by the backend, and | 98 // |path| is the destination for any files used by the backend, and |
98 // |cache_thread| is the thread where disk operations should take place. If | 99 // |cache_thread| is the thread where disk operations should take place. If |
99 // |max_bytes| is zero, a default value will be calculated automatically. | 100 // |max_bytes| is zero, a default value will be calculated automatically. |
100 DefaultBackend(CacheType type, const FilePath& path, int max_bytes, | 101 DefaultBackend(CacheType type, const FilePath& path, int max_bytes, |
101 base::MessageLoopProxy* thread); | 102 base::MessageLoopProxy* thread); |
102 virtual ~DefaultBackend(); | 103 virtual ~DefaultBackend(); |
103 | 104 |
104 // Returns a factory for an in-memory cache. | 105 // Returns a factory for an in-memory cache. |
105 static BackendFactory* InMemory(int max_bytes); | 106 static BackendFactory* InMemory(int max_bytes); |
106 | 107 |
107 // BackendFactory implementation. | 108 // BackendFactory implementation. |
108 virtual int CreateBackend(disk_cache::Backend** backend, | 109 virtual int CreateBackend(NetLog* net_log, |
| 110 disk_cache::Backend** backend, |
109 CompletionCallback* callback); | 111 CompletionCallback* callback); |
110 | 112 |
111 private: | 113 private: |
112 CacheType type_; | 114 CacheType type_; |
113 const FilePath path_; | 115 const FilePath path_; |
114 int max_bytes_; | 116 int max_bytes_; |
115 scoped_refptr<base::MessageLoopProxy> thread_; | 117 scoped_refptr<base::MessageLoopProxy> thread_; |
116 }; | 118 }; |
117 | 119 |
118 // The disk cache is initialized lazily (by CreateTransaction) in this case. | 120 // The disk cache is initialized lazily (by CreateTransaction) in this case. |
(...skipping 14 matching lines...) Expand all Loading... |
133 // network layer with a shared HttpNetworkSession in order for multiple | 135 // network layer with a shared HttpNetworkSession in order for multiple |
134 // network layers to share information (e.g. authenication data). The | 136 // network layers to share information (e.g. authenication data). The |
135 // HttpCache takes ownership of the |backend_factory|. | 137 // HttpCache takes ownership of the |backend_factory|. |
136 HttpCache(HttpNetworkSession* session, BackendFactory* backend_factory); | 138 HttpCache(HttpNetworkSession* session, BackendFactory* backend_factory); |
137 | 139 |
138 // Initialize the cache from its component parts, which is useful for | 140 // Initialize the cache from its component parts, which is useful for |
139 // testing. The lifetime of the network_layer and backend_factory are managed | 141 // testing. The lifetime of the network_layer and backend_factory are managed |
140 // by the HttpCache and will be destroyed using |delete| when the HttpCache is | 142 // by the HttpCache and will be destroyed using |delete| when the HttpCache is |
141 // destroyed. | 143 // destroyed. |
142 HttpCache(HttpTransactionFactory* network_layer, | 144 HttpCache(HttpTransactionFactory* network_layer, |
| 145 NetLog* net_log, |
143 BackendFactory* backend_factory); | 146 BackendFactory* backend_factory); |
144 | 147 |
145 HttpTransactionFactory* network_layer() { return network_layer_.get(); } | 148 HttpTransactionFactory* network_layer() { return network_layer_.get(); } |
146 | 149 |
147 // Retrieves the cache backend for this HttpCache instance. If the backend | 150 // Retrieves the cache backend for this HttpCache instance. If the backend |
148 // is not initialized yet, this method will initialize it. The return value is | 151 // is not initialized yet, this method will initialize it. The return value is |
149 // a network error code, and it could be ERR_IO_PENDING, in which case the | 152 // 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 | 153 // |callback| will be notified when the operation completes. The pointer that |
151 // receives the |backend| must remain valid until the operation completes. | 154 // receives the |backend| must remain valid until the operation completes. |
152 int GetBackend(disk_cache::Backend** backend, CompletionCallback* callback); | 155 int GetBackend(disk_cache::Backend** backend, CompletionCallback* callback); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 typedef std::list<WorkItem*> WorkItemList; | 211 typedef std::list<WorkItem*> WorkItemList; |
209 | 212 |
210 struct ActiveEntry { | 213 struct ActiveEntry { |
211 disk_cache::Entry* disk_entry; | 214 disk_cache::Entry* disk_entry; |
212 Transaction* writer; | 215 Transaction* writer; |
213 TransactionList readers; | 216 TransactionList readers; |
214 TransactionList pending_queue; | 217 TransactionList pending_queue; |
215 bool will_process_pending_queue; | 218 bool will_process_pending_queue; |
216 bool doomed; | 219 bool doomed; |
217 | 220 |
218 explicit ActiveEntry(disk_cache::Entry*); | 221 explicit ActiveEntry(disk_cache::Entry* entry); |
219 ~ActiveEntry(); | 222 ~ActiveEntry(); |
220 }; | 223 }; |
221 | 224 |
222 typedef base::hash_map<std::string, ActiveEntry*> ActiveEntriesMap; | 225 typedef base::hash_map<std::string, ActiveEntry*> ActiveEntriesMap; |
223 typedef base::hash_map<std::string, PendingOp*> PendingOpsMap; | 226 typedef base::hash_map<std::string, PendingOp*> PendingOpsMap; |
224 typedef std::set<ActiveEntry*> ActiveEntriesSet; | 227 typedef std::set<ActiveEntry*> ActiveEntriesSet; |
225 | 228 |
226 // Methods ------------------------------------------------------------------ | 229 // Methods ------------------------------------------------------------------ |
227 | 230 |
228 // Creates the |backend| object and notifies the |callback| when the operation | 231 // Creates the |backend| object and notifies the |callback| when the operation |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 | 340 |
338 // Processes BackendCallback notifications. | 341 // Processes BackendCallback notifications. |
339 void OnIOComplete(int result, PendingOp* entry); | 342 void OnIOComplete(int result, PendingOp* entry); |
340 | 343 |
341 // Processes the backend creation notification. | 344 // Processes the backend creation notification. |
342 void OnBackendCreated(int result, PendingOp* pending_op); | 345 void OnBackendCreated(int result, PendingOp* pending_op); |
343 | 346 |
344 | 347 |
345 // Variables ---------------------------------------------------------------- | 348 // Variables ---------------------------------------------------------------- |
346 | 349 |
| 350 NetLog* net_log_; |
| 351 |
347 // Used when lazily constructing the disk_cache_. | 352 // Used when lazily constructing the disk_cache_. |
348 scoped_ptr<BackendFactory> backend_factory_; | 353 scoped_ptr<BackendFactory> backend_factory_; |
349 bool building_backend_; | 354 bool building_backend_; |
350 | 355 |
351 Mode mode_; | 356 Mode mode_; |
352 | 357 |
353 scoped_ptr<SSLHostInfoFactoryAdaptor> ssl_host_info_factory_; | 358 scoped_ptr<SSLHostInfoFactoryAdaptor> ssl_host_info_factory_; |
354 | 359 |
355 scoped_ptr<HttpTransactionFactory> network_layer_; | 360 scoped_ptr<HttpTransactionFactory> network_layer_; |
356 scoped_ptr<disk_cache::Backend> disk_cache_; | 361 scoped_ptr<disk_cache::Backend> disk_cache_; |
(...skipping 11 matching lines...) Expand all Loading... |
368 | 373 |
369 typedef base::hash_map<std::string, int> PlaybackCacheMap; | 374 typedef base::hash_map<std::string, int> PlaybackCacheMap; |
370 scoped_ptr<PlaybackCacheMap> playback_cache_map_; | 375 scoped_ptr<PlaybackCacheMap> playback_cache_map_; |
371 | 376 |
372 DISALLOW_COPY_AND_ASSIGN(HttpCache); | 377 DISALLOW_COPY_AND_ASSIGN(HttpCache); |
373 }; | 378 }; |
374 | 379 |
375 } // namespace net | 380 } // namespace net |
376 | 381 |
377 #endif // NET_HTTP_HTTP_CACHE_H_ | 382 #endif // NET_HTTP_HTTP_CACHE_H_ |
OLD | NEW |