Chromium Code Reviews| 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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 51 size_t CopyToStringHelper( | 51 size_t CopyToStringHelper( |
| 52 std::string* result, const void* buffer, uint32_t num_bytes) { | 52 std::string* result, const void* buffer, uint32_t num_bytes) { |
| 53 result->append(static_cast<const char*>(buffer), num_bytes); | 53 result->append(static_cast<const char*>(buffer), num_bytes); |
| 54 return num_bytes; | 54 return num_bytes; |
| 55 } | 55 } |
| 56 | 56 |
| 57 size_t CopyToFileHelper(FILE* fp, const void* buffer, uint32_t num_bytes) { | 57 size_t CopyToFileHelper(FILE* fp, const void* buffer, uint32_t num_bytes) { |
| 58 return fwrite(buffer, 1, num_bytes, fp); | 58 return fwrite(buffer, 1, num_bytes, fp); |
| 59 } | 59 } |
| 60 | 60 |
| 61 bool BlockingCopyFromFile(const base::FilePath& source, | |
| 62 ScopedDataPipeProducerHandle destination) { | |
| 63 base::File file(source, base::File::FLAG_OPEN | base::File::FLAG_READ); | |
| 64 if (!file.IsValid()) | |
| 65 return false; | |
| 66 for (;;) { | |
| 67 void* buffer; | |
|
Aaron Boodman
2014/11/03 19:20:15
= nullptr; I know the code above does not do it. T
qsr
2014/11/04 11:58:34
Done. But I do not understand why you say this cod
Aaron Boodman
2014/11/04 18:05:00
Sorry, I was exaggerating for comedic effect. As i
| |
| 68 uint32_t buffer_num_bytes; | |
|
Aaron Boodman
2014/11/03 19:20:14
= 0;
qsr
2014/11/04 11:58:33
Done.
| |
| 69 MojoResult result = BeginWriteDataRaw(destination.get(), | |
| 70 &buffer, | |
| 71 &buffer_num_bytes, | |
| 72 MOJO_WRITE_DATA_FLAG_NONE); | |
| 73 if (result == MOJO_RESULT_OK) { | |
| 74 int bytes_read = | |
| 75 file.ReadAtCurrentPos(static_cast<char*>(buffer), buffer_num_bytes); | |
| 76 if (bytes_read >= 0) { | |
| 77 EndWriteDataRaw(destination.get(), bytes_read); | |
| 78 if (bytes_read == 0) { | |
| 79 // eof | |
| 80 return true; | |
| 81 } | |
| 82 } else { | |
| 83 // error | |
| 84 EndWriteDataRaw(destination.get(), 0); | |
| 85 return false; | |
| 86 } | |
| 87 } else if (result == MOJO_RESULT_SHOULD_WAIT) { | |
| 88 result = Wait(destination.get(), | |
| 89 MOJO_HANDLE_SIGNAL_WRITABLE, | |
| 90 MOJO_DEADLINE_INDEFINITE); | |
| 91 if (result != MOJO_RESULT_OK) { | |
| 92 // If the consumder handle was closed, then treat as EOF. | |
|
Aaron Boodman
2014/11/03 19:20:14
typo: consumer
qsr
2014/11/04 11:58:34
Done.
| |
| 93 return result == MOJO_RESULT_FAILED_PRECONDITION; | |
| 94 } | |
| 95 } else { | |
| 96 // If the consumder handle was closed, then treat as EOF. | |
| 97 return result == MOJO_RESULT_FAILED_PRECONDITION; | |
| 98 } | |
| 99 } | |
| 100 NOTREACHED(); | |
| 101 return false; | |
| 102 } | |
| 103 | |
| 61 } // namespace | 104 } // namespace |
| 62 | 105 |
| 63 | 106 |
| 64 // TODO(hansmuller): Add a max_size parameter. | 107 // TODO(hansmuller): Add a max_size parameter. |
| 65 bool BlockingCopyToString(ScopedDataPipeConsumerHandle source, | 108 bool BlockingCopyToString(ScopedDataPipeConsumerHandle source, |
| 66 std::string* result) { | 109 std::string* result) { |
| 67 CHECK(result); | 110 CHECK(result); |
| 68 result->clear(); | 111 result->clear(); |
| 69 return BlockingCopyHelper( | 112 return BlockingCopyHelper( |
| 70 source.Pass(), base::Bind(&CopyToStringHelper, result)); | 113 source.Pass(), base::Bind(&CopyToStringHelper, result)); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 83 const base::FilePath& destination, | 126 const base::FilePath& destination, |
| 84 base::TaskRunner* task_runner, | 127 base::TaskRunner* task_runner, |
| 85 const base::Callback<void(bool)>& callback) { | 128 const base::Callback<void(bool)>& callback) { |
| 86 base::PostTaskAndReplyWithResult( | 129 base::PostTaskAndReplyWithResult( |
| 87 task_runner, | 130 task_runner, |
| 88 FROM_HERE, | 131 FROM_HERE, |
| 89 base::Bind(&BlockingCopyToFile, base::Passed(&source), destination), | 132 base::Bind(&BlockingCopyToFile, base::Passed(&source), destination), |
| 90 callback); | 133 callback); |
| 91 } | 134 } |
| 92 | 135 |
| 136 void CopyFromFile(const base::FilePath& source, | |
|
Aaron Boodman
2014/11/03 19:20:15
Can you add a unit test for this?
qsr
2014/11/04 11:58:34
Done.
| |
| 137 ScopedDataPipeProducerHandle destination, | |
| 138 base::TaskRunner* task_runner, | |
| 139 const base::Callback<void(bool)>& callback) { | |
| 140 base::PostTaskAndReplyWithResult( | |
| 141 task_runner, | |
| 142 FROM_HERE, | |
| 143 base::Bind(&BlockingCopyFromFile, source, base::Passed(&destination)), | |
| 144 callback); | |
| 145 } | |
| 146 | |
| 93 } // namespace common | 147 } // namespace common |
| 94 } // namespace mojo | 148 } // namespace mojo |
| OLD | NEW |