| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/common/data_pipe_utils.h" | 5 #include "mojo/common/data_pipe_utils.h" |
| 6 | 6 |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 | 123 |
| 124 // TODO(hansmuller): Add a max_size parameter. | 124 // TODO(hansmuller): Add a max_size parameter. |
| 125 bool BlockingCopyToString(ScopedDataPipeConsumerHandle source, | 125 bool BlockingCopyToString(ScopedDataPipeConsumerHandle source, |
| 126 std::string* result) { | 126 std::string* result) { |
| 127 CHECK(result); | 127 CHECK(result); |
| 128 result->clear(); | 128 result->clear(); |
| 129 return BlockingCopyHelper( | 129 return BlockingCopyHelper( |
| 130 source.Pass(), base::Bind(&CopyToStringHelper, result)); | 130 source.Pass(), base::Bind(&CopyToStringHelper, result)); |
| 131 } | 131 } |
| 132 | 132 |
| 133 bool MOJO_COMMON_EXPORT BlockingCopyFromString( |
| 134 const std::string& source, |
| 135 const ScopedDataPipeProducerHandle& destination) { |
| 136 auto it = source.begin(); |
| 137 for (;;) { |
| 138 void* buffer = nullptr; |
| 139 uint32_t buffer_num_bytes = 0; |
| 140 MojoResult result = |
| 141 BeginWriteDataRaw(destination.get(), &buffer, &buffer_num_bytes, |
| 142 MOJO_WRITE_DATA_FLAG_NONE); |
| 143 if (result == MOJO_RESULT_OK) { |
| 144 char* char_buffer = static_cast<char*>(buffer); |
| 145 uint32_t byte_index = 0; |
| 146 while (it != source.end() && byte_index < buffer_num_bytes) { |
| 147 char_buffer[byte_index++] = *it++; |
| 148 } |
| 149 EndWriteDataRaw(destination.get(), byte_index); |
| 150 } else if (result == MOJO_RESULT_SHOULD_WAIT) { |
| 151 result = Wait(destination.get(), MOJO_HANDLE_SIGNAL_WRITABLE, |
| 152 MOJO_DEADLINE_INDEFINITE, nullptr); |
| 153 if (result != MOJO_RESULT_OK) { |
| 154 // If the consumer handle was closed, then treat as EOF. |
| 155 return result == MOJO_RESULT_FAILED_PRECONDITION; |
| 156 } |
| 157 } else { |
| 158 // If the consumer handle was closed, then treat as EOF. |
| 159 return result == MOJO_RESULT_FAILED_PRECONDITION; |
| 160 } |
| 161 } |
| 162 } |
| 163 |
| 133 bool BlockingCopyToFile(ScopedDataPipeConsumerHandle source, | 164 bool BlockingCopyToFile(ScopedDataPipeConsumerHandle source, |
| 134 const base::FilePath& destination) { | 165 const base::FilePath& destination) { |
| 135 base::ScopedFILE fp(base::OpenFile(destination, "wb")); | 166 base::ScopedFILE fp(base::OpenFile(destination, "wb")); |
| 136 if (!fp) { | 167 if (!fp) { |
| 137 LOG(ERROR) << "OpenFile('" << destination.value() | 168 LOG(ERROR) << "OpenFile('" << destination.value() |
| 138 << "'failed in BlockingCopyToFile" << std::endl; | 169 << "'failed in BlockingCopyToFile" << std::endl; |
| 139 return false; | 170 return false; |
| 140 } | 171 } |
| 141 return BlockingCopyHelper( | 172 return BlockingCopyHelper( |
| 142 source.Pass(), base::Bind(&CopyToFileHelper, fp.get())); | 173 source.Pass(), base::Bind(&CopyToFileHelper, fp.get())); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 159 base::TaskRunner* task_runner, | 190 base::TaskRunner* task_runner, |
| 160 const base::Callback<void(bool)>& callback) { | 191 const base::Callback<void(bool)>& callback) { |
| 161 base::PostTaskAndReplyWithResult(task_runner, FROM_HERE, | 192 base::PostTaskAndReplyWithResult(task_runner, FROM_HERE, |
| 162 base::Bind(&BlockingCopyFromFile, source, | 193 base::Bind(&BlockingCopyFromFile, source, |
| 163 base::Passed(&destination), skip), | 194 base::Passed(&destination), skip), |
| 164 callback); | 195 callback); |
| 165 } | 196 } |
| 166 | 197 |
| 167 } // namespace common | 198 } // namespace common |
| 168 } // namespace mojo | 199 } // namespace mojo |
| OLD | NEW |