file_proxy.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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
  3. // found in the LICENSE file.
  4. #ifndef BASE_FILES_FILE_PROXY_H_
  5. #define BASE_FILES_FILE_PROXY_H_
  6. #include <stdint.h>
  7. #include "base/base_export.h"
  8. #include "base/callback_forward.h"
  9. #include "base/files/file.h"
  10. #include "base/files/file_path.h"
  11. #include "base/macros.h"
  12. #include "base/memory/ref_counted.h"
  13. #include "base/memory/weak_ptr.h"
  14. namespace base {
  15. class TaskRunner;
  16. class Time;
  17. // This class provides asynchronous access to a File. All methods follow the
  18. // same rules of the equivalent File method, as they are implemented by bouncing
  19. // the operation to File using a TaskRunner.
  20. //
  21. // This class performs automatic proxying to close the underlying file at
  22. // destruction.
  23. //
  24. // The TaskRunner is in charge of any sequencing of the operations, but a single
  25. // operation can be proxied at a time, regardless of the use of a callback.
  26. // In other words, having a sequence like
  27. //
  28. // proxy.Write(...);
  29. // proxy.Write(...);
  30. //
  31. // means the second Write will always fail.
  32. class BASE_EXPORT FileProxy : public SupportsWeakPtr<FileProxy> {
  33. public:
  34. // This callback is used by methods that report only an error code. It is
  35. // valid to pass a null callback to some functions that takes a
  36. // StatusCallback, in which case the operation will complete silently.
  37. using StatusCallback = OnceCallback<void(File::Error)>;
  38. using CreateTemporaryCallback =
  39. OnceCallback<void(File::Error, const FilePath&)>;
  40. using GetFileInfoCallback =
  41. OnceCallback<void(File::Error, const File::Info&)>;
  42. using ReadCallback =
  43. OnceCallback<void(File::Error, const char* data, int bytes_read)>;
  44. using WriteCallback = OnceCallback<void(File::Error, int bytes_written)>;
  45. FileProxy();
  46. explicit FileProxy(TaskRunner* task_runner);
  47. ~FileProxy();
  48. // Creates or opens a file with the given flags. It is invalid to pass a null
  49. // callback. If File::FLAG_CREATE is set in |file_flags| it always tries to
  50. // create a new file at the given |file_path| and fails if the file already
  51. // exists.
  52. //
  53. // This returns false if task posting to |task_runner| has failed.
  54. bool CreateOrOpen(const FilePath& file_path,
  55. uint32_t file_flags,
  56. StatusCallback callback);
  57. // Creates a temporary file for writing. The path and an open file are
  58. // returned. It is invalid to pass a null callback. The additional file flags
  59. // will be added on top of the default file flags which are:
  60. // File::FLAG_CREATE_ALWAYS
  61. // File::FLAG_WRITE
  62. // File::FLAG_TEMPORARY.
  63. //
  64. // This returns false if task posting to |task_runner| has failed.
  65. bool CreateTemporary(uint32_t additional_file_flags,
  66. CreateTemporaryCallback callback);
  67. // Returns true if the underlying |file_| is valid.
  68. bool IsValid() const;
  69. // Returns true if a new file was created (or an old one truncated to zero
  70. // length to simulate a new file), and false otherwise.
  71. bool created() const { return file_.created(); }
  72. // Claims ownership of |file|. It is an error to call this method when
  73. // IsValid() returns true.
  74. void SetFile(File file);
  75. File TakeFile();
  76. // Returns a new File object that is a duplicate of the underlying |file_|.
  77. // See the comment at File::Duplicate for caveats.
  78. File DuplicateFile();
  79. PlatformFile GetPlatformFile() const;
  80. // Proxies File::Close. The callback can be null.
  81. // This returns false if task posting to |task_runner| has failed.
  82. bool Close(StatusCallback callback);
  83. // Proxies File::GetInfo. The callback can't be null.
  84. // This returns false if task posting to |task_runner| has failed.
  85. bool GetInfo(GetFileInfoCallback callback);
  86. // Proxies File::Read. The callback can't be null.
  87. // This returns false if |bytes_to_read| is less than zero, or
  88. // if task posting to |task_runner| has failed.
  89. bool Read(int64_t offset, int bytes_to_read, ReadCallback callback);
  90. // Proxies File::Write. The callback can be null.
  91. // This returns false if |bytes_to_write| is less than or equal to zero,
  92. // if |buffer| is NULL, or if task posting to |task_runner| has failed.
  93. bool Write(int64_t offset,
  94. const char* buffer,
  95. int bytes_to_write,
  96. WriteCallback callback);
  97. // Proxies File::SetTimes. The callback can be null.
  98. // This returns false if task posting to |task_runner| has failed.
  99. bool SetTimes(Time last_access_time,
  100. Time last_modified_time,
  101. StatusCallback callback);
  102. // Proxies File::SetLength. The callback can be null.
  103. // This returns false if task posting to |task_runner| has failed.
  104. bool SetLength(int64_t length, StatusCallback callback);
  105. // Proxies File::Flush. The callback can be null.
  106. // This returns false if task posting to |task_runner| has failed.
  107. bool Flush(StatusCallback callback);
  108. private:
  109. friend class FileHelper;
  110. TaskRunner* task_runner() { return task_runner_.get(); }
  111. scoped_refptr<TaskRunner> task_runner_;
  112. File file_;
  113. DISALLOW_COPY_AND_ASSIGN(FileProxy);
  114. };
  115. } // namespace base
  116. #endif // BASE_FILES_FILE_PROXY_H_