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.
5 #include "mojo/shell/data_pipe_peek.h"
10 #include "base/macros.h"
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.
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
29 explicit PeekSleeper(MojoTimeTicks deadline
)
30 : deadline_(deadline
),
31 last_number_bytes_read_(0) {}
33 bool MaybeSleep(uint32_t num_bytes_read
) {
34 if (num_bytes_read
> 0 && last_number_bytes_read_
>= num_bytes_read
)
36 last_number_bytes_read_
= num_bytes_read
;
38 MojoTimeTicks
now(GetTimeTicksNow());
42 MojoTimeTicks sleep_time
=
43 (deadline_
== 0) ? kMaxSleepMicros
44 : std::min
<int64
>(deadline_
- now
, kMaxSleepMicros
);
45 base::PlatformThread::Sleep(base::TimeDelta::FromMicroseconds(sleep_time
));
50 static const MojoTimeTicks kMaxSleepMicros
= 1000 * 10; // 10 ms
52 const MojoTimeTicks deadline_
; // 0 => MOJO_DEADLINE_INDEFINITE
53 uint32_t last_number_bytes_read_
;
55 DISALLOW_COPY_AND_ASSIGN(PeekSleeper
);
58 const MojoTimeTicks
PeekSleeper::kMaxSleepMicros
;
60 enum PeekStatus
{ kSuccess
, kFail
, kKeepReading
};
61 typedef const base::Callback
<PeekStatus(const void*, uint32_t, std::string
*)>&
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
,
76 MojoTimeTicks deadline
=
77 (timeout
== MOJO_DEADLINE_INDEFINITE
)
79 : 1 + GetTimeTicksNow() + static_cast<MojoTimeTicks
>(timeout
);
80 PeekSleeper
sleeper(deadline
);
87 BeginReadDataRaw(source
, &buffer
, &num_bytes
, MOJO_READ_DATA_FLAG_NONE
);
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
);
93 case PeekStatus::kSuccess
:
95 case PeekStatus::kFail
:
97 case PeekStatus::kKeepReading
:
100 if (!sleeper
.MaybeSleep(num_bytes
))
102 } else if (result
== MOJO_RESULT_SHOULD_WAIT
) {
103 MojoTimeTicks
now(GetTimeTicksNow());
104 if (timeout
== MOJO_DEADLINE_INDEFINITE
|| now
< deadline
)
106 Wait(source
, MOJO_HANDLE_SIGNAL_READABLE
, deadline
- now
, nullptr);
108 } while (result
== MOJO_RESULT_OK
);
113 PeekStatus
PeekLine(size_t max_line_length
,
115 uint32_t buffer_num_bytes
,
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
++) {
121 *line
= std::string(p
, i
+ 1); // Include the trailing newline.
122 return PeekStatus::kSuccess
;
125 return (buffer_num_bytes
>= max_line_length
) ? PeekStatus::kFail
126 : PeekStatus::kKeepReading
;
129 PeekStatus
PeekNBytes(size_t bytes_length
,
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
;
138 return PeekStatus::kKeepReading
;
143 bool BlockingPeekNBytes(DataPipeConsumerHandle source
,
146 MojoDeadline timeout
) {
147 PeekFunc peek_nbytes
= base::Bind(PeekNBytes
, bytes_length
);
148 return BlockingPeekHelper(source
, bytes
, timeout
, peek_nbytes
);
151 bool BlockingPeekLine(DataPipeConsumerHandle source
,
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
);