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.
5 #ifndef WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_CONTEXT_H_
6 #define WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_CONTEXT_H_
8 #include "base/supports_user_data.h"
9 #include "base/threading/thread_checker.h"
10 #include "webkit/browser/fileapi/task_runner_bound_observer_list.h"
11 #include "webkit/quota/quota_types.h"
12 #include "webkit/storage/webkit_storage_export.h"
15 class SequencedTaskRunner
;
20 class FileSystemContext
;
22 // A context class which is carried around by FileSystemOperation and
23 // its delegated tasks. It is valid to reuse one context instance across
24 // multiple operations as far as those operations are supposed to share
25 // the same context (e.g. use the same task runner, share the quota etc).
26 // Note that the remaining quota bytes (allowed_bytes_growth) may be
27 // updated during the execution of write operations.
28 class WEBKIT_STORAGE_EXPORT_PRIVATE FileSystemOperationContext
29 : public base::SupportsUserData
{
31 explicit FileSystemOperationContext(FileSystemContext
* context
);
33 // Specifies |task_runner| which the operation is performed on.
34 FileSystemOperationContext(FileSystemContext
* context
,
35 base::SequencedTaskRunner
* task_runner
);
37 virtual ~FileSystemOperationContext();
39 FileSystemContext
* file_system_context() const {
40 return file_system_context_
;
43 // Updates the current remaining quota.
44 // This can be called to update the remaining quota during the operation.
45 void set_allowed_bytes_growth(const int64
& allowed_bytes_growth
) {
46 allowed_bytes_growth_
= allowed_bytes_growth
;
49 // Returns the current remaining quota.
50 int64
allowed_bytes_growth() const { return allowed_bytes_growth_
; }
52 quota::QuotaLimitType
quota_limit_type() const {
53 return quota_limit_type_
;
56 // Returns TaskRunner which the operation is performed on.
57 base::SequencedTaskRunner
* task_runner() const {
58 return task_runner_
.get();
61 ChangeObserverList
* change_observers() { return &change_observers_
; }
62 AccessObserverList
* access_observers() { return &access_observers_
; }
63 UpdateObserverList
* update_observers() { return &update_observers_
; }
65 // Following setters should be called only on the same thread as the
66 // FileSystemOperationContext is created (i.e. are not supposed be updated
67 // after the context's passed onto other task runners).
68 void set_change_observers(const ChangeObserverList
& list
) {
69 DCHECK(setter_thread_checker_
.CalledOnValidThread());
70 change_observers_
= list
;
72 void set_access_observers(const AccessObserverList
& list
) {
73 DCHECK(setter_thread_checker_
.CalledOnValidThread());
74 access_observers_
= list
;
76 void set_update_observers(const UpdateObserverList
& list
) {
77 DCHECK(setter_thread_checker_
.CalledOnValidThread());
78 update_observers_
= list
;
80 void set_quota_limit_type(quota::QuotaLimitType limit_type
) {
81 DCHECK(setter_thread_checker_
.CalledOnValidThread());
82 quota_limit_type_
= limit_type
;
85 // Gets and sets value-type (or not-owned) variable as UserData.
86 // (SetUserValue can be called only on the same thread as this context
87 // is created as well as other setters.)
88 template <typename T
> T
GetUserValue(const char* key
) const {
89 ValueAdapter
<T
>* v
= static_cast<ValueAdapter
<T
>*>(GetUserData(key
));
90 return v
? v
->value() : T();
92 template <typename T
> void SetUserValue(const char* key
, const T
& value
) {
93 DCHECK(setter_thread_checker_
.CalledOnValidThread());
94 SetUserData(key
, new ValueAdapter
<T
>(value
));
98 // An adapter for setting a value-type (or not owned) data as UserData.
99 template <typename T
> class ValueAdapter
100 : public base::SupportsUserData::Data
{
102 ValueAdapter(const T
& value
) : value_(value
) {}
103 const T
& value() const { return value_
; }
106 DISALLOW_COPY_AND_ASSIGN(ValueAdapter
);
109 FileSystemContext
* file_system_context_
;
110 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
112 int64 allowed_bytes_growth_
;
113 quota::QuotaLimitType quota_limit_type_
;
115 AccessObserverList access_observers_
;
116 ChangeObserverList change_observers_
;
117 UpdateObserverList update_observers_
;
119 // Used to check its setters are not called on arbitrary thread.
120 base::ThreadChecker setter_thread_checker_
;
122 DISALLOW_COPY_AND_ASSIGN(FileSystemOperationContext
);
125 } // namespace fileapi
127 #endif // WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_CONTEXT_H_