http_cache.h revision dc0f95d653279beabeb9817299e2902918ba123e
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
3// found in the LICENSE file.
4
5// This file declares a HttpTransactionFactory implementation that can be
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).
8//
9// The HttpCache takes a disk_cache::Backend as a parameter, and uses that for
10// the cache storage.
11//
12// See HttpTransactionFactory and HttpTransaction for more details.
13
14#ifndef NET_HTTP_HTTP_CACHE_H_
15#define NET_HTTP_HTTP_CACHE_H_
16#pragma once
17
18#include <list>
19#include <set>
20#include <string>
21
22#include "base/basictypes.h"
23#include "base/file_path.h"
24#include "base/hash_tables.h"
25#include "base/message_loop_proxy.h"
26#include "base/scoped_ptr.h"
27#include "base/task.h"
28#include "base/threading/non_thread_safe.h"
29#include "base/weak_ptr.h"
30#include "net/base/cache_type.h"
31#include "net/base/completion_callback.h"
32#include "net/base/load_states.h"
33#include "net/http/http_transaction_factory.h"
34
35class GURL;
36
37namespace disk_cache {
38class Backend;
39class Entry;
40}
41
42namespace net {
43
44class CertVerifier;
45class DnsCertProvenanceChecker;
46class DnsRRResolver;
47class HostResolver;
48class HttpAuthHandlerFactory;
49class HttpNetworkSession;
50struct HttpRequestInfo;
51class HttpResponseInfo;
52class IOBuffer;
53class NetLog;
54class NetworkDelegate;
55class ProxyService;
56class SSLConfigService;
57class ViewCacheHelper;
58
59class HttpCache : public HttpTransactionFactory,
60                  public base::SupportsWeakPtr<HttpCache>,
61                  public base::NonThreadSafe {
62 public:
63  // The cache mode of operation.
64  enum Mode {
65    // Normal mode just behaves like a standard web cache.
66    NORMAL = 0,
67    // Record mode caches everything for purposes of offline playback.
68    RECORD,
69    // Playback mode replays from a cache without considering any
70    // standard invalidations.
71    PLAYBACK,
72    // Disables reads and writes from the cache.
73    // Equivalent to setting LOAD_DISABLE_CACHE on every request.
74    DISABLE
75  };
76
77  // A BackendFactory creates a backend object to be used by the HttpCache.
78  class BackendFactory {
79   public:
80    virtual ~BackendFactory() {}
81
82    // The actual method to build the backend. Returns a net error code. If
83    // ERR_IO_PENDING is returned, the |callback| will be notified when the
84    // operation completes, and |backend| must remain valid until the
85    // notification arrives.
86    // The implementation must not access the factory object after invoking the
87    // |callback| because the object can be deleted from within the callback.
88    virtual int CreateBackend(NetLog* net_log,
89                              disk_cache::Backend** backend,
90                              CompletionCallback* callback) = 0;
91  };
92
93  // A default backend factory for the common use cases.
94  class DefaultBackend : public BackendFactory {
95   public:
96    // |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    // |max_bytes| is  zero, a default value will be calculated automatically.
99    DefaultBackend(CacheType type, const FilePath& path, int max_bytes,
100                   base::MessageLoopProxy* thread);
101    virtual ~DefaultBackend();
102
103    // Returns a factory for an in-memory cache.
104    static BackendFactory* InMemory(int max_bytes);
105
106    // BackendFactory implementation.
107    virtual int CreateBackend(NetLog* net_log,
108                              disk_cache::Backend** backend,
109                              CompletionCallback* callback);
110
111   private:
112    CacheType type_;
113    const FilePath path_;
114    int max_bytes_;
115    scoped_refptr<base::MessageLoopProxy> thread_;
116  };
117
118  // The disk cache is initialized lazily (by CreateTransaction) in this case.
119  // The HttpCache takes ownership of the |backend_factory|.
120  HttpCache(HostResolver* host_resolver,
121            CertVerifier* cert_verifier,
122            DnsRRResolver* dnsrr_resolver,
123            DnsCertProvenanceChecker* dns_cert_checker,
124            ProxyService* proxy_service,
125            SSLConfigService* ssl_config_service,
126            HttpAuthHandlerFactory* http_auth_handler_factory,
127            NetworkDelegate* network_delegate,
128            NetLog* net_log,
129            BackendFactory* backend_factory);
130
131  // The disk cache is initialized lazily (by CreateTransaction) in  this case.
132  // Provide an existing HttpNetworkSession, the cache can construct a
133  // network layer with a shared HttpNetworkSession in order for multiple
134  // network layers to share information (e.g. authentication data). The
135  // HttpCache takes ownership of the |backend_factory|.
136  HttpCache(HttpNetworkSession* session, BackendFactory* backend_factory);
137
138  // Initialize the cache from its component parts, which is useful for
139  // 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
141  // destroyed.
142  HttpCache(HttpTransactionFactory* network_layer,
143            NetLog* net_log,
144            BackendFactory* backend_factory);
145
146  ~HttpCache();
147
148  HttpTransactionFactory* network_layer() { return network_layer_.get(); }
149
150  // 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
152  // 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
154  // receives the |backend| must remain valid until the operation completes.
155  int GetBackend(disk_cache::Backend** backend, CompletionCallback* callback);
156
157  // Returns the current backend (can be NULL).
158  disk_cache::Backend* GetCurrentBackend();
159
160  // Given a header data blob, convert it to a response info object.
161  static bool ParseResponseInfo(const char* data, int len,
162                                HttpResponseInfo* response_info,
163                                bool* response_truncated);
164
165  // Writes |buf_len| bytes of metadata stored in |buf| to the cache entry
166  // referenced by |url|, as long as the entry's |expected_response_time| has
167  // not changed. This method returns without blocking, and the operation will
168  // be performed asynchronously without any completion notification.
169  void WriteMetadata(const GURL& url, base::Time expected_response_time,
170                     IOBuffer* buf, int buf_len);
171
172  // Get/Set the cache's mode.
173  void set_mode(Mode value) { mode_ = value; }
174  Mode mode() { return mode_; }
175
176  // Close currently active sockets so that fresh page loads will not use any
177  // recycled connections.  For sockets currently in use, they may not close
178  // immediately, but they will not be reusable. This is for debugging.
179  void CloseAllConnections();
180
181  // HttpTransactionFactory implementation:
182  virtual int CreateTransaction(scoped_ptr<HttpTransaction>* trans);
183  virtual HttpCache* GetCache();
184  virtual HttpNetworkSession* GetSession();
185  virtual void Suspend(bool suspend);
186
187 protected:
188  // Disk cache entry data indices.
189  enum {
190    kResponseInfoIndex = 0,
191    kResponseContentIndex,
192    kMetadataIndex,
193
194    // Must remain at the end of the enum.
195    kNumCacheEntryDataIndices
196  };
197  friend class ViewCacheHelper;
198
199 private:
200  // Types --------------------------------------------------------------------
201
202  class BackendCallback;
203  class MetadataWriter;
204  class SSLHostInfoFactoryAdaptor;
205  class Transaction;
206  class WorkItem;
207  friend class Transaction;
208  struct PendingOp;  // Info for an entry under construction.
209
210  typedef std::list<Transaction*> TransactionList;
211  typedef std::list<WorkItem*> WorkItemList;
212
213  struct ActiveEntry {
214    explicit ActiveEntry(disk_cache::Entry* entry);
215    ~ActiveEntry();
216
217    disk_cache::Entry* disk_entry;
218    Transaction*       writer;
219    TransactionList    readers;
220    TransactionList    pending_queue;
221    bool               will_process_pending_queue;
222    bool               doomed;
223  };
224
225  typedef base::hash_map<std::string, ActiveEntry*> ActiveEntriesMap;
226  typedef base::hash_map<std::string, PendingOp*> PendingOpsMap;
227  typedef std::set<ActiveEntry*> ActiveEntriesSet;
228  typedef base::hash_map<std::string, int> PlaybackCacheMap;
229
230  // Methods ------------------------------------------------------------------
231
232  // Creates the |backend| object and notifies the |callback| when the operation
233  // completes. Returns an error code.
234  int CreateBackend(disk_cache::Backend** backend,
235                    CompletionCallback* callback);
236
237  // Makes sure that the backend creation is complete before allowing the
238  // provided transaction to use the object. Returns an error code.  |trans|
239  // will be notified via its IO callback if this method returns ERR_IO_PENDING.
240  // The transaction is free to use the backend directly at any time after
241  // receiving the notification.
242  int GetBackendForTransaction(Transaction* trans);
243
244  // Generates the cache key for this request.
245  std::string GenerateCacheKey(const HttpRequestInfo*);
246
247  // Dooms the entry selected by |key|. |trans| will be notified via its IO
248  // callback if this method returns ERR_IO_PENDING. The entry can be
249  // currently in use or not.
250  int DoomEntry(const std::string& key, Transaction* trans);
251
252  // Dooms the entry selected by |key|. |trans| will be notified via its IO
253  // callback if this method returns ERR_IO_PENDING. The entry should not
254  // be currently in use.
255  int AsyncDoomEntry(const std::string& key, Transaction* trans);
256
257  // Closes a previously doomed entry.
258  void FinalizeDoomedEntry(ActiveEntry* entry);
259
260  // Returns an entry that is currently in use and not doomed, or NULL.
261  ActiveEntry* FindActiveEntry(const std::string& key);
262
263  // Creates a new ActiveEntry and starts tracking it. |disk_entry| is the disk
264  // cache entry.
265  ActiveEntry* ActivateEntry(disk_cache::Entry* disk_entry);
266
267  // Deletes an ActiveEntry.
268  void DeactivateEntry(ActiveEntry* entry);
269
270  // Deletes an ActiveEntry using an exhaustive search.
271  void SlowDeactivateEntry(ActiveEntry* entry);
272
273  // Returns the PendingOp for the desired |key|. If an entry is not under
274  // construction already, a new PendingOp structure is created.
275  PendingOp* GetPendingOp(const std::string& key);
276
277  // Deletes a PendingOp.
278  void DeletePendingOp(PendingOp* pending_op);
279
280  // Opens the disk cache entry associated with |key|, returning an ActiveEntry
281  // in |*entry|. |trans| will be notified via its IO callback if this method
282  // returns ERR_IO_PENDING.
283  int OpenEntry(const std::string& key, ActiveEntry** entry,
284                Transaction* trans);
285
286  // Creates the disk cache entry associated with |key|, returning an
287  // ActiveEntry in |*entry|. |trans| will be notified via its IO callback if
288  // this method returns ERR_IO_PENDING.
289  int CreateEntry(const std::string& key, ActiveEntry** entry,
290                  Transaction* trans);
291
292  // Destroys an ActiveEntry (active or doomed).
293  void DestroyEntry(ActiveEntry* entry);
294
295  // Adds a transaction to an ActiveEntry. If this method returns ERR_IO_PENDING
296  // the transaction will be notified about completion via its IO callback. This
297  // method returns ERR_CACHE_RACE to signal the transaction that it cannot be
298  // added to the provided entry, and it should retry the process with another
299  // one (in this case, the entry is no longer valid).
300  int AddTransactionToEntry(ActiveEntry* entry, Transaction* trans);
301
302  // Called when the transaction has finished working with this entry. |cancel|
303  // is true if the operation was cancelled by the caller instead of running
304  // to completion.
305  void DoneWithEntry(ActiveEntry* entry, Transaction* trans, bool cancel);
306
307  // Called when the transaction has finished writting to this entry. |success|
308  // is false if the cache entry should be deleted.
309  void DoneWritingToEntry(ActiveEntry* entry, bool success);
310
311  // Called when the transaction has finished reading from this entry.
312  void DoneReadingFromEntry(ActiveEntry* entry, Transaction* trans);
313
314  // Convers the active writter transaction to a reader so that other
315  // transactions can start reading from this entry.
316  void ConvertWriterToReader(ActiveEntry* entry);
317
318  // Returns the LoadState of the provided pending transaction.
319  LoadState GetLoadStateForPendingTransaction(const Transaction* trans);
320
321  // Removes the transaction |trans|, from the pending list of an entry
322  // (PendingOp, active or doomed entry).
323  void RemovePendingTransaction(Transaction* trans);
324
325  // Removes the transaction |trans|, from the pending list of |entry|.
326  bool RemovePendingTransactionFromEntry(ActiveEntry* entry,
327                                         Transaction* trans);
328
329  // Removes the transaction |trans|, from the pending list of |pending_op|.
330  bool RemovePendingTransactionFromPendingOp(PendingOp* pending_op,
331                                             Transaction* trans);
332
333  // Resumes processing the pending list of |entry|.
334  void ProcessPendingQueue(ActiveEntry* entry);
335
336  // Events (called via PostTask) ---------------------------------------------
337
338  void OnProcessPendingQueue(ActiveEntry* entry);
339
340  // Callbacks ----------------------------------------------------------------
341
342  // Processes BackendCallback notifications.
343  void OnIOComplete(int result, PendingOp* entry);
344
345  // Processes the backend creation notification.
346  void OnBackendCreated(int result, PendingOp* pending_op);
347
348
349  // Variables ----------------------------------------------------------------
350
351  NetLog* net_log_;
352
353  // Used when lazily constructing the disk_cache_.
354  scoped_ptr<BackendFactory> backend_factory_;
355  bool building_backend_;
356
357  Mode mode_;
358
359  const scoped_ptr<SSLHostInfoFactoryAdaptor> ssl_host_info_factory_;
360
361  const scoped_ptr<HttpTransactionFactory> network_layer_;
362  scoped_ptr<disk_cache::Backend> disk_cache_;
363
364  // The set of active entries indexed by cache key.
365  ActiveEntriesMap active_entries_;
366
367  // The set of doomed entries.
368  ActiveEntriesSet doomed_entries_;
369
370  // The set of entries "under construction".
371  PendingOpsMap pending_ops_;
372
373  ScopedRunnableMethodFactory<HttpCache> task_factory_;
374
375  scoped_ptr<PlaybackCacheMap> playback_cache_map_;
376
377  DISALLOW_COPY_AND_ASSIGN(HttpCache);
378};
379
380}  // namespace net
381
382#endif  // NET_HTTP_HTTP_CACHE_H_
383