| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "content/browser/devtools/devtools_io_context.h" | 5 #include "content/browser/devtools/devtools_io_context.h" |
| 6 | 6 |
| 7 #include "base/files/file.h" | 7 #include "base/files/file.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/sequenced_task_runner.h" | 9 #include "base/sequenced_task_runner.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/task_scheduler/post_task.h" | 12 #include "base/task_scheduler/post_task.h" |
| 13 #include "base/third_party/icu/icu_utf.h" | 13 #include "base/third_party/icu/icu_utf.h" |
| 14 #include "base/threading/thread_restrictions.h" | 14 #include "base/threading/thread_restrictions.h" |
| 15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 16 | 16 |
| 17 namespace content { | 17 namespace content { |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 unsigned s_last_stream_handle = 0; | 20 unsigned s_last_stream_handle = 0; |
| 21 } | 21 } |
| 22 | 22 |
| 23 using Stream = DevToolsIOContext::Stream; | 23 DevToolsIOContext::Stream::Stream(base::SequencedTaskRunner* task_runner) |
| 24 | |
| 25 Stream::Stream(base::SequencedTaskRunner* task_runner) | |
| 26 : base::RefCountedDeleteOnSequence<Stream>(task_runner), | 24 : base::RefCountedDeleteOnSequence<Stream>(task_runner), |
| 27 handle_(base::UintToString(++s_last_stream_handle)), | 25 handle_(base::UintToString(++s_last_stream_handle)), |
| 28 task_runner_(task_runner), | 26 task_runner_(task_runner), |
| 29 had_errors_(false), | 27 had_errors_(false), |
| 30 last_read_pos_(0) {} | 28 last_read_pos_(0) {} |
| 31 | 29 |
| 32 Stream::~Stream() { | 30 DevToolsIOContext::Stream::~Stream() { |
| 33 DCHECK(task_runner_->RunsTasksInCurrentSequence()); | 31 DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
| 34 } | 32 } |
| 35 | 33 |
| 36 bool Stream::InitOnFileSequenceIfNeeded() { | 34 bool DevToolsIOContext::Stream::InitOnFileSequenceIfNeeded() { |
| 37 DCHECK(task_runner_->RunsTasksInCurrentSequence()); | 35 DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
| 38 base::ThreadRestrictions::AssertIOAllowed(); | 36 base::ThreadRestrictions::AssertIOAllowed(); |
| 39 if (had_errors_) | 37 if (had_errors_) |
| 40 return false; | 38 return false; |
| 41 if (file_.IsValid()) | 39 if (file_.IsValid()) |
| 42 return true; | 40 return true; |
| 43 base::FilePath temp_path; | 41 base::FilePath temp_path; |
| 44 if (!base::CreateTemporaryFile(&temp_path)) { | 42 if (!base::CreateTemporaryFile(&temp_path)) { |
| 45 LOG(ERROR) << "Failed to create temporary file"; | 43 LOG(ERROR) << "Failed to create temporary file"; |
| 46 had_errors_ = true; | 44 had_errors_ = true; |
| 47 return false; | 45 return false; |
| 48 } | 46 } |
| 49 const unsigned flags = base::File::FLAG_OPEN_TRUNCATED | | 47 const unsigned flags = base::File::FLAG_OPEN_TRUNCATED | |
| 50 base::File::FLAG_WRITE | base::File::FLAG_READ | | 48 base::File::FLAG_WRITE | base::File::FLAG_READ | |
| 51 base::File::FLAG_DELETE_ON_CLOSE; | 49 base::File::FLAG_DELETE_ON_CLOSE; |
| 52 file_.Initialize(temp_path, flags); | 50 file_.Initialize(temp_path, flags); |
| 53 if (!file_.IsValid()) { | 51 if (!file_.IsValid()) { |
| 54 LOG(ERROR) << "Failed to open temporary file: " << temp_path.value() | 52 LOG(ERROR) << "Failed to open temporary file: " << temp_path.value() |
| 55 << ", " << base::File::ErrorToString(file_.error_details()); | 53 << ", " << base::File::ErrorToString(file_.error_details()); |
| 56 had_errors_ = true; | 54 had_errors_ = true; |
| 57 DeleteFile(temp_path, false); | 55 DeleteFile(temp_path, false); |
| 58 return false; | 56 return false; |
| 59 } | 57 } |
| 60 return true; | 58 return true; |
| 61 } | 59 } |
| 62 | 60 |
| 63 void Stream::Read(off_t position, size_t max_size, ReadCallback callback) { | 61 void DevToolsIOContext::Stream::Read(off_t position, size_t max_size, ReadCallba
ck callback) { |
| 64 task_runner_->PostTask( | 62 task_runner_->PostTask( |
| 65 FROM_HERE, base::BindOnce(&Stream::ReadOnFileSequence, this, position, | 63 FROM_HERE, base::BindOnce(&Stream::ReadOnFileSequence, this, position, |
| 66 max_size, std::move(callback))); | 64 max_size, std::move(callback))); |
| 67 } | 65 } |
| 68 | 66 |
| 69 void Stream::Append(std::unique_ptr<std::string> data) { | 67 void DevToolsIOContext::Stream::Append(std::unique_ptr<std::string> data) { |
| 70 task_runner_->PostTask( | 68 task_runner_->PostTask( |
| 71 FROM_HERE, | 69 FROM_HERE, |
| 72 base::BindOnce(&Stream::AppendOnFileSequence, this, std::move(data))); | 70 base::BindOnce(&Stream::AppendOnFileSequence, this, std::move(data))); |
| 73 } | 71 } |
| 74 | 72 |
| 75 void Stream::ReadOnFileSequence(off_t position, | 73 void DevToolsIOContext::Stream::ReadOnFileSequence(off_t position, |
| 76 size_t max_size, | 74 size_t max_size, |
| 77 ReadCallback callback) { | 75 ReadCallback callback) { |
| 78 DCHECK(task_runner_->RunsTasksInCurrentSequence()); | 76 DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
| 79 Status status = StatusFailure; | 77 Status status = StatusFailure; |
| 80 std::unique_ptr<std::string> data; | 78 std::unique_ptr<std::string> data; |
| 81 | 79 |
| 82 if (file_.IsValid()) { | 80 if (file_.IsValid()) { |
| 83 std::string buffer; | 81 std::string buffer; |
| 84 buffer.resize(max_size); | 82 buffer.resize(max_size); |
| 85 if (position < 0) | 83 if (position < 0) |
| (...skipping 15 matching lines...) Expand all Loading... |
| 101 data.reset(new std::string(std::move(buffer))); | 99 data.reset(new std::string(std::move(buffer))); |
| 102 status = size_got ? StatusSuccess : StatusEOF; | 100 status = size_got ? StatusSuccess : StatusEOF; |
| 103 last_read_pos_ = position + size_got; | 101 last_read_pos_ = position + size_got; |
| 104 } | 102 } |
| 105 } | 103 } |
| 106 BrowserThread::PostTask( | 104 BrowserThread::PostTask( |
| 107 BrowserThread::UI, FROM_HERE, | 105 BrowserThread::UI, FROM_HERE, |
| 108 base::BindOnce(std::move(callback), std::move(data), status)); | 106 base::BindOnce(std::move(callback), std::move(data), status)); |
| 109 } | 107 } |
| 110 | 108 |
| 111 void Stream::AppendOnFileSequence(std::unique_ptr<std::string> data) { | 109 void DevToolsIOContext::Stream::AppendOnFileSequence(std::unique_ptr<std::string
> data) { |
| 112 if (!InitOnFileSequenceIfNeeded()) | 110 if (!InitOnFileSequenceIfNeeded()) |
| 113 return; | 111 return; |
| 114 int size_written = file_.WriteAtCurrentPos(&*data->begin(), data->length()); | 112 int size_written = file_.WriteAtCurrentPos(&*data->begin(), data->length()); |
| 115 if (size_written != static_cast<int>(data->length())) { | 113 if (size_written != static_cast<int>(data->length())) { |
| 116 LOG(ERROR) << "Failed to write temporary file"; | 114 LOG(ERROR) << "Failed to write temporary file"; |
| 117 had_errors_ = true; | 115 had_errors_ = true; |
| 118 file_.Close(); | 116 file_.Close(); |
| 119 } | 117 } |
| 120 } | 118 } |
| 121 | 119 |
| 122 DevToolsIOContext::DevToolsIOContext() = default; | 120 DevToolsIOContext::DevToolsIOContext() = default; |
| 123 | 121 |
| 124 DevToolsIOContext::~DevToolsIOContext() = default; | 122 DevToolsIOContext::~DevToolsIOContext() = default; |
| 125 | 123 |
| 126 scoped_refptr<Stream> DevToolsIOContext::CreateTempFileBackedStream() { | 124 scoped_refptr<DevToolsIOContext::Stream> DevToolsIOContext::CreateTempFileBacked
Stream() { |
| 127 if (!task_runner_) { | 125 if (!task_runner_) { |
| 128 task_runner_ = base::CreateSequencedTaskRunnerWithTraits( | 126 task_runner_ = base::CreateSequencedTaskRunnerWithTraits( |
| 129 {base::MayBlock(), base::TaskPriority::BACKGROUND}); | 127 {base::MayBlock(), base::TaskPriority::BACKGROUND}); |
| 130 } | 128 } |
| 131 scoped_refptr<Stream> result = new Stream(task_runner_.get()); | 129 scoped_refptr<DevToolsIOContext::Stream> result = new DevToolsIOContext::Strea
m(task_runner_.get()); |
| 132 bool inserted = | 130 bool inserted = |
| 133 streams_.insert(std::make_pair(result->handle(), result)).second; | 131 streams_.insert(std::make_pair(result->handle(), result)).second; |
| 134 DCHECK(inserted); | 132 DCHECK(inserted); |
| 135 return result; | 133 return result; |
| 136 } | 134 } |
| 137 | 135 |
| 138 scoped_refptr<Stream> | 136 scoped_refptr<DevToolsIOContext::Stream> |
| 139 DevToolsIOContext::GetByHandle(const std::string& handle) { | 137 DevToolsIOContext::GetByHandle(const std::string& handle) { |
| 140 StreamsMap::const_iterator it = streams_.find(handle); | 138 StreamsMap::const_iterator it = streams_.find(handle); |
| 141 return it == streams_.end() ? scoped_refptr<Stream>() : it->second; | 139 return it == streams_.end() ? scoped_refptr<Stream>() : it->second; |
| 142 } | 140 } |
| 143 | 141 |
| 144 bool DevToolsIOContext::Close(const std::string& handle) { | 142 bool DevToolsIOContext::Close(const std::string& handle) { |
| 145 return streams_.erase(handle) == 1; | 143 return streams_.erase(handle) == 1; |
| 146 } | 144 } |
| 147 | 145 |
| 148 void DevToolsIOContext::DiscardAllStreams() { | 146 void DevToolsIOContext::DiscardAllStreams() { |
| 149 return streams_.clear(); | 147 return streams_.clear(); |
| 150 } | 148 } |
| 151 | 149 |
| 152 } // namespace content | 150 } // namespace content |
| OLD | NEW |