OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/settings/settings_api.h" | 5 #include "chrome/browser/extensions/settings/settings_api.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/extensions/extensions_quota_service.h" |
| 11 #include "chrome/browser/extensions/settings/settings_api.h" |
10 #include "chrome/browser/extensions/settings/settings_frontend.h" | 12 #include "chrome/browser/extensions/settings/settings_frontend.h" |
11 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
12 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
13 | 15 |
14 namespace extensions { | 16 namespace extensions { |
15 | 17 |
16 using content::BrowserThread; | 18 using content::BrowserThread; |
17 | 19 |
18 namespace { | 20 namespace { |
19 const char* kUnsupportedArgumentType = "Unsupported argument type"; | 21 const char* kUnsupportedArgumentType = "Unsupported argument type"; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 &SettingsObserver::OnSettingsChanged, | 72 &SettingsObserver::OnSettingsChanged, |
71 extension_id(), | 73 extension_id(), |
72 SettingChange::GetEventJson(result.changes())); | 74 SettingChange::GetEventJson(result.changes())); |
73 } | 75 } |
74 | 76 |
75 return true; | 77 return true; |
76 } | 78 } |
77 | 79 |
78 // Concrete settings functions | 80 // Concrete settings functions |
79 | 81 |
| 82 namespace { |
| 83 |
80 // Adds all StringValues from a ListValue to a vector of strings. | 84 // Adds all StringValues from a ListValue to a vector of strings. |
81 static void AddAllStringValues( | 85 void AddAllStringValues(const ListValue& from, std::vector<std::string>* to) { |
82 const ListValue& from, std::vector<std::string>* to) { | |
83 DCHECK(to->empty()); | 86 DCHECK(to->empty()); |
84 std::string as_string; | 87 std::string as_string; |
85 for (ListValue::const_iterator it = from.begin(); it != from.end(); ++it) { | 88 for (ListValue::const_iterator it = from.begin(); it != from.end(); ++it) { |
86 if ((*it)->GetAsString(&as_string)) { | 89 if ((*it)->GetAsString(&as_string)) { |
87 to->push_back(as_string); | 90 to->push_back(as_string); |
88 } | 91 } |
89 } | 92 } |
90 } | 93 } |
91 | 94 |
92 // Gets the keys of a DictionaryValue. | 95 // Gets the keys of a DictionaryValue. |
93 static std::vector<std::string> GetKeys(const DictionaryValue& dict) { | 96 std::vector<std::string> GetKeys(const DictionaryValue& dict) { |
94 std::vector<std::string> keys; | 97 std::vector<std::string> keys; |
95 for (DictionaryValue::key_iterator it = dict.begin_keys(); | 98 for (DictionaryValue::key_iterator it = dict.begin_keys(); |
96 it != dict.end_keys(); ++it) { | 99 it != dict.end_keys(); ++it) { |
97 keys.push_back(*it); | 100 keys.push_back(*it); |
98 } | 101 } |
99 return keys; | 102 return keys; |
100 } | 103 } |
101 | 104 |
| 105 // Creates quota heuristics for settings modification. |
| 106 static void GetModificationQuotaLimitHeuristics( |
| 107 QuotaLimitHeuristics* heuristics) { |
| 108 // A max of 1000 operations per hour. |
| 109 QuotaLimitHeuristic::Config longLimitConfig = { |
| 110 1000, |
| 111 base::TimeDelta::FromHours(1) |
| 112 }; |
| 113 heuristics->push_back( |
| 114 new ExtensionsQuotaService::TimedLimit( |
| 115 longLimitConfig, new QuotaLimitHeuristic::SingletonBucketMapper())); |
| 116 |
| 117 // A max of 10 operations per minute, sustained over 10 minutes. |
| 118 QuotaLimitHeuristic::Config shortLimitConfig = { |
| 119 10, |
| 120 base::TimeDelta::FromMinutes(1) |
| 121 }; |
| 122 heuristics->push_back( |
| 123 new ExtensionsQuotaService::SustainedLimit( |
| 124 base::TimeDelta::FromMinutes(10), |
| 125 shortLimitConfig, |
| 126 new QuotaLimitHeuristic::SingletonBucketMapper())); |
| 127 }; |
| 128 |
| 129 } // namespace |
| 130 |
102 bool GetSettingsFunction::RunWithStorage( | 131 bool GetSettingsFunction::RunWithStorage( |
103 scoped_refptr<SettingsObserverList> observers, | 132 scoped_refptr<SettingsObserverList> observers, |
104 SettingsStorage* storage) { | 133 SettingsStorage* storage) { |
105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
106 Value *input; | 135 Value *input; |
107 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); | 136 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); |
108 | 137 |
109 switch (input->GetType()) { | 138 switch (input->GetType()) { |
110 case Value::TYPE_NULL: | 139 case Value::TYPE_NULL: |
111 return UseReadResult(storage->Get()); | 140 return UseReadResult(storage->Get()); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 bool SetSettingsFunction::RunWithStorage( | 174 bool SetSettingsFunction::RunWithStorage( |
146 scoped_refptr<SettingsObserverList> observers, | 175 scoped_refptr<SettingsObserverList> observers, |
147 SettingsStorage* storage) { | 176 SettingsStorage* storage) { |
148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
149 DictionaryValue *input; | 178 DictionaryValue *input; |
150 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &input)); | 179 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &input)); |
151 return UseWriteResult( | 180 return UseWriteResult( |
152 observers, storage->Set(SettingsStorage::DEFAULTS, *input)); | 181 observers, storage->Set(SettingsStorage::DEFAULTS, *input)); |
153 } | 182 } |
154 | 183 |
| 184 void SetSettingsFunction::GetQuotaLimitHeuristics( |
| 185 QuotaLimitHeuristics* heuristics) const { |
| 186 GetModificationQuotaLimitHeuristics(heuristics); |
| 187 } |
| 188 |
155 bool RemoveSettingsFunction::RunWithStorage( | 189 bool RemoveSettingsFunction::RunWithStorage( |
156 scoped_refptr<SettingsObserverList> observers, | 190 scoped_refptr<SettingsObserverList> observers, |
157 SettingsStorage* storage) { | 191 SettingsStorage* storage) { |
158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 192 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
159 Value *input; | 193 Value *input; |
160 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); | 194 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); |
161 | 195 |
162 switch (input->GetType()) { | 196 switch (input->GetType()) { |
163 case Value::TYPE_STRING: { | 197 case Value::TYPE_STRING: { |
164 std::string as_string; | 198 std::string as_string; |
165 input->GetAsString(&as_string); | 199 input->GetAsString(&as_string); |
166 return UseWriteResult(observers, storage->Remove(as_string)); | 200 return UseWriteResult(observers, storage->Remove(as_string)); |
167 } | 201 } |
168 | 202 |
169 case Value::TYPE_LIST: { | 203 case Value::TYPE_LIST: { |
170 std::vector<std::string> as_string_list; | 204 std::vector<std::string> as_string_list; |
171 AddAllStringValues(*static_cast<ListValue*>(input), &as_string_list); | 205 AddAllStringValues(*static_cast<ListValue*>(input), &as_string_list); |
172 return UseWriteResult(observers, storage->Remove(as_string_list)); | 206 return UseWriteResult(observers, storage->Remove(as_string_list)); |
173 } | 207 } |
174 | 208 |
175 default: | 209 default: |
176 return UseWriteResult( | 210 return UseWriteResult( |
177 observers, | 211 observers, |
178 SettingsStorage::WriteResult(kUnsupportedArgumentType)); | 212 SettingsStorage::WriteResult(kUnsupportedArgumentType)); |
179 }; | 213 }; |
180 } | 214 } |
181 | 215 |
| 216 void RemoveSettingsFunction::GetQuotaLimitHeuristics( |
| 217 QuotaLimitHeuristics* heuristics) const { |
| 218 GetModificationQuotaLimitHeuristics(heuristics); |
| 219 } |
| 220 |
182 bool ClearSettingsFunction::RunWithStorage( | 221 bool ClearSettingsFunction::RunWithStorage( |
183 scoped_refptr<SettingsObserverList> observers, | 222 scoped_refptr<SettingsObserverList> observers, |
184 SettingsStorage* storage) { | 223 SettingsStorage* storage) { |
185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 224 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
186 return UseWriteResult(observers, storage->Clear()); | 225 return UseWriteResult(observers, storage->Clear()); |
187 } | 226 } |
188 | 227 |
| 228 void ClearSettingsFunction::GetQuotaLimitHeuristics( |
| 229 QuotaLimitHeuristics* heuristics) const { |
| 230 GetModificationQuotaLimitHeuristics(heuristics); |
| 231 } |
| 232 |
189 } // namespace extensions | 233 } // namespace extensions |
OLD | NEW |