file_operations.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2018 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 REMOTING_HOST_FILE_TRANSFER_FILE_OPERATIONS_H_
  5. #define REMOTING_HOST_FILE_TRANSFER_FILE_OPERATIONS_H_
  6. #include <cstddef>
  7. #include <cstdint>
  8. #include <memory>
  9. #include <vector>
  10. #include "base/callback.h"
  11. #include "remoting/protocol/file_transfer_helpers.h"
  12. namespace base {
  13. class FilePath;
  14. }
  15. namespace remoting {
  16. // Interface for reading and writing file transfers.
  17. class FileOperations {
  18. public:
  19. enum State {
  20. // The Reader/Writer has been newly created. No file is open, yet.
  21. kCreated = 0,
  22. // The file has been opened. WriteChunk(), ReadChunk(), and Close() can be
  23. // called.
  24. kReady = 1,
  25. // A file operation is currently being processed. WriteChunk(), ReadChunk(),
  26. // and Close() cannot be called until the state changes back to kReady.
  27. kBusy = 2,
  28. // EOF has been reached (when reading) or Close() has been called and
  29. // succeeded (when writing).
  30. kComplete = 3,
  31. // An error has occurred.
  32. kFailed = 4,
  33. };
  34. class Reader {
  35. public:
  36. using OpenResult = protocol::FileTransferResult<absl::monostate>;
  37. using OpenCallback = base::OnceCallback<void(OpenResult result)>;
  38. // On success, |result| will contain the read data, or an empty vector on
  39. // EOF. Once EOF is reached, the state will transition to kComplete and no
  40. // more operations may be performed. The reader will attempt to read as much
  41. // data as requested, but there may be cases where less data is returned.
  42. using ReadResult = protocol::FileTransferResult<std::vector<std::uint8_t>>;
  43. using ReadCallback = base::OnceCallback<void(ReadResult result)>;
  44. // Once destroyed, no further callbacks will be invoked.
  45. virtual ~Reader() = default;
  46. // Prompt the user to select a file and open it for reading.
  47. virtual void Open(OpenCallback callback) = 0;
  48. // Reads a chunk of the given size from the file.
  49. virtual void ReadChunk(std::size_t size, ReadCallback callback) = 0;
  50. virtual const base::FilePath& filename() const = 0;
  51. virtual std::uint64_t size() const = 0;
  52. virtual State state() const = 0;
  53. };
  54. class Writer {
  55. public:
  56. using Result = protocol::FileTransferResult<absl::monostate>;
  57. using Callback = base::OnceCallback<void(Result result)>;
  58. // Destructing before the file is completely written and closed will
  59. // automatically delete the partial file. If the Writer is destroyed after
  60. // calling Close but before the associated callback is invoked, the file may
  61. // either be complete or deleted, depending on the exact timing. In any
  62. // event, no further callbacks will be invoked once the object is destroyed.
  63. virtual ~Writer() = default;
  64. // Starts writing a new file to the default location. This will create a
  65. // temp file at the location, which will be renamed when writing is
  66. // complete.
  67. virtual void Open(const base::FilePath& filename, Callback callback) = 0;
  68. // Writes a chunk to the file. Chunks cannot be queued; the caller must
  69. // wait until callback is called before calling WriteChunk again or calling
  70. // Close.
  71. virtual void WriteChunk(std::vector<std::uint8_t> data,
  72. Callback callback) = 0;
  73. // Closes the file, flushing any data still in the OS buffer and moving the
  74. // the file to its final location.
  75. virtual void Close(Callback callback) = 0;
  76. virtual State state() const = 0;
  77. };
  78. FileOperations() = default;
  79. FileOperations(const FileOperations&) = delete;
  80. FileOperations& operator=(const FileOperations&) = delete;
  81. virtual ~FileOperations() = default;
  82. virtual std::unique_ptr<Reader> CreateReader() = 0;
  83. virtual std::unique_ptr<Writer> CreateWriter() = 0;
  84. };
  85. } // namespace remoting
  86. #endif // REMOTING_HOST_FILE_TRANSFER_FILE_OPERATIONS_H_