OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "shell/application_manager/data_pipe_peek.h" | 5 #include "shell/application_manager/data_pipe_peek.h" |
6 | 6 |
| 7 #include <stdint.h> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/macros.h" |
8 | 11 |
9 namespace mojo { | 12 namespace mojo { |
10 namespace shell { | 13 namespace shell { |
11 | 14 |
12 namespace { | 15 namespace { |
13 | 16 |
14 // Sleep for as long as max_sleep_micros if the deadline hasn't been reached | 17 // 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 | 18 // and the number of bytes read is still increasing. Returns true if sleep |
16 // was actually called. | 19 // was actually called. |
17 // | 20 // |
18 // This class is a substitute for being able to wait until N bytes are available | 21 // 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 | 22 // 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 | 23 // 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 | 24 // Peek operation finds the same number of bytes after sleeping we assume |
22 // that there's no point in trying again. | 25 // that there's no point in trying again. |
23 // TODO(hansmuller): this heuristic is weak. crbug.com/429377 | 26 // TODO(hansmuller): this heuristic is weak. crbug.com/429377 |
24 class PeekSleeper { | 27 class PeekSleeper { |
25 public: | 28 public: |
26 explicit PeekSleeper(MojoTimeTicks deadline) | 29 explicit PeekSleeper(MojoTimeTicks deadline) |
27 : deadline_(deadline), | 30 : deadline_(deadline), |
28 kMaxSleepMicros_(1000 * 10), // 10ms | |
29 last_number_bytes_read_(0) {} | 31 last_number_bytes_read_(0) {} |
30 | 32 |
31 bool MaybeSleep(uint32 num_bytes_read) { | 33 bool MaybeSleep(uint32_t num_bytes_read) { |
32 if (num_bytes_read > 0 && last_number_bytes_read_ >= num_bytes_read) | 34 if (num_bytes_read > 0 && last_number_bytes_read_ >= num_bytes_read) |
33 return false; | 35 return false; |
34 last_number_bytes_read_ = num_bytes_read; | 36 last_number_bytes_read_ = num_bytes_read; |
35 | 37 |
36 MojoTimeTicks now(GetTimeTicksNow()); | 38 MojoTimeTicks now(GetTimeTicksNow()); |
37 if (now > deadline_) | 39 if (now > deadline_) |
38 return false; | 40 return false; |
39 | 41 |
40 MojoTimeTicks sleep_time = | 42 MojoTimeTicks sleep_time = |
41 (deadline_ == 0) | 43 (deadline_ == 0) ? kMaxSleepMicros |
42 ? kMaxSleepMicros_ | 44 : std::min<int64>(deadline_ - now, kMaxSleepMicros); |
43 : std::min<int64>(deadline_ - now, PeekSleeper::kMaxSleepMicros_); | |
44 base::PlatformThread::Sleep(base::TimeDelta::FromMicroseconds(sleep_time)); | 45 base::PlatformThread::Sleep(base::TimeDelta::FromMicroseconds(sleep_time)); |
45 return true; | 46 return true; |
46 } | 47 } |
47 | 48 |
48 private: | 49 private: |
| 50 static const MojoTimeTicks kMaxSleepMicros = 1000 * 10; // 10 ms |
| 51 |
49 const MojoTimeTicks deadline_; // 0 => MOJO_DEADLINE_INDEFINITE | 52 const MojoTimeTicks deadline_; // 0 => MOJO_DEADLINE_INDEFINITE |
50 const MojoTimeTicks kMaxSleepMicros_; | 53 uint32_t last_number_bytes_read_; |
51 uint32 last_number_bytes_read_; | |
52 | 54 |
53 MOJO_DISALLOW_COPY_AND_ASSIGN(PeekSleeper); | 55 DISALLOW_COPY_AND_ASSIGN(PeekSleeper); |
54 }; | 56 }; |
55 | 57 |
| 58 const MojoTimeTicks PeekSleeper::kMaxSleepMicros; |
| 59 |
56 enum PeekStatus { kSuccess, kFail, kKeepReading }; | 60 enum PeekStatus { kSuccess, kFail, kKeepReading }; |
57 typedef const base::Callback<PeekStatus(const void*, uint32_t, std::string*)>& | 61 typedef const base::Callback<PeekStatus(const void*, uint32_t, std::string*)>& |
58 PeekFunc; | 62 PeekFunc; |
59 | 63 |
60 // When data is available on source, call peek_func and then either return true | 64 // 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 | 65 // and value, continue waiting for enough data to satisfy peek_func, or fail |
62 // and return false. Fail if the timeout is exceeded. | 66 // and return false. Fail if the timeout is exceeded. |
63 // This function is not guaranteed to work correctly if applied to a data pipe | 67 // This function is not guaranteed to work correctly if applied to a data pipe |
64 // that's already been read from. | 68 // that's already been read from. |
65 bool BlockingPeekHelper(DataPipeConsumerHandle source, | 69 bool BlockingPeekHelper(DataPipeConsumerHandle source, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 result = | 105 result = |
102 Wait(source, MOJO_HANDLE_SIGNAL_READABLE, deadline - now, nullptr); | 106 Wait(source, MOJO_HANDLE_SIGNAL_READABLE, deadline - now, nullptr); |
103 } | 107 } |
104 } while (result == MOJO_RESULT_OK); | 108 } while (result == MOJO_RESULT_OK); |
105 | 109 |
106 return false; | 110 return false; |
107 } | 111 } |
108 | 112 |
109 PeekStatus PeekLine(size_t max_line_length, | 113 PeekStatus PeekLine(size_t max_line_length, |
110 const void* buffer, | 114 const void* buffer, |
111 uint32 buffer_num_bytes, | 115 uint32_t buffer_num_bytes, |
112 std::string* line) { | 116 std::string* line) { |
113 const char* p = static_cast<const char*>(buffer); | 117 const char* p = static_cast<const char*>(buffer); |
114 size_t max_p_index = std::min<size_t>(buffer_num_bytes, max_line_length); | 118 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++) { | 119 for (size_t i = 0; i < max_p_index; i++) { |
116 if (p[i] == '\n') { | 120 if (p[i] == '\n') { |
117 *line = std::string(p, i + 1); // Include the trailing newline. | 121 *line = std::string(p, i + 1); // Include the trailing newline. |
118 return PeekStatus::kSuccess; | 122 return PeekStatus::kSuccess; |
119 } | 123 } |
120 } | 124 } |
121 return (buffer_num_bytes >= max_line_length) ? PeekStatus::kFail | 125 return (buffer_num_bytes >= max_line_length) ? PeekStatus::kFail |
122 : PeekStatus::kKeepReading; | 126 : PeekStatus::kKeepReading; |
123 } | 127 } |
124 | 128 |
125 PeekStatus PeekNBytes(size_t bytes_length, | 129 PeekStatus PeekNBytes(size_t bytes_length, |
126 const void* buffer, | 130 const void* buffer, |
127 uint32 buffer_num_bytes, | 131 uint32_t buffer_num_bytes, |
128 std::string* bytes) { | 132 std::string* bytes) { |
129 if (buffer_num_bytes >= bytes_length) { | 133 if (buffer_num_bytes >= bytes_length) { |
130 const char* p = static_cast<const char*>(buffer); | 134 const char* p = static_cast<const char*>(buffer); |
131 *bytes = std::string(p, bytes_length); | 135 *bytes = std::string(p, bytes_length); |
132 return PeekStatus::kSuccess; | 136 return PeekStatus::kSuccess; |
133 } | 137 } |
134 return PeekStatus::kKeepReading; | 138 return PeekStatus::kKeepReading; |
135 } | 139 } |
136 | 140 |
137 } // namespace | 141 } // namespace |
138 | 142 |
139 bool BlockingPeekNBytes(DataPipeConsumerHandle source, | 143 bool BlockingPeekNBytes(DataPipeConsumerHandle source, |
140 std::string* bytes, | 144 std::string* bytes, |
141 size_t bytes_length, | 145 size_t bytes_length, |
142 MojoDeadline timeout) { | 146 MojoDeadline timeout) { |
143 PeekFunc peek_nbytes = base::Bind(PeekNBytes, bytes_length); | 147 PeekFunc peek_nbytes = base::Bind(PeekNBytes, bytes_length); |
144 return BlockingPeekHelper(source, bytes, timeout, peek_nbytes); | 148 return BlockingPeekHelper(source, bytes, timeout, peek_nbytes); |
145 } | 149 } |
146 | 150 |
147 bool BlockingPeekLine(DataPipeConsumerHandle source, | 151 bool BlockingPeekLine(DataPipeConsumerHandle source, |
148 std::string* line, | 152 std::string* line, |
149 size_t max_line_length, | 153 size_t max_line_length, |
150 MojoDeadline timeout) { | 154 MojoDeadline timeout) { |
151 PeekFunc peek_line = base::Bind(PeekLine, max_line_length); | 155 PeekFunc peek_line = base::Bind(PeekLine, max_line_length); |
152 return BlockingPeekHelper(source, line, timeout, peek_line); | 156 return BlockingPeekHelper(source, line, timeout, peek_line); |
153 } | 157 } |
154 | 158 |
155 } // namespace shell | 159 } // namespace shell |
156 } // namespace mojo | 160 } // namespace mojo |
OLD | NEW |