OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "mojo/data_pipe_utils/data_pipe_utils.h" |
| 6 |
| 7 #include <stdio.h> |
| 8 |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/task_runner_util.h" |
| 11 #include "base/threading/platform_thread.h" |
| 12 #include "base/trace_event/trace_event.h" |
| 13 #include "mojo/data_pipe_utils/data_pipe_utils_internal.h" |
| 14 #include "mojo/public/cpp/system/wait.h" |
| 15 |
| 16 namespace mojo { |
| 17 namespace common { |
| 18 |
| 19 bool BlockingCopyHelper( |
| 20 ScopedDataPipeConsumerHandle source, |
| 21 const base::Callback<size_t(const void*, uint32_t)>& write_bytes) { |
| 22 for (;;) { |
| 23 const void* buffer = nullptr; |
| 24 uint32_t num_bytes = 0; |
| 25 MojoResult result = BeginReadDataRaw(source.get(), &buffer, &num_bytes, |
| 26 MOJO_READ_DATA_FLAG_NONE); |
| 27 if (result == MOJO_RESULT_OK) { |
| 28 size_t bytes_written = write_bytes.Run(buffer, num_bytes); |
| 29 if (bytes_written < num_bytes) { |
| 30 LOG(ERROR) << "write_bytes callback wrote fewer bytes (" |
| 31 << bytes_written << ") written than expected (" << num_bytes |
| 32 << ") in BlockingCopyHelper (pipe closed? out of disk " |
| 33 "space?)"; |
| 34 // No need to call EndReadDataRaw(), since |source| will be closed. |
| 35 return false; |
| 36 } |
| 37 result = EndReadDataRaw(source.get(), num_bytes); |
| 38 if (result != MOJO_RESULT_OK) { |
| 39 LOG(ERROR) << "EndReadDataRaw error (" << result |
| 40 << ") in BlockingCopyHelper"; |
| 41 return false; |
| 42 } |
| 43 } else if (result == MOJO_RESULT_SHOULD_WAIT) { |
| 44 result = Wait(source.get(), MOJO_HANDLE_SIGNAL_READABLE, |
| 45 MOJO_DEADLINE_INDEFINITE, nullptr); |
| 46 if (result != MOJO_RESULT_OK) { |
| 47 // If the producer handle was closed, then treat as EOF. |
| 48 return result == MOJO_RESULT_FAILED_PRECONDITION; |
| 49 } |
| 50 } else if (result == MOJO_RESULT_FAILED_PRECONDITION) { |
| 51 // If the producer handle was closed, then treat as EOF. |
| 52 return true; |
| 53 } else { |
| 54 LOG(ERROR) << "Unhandled error " << result << " in BlockingCopyHelper"; |
| 55 // Some other error occurred. |
| 56 return false; |
| 57 } |
| 58 } |
| 59 } |
| 60 |
| 61 namespace { |
| 62 |
| 63 size_t CopyToStringHelper(std::string* result, |
| 64 const void* buffer, |
| 65 uint32_t num_bytes) { |
| 66 result->append(static_cast<const char*>(buffer), num_bytes); |
| 67 return num_bytes; |
| 68 } |
| 69 |
| 70 } // namespace |
| 71 |
| 72 // TODO(hansmuller): Add a max_size parameter. |
| 73 bool BlockingCopyToString(ScopedDataPipeConsumerHandle source, |
| 74 std::string* result) { |
| 75 TRACE_EVENT0("data_pipe_utils", "BlockingCopyToString"); |
| 76 CHECK(result); |
| 77 result->clear(); |
| 78 return BlockingCopyHelper(source.Pass(), |
| 79 base::Bind(&CopyToStringHelper, result)); |
| 80 } |
| 81 |
| 82 bool BlockingCopyFromString(const std::string& source, |
| 83 const ScopedDataPipeProducerHandle& destination) { |
| 84 TRACE_EVENT0("data_pipe_utils", "BlockingCopyFromString"); |
| 85 auto it = source.begin(); |
| 86 for (;;) { |
| 87 void* buffer = nullptr; |
| 88 uint32_t buffer_num_bytes = 0; |
| 89 MojoResult result = |
| 90 BeginWriteDataRaw(destination.get(), &buffer, &buffer_num_bytes, |
| 91 MOJO_WRITE_DATA_FLAG_NONE); |
| 92 if (result == MOJO_RESULT_OK) { |
| 93 char* char_buffer = static_cast<char*>(buffer); |
| 94 uint32_t byte_index = 0; |
| 95 while (it != source.end() && byte_index < buffer_num_bytes) { |
| 96 char_buffer[byte_index++] = *it++; |
| 97 } |
| 98 EndWriteDataRaw(destination.get(), byte_index); |
| 99 if (it == source.end()) |
| 100 return true; |
| 101 } else if (result == MOJO_RESULT_SHOULD_WAIT) { |
| 102 result = Wait(destination.get(), MOJO_HANDLE_SIGNAL_WRITABLE, |
| 103 MOJO_DEADLINE_INDEFINITE, nullptr); |
| 104 if (result != MOJO_RESULT_OK) { |
| 105 // If the consumer handle was closed, then treat as EOF. |
| 106 return result == MOJO_RESULT_FAILED_PRECONDITION; |
| 107 } |
| 108 } else { |
| 109 // If the consumer handle was closed, then treat as EOF. |
| 110 return result == MOJO_RESULT_FAILED_PRECONDITION; |
| 111 } |
| 112 } |
| 113 } |
| 114 |
| 115 ScopedDataPipeConsumerHandle WriteStringToConsumerHandle( |
| 116 const std::string& source) { |
| 117 TRACE_EVENT0("data_pipe_utils", "WriteStringToConsumerHandle"); |
| 118 static const size_t max_buffer_size = 2 * 1024 * 1024; // 2MB |
| 119 CHECK_LE(static_cast<uint32_t>(source.size()), max_buffer_size); |
| 120 MojoCreateDataPipeOptions options = {sizeof(MojoCreateDataPipeOptions), |
| 121 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, |
| 122 1, source.size()}; |
| 123 DataPipe pipe(options); |
| 124 BlockingCopyFromString(source, pipe.producer_handle.Pass()); |
| 125 return pipe.consumer_handle.Pass(); |
| 126 } |
| 127 |
| 128 } // namespace common |
| 129 } // namespace mojo |
OLD | NEW |