| 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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 file_.Close(); | 82 file_.Close(); |
| 83 callback.Run(mojom::FileError::OK); | 83 callback.Run(mojom::FileError::OK); |
| 84 } | 84 } |
| 85 | 85 |
| 86 // TODO(vtl): Move the implementation to a thread pool. | 86 // TODO(vtl): Move the implementation to a thread pool. |
| 87 void FileImpl::Read(uint32_t num_bytes_to_read, | 87 void FileImpl::Read(uint32_t num_bytes_to_read, |
| 88 int64_t offset, | 88 int64_t offset, |
| 89 mojom::Whence whence, | 89 mojom::Whence whence, |
| 90 const ReadCallback& callback) { | 90 const ReadCallback& callback) { |
| 91 if (!file_.IsValid()) { | 91 if (!file_.IsValid()) { |
| 92 callback.Run(GetError(file_), mojo::Array<uint8_t>()); | 92 callback.Run(GetError(file_), base::nullopt); |
| 93 return; | 93 return; |
| 94 } | 94 } |
| 95 if (num_bytes_to_read > kMaxReadSize) { | 95 if (num_bytes_to_read > kMaxReadSize) { |
| 96 callback.Run(mojom::FileError::INVALID_OPERATION, mojo::Array<uint8_t>()); | 96 callback.Run(mojom::FileError::INVALID_OPERATION, base::nullopt); |
| 97 return; | 97 return; |
| 98 } | 98 } |
| 99 mojom::FileError error = IsOffsetValid(offset); | 99 mojom::FileError error = IsOffsetValid(offset); |
| 100 if (error != mojom::FileError::OK) { | 100 if (error != mojom::FileError::OK) { |
| 101 callback.Run(error, mojo::Array<uint8_t>()); | 101 callback.Run(error, base::nullopt); |
| 102 return; | 102 return; |
| 103 } | 103 } |
| 104 error = IsWhenceValid(whence); | 104 error = IsWhenceValid(whence); |
| 105 if (error != mojom::FileError::OK) { | 105 if (error != mojom::FileError::OK) { |
| 106 callback.Run(error, mojo::Array<uint8_t>()); | 106 callback.Run(error, base::nullopt); |
| 107 return; | 107 return; |
| 108 } | 108 } |
| 109 | 109 |
| 110 if (file_.Seek(static_cast<base::File::Whence>(whence), offset) == -1) { | 110 if (file_.Seek(static_cast<base::File::Whence>(whence), offset) == -1) { |
| 111 callback.Run(mojom::FileError::FAILED, mojo::Array<uint8_t>()); | 111 callback.Run(mojom::FileError::FAILED, base::nullopt); |
| 112 return; | 112 return; |
| 113 } | 113 } |
| 114 | 114 |
| 115 mojo::Array<uint8_t> bytes_read(num_bytes_to_read); | 115 std::vector<uint8_t> bytes_read(num_bytes_to_read); |
| 116 int num_bytes_read = file_.ReadAtCurrentPos( | 116 int num_bytes_read = file_.ReadAtCurrentPos( |
| 117 reinterpret_cast<char*>(&bytes_read.front()), num_bytes_to_read); | 117 reinterpret_cast<char*>(&bytes_read.front()), num_bytes_to_read); |
| 118 if (num_bytes_read < 0) { | 118 if (num_bytes_read < 0) { |
| 119 callback.Run(mojom::FileError::FAILED, mojo::Array<uint8_t>()); | 119 callback.Run(mojom::FileError::FAILED, base::nullopt); |
| 120 return; | 120 return; |
| 121 } | 121 } |
| 122 | 122 |
| 123 DCHECK_LE(static_cast<size_t>(num_bytes_read), num_bytes_to_read); | 123 DCHECK_LE(static_cast<size_t>(num_bytes_read), num_bytes_to_read); |
| 124 bytes_read.resize(static_cast<size_t>(num_bytes_read)); | 124 bytes_read.resize(static_cast<size_t>(num_bytes_read)); |
| 125 callback.Run(mojom::FileError::OK, std::move(bytes_read)); | 125 callback.Run(mojom::FileError::OK, std::move(bytes_read)); |
| 126 } | 126 } |
| 127 | 127 |
| 128 // TODO(vtl): Move the implementation to a thread pool. | 128 // TODO(vtl): Move the implementation to a thread pool. |
| 129 void FileImpl::Write(mojo::Array<uint8_t> bytes_to_write, | 129 void FileImpl::Write(const std::vector<uint8_t>& bytes_to_write, |
| 130 int64_t offset, | 130 int64_t offset, |
| 131 mojom::Whence whence, | 131 mojom::Whence whence, |
| 132 const WriteCallback& callback) { | 132 const WriteCallback& callback) { |
| 133 DCHECK(!bytes_to_write.is_null()); | |
| 134 if (!file_.IsValid()) { | 133 if (!file_.IsValid()) { |
| 135 callback.Run(GetError(file_), 0); | 134 callback.Run(GetError(file_), 0); |
| 136 return; | 135 return; |
| 137 } | 136 } |
| 138 // Who knows what |write()| would return if the size is that big (and it | 137 // Who knows what |write()| would return if the size is that big (and it |
| 139 // actually wrote that much). | 138 // actually wrote that much). |
| 140 if (bytes_to_write.size() > | 139 if (bytes_to_write.size() > |
| 141 #if defined(OS_WIN) | 140 #if defined(OS_WIN) |
| 142 static_cast<size_t>(std::numeric_limits<int>::max())) { | 141 static_cast<size_t>(std::numeric_limits<int>::max())) { |
| 143 #else | 142 #else |
| (...skipping 12 matching lines...) Expand all Loading... |
| 156 callback.Run(error, 0); | 155 callback.Run(error, 0); |
| 157 return; | 156 return; |
| 158 } | 157 } |
| 159 | 158 |
| 160 if (file_.Seek(static_cast<base::File::Whence>(whence), offset) == -1) { | 159 if (file_.Seek(static_cast<base::File::Whence>(whence), offset) == -1) { |
| 161 callback.Run(mojom::FileError::FAILED, 0); | 160 callback.Run(mojom::FileError::FAILED, 0); |
| 162 return; | 161 return; |
| 163 } | 162 } |
| 164 | 163 |
| 165 const char* buf = (bytes_to_write.size() > 0) | 164 const char* buf = (bytes_to_write.size() > 0) |
| 166 ? reinterpret_cast<char*>(&bytes_to_write.front()) | 165 ? reinterpret_cast<const char*>(&bytes_to_write.front()) |
| 167 : nullptr; | 166 : nullptr; |
| 168 int num_bytes_written = file_.WriteAtCurrentPos( | 167 int num_bytes_written = file_.WriteAtCurrentPos( |
| 169 buf, static_cast<int>(bytes_to_write.size())); | 168 buf, static_cast<int>(bytes_to_write.size())); |
| 170 if (num_bytes_written < 0) { | 169 if (num_bytes_written < 0) { |
| 171 callback.Run(mojom::FileError::FAILED, 0); | 170 callback.Run(mojom::FileError::FAILED, 0); |
| 172 return; | 171 return; |
| 173 } | 172 } |
| 174 | 173 |
| 175 DCHECK_LE(static_cast<size_t>(num_bytes_written), | 174 DCHECK_LE(static_cast<size_t>(num_bytes_written), |
| 176 std::numeric_limits<uint32_t>::max()); | 175 std::numeric_limits<uint32_t>::max()); |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 if (info.is_directory) { | 350 if (info.is_directory) { |
| 352 callback.Run(mojom::FileError::NOT_A_FILE, ScopedHandle()); | 351 callback.Run(mojom::FileError::NOT_A_FILE, ScopedHandle()); |
| 353 return; | 352 return; |
| 354 } | 353 } |
| 355 | 354 |
| 356 callback.Run(mojom::FileError::OK, | 355 callback.Run(mojom::FileError::OK, |
| 357 mojo::WrapPlatformFile(new_file.TakePlatformFile())); | 356 mojo::WrapPlatformFile(new_file.TakePlatformFile())); |
| 358 } | 357 } |
| 359 | 358 |
| 360 } // namespace filesystem | 359 } // namespace filesystem |
| OLD | NEW |