James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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 | |
James Robinson | 94ade6b | 2015-08-25 13:02:06 -0700 | [diff] [blame] | 5 | #include "mojo/data_pipe_utils/data_pipe_utils.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 6 | |
| 7 | #include <stdio.h> |
| 8 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 9 | #include "base/message_loop/message_loop.h" |
| 10 | #include "base/task_runner_util.h" |
Hans Muller | eb1a5f9 | 2014-11-03 16:58:59 -0800 | [diff] [blame] | 11 | #include "base/threading/platform_thread.h" |
James Robinson | 170d8bd | 2015-04-10 17:33:25 -0700 | [diff] [blame] | 12 | #include "base/trace_event/trace_event.h" |
James Robinson | 94ade6b | 2015-08-25 13:02:06 -0700 | [diff] [blame] | 13 | #include "mojo/data_pipe_utils/data_pipe_utils_internal.h" |
Viet-Trung Luu | db4aa7c | 2016-03-14 11:09:22 -0700 | [diff] [blame] | 14 | #include "mojo/public/cpp/system/wait.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 15 | |
| 16 | namespace mojo { |
| 17 | namespace common { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 18 | |
James Robinson | 94ade6b | 2015-08-25 13:02:06 -0700 | [diff] [blame] | 19 | bool BlockingCopyHelper( |
| 20 | ScopedDataPipeConsumerHandle source, |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 21 | const base::Callback<size_t(const void*, uint32_t)>& write_bytes) { |
| 22 | for (;;) { |
Benjamin Lerman | da86e22 | 2014-11-07 11:04:08 +0100 | [diff] [blame] | 23 | const void* buffer = nullptr; |
| 24 | uint32_t num_bytes = 0; |
James Robinson | 94ade6b | 2015-08-25 13:02:06 -0700 | [diff] [blame] | 25 | MojoResult result = BeginReadDataRaw(source.get(), &buffer, &num_bytes, |
| 26 | MOJO_READ_DATA_FLAG_NONE); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 27 | if (result == MOJO_RESULT_OK) { |
| 28 | size_t bytes_written = write_bytes.Run(buffer, num_bytes); |
Eric Seidel | 72d6a21 | 2015-01-06 14:40:41 -0800 | [diff] [blame] | 29 | if (bytes_written < num_bytes) { |
| 30 | LOG(ERROR) << "write_bytes callback wrote fewer bytes (" |
| 31 | << bytes_written << ") written than expected (" << num_bytes |
Viet-Trung Luu | 33afb15 | 2015-03-11 16:58:49 -0700 | [diff] [blame] | 32 | << ") in BlockingCopyHelper (pipe closed? out of disk " |
| 33 | "space?)"; |
| 34 | // No need to call EndReadDataRaw(), since |source| will be closed. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 35 | return false; |
Eric Seidel | 72d6a21 | 2015-01-06 14:40:41 -0800 | [diff] [blame] | 36 | } |
| 37 | result = EndReadDataRaw(source.get(), num_bytes); |
| 38 | if (result != MOJO_RESULT_OK) { |
| 39 | LOG(ERROR) << "EndReadDataRaw error (" << result |
Viet-Trung Luu | 33afb15 | 2015-03-11 16:58:49 -0700 | [diff] [blame] | 40 | << ") in BlockingCopyHelper"; |
Eric Seidel | 72d6a21 | 2015-01-06 14:40:41 -0800 | [diff] [blame] | 41 | return false; |
| 42 | } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 43 | } else if (result == MOJO_RESULT_SHOULD_WAIT) { |
James Robinson | 94ade6b | 2015-08-25 13:02:06 -0700 | [diff] [blame] | 44 | result = Wait(source.get(), MOJO_HANDLE_SIGNAL_READABLE, |
| 45 | MOJO_DEADLINE_INDEFINITE, nullptr); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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 { |
Viet-Trung Luu | 33afb15 | 2015-03-11 16:58:49 -0700 | [diff] [blame] | 54 | LOG(ERROR) << "Unhandled error " << result << " in BlockingCopyHelper"; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 55 | // Some other error occurred. |
Viet-Trung Luu | 33afb15 | 2015-03-11 16:58:49 -0700 | [diff] [blame] | 56 | return false; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 57 | } |
| 58 | } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Nick Bray | 734bbd6 | 2015-03-12 12:31:57 -0700 | [diff] [blame] | 61 | namespace { |
| 62 | |
James Robinson | 94ade6b | 2015-08-25 13:02:06 -0700 | [diff] [blame] | 63 | size_t CopyToStringHelper(std::string* result, |
| 64 | const void* buffer, |
| 65 | uint32_t num_bytes) { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 66 | result->append(static_cast<const char*>(buffer), num_bytes); |
| 67 | return num_bytes; |
| 68 | } |
| 69 | |
James Robinson | 94ade6b | 2015-08-25 13:02:06 -0700 | [diff] [blame] | 70 | } // namespace |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 71 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 72 | // TODO(hansmuller): Add a max_size parameter. |
| 73 | bool BlockingCopyToString(ScopedDataPipeConsumerHandle source, |
| 74 | std::string* result) { |
James Robinson | 170d8bd | 2015-04-10 17:33:25 -0700 | [diff] [blame] | 75 | TRACE_EVENT0("data_pipe_utils", "BlockingCopyToString"); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 76 | CHECK(result); |
| 77 | result->clear(); |
James Robinson | 94ade6b | 2015-08-25 13:02:06 -0700 | [diff] [blame] | 78 | return BlockingCopyHelper(source.Pass(), |
| 79 | base::Bind(&CopyToStringHelper, result)); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Viet-Trung Luu | 77ebb73 | 2015-04-02 14:30:05 -0700 | [diff] [blame] | 82 | bool BlockingCopyFromString(const std::string& source, |
| 83 | const ScopedDataPipeProducerHandle& destination) { |
James Robinson | 170d8bd | 2015-04-10 17:33:25 -0700 | [diff] [blame] | 84 | TRACE_EVENT0("data_pipe_utils", "BlockingCopyFromString"); |
Adam Barth | 34a3f80 | 2015-02-26 09:18:23 -0800 | [diff] [blame] | 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); |
James Robinson | 115caf8 | 2015-04-07 12:52:40 -0700 | [diff] [blame] | 99 | if (it == source.end()) |
| 100 | return true; |
Adam Barth | 34a3f80 | 2015-02-26 09:18:23 -0800 | [diff] [blame] | 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 | |
ukode | 1195c66 | 2016-03-18 14:18:48 -0700 | [diff] [blame] | 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 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 128 | } // namespace common |
| 129 | } // namespace mojo |