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

Unified Diff: mojo/common/data_pipe_utils.cc

Issue 696543003: Mojo content handler shebang support (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 6 years, 2 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 side-by-side diff with in-line comments
Download patch
« mojo/common/data_pipe_utils.h ('K') | « mojo/common/data_pipe_utils.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/common/data_pipe_utils.cc
diff --git a/mojo/common/data_pipe_utils.cc b/mojo/common/data_pipe_utils.cc
index 428a5897b05074ebcae62029659adbace64314c7..6d9cf11aba815fef9561b027588cf04784514e5e 100644
--- a/mojo/common/data_pipe_utils.cc
+++ b/mojo/common/data_pipe_utils.cc
@@ -11,6 +11,7 @@
#include "base/files/scoped_file.h"
#include "base/message_loop/message_loop.h"
#include "base/task_runner_util.h"
+#include "base/threading/platform_thread.h"
namespace mojo {
namespace common {
@@ -58,9 +59,139 @@ size_t CopyToFileHelper(FILE* fp, const void* buffer, uint32_t num_bytes) {
return fwrite(buffer, 1, num_bytes, fp);
}
+// Sleep for as long as max_sleep_micros if the deadline hasn't been reached
+// and the number of bytes read is still increasing. Returns true if sleep
+// was actually called.
+//
+// This class is a substitute for being able to wait until N bytes are available
+// from a data pipe. The MaybeSleep method is called when num_bytes_read are
+// available but more are needed by the Peek operation. If a second
+// Peek operation finds the same number of bytes after sleeping we assume
+// that there's no point in trying again.
viettrungluu 2014/10/31 23:30:23 Please add a TODO for yourself, since this is pret
hansmuller 2014/11/01 00:06:04 Done.
+
viettrungluu 2014/10/31 23:30:22 nit: no blank line here
hansmuller 2014/11/01 00:06:05 Done.
+class PeekSleeper {
viettrungluu 2014/10/31 23:30:22 For some reason, having this helper class seems li
hansmuller 2014/11/01 00:06:05 I initially wrote the code without it. Moving the
+public:
viettrungluu 2014/10/31 23:30:23 indent one space (consider running clang-format)
hansmuller 2014/11/01 00:06:05 Done.
+ PeekSleeper(MojoTimeTicks deadline)
viettrungluu 2014/10/31 23:30:23 explicit
hansmuller 2014/11/01 00:06:05 Done.
+ : deadline_(deadline),
+ last_number_bytes_read_(0),
+ max_sleep_micros_(1000 * 10) {
viettrungluu 2014/10/31 23:30:22 This is a constant, so it should be declared as su
hansmuller 2014/11/01 00:06:05 Done.
+ }
+
+ bool MaybeSleep(uint32 num_bytes_read) {
+ if (num_bytes_read > 0 && last_number_bytes_read_ >= num_bytes_read)
+ return false;
+ last_number_bytes_read_ = num_bytes_read;
+
+ MojoTimeTicks now(GetTimeTicksNow());
+ if (now > deadline_)
+ return false;
+
+ MojoTimeTicks sleep_time = (deadline_ == 0)
+ ? max_sleep_micros_
+ : std::min<int64>(deadline_ - now, max_sleep_micros_);
+ base::PlatformThread::Sleep(base::TimeDelta::FromMicroseconds(sleep_time));
+ return true;
+ }
+
+private:
viettrungluu 2014/10/31 23:30:23 indent one space
hansmuller 2014/11/01 00:06:05 Done.
+ MojoTimeTicks deadline_; // 0 => MOJO_DEADLINE_INDEFINITE
+ uint32 last_number_bytes_read_;
+ MojoTimeTicks max_sleep_micros_; // microseconds
+};
+
+enum PeekStatus {kSuccess, kFail, kKeepReading};
viettrungluu 2014/10/31 23:30:23 space after {, before }
hansmuller 2014/11/01 00:06:05 Done.
+typedef PeekStatus (*PeekFunc)(const void*, uint32_t, size_t, std::string*);
viettrungluu 2014/10/31 23:30:23 I find the fact that the size_t argument has varyi
hansmuller 2014/11/03 23:24:24 Done.
+
+// When data is available on source, call peek_func and then either return true
+// and value, continue waiting for enough data to satisfy peek_func, or fail
+// and return false. Fail if the timeout is exceeded.
+
viettrungluu 2014/10/31 23:30:23 no blank line
hansmuller 2014/11/01 00:06:05 Done.
+bool BlockingPeekHelper(DataPipeConsumerHandle source,
+ std::string* value,
+ size_t value_length,
+ MojoDeadline timeout,
+ PeekFunc peek_func) {
+ CHECK(value);
viettrungluu 2014/10/31 23:30:23 Probably a DCHECK is sufficient.
hansmuller 2014/11/01 00:06:04 Done.
+ value->clear();
+
+ MojoTimeTicks deadline = (timeout == MOJO_DEADLINE_INDEFINITE) ? 0
+ : 1 + GetTimeTicksNow() + static_cast<MojoTimeTicks>(timeout);
+ PeekSleeper sleeper(deadline);
+ MojoResult result = MOJO_RESULT_OK;
+
+ while(result == MOJO_RESULT_OK) {
viettrungluu 2014/10/31 23:30:23 Maybe write this as a do-while instead?
viettrungluu 2014/11/03 18:36:56 You missed this.
hansmuller 2014/11/03 23:24:24 Done.
+ const void* buffer;
+ uint32_t num_bytes;
+ result =
+ BeginReadDataRaw(source, &buffer, &num_bytes, MOJO_READ_DATA_FLAG_NONE);
viettrungluu 2014/10/31 23:30:23 So if this is called not at the beginning of the b
viettrungluu 2014/11/03 18:36:56 And this....
+
+ if (result == MOJO_RESULT_OK) {
+ PeekStatus status = peek_func(buffer, num_bytes, value_length, value);
+ if (EndReadDataRaw(source, 0) != MOJO_RESULT_OK)
viettrungluu 2014/10/31 23:30:23 Probably just do a CHECK_NE.
viettrungluu 2014/11/03 18:36:56 ...
hansmuller 2014/11/03 23:24:24 Done.
+ return false;
+ switch (status) {
+ case PeekStatus::kSuccess: return true;
+ case PeekStatus::kFail: return false;
+ case PeekStatus::kKeepReading: break;
+ }
+ if (!sleeper.MaybeSleep(num_bytes))
+ return false;
+
viettrungluu 2014/10/31 23:30:23 no blank line
hansmuller 2014/11/01 00:06:05 Done.
+ } else if (result == MOJO_RESULT_SHOULD_WAIT) {
+ MojoTimeTicks now(GetTimeTicksNow());
+ if (timeout == MOJO_DEADLINE_INDEFINITE || now < deadline)
+ result = Wait(source, MOJO_HANDLE_SIGNAL_READABLE, deadline - now);
+ }
+ }
+
+ return false;
+}
+
+PeekStatus PeekLine(const void* buffer,
+ uint32 buffer_num_bytes,
+ size_t max_line_length,
+ std::string* line) {
+ const char* p = static_cast<const char*>(buffer);
+ std::string s(p, buffer_num_bytes);
viettrungluu 2014/10/31 23:30:23 It seems quite painful to build an entire string j
hansmuller 2014/11/01 00:06:04 Since you went to the trouble of writing a loop, I
+ size_t n = s.find("\n");
+ if (n != std::string::npos && n <= max_line_length) {
+ *line = s.substr(0, n);
viettrungluu 2014/10/31 23:30:23 This won't include the '\n', will it? I find that
hansmuller 2014/11/01 00:06:05 Yes he line return value doesn't include the termi
viettrungluu 2014/11/03 18:36:56 I guess for "peek", it's less odd, but you should
+ return PeekStatus::kSuccess;
+ }
+ if (s.size() > max_line_length)
+ return PeekStatus::kFail;
+ return PeekStatus::kKeepReading;
+}
+
+PeekStatus PeekNBytes(const void* buffer,
+ uint32 buffer_num_bytes,
viettrungluu 2014/10/31 23:30:23 fix indentation
hansmuller 2014/11/01 00:06:05 Done.
+ size_t bytes_length,
+ std::string* bytes) {
+ if (buffer_num_bytes >= bytes_length) {
+ const char* p = static_cast<const char*>(buffer);
+ *bytes = std::string(p, bytes_length);
+ return PeekStatus::kSuccess;
+ }
+ return PeekStatus::kKeepReading;
+}
+
} // namespace
viettrungluu 2014/10/31 23:30:23 nit: one fewer blank line
hansmuller 2014/11/01 00:06:04 Done.
+bool BlockingPeekNBytes(DataPipeConsumerHandle source,
+ std::string* bytes,
+ size_t bytes_length,
+ MojoDeadline timeout) {
+ return BlockingPeekHelper(source, bytes, bytes_length, timeout, &PeekNBytes);
+}
+
+bool BlockingPeekLine(DataPipeConsumerHandle source,
+ std::string* line,
+ size_t max_line_length,
+ MojoDeadline timeout) {
+ return BlockingPeekHelper(source, line, max_line_length, timeout, &PeekLine);
+}
+
// TODO(hansmuller): Add a max_size parameter.
bool BlockingCopyToString(ScopedDataPipeConsumerHandle source,
std::string* result) {
« mojo/common/data_pipe_utils.h ('K') | « mojo/common/data_pipe_utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698