OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "chrome/browser/nacl_host/pnacl_translation_cache.h" | 5 #include "chrome/browser/nacl_host/pnacl_translation_cache.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
| 9 #include "base/callback.h" |
9 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
10 #include "base/logging.h" | 11 #include "base/logging.h" |
11 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
12 #include "base/threading/thread_checker.h" | 13 #include "base/threading/thread_checker.h" |
13 #include "components/nacl/common/pnacl_types.h" | 14 #include "components/nacl/common/pnacl_types.h" |
14 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
15 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
16 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
17 #include "net/disk_cache/disk_cache.h" | 18 #include "net/disk_cache/disk_cache.h" |
18 | 19 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 // --> Create -- | 61 // --> Create -- |
61 // Reads: | 62 // Reads: |
62 // Start -> Open --------Read ----> Close | 63 // Start -> Open --------Read ----> Close |
63 // | ^ | 64 // | ^ |
64 // |__| | 65 // |__| |
65 enum CacheStep { | 66 enum CacheStep { |
66 UNINITIALIZED, | 67 UNINITIALIZED, |
67 OPEN_ENTRY, | 68 OPEN_ENTRY, |
68 CREATE_ENTRY, | 69 CREATE_ENTRY, |
69 TRANSFER_ENTRY, | 70 TRANSFER_ENTRY, |
70 CLOSE_ENTRY | 71 CLOSE_ENTRY, |
| 72 FINISHED |
71 }; | 73 }; |
72 | 74 |
73 private: | 75 private: |
74 friend class base::RefCounted<PnaclTranslationCacheEntry>; | 76 friend class base::RefCounted<PnaclTranslationCacheEntry>; |
75 PnaclTranslationCacheEntry(base::WeakPtr<PnaclTranslationCache> cache, | 77 PnaclTranslationCacheEntry(base::WeakPtr<PnaclTranslationCache> cache, |
76 const std::string& key, | 78 const std::string& key, |
77 bool is_read); | 79 bool is_read); |
78 ~PnaclTranslationCacheEntry(); | 80 ~PnaclTranslationCacheEntry(); |
79 | 81 |
80 // Try to open an existing entry in the backend | 82 // Try to open an existing entry in the backend |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 const std::string& key, | 137 const std::string& key, |
136 bool is_read) | 138 bool is_read) |
137 : cache_(cache), | 139 : cache_(cache), |
138 key_(key), | 140 key_(key), |
139 entry_(NULL), | 141 entry_(NULL), |
140 step_(UNINITIALIZED), | 142 step_(UNINITIALIZED), |
141 is_read_(is_read) {} | 143 is_read_(is_read) {} |
142 | 144 |
143 PnaclTranslationCacheEntry::~PnaclTranslationCacheEntry() { | 145 PnaclTranslationCacheEntry::~PnaclTranslationCacheEntry() { |
144 // Ensure we have called the user's callback | 146 // Ensure we have called the user's callback |
145 DCHECK(read_callback_.is_null()); | 147 if (step_ != FINISHED) { |
146 DCHECK(write_callback_.is_null()); | 148 if (!read_callback_.is_null()) { |
| 149 BrowserThread::PostTask( |
| 150 BrowserThread::IO, |
| 151 FROM_HERE, |
| 152 base::Bind(read_callback_, |
| 153 net::ERR_ABORTED, |
| 154 scoped_refptr<net::DrainableIOBuffer>())); |
| 155 } |
| 156 if (!write_callback_.is_null()) { |
| 157 BrowserThread::PostTask(BrowserThread::IO, |
| 158 FROM_HERE, |
| 159 base::Bind(write_callback_, net::ERR_ABORTED)); |
| 160 } |
| 161 } |
147 } | 162 } |
148 | 163 |
149 void PnaclTranslationCacheEntry::Start() { | 164 void PnaclTranslationCacheEntry::Start() { |
150 DCHECK(thread_checker_.CalledOnValidThread()); | 165 DCHECK(thread_checker_.CalledOnValidThread()); |
151 step_ = OPEN_ENTRY; | 166 step_ = OPEN_ENTRY; |
152 OpenEntry(); | 167 OpenEntry(); |
153 } | 168 } |
154 | 169 |
155 // OpenEntry, CreateEntry, WriteEntry, ReadEntry and CloseEntry are only called | 170 // OpenEntry, CreateEntry, WriteEntry, ReadEntry and CloseEntry are only called |
156 // from DispatchNext, so they know that cache_ is still valid. | 171 // from DispatchNext, so they know that cache_ is still valid. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 if (rv < 0) { | 216 if (rv < 0) { |
202 LOG(ERROR) << "Failed to close entry: " << net::ErrorToString(rv); | 217 LOG(ERROR) << "Failed to close entry: " << net::ErrorToString(rv); |
203 entry_->Doom(); | 218 entry_->Doom(); |
204 } | 219 } |
205 BrowserThread::PostTask( | 220 BrowserThread::PostTask( |
206 BrowserThread::IO, FROM_HERE, base::Bind(&CloseDiskCacheEntry, entry_)); | 221 BrowserThread::IO, FROM_HERE, base::Bind(&CloseDiskCacheEntry, entry_)); |
207 Finish(rv); | 222 Finish(rv); |
208 } | 223 } |
209 | 224 |
210 void PnaclTranslationCacheEntry::Finish(int rv) { | 225 void PnaclTranslationCacheEntry::Finish(int rv) { |
| 226 step_ = FINISHED; |
211 if (is_read_) { | 227 if (is_read_) { |
212 if (!read_callback_.is_null()) { | 228 if (!read_callback_.is_null()) { |
213 read_callback_.Run(rv, io_buf_); | 229 BrowserThread::PostTask(BrowserThread::IO, |
214 read_callback_.Reset(); | 230 FROM_HERE, |
| 231 base::Bind(read_callback_, rv, io_buf_)); |
215 } | 232 } |
216 } else { | 233 } else { |
217 if (!write_callback_.is_null()) { | 234 if (!write_callback_.is_null()) { |
218 write_callback_.Run(rv); | 235 BrowserThread::PostTask( |
219 write_callback_.Reset(); | 236 BrowserThread::IO, FROM_HERE, base::Bind(write_callback_, rv)); |
220 } | 237 } |
221 } | 238 } |
222 cache_->OpComplete(this); | 239 cache_->OpComplete(this); |
223 } | 240 } |
224 | 241 |
225 void PnaclTranslationCacheEntry::DispatchNext(int rv) { | 242 void PnaclTranslationCacheEntry::DispatchNext(int rv) { |
226 DCHECK(thread_checker_.CalledOnValidThread()); | 243 DCHECK(thread_checker_.CalledOnValidThread()); |
227 if (!cache_) | 244 if (!cache_) |
228 return; | 245 return; |
229 | 246 |
230 switch (step_) { | 247 switch (step_) { |
231 case UNINITIALIZED: | 248 case UNINITIALIZED: |
| 249 case FINISHED: |
232 LOG(ERROR) << "DispatchNext called uninitialized"; | 250 LOG(ERROR) << "DispatchNext called uninitialized"; |
233 break; | 251 break; |
234 | 252 |
235 case OPEN_ENTRY: | 253 case OPEN_ENTRY: |
236 if (rv == net::OK) { | 254 if (rv == net::OK) { |
237 step_ = TRANSFER_ENTRY; | 255 step_ = TRANSFER_ENTRY; |
238 if (is_read_) { | 256 if (is_read_) { |
239 int bytes_to_transfer = entry_->GetDataSize(1); | 257 int bytes_to_transfer = entry_->GetDataSize(1); |
240 io_buf_ = new net::DrainableIOBuffer( | 258 io_buf_ = new net::DrainableIOBuffer( |
241 new net::IOBuffer(bytes_to_transfer), bytes_to_transfer); | 259 new net::IOBuffer(bytes_to_transfer), bytes_to_transfer); |
(...skipping 24 matching lines...) Expand all Loading... |
266 } else { | 284 } else { |
267 LOG(ERROR) << "Failed to Create Entry: " << net::ErrorToString(rv); | 285 LOG(ERROR) << "Failed to Create Entry: " << net::ErrorToString(rv); |
268 Finish(rv); | 286 Finish(rv); |
269 } | 287 } |
270 break; | 288 break; |
271 | 289 |
272 case TRANSFER_ENTRY: | 290 case TRANSFER_ENTRY: |
273 if (rv < 0) { | 291 if (rv < 0) { |
274 // We do not call DispatchNext directly if WriteEntry/ReadEntry returns | 292 // We do not call DispatchNext directly if WriteEntry/ReadEntry returns |
275 // ERR_IO_PENDING, and the callback should not return that value either. | 293 // ERR_IO_PENDING, and the callback should not return that value either. |
276 LOG(ERROR) | 294 LOG(ERROR) << "Failed to complete write to entry: " |
277 << "Failed to complete write to entry: " << net::ErrorToString(rv); | 295 << net::ErrorToString(rv); |
278 step_ = CLOSE_ENTRY; | 296 step_ = CLOSE_ENTRY; |
279 CloseEntry(rv); | 297 CloseEntry(rv); |
280 break; | 298 break; |
281 } else if (rv > 0) { | 299 } else if (rv > 0) { |
282 io_buf_->DidConsume(rv); | 300 io_buf_->DidConsume(rv); |
283 if (io_buf_->BytesRemaining() > 0) { | 301 if (io_buf_->BytesRemaining() > 0) { |
284 is_read_ | 302 is_read_ |
285 ? ReadEntry(io_buf_->BytesConsumed(), io_buf_->BytesRemaining()) | 303 ? ReadEntry(io_buf_->BytesConsumed(), io_buf_->BytesRemaining()) |
286 : WriteEntry(io_buf_->BytesConsumed(), io_buf_->BytesRemaining()); | 304 : WriteEntry(io_buf_->BytesConsumed(), io_buf_->BytesRemaining()); |
287 break; | 305 break; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 int rv = disk_cache::CreateCacheBackend( | 337 int rv = disk_cache::CreateCacheBackend( |
320 cache_type, | 338 cache_type, |
321 net::CACHE_BACKEND_DEFAULT, | 339 net::CACHE_BACKEND_DEFAULT, |
322 cache_dir, | 340 cache_dir, |
323 cache_size, | 341 cache_size, |
324 true /* force_initialize */, | 342 true /* force_initialize */, |
325 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE).get(), | 343 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE).get(), |
326 NULL, /* dummy net log */ | 344 NULL, /* dummy net log */ |
327 &disk_cache_, | 345 &disk_cache_, |
328 base::Bind(&PnaclTranslationCache::OnCreateBackendComplete, AsWeakPtr())); | 346 base::Bind(&PnaclTranslationCache::OnCreateBackendComplete, AsWeakPtr())); |
329 init_callback_ = callback; | 347 if (rv == net::ERR_IO_PENDING) { |
330 if (rv != net::ERR_IO_PENDING) { | 348 init_callback_ = callback; |
331 OnCreateBackendComplete(rv); | |
332 } | 349 } |
333 return rv; | 350 return rv; |
334 } | 351 } |
335 | 352 |
336 void PnaclTranslationCache::OnCreateBackendComplete(int rv) { | 353 void PnaclTranslationCache::OnCreateBackendComplete(int rv) { |
337 if (rv < 0) { | 354 if (rv < 0) { |
338 LOG(ERROR) << "Backend init failed:" << net::ErrorToString(rv); | 355 LOG(ERROR) << "Backend init failed:" << net::ErrorToString(rv); |
339 } | 356 } |
340 // Invoke our client's callback function. | 357 // Invoke our client's callback function. |
341 if (!init_callback_.is_null()) { | 358 if (!init_callback_.is_null()) { |
342 init_callback_.Run(rv); | 359 BrowserThread::PostTask( |
343 init_callback_.Reset(); | 360 BrowserThread::IO, FROM_HERE, base::Bind(init_callback_, rv)); |
344 } | 361 } |
345 } | 362 } |
346 | 363 |
347 ////////////////////////////////////////////////////////////////////// | 364 ////////////////////////////////////////////////////////////////////// |
348 // High-level API | 365 // High-level API |
349 | 366 |
350 void PnaclTranslationCache::StoreNexe(const std::string& key, | 367 void PnaclTranslationCache::StoreNexe(const std::string& key, |
351 net::DrainableIOBuffer* nexe_data) { | |
352 StoreNexe(key, nexe_data, CompletionCallback()); | |
353 } | |
354 | |
355 void PnaclTranslationCache::StoreNexe(const std::string& key, | |
356 net::DrainableIOBuffer* nexe_data, | 368 net::DrainableIOBuffer* nexe_data, |
357 const CompletionCallback& callback) { | 369 const CompletionCallback& callback) { |
358 PnaclTranslationCacheEntry* entry = PnaclTranslationCacheEntry::GetWriteEntry( | 370 PnaclTranslationCacheEntry* entry = PnaclTranslationCacheEntry::GetWriteEntry( |
359 AsWeakPtr(), key, nexe_data, callback); | 371 AsWeakPtr(), key, nexe_data, callback); |
360 open_entries_[entry] = entry; | 372 open_entries_[entry] = entry; |
361 entry->Start(); | 373 entry->Start(); |
362 } | 374 } |
363 | 375 |
364 void PnaclTranslationCache::GetNexe(const std::string& key, | 376 void PnaclTranslationCache::GetNexe(const std::string& key, |
365 const GetNexeCallback& callback) { | 377 const GetNexeCallback& callback) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 } | 431 } |
420 | 432 |
421 int PnaclTranslationCache::DoomEntriesBetween( | 433 int PnaclTranslationCache::DoomEntriesBetween( |
422 base::Time initial, | 434 base::Time initial, |
423 base::Time end, | 435 base::Time end, |
424 const CompletionCallback& callback) { | 436 const CompletionCallback& callback) { |
425 return disk_cache_->DoomEntriesBetween(initial, end, callback); | 437 return disk_cache_->DoomEntriesBetween(initial, end, callback); |
426 } | 438 } |
427 | 439 |
428 } // namespace pnacl | 440 } // namespace pnacl |
OLD | NEW |