| 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 "components/nacl/browser/pnacl_host.h" | 5 #include "components/nacl/browser/pnacl_host.h" |
| 6 | 6 |
| 7 #include <utility> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 9 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 11 #include "base/logging.h" | 13 #include "base/logging.h" |
| 12 #include "base/numerics/safe_math.h" | 14 #include "base/numerics/safe_math.h" |
| 13 #include "base/task_runner_util.h" | 15 #include "base/task_runner_util.h" |
| 14 #include "base/threading/sequenced_worker_pool.h" | 16 #include "base/threading/sequenced_worker_pool.h" |
| 15 #include "components/nacl/browser/nacl_browser.h" | 17 #include "components/nacl/browser/nacl_browser.h" |
| 16 #include "components/nacl/browser/pnacl_translation_cache.h" | 18 #include "components/nacl/browser/pnacl_translation_cache.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 43 int Write(scoped_refptr<net::DrainableIOBuffer> buffer); | 45 int Write(scoped_refptr<net::DrainableIOBuffer> buffer); |
| 44 void WriteDone(const PnaclHost::TranslationID& id, int result); | 46 void WriteDone(const PnaclHost::TranslationID& id, int result); |
| 45 | 47 |
| 46 private: | 48 private: |
| 47 scoped_ptr<base::File> file_; | 49 scoped_ptr<base::File> file_; |
| 48 base::WeakPtr<pnacl::PnaclHost> host_; | 50 base::WeakPtr<pnacl::PnaclHost> host_; |
| 49 }; | 51 }; |
| 50 | 52 |
| 51 FileProxy::FileProxy(scoped_ptr<base::File> file, | 53 FileProxy::FileProxy(scoped_ptr<base::File> file, |
| 52 base::WeakPtr<pnacl::PnaclHost> host) | 54 base::WeakPtr<pnacl::PnaclHost> host) |
| 53 : file_(file.Pass()), | 55 : file_(std::move(file)), host_(host) {} |
| 54 host_(host) { | |
| 55 } | |
| 56 | 56 |
| 57 int FileProxy::Write(scoped_refptr<net::DrainableIOBuffer> buffer) { | 57 int FileProxy::Write(scoped_refptr<net::DrainableIOBuffer> buffer) { |
| 58 int rv = file_->Write(0, buffer->data(), buffer->size()); | 58 int rv = file_->Write(0, buffer->data(), buffer->size()); |
| 59 if (rv == -1) | 59 if (rv == -1) |
| 60 PLOG(ERROR) << "FileProxy::Write error"; | 60 PLOG(ERROR) << "FileProxy::Write error"; |
| 61 return rv; | 61 return rv; |
| 62 } | 62 } |
| 63 | 63 |
| 64 void FileProxy::WriteDone(const PnaclHost::TranslationID& id, int result) { | 64 void FileProxy::WriteDone(const PnaclHost::TranslationID& id, int result) { |
| 65 if (host_) { | 65 if (host_) { |
| 66 host_->OnBufferCopiedToTempFile(id, file_.Pass(), result); | 66 host_->OnBufferCopiedToTempFile(id, std::move(file_), result); |
| 67 } else { | 67 } else { |
| 68 BrowserThread::PostBlockingPoolTask( | 68 BrowserThread::PostBlockingPoolTask( |
| 69 FROM_HERE, | 69 FROM_HERE, |
| 70 base::Bind(CloseScopedFile, Passed(&file_))); | 70 base::Bind(CloseScopedFile, Passed(&file_))); |
| 71 } | 71 } |
| 72 } | 72 } |
| 73 | 73 |
| 74 PnaclHost::PnaclHost() | 74 PnaclHost::PnaclHost() |
| 75 : pending_backend_operations_(0), | 75 : pending_backend_operations_(0), |
| 76 cache_state_(CacheUninitialized), | 76 cache_state_(CacheUninitialized), |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 } else { | 200 } else { |
| 201 file.Initialize( | 201 file.Initialize( |
| 202 file_path, | 202 file_path, |
| 203 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_READ | | 203 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_READ | |
| 204 base::File::FLAG_WRITE | base::File::FLAG_TEMPORARY | | 204 base::File::FLAG_WRITE | base::File::FLAG_TEMPORARY | |
| 205 base::File::FLAG_DELETE_ON_CLOSE); | 205 base::File::FLAG_DELETE_ON_CLOSE); |
| 206 | 206 |
| 207 if (!file.IsValid()) | 207 if (!file.IsValid()) |
| 208 PLOG(ERROR) << "Temp file open failed: " << file.error_details(); | 208 PLOG(ERROR) << "Temp file open failed: " << file.error_details(); |
| 209 } | 209 } |
| 210 BrowserThread::PostTask( | 210 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 211 BrowserThread::IO, FROM_HERE, base::Bind(cb, Passed(file.Pass()))); | 211 base::Bind(cb, Passed(std::move(file)))); |
| 212 } | 212 } |
| 213 | 213 |
| 214 void PnaclHost::CreateTemporaryFile(TempFileCallback cb) { | 214 void PnaclHost::CreateTemporaryFile(TempFileCallback cb) { |
| 215 if (!BrowserThread::PostBlockingPoolSequencedTask( | 215 if (!BrowserThread::PostBlockingPoolSequencedTask( |
| 216 "PnaclHostCreateTempFile", | 216 "PnaclHostCreateTempFile", |
| 217 FROM_HERE, | 217 FROM_HERE, |
| 218 base::Bind(&PnaclHost::DoCreateTemporaryFile, temp_dir_, cb))) { | 218 base::Bind(&PnaclHost::DoCreateTemporaryFile, temp_dir_, cb))) { |
| 219 DCHECK(thread_checker_.CalledOnValidThread()); | 219 DCHECK(thread_checker_.CalledOnValidThread()); |
| 220 cb.Run(base::File()); | 220 cb.Run(base::File()); |
| 221 } | 221 } |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 // could be cancelled before they get called). | 324 // could be cancelled before they get called). |
| 325 void PnaclHost::OnTempFileReturn(const TranslationID& id, | 325 void PnaclHost::OnTempFileReturn(const TranslationID& id, |
| 326 base::File file) { | 326 base::File file) { |
| 327 DCHECK(thread_checker_.CalledOnValidThread()); | 327 DCHECK(thread_checker_.CalledOnValidThread()); |
| 328 PendingTranslationMap::iterator entry(pending_translations_.find(id)); | 328 PendingTranslationMap::iterator entry(pending_translations_.find(id)); |
| 329 if (entry == pending_translations_.end()) { | 329 if (entry == pending_translations_.end()) { |
| 330 // The renderer may have signaled an error or closed while the temp | 330 // The renderer may have signaled an error or closed while the temp |
| 331 // file was being created. | 331 // file was being created. |
| 332 LOG(ERROR) << "OnTempFileReturn: id not found"; | 332 LOG(ERROR) << "OnTempFileReturn: id not found"; |
| 333 BrowserThread::PostBlockingPoolTask( | 333 BrowserThread::PostBlockingPoolTask( |
| 334 FROM_HERE, base::Bind(CloseBaseFile, Passed(file.Pass()))); | 334 FROM_HERE, base::Bind(CloseBaseFile, Passed(std::move(file)))); |
| 335 return; | 335 return; |
| 336 } | 336 } |
| 337 if (!file.IsValid()) { | 337 if (!file.IsValid()) { |
| 338 // This translation will fail, but we need to retry any translation | 338 // This translation will fail, but we need to retry any translation |
| 339 // waiting for its result. | 339 // waiting for its result. |
| 340 LOG(ERROR) << "OnTempFileReturn: temp file creation failed"; | 340 LOG(ERROR) << "OnTempFileReturn: temp file creation failed"; |
| 341 std::string key(entry->second.cache_key); | 341 std::string key(entry->second.cache_key); |
| 342 entry->second.callback.Run(base::File(), false); | 342 entry->second.callback.Run(base::File(), false); |
| 343 bool may_be_cached = TranslationMayBeCached(entry); | 343 bool may_be_cached = TranslationMayBeCached(entry); |
| 344 pending_translations_.erase(entry); | 344 pending_translations_.erase(entry); |
| 345 // No translations will be waiting for entries that will not be stored. | 345 // No translations will be waiting for entries that will not be stored. |
| 346 if (may_be_cached) | 346 if (may_be_cached) |
| 347 RequeryMatchingTranslations(key); | 347 RequeryMatchingTranslations(key); |
| 348 return; | 348 return; |
| 349 } | 349 } |
| 350 PendingTranslation* pt = &entry->second; | 350 PendingTranslation* pt = &entry->second; |
| 351 pt->got_nexe_fd = true; | 351 pt->got_nexe_fd = true; |
| 352 pt->nexe_fd = new base::File(file.Pass()); | 352 pt->nexe_fd = new base::File(std::move(file)); |
| 353 CheckCacheQueryReady(entry); | 353 CheckCacheQueryReady(entry); |
| 354 } | 354 } |
| 355 | 355 |
| 356 // Check whether both the cache query and the temp file have returned, and check | 356 // Check whether both the cache query and the temp file have returned, and check |
| 357 // whether we actually got a hit or not. | 357 // whether we actually got a hit or not. |
| 358 void PnaclHost::CheckCacheQueryReady( | 358 void PnaclHost::CheckCacheQueryReady( |
| 359 const PendingTranslationMap::iterator& entry) { | 359 const PendingTranslationMap::iterator& entry) { |
| 360 PendingTranslation* pt = &entry->second; | 360 PendingTranslation* pt = &entry->second; |
| 361 if (!(pt->got_cache_reply && pt->got_nexe_fd)) | 361 if (!(pt->got_cache_reply && pt->got_nexe_fd)) |
| 362 return; | 362 return; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 378 return; | 378 return; |
| 379 } | 379 } |
| 380 } | 380 } |
| 381 ReturnMiss(entry); | 381 ReturnMiss(entry); |
| 382 return; | 382 return; |
| 383 } | 383 } |
| 384 | 384 |
| 385 scoped_ptr<base::File> file(pt->nexe_fd); | 385 scoped_ptr<base::File> file(pt->nexe_fd); |
| 386 pt->nexe_fd = NULL; | 386 pt->nexe_fd = NULL; |
| 387 pt->got_nexe_fd = false; | 387 pt->got_nexe_fd = false; |
| 388 FileProxy* proxy(new FileProxy(file.Pass(), weak_factory_.GetWeakPtr())); | 388 FileProxy* proxy(new FileProxy(std::move(file), weak_factory_.GetWeakPtr())); |
| 389 | 389 |
| 390 if (!base::PostTaskAndReplyWithResult( | 390 if (!base::PostTaskAndReplyWithResult( |
| 391 BrowserThread::GetBlockingPool(), | 391 BrowserThread::GetBlockingPool(), |
| 392 FROM_HERE, | 392 FROM_HERE, |
| 393 base::Bind(&FileProxy::Write, base::Unretained(proxy), | 393 base::Bind(&FileProxy::Write, base::Unretained(proxy), |
| 394 pt->nexe_read_buffer), | 394 pt->nexe_read_buffer), |
| 395 base::Bind(&FileProxy::WriteDone, base::Owned(proxy), | 395 base::Bind(&FileProxy::WriteDone, base::Owned(proxy), |
| 396 entry->first))) { | 396 entry->first))) { |
| 397 pt->callback.Run(base::File(), false); | 397 pt->callback.Run(base::File(), false); |
| 398 } | 398 } |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 686 DCHECK(pending_backend_operations_ >= 0); | 686 DCHECK(pending_backend_operations_ >= 0); |
| 687 if (pending_translations_.empty() && | 687 if (pending_translations_.empty() && |
| 688 pending_backend_operations_ <= 0 && | 688 pending_backend_operations_ <= 0 && |
| 689 cache_state_ == CacheReady) { | 689 cache_state_ == CacheReady) { |
| 690 cache_state_ = CacheUninitialized; | 690 cache_state_ = CacheUninitialized; |
| 691 disk_cache_.reset(); | 691 disk_cache_.reset(); |
| 692 } | 692 } |
| 693 } | 693 } |
| 694 | 694 |
| 695 } // namespace pnacl | 695 } // namespace pnacl |
| OLD | NEW |