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 #include <utility> |
8 | 9 |
9 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
10 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
11 #include "base/files/scoped_file.h" | 12 #include "base/files/scoped_file.h" |
12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
13 #include "base/task_runner_util.h" | 14 #include "base/task_runner_util.h" |
14 | 15 |
15 namespace mojo { | 16 namespace mojo { |
16 namespace common { | 17 namespace common { |
17 namespace { | 18 namespace { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 } | 61 } |
61 | 62 |
62 } // namespace | 63 } // namespace |
63 | 64 |
64 | 65 |
65 // TODO(hansmuller): Add a max_size parameter. | 66 // TODO(hansmuller): Add a max_size parameter. |
66 bool BlockingCopyToString(ScopedDataPipeConsumerHandle source, | 67 bool BlockingCopyToString(ScopedDataPipeConsumerHandle source, |
67 std::string* result) { | 68 std::string* result) { |
68 CHECK(result); | 69 CHECK(result); |
69 result->clear(); | 70 result->clear(); |
70 return BlockingCopyHelper( | 71 return BlockingCopyHelper(std::move(source), |
71 source.Pass(), base::Bind(&CopyToStringHelper, result)); | 72 base::Bind(&CopyToStringHelper, result)); |
72 } | 73 } |
73 | 74 |
74 bool MOJO_COMMON_EXPORT BlockingCopyFromString( | 75 bool MOJO_COMMON_EXPORT BlockingCopyFromString( |
75 const std::string& source, | 76 const std::string& source, |
76 const ScopedDataPipeProducerHandle& destination) { | 77 const ScopedDataPipeProducerHandle& destination) { |
77 auto it = source.begin(); | 78 auto it = source.begin(); |
78 for (;;) { | 79 for (;;) { |
79 void* buffer = nullptr; | 80 void* buffer = nullptr; |
80 uint32_t buffer_num_bytes = 0; | 81 uint32_t buffer_num_bytes = 0; |
81 MojoResult result = | 82 MojoResult result = |
(...skipping 20 matching lines...) Expand all Loading... |
102 return result == MOJO_RESULT_FAILED_PRECONDITION; | 103 return result == MOJO_RESULT_FAILED_PRECONDITION; |
103 } | 104 } |
104 } | 105 } |
105 } | 106 } |
106 | 107 |
107 bool BlockingCopyToFile(ScopedDataPipeConsumerHandle source, | 108 bool BlockingCopyToFile(ScopedDataPipeConsumerHandle source, |
108 const base::FilePath& destination) { | 109 const base::FilePath& destination) { |
109 base::ScopedFILE fp(base::OpenFile(destination, "wb")); | 110 base::ScopedFILE fp(base::OpenFile(destination, "wb")); |
110 if (!fp) | 111 if (!fp) |
111 return false; | 112 return false; |
112 return BlockingCopyHelper( | 113 return BlockingCopyHelper(std::move(source), |
113 source.Pass(), base::Bind(&CopyToFileHelper, fp.get())); | 114 base::Bind(&CopyToFileHelper, fp.get())); |
114 } | 115 } |
115 | 116 |
116 void CopyToFile(ScopedDataPipeConsumerHandle source, | 117 void CopyToFile(ScopedDataPipeConsumerHandle source, |
117 const base::FilePath& destination, | 118 const base::FilePath& destination, |
118 base::TaskRunner* task_runner, | 119 base::TaskRunner* task_runner, |
119 const base::Callback<void(bool)>& callback) { | 120 const base::Callback<void(bool)>& callback) { |
120 base::PostTaskAndReplyWithResult( | 121 base::PostTaskAndReplyWithResult( |
121 task_runner, | 122 task_runner, |
122 FROM_HERE, | 123 FROM_HERE, |
123 base::Bind(&BlockingCopyToFile, base::Passed(&source), destination), | 124 base::Bind(&BlockingCopyToFile, base::Passed(&source), destination), |
124 callback); | 125 callback); |
125 } | 126 } |
126 | 127 |
127 } // namespace common | 128 } // namespace common |
128 } // namespace mojo | 129 } // namespace mojo |
OLD | NEW |