file_system_operation_context.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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_FILE_SYSTEM_OPERATION_CONTEXT_H_
  5. #define STORAGE_BROWSER_FILE_SYSTEM_FILE_SYSTEM_OPERATION_CONTEXT_H_
  6. #include <stdint.h>
  7. #include "base/component_export.h"
  8. #include "base/supports_user_data.h"
  9. #include "base/threading/thread_checker.h"
  10. #include "storage/browser/file_system/task_runner_bound_observer_list.h"
  11. namespace base {
  12. class SequencedTaskRunner;
  13. }
  14. namespace storage {
  15. class FileSystemContext;
  16. enum class QuotaLimitType;
  17. // A context class which is carried around by FileSystemOperation and
  18. // its delegated tasks. It is valid to reuse one context instance across
  19. // multiple operations as far as those operations are supposed to share
  20. // the same context (e.g. use the same task runner, share the quota etc).
  21. // Note that the remaining quota bytes (allowed_bytes_growth) may be
  22. // updated during the execution of write operations.
  23. class COMPONENT_EXPORT(STORAGE_BROWSER) FileSystemOperationContext
  24. : public base::SupportsUserData {
  25. public:
  26. explicit FileSystemOperationContext(FileSystemContext* context);
  27. // Specifies |task_runner| which the operation is performed on.
  28. // The backend of |task_runner| must outlive the IO thread.
  29. FileSystemOperationContext(FileSystemContext* context,
  30. base::SequencedTaskRunner* task_runner);
  31. FileSystemOperationContext(const FileSystemOperationContext&) = delete;
  32. FileSystemOperationContext& operator=(const FileSystemOperationContext&) =
  33. delete;
  34. ~FileSystemOperationContext() override;
  35. FileSystemContext* file_system_context() const {
  36. return file_system_context_.get();
  37. }
  38. // Updates the current remaining quota.
  39. // This can be called to update the remaining quota during the operation.
  40. void set_allowed_bytes_growth(const int64_t& allowed_bytes_growth) {
  41. allowed_bytes_growth_ = allowed_bytes_growth;
  42. }
  43. // Returns the current remaining quota.
  44. int64_t allowed_bytes_growth() const { return allowed_bytes_growth_; }
  45. QuotaLimitType quota_limit_type() const { return quota_limit_type_; }
  46. base::SequencedTaskRunner* task_runner() const { return task_runner_.get(); }
  47. ChangeObserverList* change_observers() { return &change_observers_; }
  48. UpdateObserverList* update_observers() { return &update_observers_; }
  49. // Following setters should be called only on the same thread as the
  50. // FileSystemOperationContext is created (i.e. are not supposed be updated
  51. // after the context's passed onto other task runners).
  52. void set_change_observers(const ChangeObserverList& list) {
  53. DCHECK_CALLED_ON_VALID_THREAD(setter_thread_checker_);
  54. change_observers_ = list;
  55. }
  56. void set_update_observers(const UpdateObserverList& list) {
  57. DCHECK_CALLED_ON_VALID_THREAD(setter_thread_checker_);
  58. update_observers_ = list;
  59. }
  60. void set_quota_limit_type(QuotaLimitType limit_type) {
  61. DCHECK_CALLED_ON_VALID_THREAD(setter_thread_checker_);
  62. quota_limit_type_ = limit_type;
  63. }
  64. private:
  65. scoped_refptr<FileSystemContext> file_system_context_;
  66. scoped_refptr<base::SequencedTaskRunner> task_runner_;
  67. // The current remaining quota, used by ObfuscatedFileUtil.
  68. int64_t allowed_bytes_growth_;
  69. // The current quota limit type, used by ObfuscatedFileUtil.
  70. QuotaLimitType quota_limit_type_;
  71. // Observers attached to this context.
  72. ChangeObserverList change_observers_;
  73. UpdateObserverList update_observers_;
  74. // Used to check its setters are not called on arbitrary thread.
  75. THREAD_CHECKER(setter_thread_checker_);
  76. };
  77. } // namespace storage
  78. #endif // STORAGE_BROWSER_FILE_SYSTEM_FILE_SYSTEM_OPERATION_CONTEXT_H_