sandbox_file_stream_reader.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 STORAGE_BROWSER_FILE_SYSTEM_SANDBOX_FILE_STREAM_READER_H_
  5. #define STORAGE_BROWSER_FILE_SYSTEM_SANDBOX_FILE_STREAM_READER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include "base/bind.h"
  9. #include "base/component_export.h"
  10. #include "base/files/file.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/memory/ref_counted.h"
  13. #include "base/time/time.h"
  14. #include "net/base/completion_once_callback.h"
  15. #include "storage/browser/blob/shareable_file_reference.h"
  16. #include "storage/browser/file_system/file_stream_reader.h"
  17. #include "storage/browser/file_system/file_system_url.h"
  18. namespace base {
  19. class FilePath;
  20. }
  21. namespace storage {
  22. class FileSystemContext;
  23. // FileStreamReader implementation for Sandboxed FileSystem files.
  24. // This wraps either a LocalFileStreamReader or a MemoryFileStreamReader,
  25. // depending on if we're in an incognito profile or not.
  26. class COMPONENT_EXPORT(STORAGE_BROWSER) SandboxFileStreamReader
  27. : public FileStreamReader {
  28. public:
  29. // Creates a new reader for a filesystem URL |url| from |initial_offset|.
  30. // |expected_modification_time| specifies the expected last modification. if
  31. // the value is non-null, the reader will check the underlying file's actual
  32. // modification time to see if the file has been modified, and if it does any
  33. // succeeding read operations should fail with ERR_UPLOAD_FILE_CHANGED error.
  34. SandboxFileStreamReader(FileSystemContext* file_system_context,
  35. const FileSystemURL& url,
  36. int64_t initial_offset,
  37. const base::Time& expected_modification_time);
  38. SandboxFileStreamReader(const SandboxFileStreamReader&) = delete;
  39. SandboxFileStreamReader& operator=(const SandboxFileStreamReader&) = delete;
  40. ~SandboxFileStreamReader() override;
  41. // FileStreamReader overrides.
  42. int Read(net::IOBuffer* buf,
  43. int buf_len,
  44. net::CompletionOnceCallback callback) override;
  45. int64_t GetLength(net::Int64CompletionOnceCallback callback) override;
  46. private:
  47. friend class FileStreamReader;
  48. int CreateSnapshot();
  49. void DidCreateSnapshot(base::File::Error file_error,
  50. const base::File::Info& file_info,
  51. const base::FilePath& platform_path,
  52. scoped_refptr<ShareableFileReference> file_ref);
  53. void OnRead(int rv);
  54. void OnGetLength(int64_t rv);
  55. raw_ptr<net::IOBuffer, DanglingUntriaged> read_buf_;
  56. int read_buf_len_;
  57. net::CompletionOnceCallback read_callback_;
  58. net::Int64CompletionOnceCallback get_length_callback_;
  59. scoped_refptr<FileSystemContext> file_system_context_;
  60. FileSystemURL url_;
  61. const int64_t initial_offset_;
  62. const base::Time expected_modification_time_;
  63. std::unique_ptr<FileStreamReader> file_reader_;
  64. scoped_refptr<ShareableFileReference> snapshot_ref_;
  65. bool has_pending_create_snapshot_;
  66. base::WeakPtrFactory<SandboxFileStreamReader> weak_factory_{this};
  67. };
  68. } // namespace storage
  69. #endif // STORAGE_BROWSER_FILE_SYSTEM_SANDBOX_FILE_STREAM_READER_H_