upload_file_element_reader.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright (c) 2012 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 NET_BASE_UPLOAD_FILE_ELEMENT_READER_H_
  5. #define NET_BASE_UPLOAD_FILE_ELEMENT_READER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include "base/compiler_specific.h"
  9. #include "base/files/file.h"
  10. #include "base/files/file_path.h"
  11. #include "base/gtest_prod_util.h"
  12. #include "base/memory/ref_counted.h"
  13. #include "base/memory/weak_ptr.h"
  14. #include "base/time/time.h"
  15. #include "net/base/net_export.h"
  16. #include "net/base/upload_element_reader.h"
  17. namespace base {
  18. class TaskRunner;
  19. }
  20. namespace net {
  21. class FileStream;
  22. // An UploadElementReader implementation for file.
  23. class NET_EXPORT UploadFileElementReader : public UploadElementReader {
  24. public:
  25. // |file| must be valid and opened for reading. On Windows, the file must have
  26. // been opened with File::FLAG_ASYNC, and elsewhere it must have ben opened
  27. // without it. |path| is never validated or used to re-open the file. It's
  28. // only used as the return value for path().
  29. // |task_runner| is used to perform file operations. It must not be NULL.
  30. //
  31. // TODO(mmenke): Remove |task_runner| argument, and use the ThreadPool
  32. // instead.
  33. UploadFileElementReader(base::TaskRunner* task_runner,
  34. base::File file,
  35. const base::FilePath& path,
  36. uint64_t range_offset,
  37. uint64_t range_length,
  38. const base::Time& expected_modification_time);
  39. // Same a above, but takes a FilePath instead.
  40. // TODO(mmenke): Remove if all consumers can be switched to the first
  41. // constructor.
  42. UploadFileElementReader(base::TaskRunner* task_runner,
  43. const base::FilePath& path,
  44. uint64_t range_offset,
  45. uint64_t range_length,
  46. const base::Time& expected_modification_time);
  47. UploadFileElementReader(const UploadFileElementReader&) = delete;
  48. UploadFileElementReader& operator=(const UploadFileElementReader&) = delete;
  49. ~UploadFileElementReader() override;
  50. const base::FilePath& path() const { return path_; }
  51. uint64_t range_offset() const { return range_offset_; }
  52. uint64_t range_length() const { return range_length_; }
  53. const base::Time& expected_modification_time() const {
  54. return expected_modification_time_;
  55. }
  56. // UploadElementReader overrides:
  57. const UploadFileElementReader* AsFileReader() const override;
  58. int Init(CompletionOnceCallback callback) override;
  59. uint64_t GetContentLength() const override;
  60. uint64_t BytesRemaining() const override;
  61. int Read(IOBuffer* buf,
  62. int buf_length,
  63. CompletionOnceCallback callback) override;
  64. private:
  65. enum class State {
  66. // No async operation is pending.
  67. IDLE,
  68. // The ordered sequence of events started by calling Init().
  69. // Opens file. State is skipped if file already open.
  70. OPEN,
  71. OPEN_COMPLETE,
  72. SEEK,
  73. GET_FILE_INFO,
  74. GET_FILE_INFO_COMPLETE,
  75. // There is no READ state as reads are always started immediately on Read().
  76. READ_COMPLETE,
  77. };
  78. FRIEND_TEST_ALL_PREFIXES(ElementsUploadDataStreamTest, FileSmallerThanLength);
  79. FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest,
  80. UploadFileSmallerThanLength);
  81. int DoLoop(int result);
  82. int DoOpen();
  83. int DoOpenComplete(int result);
  84. int DoSeek();
  85. int DoGetFileInfo(int result);
  86. int DoGetFileInfoComplete(int result);
  87. int DoReadComplete(int result);
  88. void OnIOComplete(int result);
  89. // Sets an value to override the result for GetContentLength().
  90. // Used for tests.
  91. struct NET_EXPORT_PRIVATE ScopedOverridingContentLengthForTests {
  92. explicit ScopedOverridingContentLengthForTests(uint64_t value);
  93. ~ScopedOverridingContentLengthForTests();
  94. };
  95. scoped_refptr<base::TaskRunner> task_runner_;
  96. const base::FilePath path_;
  97. const uint64_t range_offset_;
  98. const uint64_t range_length_;
  99. const base::Time expected_modification_time_;
  100. std::unique_ptr<FileStream> file_stream_;
  101. uint64_t content_length_ = 0;
  102. uint64_t bytes_remaining_ = 0;
  103. // File information. Only valid during GET_FILE_INFO_COMPLETE state.
  104. base::File::Info file_info_;
  105. State next_state_ = State::IDLE;
  106. CompletionOnceCallback pending_callback_;
  107. // True if Init() was called while an async operation was in progress.
  108. bool init_called_while_operation_pending_ = false;
  109. base::WeakPtrFactory<UploadFileElementReader> weak_ptr_factory_{this};
  110. };
  111. } // namespace net
  112. #endif // NET_BASE_UPLOAD_FILE_ELEMENT_READER_H_