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