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

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

Issue 1049993002: Get mojo_shell building inside chromium checkout. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix presubmit Created 5 years, 8 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
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/application_manager/data_pipe_peek.h"
6
7 #include <stdint.h>
8
9 #include "base/bind.h"
10 #include "base/macros.h"
11
12 namespace mojo {
13 namespace shell {
14
15 namespace {
16
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.
20 //
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) {}
32
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;
37
38 MojoTimeTicks now(GetTimeTicksNow());
39 if (now > deadline_)
40 return false;
41
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;
47 }
48
49 private:
50 static const MojoTimeTicks kMaxSleepMicros = 1000 * 10; // 10 ms
51
52 const MojoTimeTicks deadline_; // 0 => MOJO_DEADLINE_INDEFINITE
53 uint32_t last_number_bytes_read_;
54
55 DISALLOW_COPY_AND_ASSIGN(PeekSleeper);
56 };
57
58 const MojoTimeTicks PeekSleeper::kMaxSleepMicros;
59
60 enum PeekStatus { kSuccess, kFail, kKeepReading };
61 typedef const base::Callback<PeekStatus(const void*, uint32_t, std::string*)>&
62 PeekFunc;
63
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();
75
76 MojoTimeTicks deadline =
77 (timeout == MOJO_DEADLINE_INDEFINITE)
78 ? 0
79 : 1 + GetTimeTicksNow() + static_cast<MojoTimeTicks>(timeout);
80 PeekSleeper sleeper(deadline);
81
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);
88
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;
99 }
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);
107 }
108 } while (result == MOJO_RESULT_OK);
109
110 return false;
111 }
112
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;
123 }
124 }
125 return (buffer_num_bytes >= max_line_length) ? PeekStatus::kFail
126 : PeekStatus::kKeepReading;
127 }
128
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;
137 }
138 return PeekStatus::kKeepReading;
139 }
140
141 } // namespace
142
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);
149 }
150
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);
157 }
158
159 } // namespace shell
160 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/shell/application_manager/data_pipe_peek.h ('k') | mojo/shell/application_manager/fetcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698