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

Unified Diff: mojo/common/data_pipe_utils.cc

Issue 952413002: The tracing service sometimes creates corrupt data (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 10 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
Index: mojo/common/data_pipe_utils.cc
diff --git a/mojo/common/data_pipe_utils.cc b/mojo/common/data_pipe_utils.cc
index 47a5c303e860cb370f5fbace873400eac2c9c521..fd2b7b7492e63c88f46447136ec5541fb8b8e641 100644
--- a/mojo/common/data_pipe_utils.cc
+++ b/mojo/common/data_pipe_utils.cc
@@ -130,6 +130,37 @@ bool BlockingCopyToString(ScopedDataPipeConsumerHandle source,
source.Pass(), base::Bind(&CopyToStringHelper, result));
}
+bool MOJO_COMMON_EXPORT BlockingCopyFromString(
+ const std::string& source,
+ const ScopedDataPipeProducerHandle& destination) {
+ auto it = source.begin();
+ for (;;) {
+ void* buffer = nullptr;
+ uint32_t buffer_num_bytes = 0;
+ MojoResult result =
+ BeginWriteDataRaw(destination.get(), &buffer, &buffer_num_bytes,
+ MOJO_WRITE_DATA_FLAG_NONE);
+ if (result == MOJO_RESULT_OK) {
+ char* char_buffer = static_cast<char*>(buffer);
+ uint32_t byte_index = 0;
+ while (it != source.end() && byte_index < buffer_num_bytes) {
+ char_buffer[byte_index++] = *it++;
+ }
+ EndWriteDataRaw(destination.get(), byte_index);
+ } else if (result == MOJO_RESULT_SHOULD_WAIT) {
+ result = Wait(destination.get(), MOJO_HANDLE_SIGNAL_WRITABLE,
+ MOJO_DEADLINE_INDEFINITE, nullptr);
+ if (result != MOJO_RESULT_OK) {
+ // If the consumer handle was closed, then treat as EOF.
+ return result == MOJO_RESULT_FAILED_PRECONDITION;
+ }
+ } else {
+ // If the consumer handle was closed, then treat as EOF.
+ return result == MOJO_RESULT_FAILED_PRECONDITION;
+ }
+ }
+}
+
bool BlockingCopyToFile(ScopedDataPipeConsumerHandle source,
const base::FilePath& destination) {
base::ScopedFILE fp(base::OpenFile(destination, "wb"));

Powered by Google App Engine
This is Rietveld 408576698