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> |
11 | 11 |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/files/scoped_file.h" | 13 #include "base/files/scoped_file.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
16 #include "components/filesystem/lock_table.h" | 16 #include "components/filesystem/lock_table.h" |
17 #include "components/filesystem/shared_temp_dir.h" | 17 #include "components/filesystem/shared_temp_dir.h" |
18 #include "components/filesystem/util.h" | 18 #include "components/filesystem/util.h" |
19 #include "mojo/common/common_type_converters.h" | 19 #include "mojo/common/common_type_converters.h" |
| 20 #include "mojo/public/cpp/bindings/strong_binding.h" |
20 #include "mojo/public/cpp/system/platform_handle.h" | 21 #include "mojo/public/cpp/system/platform_handle.h" |
21 | 22 |
22 static_assert(sizeof(off_t) <= sizeof(int64_t), "off_t too big"); | 23 static_assert(sizeof(off_t) <= sizeof(int64_t), "off_t too big"); |
23 static_assert(sizeof(size_t) >= sizeof(uint32_t), "size_t too small"); | 24 static_assert(sizeof(size_t) >= sizeof(uint32_t), "size_t too small"); |
24 | 25 |
25 using base::Time; | 26 using base::Time; |
26 using mojo::ScopedHandle; | 27 using mojo::ScopedHandle; |
27 | 28 |
28 namespace filesystem { | 29 namespace filesystem { |
29 namespace { | 30 namespace { |
30 | 31 |
31 const size_t kMaxReadSize = 1 * 1024 * 1024; // 1 MB. | 32 const size_t kMaxReadSize = 1 * 1024 * 1024; // 1 MB. |
32 | 33 |
33 } // namespace | 34 } // namespace |
34 | 35 |
35 FileImpl::FileImpl(mojo::InterfaceRequest<mojom::File> request, | 36 FileImpl::FileImpl(const base::FilePath& path, |
36 const base::FilePath& path, | |
37 uint32_t flags, | 37 uint32_t flags, |
38 scoped_refptr<SharedTempDir> temp_dir, | 38 scoped_refptr<SharedTempDir> temp_dir, |
39 scoped_refptr<LockTable> lock_table) | 39 scoped_refptr<LockTable> lock_table) |
40 : binding_(this, std::move(request)), | 40 : file_(path, flags), |
41 file_(path, flags), | |
42 path_(path), | 41 path_(path), |
43 temp_dir_(std::move(temp_dir)), | 42 temp_dir_(std::move(temp_dir)), |
44 lock_table_(std::move(lock_table)) { | 43 lock_table_(std::move(lock_table)) { |
45 DCHECK(file_.IsValid()); | 44 DCHECK(file_.IsValid()); |
46 } | 45 } |
47 | 46 |
48 FileImpl::FileImpl(mojo::InterfaceRequest<mojom::File> request, | 47 FileImpl::FileImpl(const base::FilePath& path, |
49 const base::FilePath& path, | |
50 base::File file, | 48 base::File file, |
51 scoped_refptr<SharedTempDir> temp_dir, | 49 scoped_refptr<SharedTempDir> temp_dir, |
52 scoped_refptr<LockTable> lock_table) | 50 scoped_refptr<LockTable> lock_table) |
53 : binding_(this, std::move(request)), | 51 : file_(std::move(file)), |
54 file_(std::move(file)), | |
55 path_(path), | 52 path_(path), |
56 temp_dir_(std::move(temp_dir)), | 53 temp_dir_(std::move(temp_dir)), |
57 lock_table_(std::move(lock_table)) { | 54 lock_table_(std::move(lock_table)) { |
58 DCHECK(file_.IsValid()); | 55 DCHECK(file_.IsValid()); |
59 } | 56 } |
60 | 57 |
61 FileImpl::~FileImpl() { | 58 FileImpl::~FileImpl() { |
62 if (file_.IsValid()) | 59 if (file_.IsValid()) |
63 lock_table_->RemoveFromLockTable(path_); | 60 lock_table_->RemoveFromLockTable(path_); |
64 } | 61 } |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 | 278 |
282 base_mtime = info.last_modified; | 279 base_mtime = info.last_modified; |
283 } else if (!mtime->now) { | 280 } else if (!mtime->now) { |
284 base_mtime = Time::FromDoubleT(mtime->seconds); | 281 base_mtime = Time::FromDoubleT(mtime->seconds); |
285 } | 282 } |
286 | 283 |
287 file_.SetTimes(base_atime, base_mtime); | 284 file_.SetTimes(base_atime, base_mtime); |
288 callback.Run(mojom::FileError::OK); | 285 callback.Run(mojom::FileError::OK); |
289 } | 286 } |
290 | 287 |
291 void FileImpl::Dup(mojo::InterfaceRequest<mojom::File> file, | 288 void FileImpl::Dup(mojom::FileRequest file, const DupCallback& callback) { |
292 const DupCallback& callback) { | |
293 if (!file_.IsValid()) { | 289 if (!file_.IsValid()) { |
294 callback.Run(GetError(file_)); | 290 callback.Run(GetError(file_)); |
295 return; | 291 return; |
296 } | 292 } |
297 | 293 |
298 base::File new_file = file_.Duplicate(); | 294 base::File new_file = file_.Duplicate(); |
299 if (!new_file.IsValid()) { | 295 if (!new_file.IsValid()) { |
300 callback.Run(GetError(new_file)); | 296 callback.Run(GetError(new_file)); |
301 return; | 297 return; |
302 } | 298 } |
303 | 299 |
304 if (file.is_pending()) | 300 if (file.is_pending()) { |
305 new FileImpl(std::move(file), path_, std::move(new_file), temp_dir_, | 301 mojo::MakeStrongBinding( |
306 lock_table_); | 302 base::MakeUnique<FileImpl>(path_, std::move(new_file), temp_dir_, |
| 303 lock_table_), |
| 304 std::move(file)); |
| 305 } |
307 callback.Run(mojom::FileError::OK); | 306 callback.Run(mojom::FileError::OK); |
308 } | 307 } |
309 | 308 |
310 void FileImpl::Flush(const FlushCallback& callback) { | 309 void FileImpl::Flush(const FlushCallback& callback) { |
311 if (!file_.IsValid()) { | 310 if (!file_.IsValid()) { |
312 callback.Run(GetError(file_)); | 311 callback.Run(GetError(file_)); |
313 return; | 312 return; |
314 } | 313 } |
315 | 314 |
316 bool ret = file_.Flush(); | 315 bool ret = file_.Flush(); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 if (info.is_directory) { | 351 if (info.is_directory) { |
353 callback.Run(mojom::FileError::NOT_A_FILE, ScopedHandle()); | 352 callback.Run(mojom::FileError::NOT_A_FILE, ScopedHandle()); |
354 return; | 353 return; |
355 } | 354 } |
356 | 355 |
357 callback.Run(mojom::FileError::OK, | 356 callback.Run(mojom::FileError::OK, |
358 mojo::WrapPlatformFile(new_file.TakePlatformFile())); | 357 mojo::WrapPlatformFile(new_file.TakePlatformFile())); |
359 } | 358 } |
360 | 359 |
361 } // namespace filesystem | 360 } // namespace filesystem |
OLD | NEW |