OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "mojo/shell/data_pipe_peek.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 |
| 9 namespace mojo { |
| 10 namespace shell { |
| 11 |
| 12 namespace { |
| 13 |
| 14 // Sleep for as long as max_sleep_micros if the deadline hasn't been reached |
| 15 // and the number of bytes read is still increasing. Returns true if sleep |
| 16 // was actually called. |
| 17 // |
| 18 // This class is a substitute for being able to wait until N bytes are available |
| 19 // from a data pipe. The MaybeSleep method is called when num_bytes_read are |
| 20 // available but more are needed by the Peek operation. If a second |
| 21 // Peek operation finds the same number of bytes after sleeping we assume |
| 22 // that there's no point in trying again. |
| 23 // TODO(hansmuller): this heuristic is weak. crbug.com/429377 |
| 24 class PeekSleeper { |
| 25 public: |
| 26 explicit PeekSleeper(MojoTimeTicks deadline) |
| 27 : deadline_(deadline), |
| 28 kMaxSleepMicros_(1000 * 10), // 10ms |
| 29 last_number_bytes_read_(0) { |
| 30 } |
| 31 |
| 32 bool MaybeSleep(uint32 num_bytes_read) { |
| 33 if (num_bytes_read > 0 && last_number_bytes_read_ >= num_bytes_read) |
| 34 return false; |
| 35 last_number_bytes_read_ = num_bytes_read; |
| 36 |
| 37 MojoTimeTicks now(GetTimeTicksNow()); |
| 38 if (now > deadline_) |
| 39 return false; |
| 40 |
| 41 MojoTimeTicks sleep_time = (deadline_ == 0) |
| 42 ? kMaxSleepMicros_ |
| 43 : std::min<int64>(deadline_ - now, PeekSleeper::kMaxSleepMicros_); |
| 44 base::PlatformThread::Sleep(base::TimeDelta::FromMicroseconds(sleep_time)); |
| 45 return true; |
| 46 } |
| 47 |
| 48 private: |
| 49 const MojoTimeTicks deadline_; // 0 => MOJO_DEADLINE_INDEFINITE |
| 50 const MojoTimeTicks kMaxSleepMicros_; |
| 51 uint32 last_number_bytes_read_; |
| 52 |
| 53 MOJO_DISALLOW_COPY_AND_ASSIGN(PeekSleeper); |
| 54 }; |
| 55 |
| 56 enum PeekStatus { kSuccess, kFail, kKeepReading }; |
| 57 typedef const base::Callback<PeekStatus(const void*, uint32_t, std::string*)>& |
| 58 PeekFunc; |
| 59 |
| 60 // When data is available on source, call peek_func and then either return true |
| 61 // and value, continue waiting for enough data to satisfy peek_func, or fail |
| 62 // and return false. Fail if the timeout is exceeded. |
| 63 // This function is not guaranteed to work correctly if applied to a data pipe |
| 64 // that's already been read from. |
| 65 bool BlockingPeekHelper(DataPipeConsumerHandle source, |
| 66 std::string* value, |
| 67 MojoDeadline timeout, |
| 68 PeekFunc peek_func) { |
| 69 DCHECK(value); |
| 70 value->clear(); |
| 71 |
| 72 MojoTimeTicks deadline = (timeout == MOJO_DEADLINE_INDEFINITE) ? 0 |
| 73 : 1 + GetTimeTicksNow() + static_cast<MojoTimeTicks>(timeout); |
| 74 PeekSleeper sleeper(deadline); |
| 75 |
| 76 MojoResult result; |
| 77 do { |
| 78 const void* buffer; |
| 79 uint32_t num_bytes; |
| 80 result = |
| 81 BeginReadDataRaw(source, &buffer, &num_bytes, MOJO_READ_DATA_FLAG_NONE); |
| 82 |
| 83 if (result == MOJO_RESULT_OK) { |
| 84 PeekStatus status = peek_func.Run(buffer, num_bytes, value); |
| 85 CHECK_EQ(EndReadDataRaw(source, 0), MOJO_RESULT_OK); |
| 86 switch (status) { |
| 87 case PeekStatus::kSuccess: return true; |
| 88 case PeekStatus::kFail: return false; |
| 89 case PeekStatus::kKeepReading: break; |
| 90 } |
| 91 if (!sleeper.MaybeSleep(num_bytes)) |
| 92 return false; |
| 93 } else if (result == MOJO_RESULT_SHOULD_WAIT) { |
| 94 MojoTimeTicks now(GetTimeTicksNow()); |
| 95 if (timeout == MOJO_DEADLINE_INDEFINITE || now < deadline) |
| 96 result = Wait(source, MOJO_HANDLE_SIGNAL_READABLE, deadline - now); |
| 97 } |
| 98 } |
| 99 while(result == MOJO_RESULT_OK); |
| 100 |
| 101 return false; |
| 102 } |
| 103 |
| 104 PeekStatus PeekLine(size_t max_line_length, |
| 105 const void* buffer, |
| 106 uint32 buffer_num_bytes, |
| 107 std::string* line) { |
| 108 const char* p = static_cast<const char*>(buffer); |
| 109 size_t max_p_index = std::min<size_t>(buffer_num_bytes, max_line_length); |
| 110 for (size_t i = 0; i < max_p_index; i++) { |
| 111 if (p[i] == '\n') { |
| 112 *line = std::string(p, i + 1); // Include the trailing newline. |
| 113 return PeekStatus::kSuccess; |
| 114 } |
| 115 } |
| 116 return (buffer_num_bytes >= max_line_length) |
| 117 ? PeekStatus::kFail : PeekStatus::kKeepReading; |
| 118 } |
| 119 |
| 120 PeekStatus PeekNBytes(size_t bytes_length, |
| 121 const void* buffer, |
| 122 uint32 buffer_num_bytes, |
| 123 std::string* bytes) { |
| 124 if (buffer_num_bytes >= bytes_length) { |
| 125 const char* p = static_cast<const char*>(buffer); |
| 126 *bytes = std::string(p, bytes_length); |
| 127 return PeekStatus::kSuccess; |
| 128 } |
| 129 return PeekStatus::kKeepReading; |
| 130 } |
| 131 |
| 132 } // namespace |
| 133 |
| 134 |
| 135 bool BlockingPeekNBytes(DataPipeConsumerHandle source, |
| 136 std::string* bytes, |
| 137 size_t bytes_length, |
| 138 MojoDeadline timeout) { |
| 139 PeekFunc peek_nbytes = base::Bind(PeekNBytes, bytes_length); |
| 140 return BlockingPeekHelper(source, bytes, timeout, peek_nbytes); |
| 141 } |
| 142 |
| 143 bool BlockingPeekLine(DataPipeConsumerHandle source, |
| 144 std::string* line, |
| 145 size_t max_line_length, |
| 146 MojoDeadline timeout) { |
| 147 PeekFunc peek_line = base::Bind(PeekLine, max_line_length); |
| 148 return BlockingPeekHelper(source, line, timeout, peek_line); |
| 149 } |
| 150 |
| 151 } // namespace shell |
| 152 } // namespace mojo |
OLD | NEW |