Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(124)

Side by Side Diff: chrome/browser/extensions/settings/settings_api.cc

Issue 8491033: Extension Settings API: throttle the API in the same way that the Bookmarks API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: sync, use SingletonBucketMapper Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 173
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(observers, storage->Set(*input)); 180 return UseWriteResult(observers, storage->Set(*input));
152 } 181 }
153 182
183 void SetSettingsFunction::GetQuotaLimitHeuristics(
184 QuotaLimitHeuristics* heuristics) const {
185 GetModificationQuotaLimitHeuristics(heuristics);
186 }
187
154 bool RemoveSettingsFunction::RunWithStorage( 188 bool RemoveSettingsFunction::RunWithStorage(
155 scoped_refptr<SettingsObserverList> observers, 189 scoped_refptr<SettingsObserverList> observers,
156 SettingsStorage* storage) { 190 SettingsStorage* storage) {
157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
158 Value *input; 192 Value *input;
159 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); 193 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input));
160 194
161 switch (input->GetType()) { 195 switch (input->GetType()) {
162 case Value::TYPE_STRING: { 196 case Value::TYPE_STRING: {
163 std::string as_string; 197 std::string as_string;
164 input->GetAsString(&as_string); 198 input->GetAsString(&as_string);
165 return UseWriteResult(observers, storage->Remove(as_string)); 199 return UseWriteResult(observers, storage->Remove(as_string));
166 } 200 }
167 201
168 case Value::TYPE_LIST: { 202 case Value::TYPE_LIST: {
169 std::vector<std::string> as_string_list; 203 std::vector<std::string> as_string_list;
170 AddAllStringValues(*static_cast<ListValue*>(input), &as_string_list); 204 AddAllStringValues(*static_cast<ListValue*>(input), &as_string_list);
171 return UseWriteResult(observers, storage->Remove(as_string_list)); 205 return UseWriteResult(observers, storage->Remove(as_string_list));
172 } 206 }
173 207
174 default: 208 default:
175 return UseWriteResult( 209 return UseWriteResult(
176 observers, 210 observers,
177 SettingsStorage::WriteResult(kUnsupportedArgumentType)); 211 SettingsStorage::WriteResult(kUnsupportedArgumentType));
178 }; 212 };
179 } 213 }
180 214
215 void RemoveSettingsFunction::GetQuotaLimitHeuristics(
216 QuotaLimitHeuristics* heuristics) const {
217 GetModificationQuotaLimitHeuristics(heuristics);
218 }
219
181 bool ClearSettingsFunction::RunWithStorage( 220 bool ClearSettingsFunction::RunWithStorage(
182 scoped_refptr<SettingsObserverList> observers, 221 scoped_refptr<SettingsObserverList> observers,
183 SettingsStorage* storage) { 222 SettingsStorage* storage) {
184 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 223 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
185 return UseWriteResult(observers, storage->Clear()); 224 return UseWriteResult(observers, storage->Clear());
186 } 225 }
187 226
227 void ClearSettingsFunction::GetQuotaLimitHeuristics(
228 QuotaLimitHeuristics* heuristics) const {
229 GetModificationQuotaLimitHeuristics(heuristics);
230 }
231
188 } // namespace extensions 232 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698