| 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 <stddef.h> | |
| 8 #include <stdint.h> | |
| 9 #include <stdio.h> | |
| 10 #include <utility> | 7 #include <utility> |
| 11 | 8 |
| 12 #include "base/files/file_path.h" | 9 #include "base/bind.h" |
| 13 #include "base/files/file_util.h" | |
| 14 #include "base/files/scoped_file.h" | |
| 15 #include "base/message_loop/message_loop.h" | |
| 16 #include "base/task_runner_util.h" | |
| 17 | 10 |
| 18 namespace mojo { | 11 namespace mojo { |
| 19 namespace common { | 12 namespace common { |
| 20 namespace { | 13 namespace { |
| 21 | 14 |
| 22 bool BlockingCopyHelper(ScopedDataPipeConsumerHandle source, | 15 bool BlockingCopyHelper(ScopedDataPipeConsumerHandle source, |
| 23 const base::Callback<size_t(const void*, uint32_t)>& write_bytes) { | 16 const base::Callback<size_t(const void*, uint32_t)>& write_bytes) { |
| 24 for (;;) { | 17 for (;;) { |
| 25 const void* buffer; | 18 const void* buffer; |
| 26 uint32_t num_bytes; | 19 uint32_t num_bytes; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 51 | 44 |
| 52 return false; | 45 return false; |
| 53 } | 46 } |
| 54 | 47 |
| 55 size_t CopyToStringHelper( | 48 size_t CopyToStringHelper( |
| 56 std::string* result, const void* buffer, uint32_t num_bytes) { | 49 std::string* result, const void* buffer, uint32_t num_bytes) { |
| 57 result->append(static_cast<const char*>(buffer), num_bytes); | 50 result->append(static_cast<const char*>(buffer), num_bytes); |
| 58 return num_bytes; | 51 return num_bytes; |
| 59 } | 52 } |
| 60 | 53 |
| 61 size_t CopyToFileHelper(FILE* fp, const void* buffer, uint32_t num_bytes) { | 54 } // namespace |
| 62 return fwrite(buffer, 1, num_bytes, fp); | |
| 63 } | |
| 64 | |
| 65 } // namespace | |
| 66 | |
| 67 | 55 |
| 68 // TODO(hansmuller): Add a max_size parameter. | 56 // TODO(hansmuller): Add a max_size parameter. |
| 69 bool BlockingCopyToString(ScopedDataPipeConsumerHandle source, | 57 bool BlockingCopyToString(ScopedDataPipeConsumerHandle source, |
| 70 std::string* result) { | 58 std::string* result) { |
| 71 CHECK(result); | 59 CHECK(result); |
| 72 result->clear(); | 60 result->clear(); |
| 73 return BlockingCopyHelper(std::move(source), | 61 return BlockingCopyHelper(std::move(source), |
| 74 base::Bind(&CopyToStringHelper, result)); | 62 base::Bind(&CopyToStringHelper, result)); |
| 75 } | 63 } |
| 76 | 64 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 100 // If the consumer handle was closed, then treat as EOF. | 88 // If the consumer handle was closed, then treat as EOF. |
| 101 return result == MOJO_RESULT_FAILED_PRECONDITION; | 89 return result == MOJO_RESULT_FAILED_PRECONDITION; |
| 102 } | 90 } |
| 103 } else { | 91 } else { |
| 104 // If the consumer handle was closed, then treat as EOF. | 92 // If the consumer handle was closed, then treat as EOF. |
| 105 return result == MOJO_RESULT_FAILED_PRECONDITION; | 93 return result == MOJO_RESULT_FAILED_PRECONDITION; |
| 106 } | 94 } |
| 107 } | 95 } |
| 108 } | 96 } |
| 109 | 97 |
| 110 bool BlockingCopyToFile(ScopedDataPipeConsumerHandle source, | |
| 111 const base::FilePath& destination) { | |
| 112 base::ScopedFILE fp(base::OpenFile(destination, "wb")); | |
| 113 if (!fp) | |
| 114 return false; | |
| 115 return BlockingCopyHelper(std::move(source), | |
| 116 base::Bind(&CopyToFileHelper, fp.get())); | |
| 117 } | |
| 118 | |
| 119 void CopyToFile(ScopedDataPipeConsumerHandle source, | |
| 120 const base::FilePath& destination, | |
| 121 base::TaskRunner* task_runner, | |
| 122 const base::Callback<void(bool)>& callback) { | |
| 123 base::PostTaskAndReplyWithResult( | |
| 124 task_runner, | |
| 125 FROM_HERE, | |
| 126 base::Bind(&BlockingCopyToFile, base::Passed(&source), destination), | |
| 127 callback); | |
| 128 } | |
| 129 | |
| 130 } // namespace common | 98 } // namespace common |
| 131 } // namespace mojo | 99 } // namespace mojo |
| OLD | NEW |