blob: 1084db30a9fd1d068b36389f63c60c2978846e87 [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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 Robinson94ade6b2015-08-25 13:02:06 -07005#include "mojo/data_pipe_utils/data_pipe_utils.h"
James Robinson646469d2014-10-03 15:33:28 -07006
7#include <stdio.h>
8
James Robinson646469d2014-10-03 15:33:28 -07009#include "base/message_loop/message_loop.h"
10#include "base/task_runner_util.h"
Hans Mullereb1a5f92014-11-03 16:58:59 -080011#include "base/threading/platform_thread.h"
James Robinson170d8bd2015-04-10 17:33:25 -070012#include "base/trace_event/trace_event.h"
James Robinson94ade6b2015-08-25 13:02:06 -070013#include "mojo/data_pipe_utils/data_pipe_utils_internal.h"
Viet-Trung Luudb4aa7c2016-03-14 11:09:22 -070014#include "mojo/public/cpp/system/wait.h"
James Robinson646469d2014-10-03 15:33:28 -070015
16namespace mojo {
17namespace common {
James Robinson646469d2014-10-03 15:33:28 -070018
James Robinson94ade6b2015-08-25 13:02:06 -070019bool BlockingCopyHelper(
20 ScopedDataPipeConsumerHandle source,
James Robinson646469d2014-10-03 15:33:28 -070021 const base::Callback<size_t(const void*, uint32_t)>& write_bytes) {
22 for (;;) {
Benjamin Lermanda86e222014-11-07 11:04:08 +010023 const void* buffer = nullptr;
24 uint32_t num_bytes = 0;
James Robinson94ade6b2015-08-25 13:02:06 -070025 MojoResult result = BeginReadDataRaw(source.get(), &buffer, &num_bytes,
26 MOJO_READ_DATA_FLAG_NONE);
James Robinson646469d2014-10-03 15:33:28 -070027 if (result == MOJO_RESULT_OK) {
28 size_t bytes_written = write_bytes.Run(buffer, num_bytes);
Eric Seidel72d6a212015-01-06 14:40:41 -080029 if (bytes_written < num_bytes) {
30 LOG(ERROR) << "write_bytes callback wrote fewer bytes ("
31 << bytes_written << ") written than expected (" << num_bytes
Viet-Trung Luu33afb152015-03-11 16:58:49 -070032 << ") in BlockingCopyHelper (pipe closed? out of disk "
33 "space?)";
34 // No need to call EndReadDataRaw(), since |source| will be closed.
James Robinson646469d2014-10-03 15:33:28 -070035 return false;
Eric Seidel72d6a212015-01-06 14:40:41 -080036 }
37 result = EndReadDataRaw(source.get(), num_bytes);
38 if (result != MOJO_RESULT_OK) {
39 LOG(ERROR) << "EndReadDataRaw error (" << result
Viet-Trung Luu33afb152015-03-11 16:58:49 -070040 << ") in BlockingCopyHelper";
Eric Seidel72d6a212015-01-06 14:40:41 -080041 return false;
42 }
James Robinson646469d2014-10-03 15:33:28 -070043 } else if (result == MOJO_RESULT_SHOULD_WAIT) {
James Robinson94ade6b2015-08-25 13:02:06 -070044 result = Wait(source.get(), MOJO_HANDLE_SIGNAL_READABLE,
45 MOJO_DEADLINE_INDEFINITE, nullptr);
James Robinson646469d2014-10-03 15:33:28 -070046 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 Luu33afb152015-03-11 16:58:49 -070054 LOG(ERROR) << "Unhandled error " << result << " in BlockingCopyHelper";
James Robinson646469d2014-10-03 15:33:28 -070055 // Some other error occurred.
Viet-Trung Luu33afb152015-03-11 16:58:49 -070056 return false;
James Robinson646469d2014-10-03 15:33:28 -070057 }
58 }
James Robinson646469d2014-10-03 15:33:28 -070059}
60
Nick Bray734bbd62015-03-12 12:31:57 -070061namespace {
62
James Robinson94ade6b2015-08-25 13:02:06 -070063size_t CopyToStringHelper(std::string* result,
64 const void* buffer,
65 uint32_t num_bytes) {
James Robinson646469d2014-10-03 15:33:28 -070066 result->append(static_cast<const char*>(buffer), num_bytes);
67 return num_bytes;
68}
69
James Robinson94ade6b2015-08-25 13:02:06 -070070} // namespace
James Robinson646469d2014-10-03 15:33:28 -070071
James Robinson646469d2014-10-03 15:33:28 -070072// TODO(hansmuller): Add a max_size parameter.
73bool BlockingCopyToString(ScopedDataPipeConsumerHandle source,
74 std::string* result) {
James Robinson170d8bd2015-04-10 17:33:25 -070075 TRACE_EVENT0("data_pipe_utils", "BlockingCopyToString");
James Robinson646469d2014-10-03 15:33:28 -070076 CHECK(result);
77 result->clear();
James Robinson94ade6b2015-08-25 13:02:06 -070078 return BlockingCopyHelper(source.Pass(),
79 base::Bind(&CopyToStringHelper, result));
James Robinson646469d2014-10-03 15:33:28 -070080}
81
Viet-Trung Luu77ebb732015-04-02 14:30:05 -070082bool BlockingCopyFromString(const std::string& source,
83 const ScopedDataPipeProducerHandle& destination) {
James Robinson170d8bd2015-04-10 17:33:25 -070084 TRACE_EVENT0("data_pipe_utils", "BlockingCopyFromString");
Adam Barth34a3f802015-02-26 09:18:23 -080085 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 Robinson115caf82015-04-07 12:52:40 -070099 if (it == source.end())
100 return true;
Adam Barth34a3f802015-02-26 09:18:23 -0800101 } 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
ukode1195c662016-03-18 14:18:48 -0700115ScopedDataPipeConsumerHandle 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 Robinson646469d2014-10-03 15:33:28 -0700128} // namespace common
129} // namespace mojo