Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/devtools/devtools_io_context.h" | |
| 6 | |
| 7 #include "base/files/file.h" | |
| 8 #include "base/files/file_util.h" | |
| 9 #include "base/strings/string_number_conversions.h" | |
| 10 #include "base/strings/string_util.h" | |
| 11 #include "base/third_party/icu/icu_utf.h" | |
| 12 #include "content/public/browser/browser_thread.h" | |
| 13 | |
| 14 namespace content { | |
| 15 namespace devtools { | |
| 16 | |
| 17 namespace { | |
| 18 unsigned s_last_stream_id = 0; | |
| 19 } | |
| 20 | |
| 21 using Stream = DevToolsIOContext::Stream; | |
| 22 | |
| 23 Stream::Stream() | |
| 24 : base::RefCountedDeleteOnMessageLoop<Stream>( | |
| 25 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)), | |
| 26 id_(base::IntToString(++s_last_stream_id)), | |
| 27 had_errors_(false), | |
| 28 last_read_pos_(0) {} | |
| 29 | |
| 30 Stream::~Stream() { | |
| 31 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
| 32 } | |
| 33 | |
| 34 bool Stream::InitOnFileThreadIdNeeded() { | |
| 35 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
| 36 if (had_errors_) | |
| 37 return false; | |
| 38 if (file_.IsValid()) | |
| 39 return true; | |
| 40 base::FilePath temp_path; | |
| 41 if (!base::CreateTemporaryFile(&temp_path)) { | |
| 42 LOG(ERROR) << "Failed to create temporary file"; | |
| 43 had_errors_ = true; | |
| 44 return false; | |
| 45 } | |
| 46 const unsigned flags = base::File::FLAG_OPEN_TRUNCATED | | |
| 47 base::File::FLAG_APPEND | base::File::FLAG_READ | | |
| 48 base::File::FLAG_DELETE_ON_CLOSE; | |
| 49 file_.Initialize(temp_path, flags); | |
| 50 if (!file_.IsValid()) { | |
| 51 LOG(ERROR) << "Failed to open temporary file: " << temp_path.value() | |
| 52 << ", " << base::File::ErrorToString(file_.error_details()); | |
| 53 had_errors_ = true; | |
| 54 DeleteFile(temp_path, false); | |
| 55 return false; | |
| 56 } | |
| 57 return true; | |
| 58 } | |
| 59 | |
| 60 void Stream::Read(off_t position, size_t max_size, ReadCallback callback) { | |
|
pfeldman
2015/08/24 18:17:55
DCHECK file thread, also don't call on main thread
| |
| 61 Status status = StatusFailure; | |
| 62 scoped_refptr<base::RefCountedString> data; | |
| 63 | |
| 64 if (file_.IsValid()) { | |
| 65 std::string buffer; | |
| 66 buffer.resize(max_size); | |
| 67 if (position < 0) | |
| 68 position = last_read_pos_; | |
| 69 int size_got = file_.ReadNoBestEffort(position, &*buffer.begin(), max_size); | |
| 70 if (size_got < 0) { | |
| 71 LOG(ERROR) << "Failed to read temporary file"; | |
| 72 had_errors_ = true; | |
| 73 file_.Close(); | |
| 74 } else { | |
| 75 // Provided client has requested sufficient large block, make their | |
| 76 // life easier by not truncating in the middle of a UTF-8 character. | |
| 77 if (size_got > 6 && !CBU8_IS_SINGLE(buffer[size_got - 1])) { | |
| 78 base::TruncateUTF8ToByteSize(buffer, size_got, &buffer); | |
| 79 size_got = buffer.size(); | |
| 80 } else { | |
| 81 buffer.resize(size_got); | |
| 82 } | |
| 83 data = base::RefCountedString::TakeString(&buffer); | |
| 84 status = size_got ? StatusSuccess : StatusEOF; | |
| 85 last_read_pos_ = position + size_got; | |
| 86 } | |
| 87 } | |
| 88 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 89 base::Bind(callback, data, status)); | |
| 90 } | |
| 91 | |
| 92 void Stream::Append(const scoped_refptr<base::RefCountedString>& data) { | |
| 93 if (!InitOnFileThreadIdNeeded()) | |
| 94 return; | |
| 95 const std::string& buffer = data->data(); | |
| 96 int size_written = file_.WriteAtCurrentPos(&*buffer.begin(), buffer.size()); | |
| 97 if (size_written != static_cast<int>(buffer.size())) { | |
| 98 LOG(ERROR) << "Failed to write temporary file"; | |
| 99 had_errors_ = true; | |
| 100 file_.Close(); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 DevToolsIOContext::DevToolsIOContext() {} | |
| 105 | |
| 106 DevToolsIOContext::~DevToolsIOContext() {} | |
| 107 | |
| 108 scoped_refptr<Stream> DevToolsIOContext::CreateTempFileBackedStream() { | |
| 109 scoped_refptr<Stream> result = new Stream(); | |
| 110 bool inserted = streams_.insert(std::make_pair(result->id(), result)).second; | |
| 111 DCHECK(inserted); | |
| 112 return result; | |
| 113 } | |
| 114 | |
| 115 scoped_refptr<Stream> DevToolsIOContext::GetById(const std::string& id) { | |
| 116 StreamsMap::const_iterator it = streams_.find(id); | |
| 117 return it == streams_.end() ? scoped_refptr<Stream>() : it->second; | |
| 118 } | |
| 119 | |
| 120 bool DevToolsIOContext::Close(const std::string& id) { | |
| 121 return streams_.erase(id) == 1; | |
| 122 } | |
| 123 | |
| 124 void DevToolsIOContext::DiscardAllStreams() { | |
| 125 return streams_.clear(); | |
| 126 } | |
| 127 | |
| 128 } // namespace devtools | |
| 129 } // namespace content | |
| OLD | NEW |