OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "extensions/browser/file_reader.h" | 5 #include "extensions/browser/file_reader.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" |
8 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
9 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
10 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
11 | 12 |
12 using content::BrowserThread; | 13 using content::BrowserThread; |
13 | 14 |
14 FileReader::FileReader(const extensions::ExtensionResource& resource, | 15 FileReader::FileReader( |
15 const Callback& callback) | 16 const extensions::ExtensionResource& resource, |
| 17 const OptionalFileThreadTaskCallback& optional_file_thread_task_callback, |
| 18 const DoneCallback& done_callback) |
16 : resource_(resource), | 19 : resource_(resource), |
17 callback_(callback), | 20 optional_file_thread_task_callback_(optional_file_thread_task_callback), |
| 21 done_callback_(done_callback), |
18 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) {} | 22 origin_task_runner_(base::ThreadTaskRunnerHandle::Get()) {} |
19 | 23 |
20 void FileReader::Start() { | 24 void FileReader::Start() { |
21 BrowserThread::PostTask( | 25 BrowserThread::PostTask( |
22 BrowserThread::FILE, FROM_HERE, | 26 BrowserThread::FILE, FROM_HERE, |
23 base::Bind(&FileReader::ReadFileOnBackgroundThread, this)); | 27 base::Bind(&FileReader::ReadFileOnBackgroundThread, this)); |
24 } | 28 } |
25 | 29 |
26 FileReader::~FileReader() {} | 30 FileReader::~FileReader() {} |
27 | 31 |
28 void FileReader::ReadFileOnBackgroundThread() { | 32 void FileReader::ReadFileOnBackgroundThread() { |
29 std::unique_ptr<std::string> data(new std::string()); | 33 std::unique_ptr<std::string> data(new std::string()); |
30 bool success = base::ReadFileToString(resource_.GetFilePath(), data.get()); | 34 bool success = base::ReadFileToString(resource_.GetFilePath(), data.get()); |
| 35 |
| 36 if (!optional_file_thread_task_callback_.is_null()) { |
| 37 if (success) { |
| 38 base::ResetAndReturn(&optional_file_thread_task_callback_) |
| 39 .Run(data.get()); |
| 40 } else { |
| 41 optional_file_thread_task_callback_.Reset(); |
| 42 } |
| 43 } |
| 44 |
31 origin_task_runner_->PostTask( | 45 origin_task_runner_->PostTask( |
32 FROM_HERE, base::Bind(callback_, success, base::Passed(std::move(data)))); | 46 FROM_HERE, base::Bind(base::ResetAndReturn(&done_callback_), success, |
| 47 base::Passed(std::move(data)))); |
33 } | 48 } |
OLD | NEW |