• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <utility>
6 
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "mojo/public/cpp/system/data_pipe_utils.h"
10 #include "mojo/public/cpp/system/wait.h"
11 
12 namespace mojo {
13 namespace {
14 
BlockingCopyHelper(ScopedDataPipeConsumerHandle source,const base::Callback<size_t (const void *,uint32_t)> & write_bytes)15 bool BlockingCopyHelper(
16     ScopedDataPipeConsumerHandle source,
17     const base::Callback<size_t(const void*, uint32_t)>& write_bytes) {
18   for (;;) {
19     const void* buffer;
20     uint32_t num_bytes;
21     MojoResult result =
22         source->BeginReadData(&buffer, &num_bytes, MOJO_READ_DATA_FLAG_NONE);
23     if (result == MOJO_RESULT_OK) {
24       size_t bytes_written = write_bytes.Run(buffer, num_bytes);
25       result = source->EndReadData(num_bytes);
26       if (bytes_written < num_bytes || result != MOJO_RESULT_OK)
27         return false;
28     } else if (result == MOJO_RESULT_SHOULD_WAIT) {
29       result = Wait(source.get(), MOJO_HANDLE_SIGNAL_READABLE);
30       if (result != MOJO_RESULT_OK) {
31         // If the producer handle was closed, then treat as EOF.
32         return result == MOJO_RESULT_FAILED_PRECONDITION;
33       }
34     } else if (result == MOJO_RESULT_FAILED_PRECONDITION) {
35       // If the producer handle was closed, then treat as EOF.
36       return true;
37     } else {
38       // Some other error occurred.
39       break;
40     }
41   }
42 
43   return false;
44 }
45 
CopyToStringHelper(std::string * result,const void * buffer,uint32_t num_bytes)46 size_t CopyToStringHelper(std::string* result,
47                           const void* buffer,
48                           uint32_t num_bytes) {
49   result->append(static_cast<const char*>(buffer), num_bytes);
50   return num_bytes;
51 }
52 
53 }  // namespace
54 
55 // TODO(hansmuller): Add a max_size parameter.
BlockingCopyToString(ScopedDataPipeConsumerHandle source,std::string * result)56 bool BlockingCopyToString(ScopedDataPipeConsumerHandle source,
57                           std::string* result) {
58   CHECK(result);
59   result->clear();
60   return BlockingCopyHelper(std::move(source),
61                             base::Bind(&CopyToStringHelper, result));
62 }
63 
64 bool MOJO_CPP_SYSTEM_EXPORT
BlockingCopyFromString(const std::string & source,const ScopedDataPipeProducerHandle & destination)65 BlockingCopyFromString(const std::string& source,
66                        const ScopedDataPipeProducerHandle& destination) {
67   auto it = source.begin();
68   for (;;) {
69     void* buffer = nullptr;
70     uint32_t buffer_num_bytes = 0;
71     MojoResult result = destination->BeginWriteData(&buffer, &buffer_num_bytes,
72                                                     MOJO_WRITE_DATA_FLAG_NONE);
73     if (result == MOJO_RESULT_OK) {
74       char* char_buffer = static_cast<char*>(buffer);
75       uint32_t byte_index = 0;
76       while (it != source.end() && byte_index < buffer_num_bytes) {
77         char_buffer[byte_index++] = *it++;
78       }
79       destination->EndWriteData(byte_index);
80       if (it == source.end())
81         return true;
82     } else if (result == MOJO_RESULT_SHOULD_WAIT) {
83       result = Wait(destination.get(), MOJO_HANDLE_SIGNAL_WRITABLE);
84       if (result != MOJO_RESULT_OK) {
85         // If the consumer handle was closed, then treat as EOF.
86         return result == MOJO_RESULT_FAILED_PRECONDITION;
87       }
88     } else {
89       // If the consumer handle was closed, then treat as EOF.
90       return result == MOJO_RESULT_FAILED_PRECONDITION;
91     }
92   }
93 }
94 
95 }  // namespace mojo
96