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