memory_file_stream_reader.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2019 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 STORAGE_BROWSER_FILE_SYSTEM_MEMORY_FILE_STREAM_READER_H_
  5. #define STORAGE_BROWSER_FILE_SYSTEM_MEMORY_FILE_STREAM_READER_H_
  6. #include "base/component_export.h"
  7. #include "base/files/file.h"
  8. #include "base/files/file_path.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/time/time.h"
  11. #include "net/base/completion_once_callback.h"
  12. #include "storage/browser/file_system/file_stream_reader.h"
  13. #include "storage/browser/file_system/obfuscated_file_util_memory_delegate.h"
  14. namespace storage {
  15. // A stream reader for memory files.
  16. class COMPONENT_EXPORT(STORAGE_BROWSER) MemoryFileStreamReader
  17. : public FileStreamReader {
  18. public:
  19. // Creates a new FileReader for a memory file |file_path|.
  20. // |initial_offset| specifies the offset in the file where the first read
  21. // should start. If the given offset is out of the file range any
  22. // read operation may error out with net::ERR_REQUEST_RANGE_NOT_SATISFIABLE.
  23. // |expected_modification_time| specifies the expected last modification
  24. // If the value is non-null, the reader will check the underlying file's
  25. // actual modification time to see if the file has been modified, and if
  26. // it does any succeeding read operations should fail with
  27. // ERR_UPLOAD_FILE_CHANGED error.
  28. MemoryFileStreamReader(
  29. scoped_refptr<base::TaskRunner> task_runner,
  30. base::WeakPtr<ObfuscatedFileUtilMemoryDelegate> memory_file_util,
  31. const base::FilePath& file_path,
  32. int64_t initial_offset,
  33. const base::Time& expected_modification_time);
  34. MemoryFileStreamReader(const MemoryFileStreamReader&) = delete;
  35. MemoryFileStreamReader& operator=(const MemoryFileStreamReader&) = delete;
  36. ~MemoryFileStreamReader() override;
  37. // FileStreamReader overrides.
  38. int Read(net::IOBuffer* buf,
  39. int buf_len,
  40. net::CompletionOnceCallback callback) override;
  41. int64_t GetLength(net::Int64CompletionOnceCallback callback) override;
  42. private:
  43. void OnReadCompleted(net::CompletionOnceCallback callback, int result);
  44. void OnGetLengthCompleted(net::Int64CompletionOnceCallback callback,
  45. int64_t result);
  46. base::WeakPtr<ObfuscatedFileUtilMemoryDelegate> memory_file_util_;
  47. const scoped_refptr<base::TaskRunner> task_runner_;
  48. const base::FilePath file_path_;
  49. const base::Time expected_modification_time_;
  50. int64_t offset_;
  51. base::WeakPtrFactory<MemoryFileStreamReader> weak_factory_{this};
  52. };
  53. } // namespace storage
  54. #endif // STORAGE_BROWSER_FILE_SYSTEM_MEMORY_FILE_STREAM_READER_H_