sandbox_file_stream_writer.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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_WRITER_H_
  5. #define STORAGE_BROWSER_FILE_SYSTEM_SANDBOX_FILE_STREAM_WRITER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include "base/component_export.h"
  9. #include "base/files/file.h"
  10. #include "base/files/file_path.h"
  11. #include "storage/browser/blob/shareable_file_reference.h"
  12. #include "storage/browser/file_system/file_stream_writer.h"
  13. #include "storage/browser/file_system/file_system_url.h"
  14. #include "storage/browser/file_system/task_runner_bound_observer_list.h"
  15. #include "storage/common/file_system/file_system_types.h"
  16. #include "third_party/blink/public/mojom/quota/quota_types.mojom.h"
  17. #include "url/gurl.h"
  18. namespace storage {
  19. class FileSystemContext;
  20. class FileStreamWriter;
  21. class COMPONENT_EXPORT(STORAGE_BROWSER) SandboxFileStreamWriter
  22. : public FileStreamWriter {
  23. public:
  24. SandboxFileStreamWriter(FileSystemContext* file_system_context,
  25. const FileSystemURL& url,
  26. int64_t initial_offset,
  27. const UpdateObserverList& observers);
  28. SandboxFileStreamWriter(const SandboxFileStreamWriter&) = delete;
  29. SandboxFileStreamWriter& operator=(const SandboxFileStreamWriter&) = delete;
  30. ~SandboxFileStreamWriter() override;
  31. // FileStreamWriter overrides.
  32. int Write(net::IOBuffer* buf,
  33. int buf_len,
  34. net::CompletionOnceCallback callback) override;
  35. int Cancel(net::CompletionOnceCallback callback) override;
  36. int Flush(net::CompletionOnceCallback callback) override;
  37. // Used only by tests.
  38. void set_default_quota(int64_t quota) { default_quota_ = quota; }
  39. private:
  40. // Performs quota calculation and calls file_writer_->Write().
  41. // Will either return synchronously, or run asynchronously and call
  42. // |write_callback_|.
  43. int WriteInternal(net::IOBuffer* buf, int buf_len);
  44. // Callbacks that are chained for the first write. This eventually calls
  45. // WriteInternal.
  46. void DidCreateSnapshotFile(net::CompletionOnceCallback callback,
  47. base::File::Error file_error,
  48. const base::File::Info& file_info,
  49. const base::FilePath& platform_path,
  50. scoped_refptr<ShareableFileReference> file_ref);
  51. void DidGetUsageAndQuota(net::CompletionOnceCallback callback,
  52. blink::mojom::QuotaStatusCode status,
  53. int64_t usage,
  54. int64_t quota);
  55. void DidInitializeForWrite(net::IOBuffer* buf, int buf_len, int init_status);
  56. // Will call |write_callback_| if set, or return synchronously.
  57. void DidWrite(int write_response);
  58. void DidFlush(net::CompletionOnceCallback callback, int result);
  59. // Stops the in-flight operation, calls |cancel_callback_| and returns true
  60. // if there's a pending cancel request.
  61. bool CancelIfRequested();
  62. scoped_refptr<FileSystemContext> file_system_context_;
  63. FileSystemURL url_;
  64. int64_t initial_offset_;
  65. std::unique_ptr<FileStreamWriter> file_writer_;
  66. net::CompletionOnceCallback write_callback_;
  67. net::CompletionOnceCallback cancel_callback_;
  68. UpdateObserverList observers_;
  69. base::FilePath file_path_;
  70. int64_t file_size_;
  71. int64_t total_bytes_written_;
  72. int64_t allowed_bytes_to_write_;
  73. bool has_pending_operation_;
  74. int64_t default_quota_;
  75. base::WeakPtrFactory<SandboxFileStreamWriter> weak_factory_{this};
  76. };
  77. } // namespace storage
  78. #endif // STORAGE_BROWSER_FILE_SYSTEM_SANDBOX_FILE_STREAM_WRITER_H_