OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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 #include "net/disk_cache/tracing_cache_backend.h" | |
6 | |
7 #include "net/base/net_errors.h" | |
8 | |
9 namespace disk_cache { | |
10 | |
11 // Proxies entry objects created by the real underlying backend. Backend users | |
12 // will only see the proxy entries. It is necessary for recording the backend | |
13 // operations since often non-trivial work is invoked directly on entries. | |
14 class EntryProxy : public Entry, public base::RefCountedThreadSafe<EntryProxy> { | |
15 public: | |
16 EntryProxy(Entry *entry, TracingCacheBackend* backend); | |
17 virtual void Doom() OVERRIDE; | |
18 virtual void Close() OVERRIDE; | |
19 virtual std::string GetKey() const OVERRIDE; | |
20 virtual base::Time GetLastUsed() const OVERRIDE; | |
21 virtual base::Time GetLastModified() const OVERRIDE; | |
22 virtual int32 GetDataSize(int index) const OVERRIDE; | |
23 virtual int ReadData(int index, int offset, IOBuffer* buf, int buf_len, | |
24 const CompletionCallback& callback) OVERRIDE; | |
25 virtual int WriteData(int index, int offset, IOBuffer* buf, int buf_len, | |
26 const CompletionCallback& callback, | |
27 bool truncate) OVERRIDE; | |
28 virtual int ReadSparseData(int64 offset, IOBuffer* buf, int buf_len, | |
29 const CompletionCallback& callback) OVERRIDE; | |
30 virtual int WriteSparseData(int64 offset, IOBuffer* buf, int buf_len, | |
31 const CompletionCallback& callback) OVERRIDE; | |
32 virtual int GetAvailableRange(int64 offset, int len, int64* start, | |
33 const CompletionCallback& callback) OVERRIDE; | |
34 virtual bool CouldBeSparse() const OVERRIDE; | |
35 virtual void CancelSparseIO() OVERRIDE; | |
36 virtual int ReadyForSparseIO(const CompletionCallback& callback) OVERRIDE; | |
37 | |
38 private: | |
39 friend class base::RefCountedThreadSafe<EntryProxy>; | |
40 typedef TracingCacheBackend::Operation Operation; | |
rvargas (doing something else)
2013/04/09 18:54:01
do you need this?
pasko-google - do not use
2013/04/10 17:06:38
Makes referencing some parameter types a little sh
| |
41 virtual ~EntryProxy(); | |
42 | |
43 struct RwOpExtra { | |
44 int index; | |
45 int offset; | |
46 int buf_len; | |
47 bool truncate; | |
48 }; | |
49 | |
50 void RecordEvent(int64 start_time, Operation op, RwOpExtra extra, | |
51 int rv); | |
52 void EntryOpComplete(int64 start_time, Operation op, | |
53 RwOpExtra extra, const CompletionCallback& cb, int rv); | |
rvargas (doing something else)
2013/04/09 18:54:01
nit: if it's the same to you, invert the order of
pasko-google - do not use
2013/04/10 17:06:38
I am confused, this function gets curried down to
| |
54 Entry* entry_; | |
55 base::WeakPtr<TracingCacheBackend> backend_; | |
56 | |
57 DISALLOW_COPY_AND_ASSIGN(EntryProxy); | |
58 }; | |
59 | |
60 EntryProxy::EntryProxy(Entry *entry, TracingCacheBackend* backend) | |
61 : entry_(entry), | |
62 backend_(backend->AsWeakPtr()) { | |
63 } | |
64 | |
65 void EntryProxy::Doom() { | |
66 // TODO(pasko): Record the event. | |
67 entry_->Doom(); | |
68 } | |
69 | |
70 void EntryProxy::Close() { | |
71 // TODO(pasko): Record the event. | |
72 entry_->Close(); | |
73 Release(); | |
74 } | |
75 | |
76 std::string EntryProxy::GetKey() const { | |
77 return entry_->GetKey(); | |
78 } | |
79 | |
80 base::Time EntryProxy::GetLastUsed() const { | |
81 return entry_->GetLastUsed(); | |
82 } | |
83 | |
84 base::Time EntryProxy::GetLastModified() const { | |
85 return entry_->GetLastModified(); | |
86 } | |
87 | |
88 int32 EntryProxy::GetDataSize(int index) const { | |
89 return entry_->GetDataSize(index); | |
90 } | |
91 | |
92 int EntryProxy::ReadData(int index, int offset, IOBuffer* buf, int buf_len, | |
93 const CompletionCallback& callback) { | |
94 int64 start_time = base::TimeTicks::Now().ToInternalValue(); | |
95 RwOpExtra extra; | |
96 extra.index = index; | |
97 extra.offset = offset; | |
98 extra.buf_len = buf_len; | |
99 extra.truncate = false; | |
100 int rv = entry_->ReadData( | |
101 index, offset, buf, buf_len, | |
102 base::Bind(&EntryProxy::EntryOpComplete, this, start_time, | |
103 TracingCacheBackend::OP_READ, extra, callback)); | |
104 if (rv != net::ERR_IO_PENDING) { | |
105 RecordEvent(start_time, TracingCacheBackend::OP_READ, extra, rv); | |
106 } | |
107 return rv; | |
108 } | |
109 | |
110 int EntryProxy::WriteData(int index, int offset, IOBuffer* buf, int buf_len, | |
111 const CompletionCallback& callback, | |
112 bool truncate) { | |
113 int64 start_time = base::TimeTicks::Now().ToInternalValue(); | |
114 RwOpExtra extra; | |
115 extra.index = index; | |
116 extra.offset = offset; | |
117 extra.buf_len = buf_len; | |
118 extra.truncate = truncate; | |
119 int rv = entry_->WriteData(index, offset, buf, buf_len, | |
120 base::Bind(&EntryProxy::EntryOpComplete, this, start_time, | |
121 TracingCacheBackend::OP_WRITE, extra, callback), | |
122 truncate); | |
123 if (rv != net::ERR_IO_PENDING) { | |
124 RecordEvent(start_time, TracingCacheBackend::OP_WRITE, extra, rv); | |
125 } | |
126 return rv; | |
127 } | |
128 | |
129 int EntryProxy::ReadSparseData(int64 offset, IOBuffer* buf, int buf_len, | |
130 const CompletionCallback& callback) { | |
131 // TODO(pasko): Record the event. | |
132 return entry_->ReadSparseData(offset, buf, buf_len, callback); | |
133 } | |
134 | |
135 int EntryProxy::WriteSparseData(int64 offset, IOBuffer* buf, int buf_len, | |
136 const CompletionCallback& callback) { | |
137 // TODO(pasko): Record the event. | |
138 return entry_->WriteSparseData(offset, buf, buf_len, callback); | |
139 } | |
140 | |
141 int EntryProxy::GetAvailableRange(int64 offset, int len, int64* start, | |
142 const CompletionCallback& callback) { | |
143 return entry_->GetAvailableRange(offset, len, start, callback); | |
144 } | |
145 | |
146 bool EntryProxy::CouldBeSparse() const { | |
147 return entry_->CouldBeSparse(); | |
148 } | |
149 | |
150 void EntryProxy::CancelSparseIO() { | |
151 return entry_->CancelSparseIO(); | |
152 } | |
153 | |
154 int EntryProxy::ReadyForSparseIO(const CompletionCallback& callback) { | |
155 return entry_->ReadyForSparseIO(callback); | |
156 } | |
157 | |
158 void EntryProxy::RecordEvent(int64 start_time, Operation op, RwOpExtra extra, | |
159 int rv) { | |
160 // TODO(pasko): Implement. | |
161 } | |
162 | |
163 void EntryProxy::EntryOpComplete(int64 start_time, Operation op, | |
164 RwOpExtra extra, const CompletionCallback& cb, | |
165 int rv) { | |
166 RecordEvent(start_time, op, extra, rv); | |
167 if (!cb.is_null()) { | |
168 cb.Run(rv); | |
169 } | |
170 } | |
171 | |
172 EntryProxy::~EntryProxy() { | |
173 if (backend_.get()) { | |
174 backend_->OnDeleteEntry(entry_); | |
175 } | |
176 } | |
177 | |
178 TracingCacheBackend::TracingCacheBackend(Backend* backend) | |
179 : backend_(backend) { | |
180 } | |
181 | |
182 TracingCacheBackend::~TracingCacheBackend() { | |
183 } | |
184 | |
185 net::CacheType TracingCacheBackend::GetCacheType() const { | |
186 return backend_->GetCacheType(); | |
187 } | |
188 | |
189 int32 TracingCacheBackend::GetEntryCount() const { | |
190 return backend_->GetEntryCount(); | |
191 } | |
192 | |
193 void TracingCacheBackend::RecordEvent(int64 start_time, Operation op, | |
194 std::string key, Entry* entry, int rv) { | |
195 // TODO(pasko): Implement. | |
196 } | |
197 | |
198 EntryProxy* TracingCacheBackend::FindOrCreateEntryProxy(Entry* entry) { | |
199 EntryProxy* entry_proxy; | |
200 EntryToProxyMap::iterator it = open_entries_.find(entry); | |
201 if (it != open_entries_.end()) { | |
202 entry_proxy = it->second; | |
203 entry_proxy->AddRef(); | |
204 return entry_proxy; | |
205 } | |
206 entry_proxy = new EntryProxy(entry, this); | |
207 entry_proxy->AddRef(); | |
208 open_entries_[entry] = entry_proxy; | |
209 return entry_proxy; | |
210 } | |
211 | |
212 void TracingCacheBackend::OnDeleteEntry(Entry* entry) { | |
213 EntryToProxyMap::iterator it = open_entries_.find(entry); | |
214 if (it != open_entries_.end()) { | |
215 open_entries_.erase(it); | |
216 } | |
217 } | |
218 | |
219 void TracingCacheBackend::BackendOpComplete(int64 start_time, Operation op, | |
220 std::string key, Entry** entry, | |
221 const CompletionCallback& callback, | |
222 int rv) { | |
223 RecordEvent(start_time, op, key, *entry, rv); | |
224 if (*entry) { | |
225 *entry = FindOrCreateEntryProxy(*entry); | |
226 } | |
227 if (!callback.is_null()) { | |
228 callback.Run(rv); | |
229 } | |
230 } | |
231 | |
232 net::CompletionCallback TracingCacheBackend::BindCompletion( | |
233 Operation op, int64 start_time, const std::string& key, | |
234 Entry **entry, const net::CompletionCallback& cb) { | |
235 return base::Bind(&TracingCacheBackend::BackendOpComplete, | |
236 AsWeakPtr(), start_time, op, key, entry, cb); | |
237 } | |
238 | |
239 int TracingCacheBackend::OpenEntry(const std::string& key, Entry** entry, | |
240 const CompletionCallback& callback) { | |
241 DCHECK(*entry == NULL); | |
242 int64 start_time = base::TimeTicks::Now().ToInternalValue(); | |
243 int rv = backend_->OpenEntry(key, entry, | |
244 BindCompletion(OP_OPEN, start_time, key, entry, | |
245 callback)); | |
246 if (rv != net::ERR_IO_PENDING) { | |
247 RecordEvent(start_time, OP_OPEN, key, *entry, rv); | |
248 if (*entry) { | |
249 *entry = FindOrCreateEntryProxy(*entry); | |
250 } | |
251 } | |
252 return rv; | |
253 } | |
254 | |
255 int TracingCacheBackend::CreateEntry(const std::string& key, Entry** entry, | |
256 const CompletionCallback& callback) { | |
257 int64 start_time = base::TimeTicks::Now().ToInternalValue(); | |
258 int rv = backend_->CreateEntry( | |
259 key, entry, BindCompletion(OP_CREATE, start_time, key, entry, | |
rvargas (doing something else)
2013/04/09 18:54:01
nit: first two args on the previous line (as OpenE
pasko-google - do not use
2013/04/10 17:06:38
Done.
| |
260 callback)); | |
261 if (rv != net::ERR_IO_PENDING) { | |
262 RecordEvent(start_time, OP_CREATE, key, *entry, rv); | |
263 if (*entry) { | |
264 *entry = FindOrCreateEntryProxy(*entry); | |
265 } | |
266 } | |
267 return rv; | |
268 } | |
269 | |
270 int TracingCacheBackend::DoomEntry(const std::string& key, | |
271 const CompletionCallback& callback) { | |
272 int64 start_time = base::TimeTicks::Now().ToInternalValue(); | |
273 int rv = backend_->DoomEntry(key, BindCompletion(OP_DOOM_ENTRY, | |
274 start_time, key, NULL, | |
275 callback)); | |
276 if (rv != net::ERR_IO_PENDING) { | |
277 RecordEvent(start_time, OP_DOOM_ENTRY, key, NULL, rv); | |
278 } | |
279 return rv; | |
280 } | |
281 | |
282 int TracingCacheBackend::DoomAllEntries(const CompletionCallback& callback) { | |
283 return backend_->DoomAllEntries(callback); | |
284 } | |
285 | |
286 int TracingCacheBackend::DoomEntriesBetween(base::Time initial_time, | |
287 base::Time end_time, | |
288 const CompletionCallback& cb) { | |
289 return backend_->DoomEntriesBetween(initial_time, end_time, cb); | |
290 } | |
291 | |
292 int TracingCacheBackend::DoomEntriesSince(base::Time initial_time, | |
293 const CompletionCallback& callback) { | |
294 return backend_->DoomEntriesSince(initial_time, callback); | |
295 } | |
296 | |
297 int TracingCacheBackend::OpenNextEntry(void** iter, Entry** next_entry, | |
298 const CompletionCallback& callback) { | |
299 return backend_->OpenNextEntry(iter, next_entry, callback); | |
300 } | |
301 | |
302 void TracingCacheBackend::EndEnumeration(void** iter) { | |
303 return backend_->EndEnumeration(iter); | |
304 } | |
305 | |
306 void TracingCacheBackend::GetStats(StatsItems* stats) { | |
307 return backend_->GetStats(stats); | |
308 } | |
309 | |
310 void TracingCacheBackend::OnExternalCacheHit(const std::string& key) { | |
311 return backend_->OnExternalCacheHit(key); | |
312 } | |
313 | |
314 } // namespace disk_cache | |
OLD | NEW |