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_host.h" | 5 #include "chrome/browser/nacl_host/pnacl_host.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 disk_cache_->InitCache( | 106 disk_cache_->InitCache( |
107 temp_dir, | 107 temp_dir, |
108 true, // Use in-memory backend | 108 true, // Use in-memory backend |
109 base::Bind(&PnaclHost::OnCacheInitialized, weak_factory_.GetWeakPtr())); | 109 base::Bind(&PnaclHost::OnCacheInitialized, weak_factory_.GetWeakPtr())); |
110 } | 110 } |
111 | 111 |
112 ///////////////////////////////////////// Temp files | 112 ///////////////////////////////////////// Temp files |
113 | 113 |
114 // Create a temporary file on the blocking pool | 114 // Create a temporary file on the blocking pool |
115 // static | 115 // static |
116 base::PlatformFile PnaclHost::DoCreateTemporaryFile(base::FilePath temp_dir) { | 116 void PnaclHost::DoCreateTemporaryFile(base::FilePath temp_dir, |
| 117 TempFileCallback cb) { |
117 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 118 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
118 | 119 |
119 base::FilePath file_path; | 120 base::FilePath file_path; |
| 121 base::PlatformFile file_handle(base::kInvalidPlatformFileValue); |
120 bool rv = temp_dir.empty() | 122 bool rv = temp_dir.empty() |
121 ? file_util::CreateTemporaryFile(&file_path) | 123 ? file_util::CreateTemporaryFile(&file_path) |
122 : file_util::CreateTemporaryFileInDir(temp_dir, &file_path); | 124 : file_util::CreateTemporaryFileInDir(temp_dir, &file_path); |
123 if (!rv) { | 125 if (!rv) { |
124 PLOG(ERROR) << "Temp file creation failed."; | 126 PLOG(ERROR) << "Temp file creation failed."; |
125 return base::kInvalidPlatformFileValue; | 127 } else { |
| 128 base::PlatformFileError error; |
| 129 file_handle = base::CreatePlatformFile( |
| 130 file_path, |
| 131 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_READ | |
| 132 base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_TEMPORARY | |
| 133 base::PLATFORM_FILE_DELETE_ON_CLOSE, |
| 134 NULL, |
| 135 &error); |
| 136 |
| 137 if (error != base::PLATFORM_FILE_OK) { |
| 138 PLOG(ERROR) << "Temp file open failed: " << error; |
| 139 file_handle = base::kInvalidPlatformFileValue; |
| 140 } |
126 } | 141 } |
127 base::PlatformFileError error; | 142 BrowserThread::PostTask( |
128 base::PlatformFile file_handle(base::CreatePlatformFile( | 143 BrowserThread::IO, FROM_HERE, base::Bind(cb, file_handle)); |
129 file_path, | |
130 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_READ | | |
131 base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_TEMPORARY | | |
132 base::PLATFORM_FILE_DELETE_ON_CLOSE, | |
133 NULL, | |
134 &error)); | |
135 | |
136 if (error != base::PLATFORM_FILE_OK) { | |
137 PLOG(ERROR) << "Temp file open failed: " << error; | |
138 return base::kInvalidPlatformFileValue; | |
139 } | |
140 | |
141 return file_handle; | |
142 } | 144 } |
143 | 145 |
144 void PnaclHost::CreateTemporaryFile(TempFileCallback cb) { | 146 void PnaclHost::CreateTemporaryFile(TempFileCallback cb) { |
145 if (!base::PostTaskAndReplyWithResult( | 147 if (!BrowserThread::PostBlockingPoolSequencedTask( |
146 BrowserThread::GetBlockingPool(), | 148 "PnaclHostCreateTempFile", |
147 FROM_HERE, | 149 FROM_HERE, |
148 base::Bind(&PnaclHost::DoCreateTemporaryFile, temp_dir_), | 150 base::Bind(&PnaclHost::DoCreateTemporaryFile, temp_dir_, cb))) { |
149 cb)) { | |
150 DCHECK(thread_checker_.CalledOnValidThread()); | 151 DCHECK(thread_checker_.CalledOnValidThread()); |
151 cb.Run(base::kInvalidPlatformFileValue); | 152 cb.Run(base::kInvalidPlatformFileValue); |
152 } | 153 } |
153 } | 154 } |
154 | 155 |
155 ///////////////////////////////////////// GetNexeFd implementation | 156 ///////////////////////////////////////// GetNexeFd implementation |
156 ////////////////////// Common steps | 157 ////////////////////// Common steps |
157 | 158 |
158 void PnaclHost::GetNexeFd(int render_process_id, | 159 void PnaclHost::GetNexeFd(int render_process_id, |
159 int render_view_id, | 160 int render_view_id, |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 it->second.got_cache_reply && | 305 it->second.got_cache_reply && |
305 it->second.got_nexe_fd) { | 306 it->second.got_nexe_fd) { |
306 return; | 307 return; |
307 } | 308 } |
308 } | 309 } |
309 ReturnMiss(entry); | 310 ReturnMiss(entry); |
310 return; | 311 return; |
311 } | 312 } |
312 | 313 |
313 if (!base::PostTaskAndReplyWithResult( | 314 if (!base::PostTaskAndReplyWithResult( |
314 BrowserThread::GetBlockingPool(), | 315 BrowserThread::GetBlockingPool(), |
315 FROM_HERE, | 316 FROM_HERE, |
316 base::Bind( | 317 base::Bind( |
317 &PnaclHost::CopyBufferToFile, pt->nexe_fd, pt->nexe_read_buffer), | 318 &PnaclHost::CopyBufferToFile, pt->nexe_fd, pt->nexe_read_buffer), |
318 base::Bind(&PnaclHost::OnBufferCopiedToTempFile, | 319 base::Bind(&PnaclHost::OnBufferCopiedToTempFile, |
319 weak_factory_.GetWeakPtr(), | 320 weak_factory_.GetWeakPtr(), |
320 entry->first))) { | 321 entry->first))) { |
321 pt->callback.Run(base::kInvalidPlatformFileValue, false); | 322 pt->callback.Run(base::kInvalidPlatformFileValue, false); |
322 } | 323 } |
323 } | 324 } |
324 | 325 |
325 //////////////////// GetNexeFd miss path | 326 //////////////////// GetNexeFd miss path |
326 // Return the temp fd to the renderer, reporting a miss. | 327 // Return the temp fd to the renderer, reporting a miss. |
327 void PnaclHost::ReturnMiss(const PendingTranslationMap::iterator& entry) { | 328 void PnaclHost::ReturnMiss(const PendingTranslationMap::iterator& entry) { |
328 // Return the fd | 329 // Return the fd |
329 PendingTranslation* pt = &entry->second; | 330 PendingTranslation* pt = &entry->second; |
330 NexeFdCallback cb(pt->callback); | 331 NexeFdCallback cb(pt->callback); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 bool store_nexe = true; | 380 bool store_nexe = true; |
380 // If this is a premature response (i.e. we haven't returned a temp file | 381 // If this is a premature response (i.e. we haven't returned a temp file |
381 // yet) or if it's an unsuccessful translation, or if we are incognito, | 382 // yet) or if it's an unsuccessful translation, or if we are incognito, |
382 // don't store in the cache. | 383 // don't store in the cache. |
383 // TODO(dschuff): use a separate in-memory cache for incognito | 384 // TODO(dschuff): use a separate in-memory cache for incognito |
384 // translations. | 385 // translations. |
385 if (!entry->second.got_nexe_fd || !entry->second.got_cache_reply || | 386 if (!entry->second.got_nexe_fd || !entry->second.got_cache_reply || |
386 !success || entry->second.is_incognito) { | 387 !success || entry->second.is_incognito) { |
387 store_nexe = false; | 388 store_nexe = false; |
388 } else if (!base::PostTaskAndReplyWithResult( | 389 } else if (!base::PostTaskAndReplyWithResult( |
389 BrowserThread::GetBlockingPool(), | 390 BrowserThread::GetBlockingPool(), |
390 FROM_HERE, | 391 FROM_HERE, |
391 base::Bind(&PnaclHost::CopyFileToBuffer, | 392 base::Bind(&PnaclHost::CopyFileToBuffer, |
392 entry->second.nexe_fd), | 393 entry->second.nexe_fd), |
393 base::Bind(&PnaclHost::StoreTranslatedNexe, | 394 base::Bind(&PnaclHost::StoreTranslatedNexe, |
394 weak_factory_.GetWeakPtr(), | 395 weak_factory_.GetWeakPtr(), |
395 id))) { | 396 id))) { |
396 store_nexe = false; | 397 store_nexe = false; |
397 } | 398 } |
398 | 399 |
399 if (!store_nexe) { | 400 if (!store_nexe) { |
400 // If store_nexe is true, the fd will be closed by CopyFileToBuffer. | 401 // If store_nexe is true, the fd will be closed by CopyFileToBuffer. |
401 if (entry->second.got_nexe_fd) { | 402 if (entry->second.got_nexe_fd) { |
402 BrowserThread::PostBlockingPoolTask( | 403 BrowserThread::PostBlockingPoolTask( |
403 FROM_HERE, | 404 FROM_HERE, |
404 base::Bind(base::IgnoreResult(base::ClosePlatformFile), | 405 base::Bind(base::IgnoreResult(base::ClosePlatformFile), |
405 entry->second.nexe_fd)); | 406 entry->second.nexe_fd)); |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
567 end_time, | 568 end_time, |
568 base::Bind(&PnaclHost::OnEntriesDoomed, callback)); | 569 base::Bind(&PnaclHost::OnEntriesDoomed, callback)); |
569 if (rv != net::ERR_IO_PENDING) | 570 if (rv != net::ERR_IO_PENDING) |
570 OnEntriesDoomed(callback, rv); | 571 OnEntriesDoomed(callback, rv); |
571 } | 572 } |
572 | 573 |
573 // static | 574 // static |
574 void PnaclHost::OnEntriesDoomed(const base::Closure& callback, int net_error) { | 575 void PnaclHost::OnEntriesDoomed(const base::Closure& callback, int net_error) { |
575 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, callback); | 576 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, callback); |
576 } | 577 } |
OLD | NEW |