| 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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 | 64 |
| 65 // TODO(hansmuller): Add a max_size parameter. | 65 // TODO(hansmuller): Add a max_size parameter. |
| 66 bool BlockingCopyToString(ScopedDataPipeConsumerHandle source, | 66 bool BlockingCopyToString(ScopedDataPipeConsumerHandle source, |
| 67 std::string* result) { | 67 std::string* result) { |
| 68 CHECK(result); | 68 CHECK(result); |
| 69 result->clear(); | 69 result->clear(); |
| 70 return BlockingCopyHelper( | 70 return BlockingCopyHelper( |
| 71 source.Pass(), base::Bind(&CopyToStringHelper, result)); | 71 source.Pass(), base::Bind(&CopyToStringHelper, result)); |
| 72 } | 72 } |
| 73 | 73 |
| 74 bool MOJO_COMMON_EXPORT BlockingCopyFromString( |
| 75 const std::string& source, |
| 76 const ScopedDataPipeProducerHandle& destination) { |
| 77 auto it = source.begin(); |
| 78 for (;;) { |
| 79 void* buffer = nullptr; |
| 80 uint32_t buffer_num_bytes = 0; |
| 81 MojoResult result = |
| 82 BeginWriteDataRaw(destination.get(), &buffer, &buffer_num_bytes, |
| 83 MOJO_WRITE_DATA_FLAG_NONE); |
| 84 if (result == MOJO_RESULT_OK) { |
| 85 char* char_buffer = static_cast<char*>(buffer); |
| 86 uint32_t byte_index = 0; |
| 87 while (it != source.end() && byte_index < buffer_num_bytes) { |
| 88 char_buffer[byte_index++] = *it++; |
| 89 } |
| 90 EndWriteDataRaw(destination.get(), byte_index); |
| 91 } else if (result == MOJO_RESULT_SHOULD_WAIT) { |
| 92 result = Wait(destination.get(), MOJO_HANDLE_SIGNAL_WRITABLE, |
| 93 MOJO_DEADLINE_INDEFINITE, nullptr); |
| 94 if (result != MOJO_RESULT_OK) { |
| 95 // If the consumer handle was closed, then treat as EOF. |
| 96 return result == MOJO_RESULT_FAILED_PRECONDITION; |
| 97 } |
| 98 } else { |
| 99 // If the consumer handle was closed, then treat as EOF. |
| 100 return result == MOJO_RESULT_FAILED_PRECONDITION; |
| 101 } |
| 102 } |
| 103 } |
| 104 |
| 74 bool BlockingCopyToFile(ScopedDataPipeConsumerHandle source, | 105 bool BlockingCopyToFile(ScopedDataPipeConsumerHandle source, |
| 75 const base::FilePath& destination) { | 106 const base::FilePath& destination) { |
| 76 base::ScopedFILE fp(base::OpenFile(destination, "wb")); | 107 base::ScopedFILE fp(base::OpenFile(destination, "wb")); |
| 77 if (!fp) | 108 if (!fp) |
| 78 return false; | 109 return false; |
| 79 return BlockingCopyHelper( | 110 return BlockingCopyHelper( |
| 80 source.Pass(), base::Bind(&CopyToFileHelper, fp.get())); | 111 source.Pass(), base::Bind(&CopyToFileHelper, fp.get())); |
| 81 } | 112 } |
| 82 | 113 |
| 83 void CopyToFile(ScopedDataPipeConsumerHandle source, | 114 void CopyToFile(ScopedDataPipeConsumerHandle source, |
| 84 const base::FilePath& destination, | 115 const base::FilePath& destination, |
| 85 base::TaskRunner* task_runner, | 116 base::TaskRunner* task_runner, |
| 86 const base::Callback<void(bool)>& callback) { | 117 const base::Callback<void(bool)>& callback) { |
| 87 base::PostTaskAndReplyWithResult( | 118 base::PostTaskAndReplyWithResult( |
| 88 task_runner, | 119 task_runner, |
| 89 FROM_HERE, | 120 FROM_HERE, |
| 90 base::Bind(&BlockingCopyToFile, base::Passed(&source), destination), | 121 base::Bind(&BlockingCopyToFile, base::Passed(&source), destination), |
| 91 callback); | 122 callback); |
| 92 } | 123 } |
| 93 | 124 |
| 94 } // namespace common | 125 } // namespace common |
| 95 } // namespace mojo | 126 } // namespace mojo |
| OLD | NEW |