blob: 2e7e6e574a470f8a9fecfaab56788b2a842ee1b9 [file] [log] [blame]
Hans Mullereb1a5f92014-11-03 16:58:59 -08001// 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 Boodman7a1fe9b2015-02-23 16:36:57 -08005#include "shell/data_pipe_peek.h"
Hans Mullereb1a5f92014-11-03 16:58:59 -08006
7#include "base/bind.h"
8
9namespace mojo {
10namespace shell {
11
12namespace {
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
24class PeekSleeper {
25 public:
26 explicit PeekSleeper(MojoTimeTicks deadline)
27 : deadline_(deadline),
James Robinsonb4b7af22014-12-05 11:21:01 -080028 kMaxSleepMicros_(1000 * 10), // 10ms
29 last_number_bytes_read_(0) {}
Hans Mullereb1a5f92014-11-03 16:58:59 -080030
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 Robinsonb4b7af22014-12-05 11:21:01 -080040 MojoTimeTicks sleep_time =
41 (deadline_ == 0)
42 ? kMaxSleepMicros_
43 : std::min<int64>(deadline_ - now, PeekSleeper::kMaxSleepMicros_);
Hans Mullereb1a5f92014-11-03 16:58:59 -080044 base::PlatformThread::Sleep(base::TimeDelta::FromMicroseconds(sleep_time));
45 return true;
46 }
47
48 private:
James Robinsonb4b7af22014-12-05 11:21:01 -080049 const MojoTimeTicks deadline_; // 0 => MOJO_DEADLINE_INDEFINITE
Hans Mullereb1a5f92014-11-03 16:58:59 -080050 const MojoTimeTicks kMaxSleepMicros_;
51 uint32 last_number_bytes_read_;
52
53 MOJO_DISALLOW_COPY_AND_ASSIGN(PeekSleeper);
54};
55
56enum PeekStatus { kSuccess, kFail, kKeepReading };
57typedef 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.
65bool BlockingPeekHelper(DataPipeConsumerHandle source,
66 std::string* value,
67 MojoDeadline timeout,
68 PeekFunc peek_func) {
69 DCHECK(value);
70 value->clear();
71
James Robinsonb4b7af22014-12-05 11:21:01 -080072 MojoTimeTicks deadline =
73 (timeout == MOJO_DEADLINE_INDEFINITE)
74 ? 0
75 : 1 + GetTimeTicksNow() + static_cast<MojoTimeTicks>(timeout);
Hans Mullereb1a5f92014-11-03 16:58:59 -080076 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 Robinsonb4b7af22014-12-05 11:21:01 -080089 case PeekStatus::kSuccess:
90 return true;
91 case PeekStatus::kFail:
92 return false;
93 case PeekStatus::kKeepReading:
94 break;
Hans Mullereb1a5f92014-11-03 16:58:59 -080095 }
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 Beveridge3427b9a2015-01-08 15:29:23 -0800101 result =
102 Wait(source, MOJO_HANDLE_SIGNAL_READABLE, deadline - now, nullptr);
Hans Mullereb1a5f92014-11-03 16:58:59 -0800103 }
James Robinsonb4b7af22014-12-05 11:21:01 -0800104 } while (result == MOJO_RESULT_OK);
Hans Mullereb1a5f92014-11-03 16:58:59 -0800105
106 return false;
107}
108
109PeekStatus 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 Robinsonb4b7af22014-12-05 11:21:01 -0800117 *line = std::string(p, i + 1); // Include the trailing newline.
Hans Mullereb1a5f92014-11-03 16:58:59 -0800118 return PeekStatus::kSuccess;
119 }
120 }
James Robinsonb4b7af22014-12-05 11:21:01 -0800121 return (buffer_num_bytes >= max_line_length) ? PeekStatus::kFail
122 : PeekStatus::kKeepReading;
Hans Mullereb1a5f92014-11-03 16:58:59 -0800123}
124
125PeekStatus 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 Robinsonb4b7af22014-12-05 11:21:01 -0800137} // namespace
Hans Mullereb1a5f92014-11-03 16:58:59 -0800138
139bool 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
147bool 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 Robinsonb4b7af22014-12-05 11:21:01 -0800155} // namespace shell
156} // namespace mojo