Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: mojo/shell/data_pipe_peek.cc

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

Powered by Google App Engine
This is Rietveld 408576698