OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/filesystem/file_impl.h" | 5 #include "components/filesystem/file_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <limits> | 9 #include <limits> |
10 #include <utility> | 10 #include <utility> |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 if (num_bytes_read < 0) { | 87 if (num_bytes_read < 0) { |
88 callback.Run(FileError::FAILED, mojo::Array<uint8_t>()); | 88 callback.Run(FileError::FAILED, mojo::Array<uint8_t>()); |
89 return; | 89 return; |
90 } | 90 } |
91 | 91 |
92 DCHECK_LE(static_cast<size_t>(num_bytes_read), num_bytes_to_read); | 92 DCHECK_LE(static_cast<size_t>(num_bytes_read), num_bytes_to_read); |
93 bytes_read.resize(static_cast<size_t>(num_bytes_read)); | 93 bytes_read.resize(static_cast<size_t>(num_bytes_read)); |
94 callback.Run(FileError::OK, std::move(bytes_read)); | 94 callback.Run(FileError::OK, std::move(bytes_read)); |
95 } | 95 } |
96 | 96 |
97 void FileImpl::ReadEntireFile(const ReadEntireFileCallback& callback) { | |
98 if (!file_.IsValid()) { | |
99 callback.Run(GetError(file_), mojo::Array<uint8_t>()); | |
100 return; | |
101 } | |
102 | |
103 // Seek to the front of the file. | |
104 if (file_.Seek(base::File::FROM_BEGIN, 0) == -1) { | |
105 callback.Run(FileError::FAILED, mojo::Array<uint8_t>()); | |
106 return; | |
107 } | |
108 | |
109 std::string contents; | |
110 const int kBufferSize = 1 << 16; | |
111 scoped_ptr<char[]> buf(new char[kBufferSize]); | |
112 size_t len; | |
113 while ((len = file_.ReadAtCurrentPos(buf.get(), kBufferSize)) > 0) | |
114 contents.append(buf.get(), len); | |
115 | |
116 callback.Run(FileError::OK, mojo::Array<uint8_t>::From(contents)); | |
117 } | |
118 | |
119 // TODO(vtl): Move the implementation to a thread pool. | 97 // TODO(vtl): Move the implementation to a thread pool. |
120 void FileImpl::Write(mojo::Array<uint8_t> bytes_to_write, | 98 void FileImpl::Write(mojo::Array<uint8_t> bytes_to_write, |
121 int64_t offset, | 99 int64_t offset, |
122 Whence whence, | 100 Whence whence, |
123 const WriteCallback& callback) { | 101 const WriteCallback& callback) { |
124 DCHECK(!bytes_to_write.is_null()); | 102 DCHECK(!bytes_to_write.is_null()); |
125 if (!file_.IsValid()) { | 103 if (!file_.IsValid()) { |
126 callback.Run(GetError(file_), 0); | 104 callback.Run(GetError(file_), 0); |
127 return; | 105 return; |
128 } | 106 } |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 new_file.TakePlatformFile(), &mojo_handle); | 314 new_file.TakePlatformFile(), &mojo_handle); |
337 if (create_result != MOJO_RESULT_OK) { | 315 if (create_result != MOJO_RESULT_OK) { |
338 callback.Run(FileError::FAILED, ScopedHandle()); | 316 callback.Run(FileError::FAILED, ScopedHandle()); |
339 return; | 317 return; |
340 } | 318 } |
341 | 319 |
342 callback.Run(FileError::OK, ScopedHandle(mojo::Handle(mojo_handle))); | 320 callback.Run(FileError::OK, ScopedHandle(mojo::Handle(mojo_handle))); |
343 } | 321 } |
344 | 322 |
345 } // namespace filesystem | 323 } // namespace filesystem |
OLD | NEW |