OLD | NEW |
| (Empty) |
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 | |
5 #ifndef WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_CONTEXT_H_ | |
6 #define WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_CONTEXT_H_ | |
7 | |
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" | |
13 | |
14 namespace base { | |
15 class SequencedTaskRunner; | |
16 } | |
17 | |
18 namespace fileapi { | |
19 | |
20 class FileSystemContext; | |
21 | |
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 { | |
30 public: | |
31 explicit FileSystemOperationContext(FileSystemContext* context); | |
32 | |
33 // Specifies |task_runner| which the operation is performed on. | |
34 FileSystemOperationContext(FileSystemContext* context, | |
35 base::SequencedTaskRunner* task_runner); | |
36 | |
37 virtual ~FileSystemOperationContext(); | |
38 | |
39 FileSystemContext* file_system_context() const { | |
40 return file_system_context_; | |
41 } | |
42 | |
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; | |
47 } | |
48 | |
49 // Returns the current remaining quota. | |
50 int64 allowed_bytes_growth() const { return allowed_bytes_growth_; } | |
51 | |
52 quota::QuotaLimitType quota_limit_type() const { | |
53 return quota_limit_type_; | |
54 } | |
55 | |
56 // Returns TaskRunner which the operation is performed on. | |
57 base::SequencedTaskRunner* task_runner() const { | |
58 return task_runner_.get(); | |
59 } | |
60 | |
61 ChangeObserverList* change_observers() { return &change_observers_; } | |
62 AccessObserverList* access_observers() { return &access_observers_; } | |
63 UpdateObserverList* update_observers() { return &update_observers_; } | |
64 | |
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; | |
71 } | |
72 void set_access_observers(const AccessObserverList& list) { | |
73 DCHECK(setter_thread_checker_.CalledOnValidThread()); | |
74 access_observers_ = list; | |
75 } | |
76 void set_update_observers(const UpdateObserverList& list) { | |
77 DCHECK(setter_thread_checker_.CalledOnValidThread()); | |
78 update_observers_ = list; | |
79 } | |
80 void set_quota_limit_type(quota::QuotaLimitType limit_type) { | |
81 DCHECK(setter_thread_checker_.CalledOnValidThread()); | |
82 quota_limit_type_ = limit_type; | |
83 } | |
84 | |
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(); | |
91 } | |
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)); | |
95 } | |
96 | |
97 private: | |
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 { | |
101 public: | |
102 ValueAdapter(const T& value) : value_(value) {} | |
103 const T& value() const { return value_; } | |
104 private: | |
105 T value_; | |
106 DISALLOW_COPY_AND_ASSIGN(ValueAdapter); | |
107 }; | |
108 | |
109 FileSystemContext* file_system_context_; | |
110 scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
111 | |
112 int64 allowed_bytes_growth_; | |
113 quota::QuotaLimitType quota_limit_type_; | |
114 | |
115 AccessObserverList access_observers_; | |
116 ChangeObserverList change_observers_; | |
117 UpdateObserverList update_observers_; | |
118 | |
119 // Used to check its setters are not called on arbitrary thread. | |
120 base::ThreadChecker setter_thread_checker_; | |
121 | |
122 DISALLOW_COPY_AND_ASSIGN(FileSystemOperationContext); | |
123 }; | |
124 | |
125 } // namespace fileapi | |
126 | |
127 #endif // WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_CONTEXT_H_ | |
OLD | NEW |