summaryrefslogtreecommitdiffstats
path: root/net/http/infinite_cache.h
blob: 30d90257cd594e2d8abb3a0baac313eb40c5734f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This is the interface of a simulated infinite cache. The purpose of this
// code is to evaluate the performance of an HTTP cache that is not constrained
// to evict resources.

#ifndef NET_HTTP_INFINITE_CACHE_H_
#define NET_HTTP_INFINITE_CACHE_H_

#include <string>

#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/timer.h"
#include "net/base/completion_callback.h"
#include "net/base/net_export.h"

class FilePath;

namespace base {
class SequencedTaskRunner;
class SequencedWorkerPool;
}

namespace net {

class HttpCache;
class HttpResponseInfo;
class InfiniteCache;
struct HttpRequestInfo;

// An InfiniteCacheTransaction is paired with an HttpCache::Transaction to track
// every request that goes through the HttpCache. This object is notified when
// relevant steps are reached while processing the request.
class NET_EXPORT_PRIVATE InfiniteCacheTransaction {
 public:
  explicit InfiniteCacheTransaction(InfiniteCache* cache);
  ~InfiniteCacheTransaction();

  // Called when a new HttpTransaction is started.
  void OnRequestStart(const HttpRequestInfo* request);

  // Called when the transaction corresponds to a back navigation.
  void OnBackForwardNavigation();

  // Called when the response headers are available.
  void OnResponseReceived(const HttpResponseInfo* response);

  // Called when the response data is received from the network.
  void OnDataRead(const char* data, int data_len);

  // Called when the resource is marked as truncated by the HttpCache.
  void OnTruncatedResponse();

  // Called when the resource is served from the cache, so OnDataRead will not
  // be called for this request.
  void OnServedFromCache(const HttpResponseInfo* response);

 private:
  friend class InfiniteCache;
  struct ResourceData;
  void Finish();

  base::WeakPtr<InfiniteCache> cache_;
  scoped_ptr<ResourceData> resource_data_;
  DISALLOW_COPY_AND_ASSIGN(InfiniteCacheTransaction);
};

// An InfiniteCache is paired with an HttpCache instance to simulate an infinite
// backend storage.
class NET_EXPORT_PRIVATE InfiniteCache
    : public base::SupportsWeakPtr<InfiniteCache> {
 public:
  InfiniteCache();
  ~InfiniteCache();

  // Initializes this object to start tracking requests. |path| is the location
  // of the file to use to store data; it can be empty, in which case the data
  // will not be persisted to disk.
  void Init(const FilePath& path);

  InfiniteCacheTransaction* CreateInfiniteCacheTransaction();

  // Removes all data for this experiment. Returns a net error code.
  int DeleteData(const CompletionCallback& callback);

  // Removes requests between |initial_time| and |end_time|.
  int DeleteDataBetween(base::Time initial_time,
                        base::Time end_time,
                        const CompletionCallback& callback);

  // Returns the number of elements currently tracked.
  int QueryItemsForTest(const CompletionCallback& callback);

  // Flush the data to disk, preventing flushing when the destructor runs. Any
  // data added to the cache after calling this method will not be written to
  // disk, unless this method is called again to do so.
  int FlushDataForTest(const CompletionCallback& callback);

 private:
  class Worker;
  friend class base::RepeatingTimer<InfiniteCache>;
  friend class InfiniteCacheTransaction;

  std::string GenerateKey(const HttpRequestInfo* request);
  void ProcessResource(scoped_ptr<InfiniteCacheTransaction::ResourceData> data);
  void OnTimer();

  scoped_refptr<Worker> worker_;
  scoped_refptr<base::SequencedWorkerPool> worker_pool_;
  scoped_refptr<base::SequencedTaskRunner> task_runner_;
  base::RepeatingTimer<InfiniteCache> timer_;

  DISALLOW_COPY_AND_ASSIGN(InfiniteCache);
};

}  // namespace net

#endif  // NET_HTTP_INFINITE_CACHE_H_