Hans Muller | eb1a5f9 | 2014-11-03 16:58:59 -0800 | [diff] [blame] | 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 | |
Aaron Boodman | 7a1fe9b | 2015-02-23 16:36:57 -0800 | [diff] [blame] | 5 | #include "shell/data_pipe_peek.h" |
Hans Muller | eb1a5f9 | 2014-11-03 16:58:59 -0800 | [diff] [blame] | 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), |
James Robinson | b4b7af2 | 2014-12-05 11:21:01 -0800 | [diff] [blame] | 28 | kMaxSleepMicros_(1000 * 10), // 10ms |
| 29 | last_number_bytes_read_(0) {} |
Hans Muller | eb1a5f9 | 2014-11-03 16:58:59 -0800 | [diff] [blame] | 30 | |
| 31 | bool MaybeSleep(uint32 num_bytes_read) { |
| 32 | if (num_bytes_read > 0 && last_number_bytes_read_ >= num_bytes_read) |
| 33 | return false; |
| 34 | last_number_bytes_read_ = num_bytes_read; |
| 35 | |
| 36 | MojoTimeTicks now(GetTimeTicksNow()); |
| 37 | if (now > deadline_) |
| 38 | return false; |
| 39 | |
James Robinson | b4b7af2 | 2014-12-05 11:21:01 -0800 | [diff] [blame] | 40 | MojoTimeTicks sleep_time = |
| 41 | (deadline_ == 0) |
| 42 | ? kMaxSleepMicros_ |
| 43 | : std::min<int64>(deadline_ - now, PeekSleeper::kMaxSleepMicros_); |
Hans Muller | eb1a5f9 | 2014-11-03 16:58:59 -0800 | [diff] [blame] | 44 | base::PlatformThread::Sleep(base::TimeDelta::FromMicroseconds(sleep_time)); |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | private: |
James Robinson | b4b7af2 | 2014-12-05 11:21:01 -0800 | [diff] [blame] | 49 | const MojoTimeTicks deadline_; // 0 => MOJO_DEADLINE_INDEFINITE |
Hans Muller | eb1a5f9 | 2014-11-03 16:58:59 -0800 | [diff] [blame] | 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 | |
James Robinson | b4b7af2 | 2014-12-05 11:21:01 -0800 | [diff] [blame] | 72 | MojoTimeTicks deadline = |
| 73 | (timeout == MOJO_DEADLINE_INDEFINITE) |
| 74 | ? 0 |
| 75 | : 1 + GetTimeTicksNow() + static_cast<MojoTimeTicks>(timeout); |
Hans Muller | eb1a5f9 | 2014-11-03 16:58:59 -0800 | [diff] [blame] | 76 | PeekSleeper sleeper(deadline); |
| 77 | |
| 78 | MojoResult result; |
| 79 | do { |
| 80 | const void* buffer; |
| 81 | uint32_t num_bytes; |
| 82 | result = |
| 83 | BeginReadDataRaw(source, &buffer, &num_bytes, MOJO_READ_DATA_FLAG_NONE); |
| 84 | |
| 85 | if (result == MOJO_RESULT_OK) { |
| 86 | PeekStatus status = peek_func.Run(buffer, num_bytes, value); |
| 87 | CHECK_EQ(EndReadDataRaw(source, 0), MOJO_RESULT_OK); |
| 88 | switch (status) { |
James Robinson | b4b7af2 | 2014-12-05 11:21:01 -0800 | [diff] [blame] | 89 | case PeekStatus::kSuccess: |
| 90 | return true; |
| 91 | case PeekStatus::kFail: |
| 92 | return false; |
| 93 | case PeekStatus::kKeepReading: |
| 94 | break; |
Hans Muller | eb1a5f9 | 2014-11-03 16:58:59 -0800 | [diff] [blame] | 95 | } |
| 96 | if (!sleeper.MaybeSleep(num_bytes)) |
| 97 | return false; |
| 98 | } else if (result == MOJO_RESULT_SHOULD_WAIT) { |
| 99 | MojoTimeTicks now(GetTimeTicksNow()); |
| 100 | if (timeout == MOJO_DEADLINE_INDEFINITE || now < deadline) |
Jim Beveridge | 3427b9a | 2015-01-08 15:29:23 -0800 | [diff] [blame] | 101 | result = |
| 102 | Wait(source, MOJO_HANDLE_SIGNAL_READABLE, deadline - now, nullptr); |
Hans Muller | eb1a5f9 | 2014-11-03 16:58:59 -0800 | [diff] [blame] | 103 | } |
James Robinson | b4b7af2 | 2014-12-05 11:21:01 -0800 | [diff] [blame] | 104 | } while (result == MOJO_RESULT_OK); |
Hans Muller | eb1a5f9 | 2014-11-03 16:58:59 -0800 | [diff] [blame] | 105 | |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | PeekStatus PeekLine(size_t max_line_length, |
| 110 | const void* buffer, |
| 111 | uint32 buffer_num_bytes, |
| 112 | std::string* line) { |
| 113 | const char* p = static_cast<const char*>(buffer); |
| 114 | size_t max_p_index = std::min<size_t>(buffer_num_bytes, max_line_length); |
| 115 | for (size_t i = 0; i < max_p_index; i++) { |
| 116 | if (p[i] == '\n') { |
James Robinson | b4b7af2 | 2014-12-05 11:21:01 -0800 | [diff] [blame] | 117 | *line = std::string(p, i + 1); // Include the trailing newline. |
Hans Muller | eb1a5f9 | 2014-11-03 16:58:59 -0800 | [diff] [blame] | 118 | return PeekStatus::kSuccess; |
| 119 | } |
| 120 | } |
James Robinson | b4b7af2 | 2014-12-05 11:21:01 -0800 | [diff] [blame] | 121 | return (buffer_num_bytes >= max_line_length) ? PeekStatus::kFail |
| 122 | : PeekStatus::kKeepReading; |
Hans Muller | eb1a5f9 | 2014-11-03 16:58:59 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | PeekStatus PeekNBytes(size_t bytes_length, |
| 126 | const void* buffer, |
| 127 | uint32 buffer_num_bytes, |
| 128 | std::string* bytes) { |
| 129 | if (buffer_num_bytes >= bytes_length) { |
| 130 | const char* p = static_cast<const char*>(buffer); |
| 131 | *bytes = std::string(p, bytes_length); |
| 132 | return PeekStatus::kSuccess; |
| 133 | } |
| 134 | return PeekStatus::kKeepReading; |
| 135 | } |
| 136 | |
James Robinson | b4b7af2 | 2014-12-05 11:21:01 -0800 | [diff] [blame] | 137 | } // namespace |
Hans Muller | eb1a5f9 | 2014-11-03 16:58:59 -0800 | [diff] [blame] | 138 | |
| 139 | bool BlockingPeekNBytes(DataPipeConsumerHandle source, |
| 140 | std::string* bytes, |
| 141 | size_t bytes_length, |
| 142 | MojoDeadline timeout) { |
| 143 | PeekFunc peek_nbytes = base::Bind(PeekNBytes, bytes_length); |
| 144 | return BlockingPeekHelper(source, bytes, timeout, peek_nbytes); |
| 145 | } |
| 146 | |
| 147 | bool BlockingPeekLine(DataPipeConsumerHandle source, |
| 148 | std::string* line, |
| 149 | size_t max_line_length, |
| 150 | MojoDeadline timeout) { |
| 151 | PeekFunc peek_line = base::Bind(PeekLine, max_line_length); |
| 152 | return BlockingPeekHelper(source, line, timeout, peek_line); |
| 153 | } |
| 154 | |
James Robinson | b4b7af2 | 2014-12-05 11:21:01 -0800 | [diff] [blame] | 155 | } // namespace shell |
| 156 | } // namespace mojo |