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