Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "mojo/common/data_pipe_utils.h" | 5 #include "mojo/common/data_pipe_utils.h" |
| 6 | 6 |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 11 #include "base/files/scoped_file.h" | 11 #include "base/files/scoped_file.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/task_runner_util.h" | 13 #include "base/task_runner_util.h" |
| 14 #include "base/threading/platform_thread.h" | |
| 14 | 15 |
| 15 namespace mojo { | 16 namespace mojo { |
| 16 namespace common { | 17 namespace common { |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 bool BlockingCopyHelper(ScopedDataPipeConsumerHandle source, | 20 bool BlockingCopyHelper(ScopedDataPipeConsumerHandle source, |
| 20 const base::Callback<size_t(const void*, uint32_t)>& write_bytes) { | 21 const base::Callback<size_t(const void*, uint32_t)>& write_bytes) { |
| 21 for (;;) { | 22 for (;;) { |
| 22 const void* buffer; | 23 const void* buffer; |
| 23 uint32_t num_bytes; | 24 uint32_t num_bytes; |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 51 size_t CopyToStringHelper( | 52 size_t CopyToStringHelper( |
| 52 std::string* result, const void* buffer, uint32_t num_bytes) { | 53 std::string* result, const void* buffer, uint32_t num_bytes) { |
| 53 result->append(static_cast<const char*>(buffer), num_bytes); | 54 result->append(static_cast<const char*>(buffer), num_bytes); |
| 54 return num_bytes; | 55 return num_bytes; |
| 55 } | 56 } |
| 56 | 57 |
| 57 size_t CopyToFileHelper(FILE* fp, const void* buffer, uint32_t num_bytes) { | 58 size_t CopyToFileHelper(FILE* fp, const void* buffer, uint32_t num_bytes) { |
| 58 return fwrite(buffer, 1, num_bytes, fp); | 59 return fwrite(buffer, 1, num_bytes, fp); |
| 59 } | 60 } |
| 60 | 61 |
| 62 // Sleep for as long as max_sleep_micros if the deadline hasn't been reached | |
| 63 // and the number of bytes read is still increasing. Returns true if sleep | |
| 64 // was actually called. | |
| 65 // | |
| 66 // This class is a substitute for being able to wait until N bytes are available | |
| 67 // from a data pipe. The MaybeSleep method is called when num_bytes_read are | |
| 68 // available but more are needed by the Peek operation. If a second | |
| 69 // Peek operation finds the same number of bytes after sleeping we assume | |
| 70 // that there's no point in trying again. | |
| 71 // TODO(hansmuller): this heuristic is weak. crbug.com/429377 | |
| 72 class PeekSleeper { | |
| 73 public: | |
| 74 explicit PeekSleeper(MojoTimeTicks deadline) | |
| 75 : deadline_(deadline), | |
| 76 last_number_bytes_read_(0), | |
| 77 max_sleep_micros_(1000 * 10) { | |
| 78 } | |
| 79 | |
| 80 bool MaybeSleep(uint32 num_bytes_read) { | |
| 81 if (num_bytes_read > 0 && last_number_bytes_read_ >= num_bytes_read) | |
| 82 return false; | |
| 83 last_number_bytes_read_ = num_bytes_read; | |
| 84 | |
| 85 MojoTimeTicks now(GetTimeTicksNow()); | |
| 86 if (now > deadline_) | |
| 87 return false; | |
| 88 | |
| 89 MojoTimeTicks sleep_time = (deadline_ == 0) | |
| 90 ? max_sleep_micros_ | |
| 91 : std::min<int64>(deadline_ - now, max_sleep_micros_); | |
| 92 base::PlatformThread::Sleep(base::TimeDelta::FromMicroseconds(sleep_time)); | |
| 93 return true; | |
| 94 } | |
| 95 | |
| 96 private: | |
| 97 MojoTimeTicks deadline_; // 0 => MOJO_DEADLINE_INDEFINITE | |
| 98 uint32 last_number_bytes_read_; | |
| 99 const MojoTimeTicks max_sleep_micros_; // microseconds | |
|
viettrungluu
2014/11/03 18:36:56
By "const", I meant a static const, named "kMaxSle
hansmuller
2014/11/03 23:24:24
Done.
| |
| 100 }; | |
|
viettrungluu
2014/11/03 18:36:56
DISALLOW_COPY_AND_ASSIGN
hansmuller
2014/11/03 23:24:25
Done.
| |
| 101 | |
| 102 enum PeekStatus { kSuccess, kFail, kKeepReading }; | |
| 103 typedef const base::Callback<PeekStatus(const void*, uint32_t, std::string*)>& | |
| 104 PeekFunc; | |
| 105 | |
| 106 // When data is available on source, call peek_func and then either return true | |
| 107 // and value, continue waiting for enough data to satisfy peek_func, or fail | |
| 108 // and return false. Fail if the timeout is exceeded. | |
| 109 bool BlockingPeekHelper(DataPipeConsumerHandle source, | |
| 110 std::string* value, | |
| 111 MojoDeadline timeout, | |
| 112 PeekFunc peek_func) { | |
| 113 DCHECK(value); | |
| 114 value->clear(); | |
| 115 | |
| 116 MojoTimeTicks deadline = (timeout == MOJO_DEADLINE_INDEFINITE) ? 0 | |
| 117 : 1 + GetTimeTicksNow() + static_cast<MojoTimeTicks>(timeout); | |
| 118 PeekSleeper sleeper(deadline); | |
| 119 MojoResult result = MOJO_RESULT_OK; | |
| 120 | |
| 121 while(result == MOJO_RESULT_OK) { | |
| 122 const void* buffer; | |
| 123 uint32_t num_bytes; | |
| 124 result = | |
| 125 BeginReadDataRaw(source, &buffer, &num_bytes, MOJO_READ_DATA_FLAG_NONE); | |
| 126 | |
| 127 if (result == MOJO_RESULT_OK) { | |
| 128 PeekStatus status = peek_func.Run(buffer, num_bytes, value); | |
| 129 if (EndReadDataRaw(source, 0) != MOJO_RESULT_OK) | |
| 130 return false; | |
| 131 switch (status) { | |
| 132 case PeekStatus::kSuccess: return true; | |
| 133 case PeekStatus::kFail: return false; | |
| 134 case PeekStatus::kKeepReading: break; | |
| 135 } | |
| 136 if (!sleeper.MaybeSleep(num_bytes)) | |
| 137 return false; | |
| 138 } else if (result == MOJO_RESULT_SHOULD_WAIT) { | |
| 139 MojoTimeTicks now(GetTimeTicksNow()); | |
| 140 if (timeout == MOJO_DEADLINE_INDEFINITE || now < deadline) | |
| 141 result = Wait(source, MOJO_HANDLE_SIGNAL_READABLE, deadline - now); | |
| 142 } | |
| 143 } | |
| 144 | |
| 145 return false; | |
| 146 } | |
| 147 | |
| 148 PeekStatus PeekLine(size_t max_line_length, | |
| 149 const void* buffer, | |
| 150 uint32 buffer_num_bytes, | |
| 151 std::string* line) { | |
| 152 const char* p = static_cast<const char*>(buffer); | |
| 153 size_t max_p_index = std::min<size_t>(buffer_num_bytes, max_line_length); | |
| 154 for (size_t i = 0; i < max_p_index; i++) { | |
| 155 if (p[i] == '\n') { | |
| 156 *line = std::string(p, i + 1); // Include the trailing newline. | |
| 157 return PeekStatus::kSuccess; | |
| 158 } | |
| 159 } | |
| 160 return (buffer_num_bytes >= max_line_length) | |
| 161 ? PeekStatus::kFail : PeekStatus::kKeepReading; | |
| 162 } | |
| 163 | |
| 164 PeekStatus PeekNBytes(size_t bytes_length, | |
| 165 const void* buffer, | |
| 166 uint32 buffer_num_bytes, | |
| 167 std::string* bytes) { | |
| 168 if (buffer_num_bytes >= bytes_length) { | |
| 169 const char* p = static_cast<const char*>(buffer); | |
| 170 *bytes = std::string(p, bytes_length); | |
| 171 return PeekStatus::kSuccess; | |
| 172 } | |
| 173 return PeekStatus::kKeepReading; | |
| 174 } | |
| 175 | |
| 61 } // namespace | 176 } // namespace |
| 62 | 177 |
| 178 bool BlockingPeekNBytes(DataPipeConsumerHandle source, | |
| 179 std::string* bytes, | |
| 180 size_t bytes_length, | |
| 181 MojoDeadline timeout) { | |
| 182 PeekFunc peek_nbytes = base::Bind(PeekNBytes, bytes_length); | |
| 183 return BlockingPeekHelper(source, bytes, timeout, peek_nbytes); | |
| 184 } | |
| 185 | |
| 186 bool BlockingPeekLine(DataPipeConsumerHandle source, | |
| 187 std::string* line, | |
| 188 size_t max_line_length, | |
| 189 MojoDeadline timeout) { | |
| 190 PeekFunc peek_line = base::Bind(PeekLine, max_line_length); | |
| 191 return BlockingPeekHelper(source, line, timeout, peek_line); | |
| 192 } | |
| 63 | 193 |
| 64 // TODO(hansmuller): Add a max_size parameter. | 194 // TODO(hansmuller): Add a max_size parameter. |
| 65 bool BlockingCopyToString(ScopedDataPipeConsumerHandle source, | 195 bool BlockingCopyToString(ScopedDataPipeConsumerHandle source, |
| 66 std::string* result) { | 196 std::string* result) { |
| 67 CHECK(result); | 197 CHECK(result); |
| 68 result->clear(); | 198 result->clear(); |
| 69 return BlockingCopyHelper( | 199 return BlockingCopyHelper( |
| 70 source.Pass(), base::Bind(&CopyToStringHelper, result)); | 200 source.Pass(), base::Bind(&CopyToStringHelper, result)); |
| 71 } | 201 } |
| 72 | 202 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 85 const base::Callback<void(bool)>& callback) { | 215 const base::Callback<void(bool)>& callback) { |
| 86 base::PostTaskAndReplyWithResult( | 216 base::PostTaskAndReplyWithResult( |
| 87 task_runner, | 217 task_runner, |
| 88 FROM_HERE, | 218 FROM_HERE, |
| 89 base::Bind(&BlockingCopyToFile, base::Passed(&source), destination), | 219 base::Bind(&BlockingCopyToFile, base::Passed(&source), destination), |
| 90 callback); | 220 callback); |
| 91 } | 221 } |
| 92 | 222 |
| 93 } // namespace common | 223 } // namespace common |
| 94 } // namespace mojo | 224 } // namespace mojo |
| OLD | NEW |