| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 <string> |
| 8 #include <vector> |
| 9 |
| 7 #include "base/bind.h" | 10 #include "base/bind.h" |
| 8 #include "base/values.h" | 11 #include "base/values.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/extensions/extensions_quota_service.h" | 13 #include "chrome/browser/extensions/extensions_quota_service.h" |
| 11 #include "chrome/browser/extensions/settings/settings_api.h" | |
| 12 #include "chrome/browser/extensions/settings/settings_frontend.h" | 14 #include "chrome/browser/extensions/settings/settings_frontend.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/common/extensions/api/storage.h" | 16 #include "chrome/common/extensions/api/storage.h" |
| 15 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 16 | 18 |
| 17 namespace extensions { | 19 namespace extensions { |
| 18 | 20 |
| 19 using content::BrowserThread; | 21 using content::BrowserThread; |
| 20 | 22 |
| 21 namespace { | 23 namespace { |
| 22 const char* kUnsupportedArgumentType = "Unsupported argument type"; | 24 const char kUnsupportedArgumentType[] = "Unsupported argument type"; |
| 23 } // namespace | 25 } // namespace |
| 24 | 26 |
| 25 // SettingsFunction | 27 // SettingsFunction |
| 26 | 28 |
| 27 SettingsFunction::SettingsFunction() | 29 SettingsFunction::SettingsFunction() |
| 28 : settings_namespace_(settings_namespace::INVALID) {} | 30 : settings_namespace_(settings_namespace::INVALID) {} |
| 29 | 31 |
| 30 SettingsFunction::~SettingsFunction() {} | 32 SettingsFunction::~SettingsFunction() {} |
| 31 | 33 |
| 32 bool SettingsFunction::ShouldSkipQuotaLimiting() const { | 34 bool SettingsFunction::ShouldSkipQuotaLimiting() const { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 51 EXTENSION_FUNCTION_VALIDATE( | 53 EXTENSION_FUNCTION_VALIDATE( |
| 52 settings_namespace_ != settings_namespace::INVALID); | 54 settings_namespace_ != settings_namespace::INVALID); |
| 53 } | 55 } |
| 54 | 56 |
| 55 SettingsFrontend* frontend = | 57 SettingsFrontend* frontend = |
| 56 profile()->GetExtensionService()->settings_frontend(); | 58 profile()->GetExtensionService()->settings_frontend(); |
| 57 observers_ = frontend->GetObservers(); | 59 observers_ = frontend->GetObservers(); |
| 58 frontend->RunWithStorage( | 60 frontend->RunWithStorage( |
| 59 extension_id(), | 61 extension_id(), |
| 60 settings_namespace_, | 62 settings_namespace_, |
| 61 base::Bind(&SettingsFunction::RunWithStorageOnFileThread, this)); | 63 base::Bind(&SettingsFunction::AsyncRunWithStorage, this)); |
| 62 return true; | 64 return true; |
| 63 } | 65 } |
| 64 | 66 |
| 65 void SettingsFunction::RunWithStorageOnFileThread(ValueStore* storage) { | 67 void SettingsFunction::AsyncRunWithStorage(ValueStore* storage) { |
| 66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 67 bool success = RunWithStorage(storage); | 68 bool success = RunWithStorage(storage); |
| 68 BrowserThread::PostTask( | 69 BrowserThread::PostTask( |
| 69 BrowserThread::UI, | 70 BrowserThread::UI, |
| 70 FROM_HERE, | 71 FROM_HERE, |
| 71 base::Bind(&SettingsFunction::SendResponse, this, success)); | 72 base::Bind(&SettingsFunction::SendResponse, this, success)); |
| 72 } | 73 } |
| 73 | 74 |
| 74 bool SettingsFunction::UseReadResult(ValueStore::ReadResult result) { | 75 bool SettingsFunction::UseReadResult(ValueStore::ReadResult result) { |
| 75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 76 if (result->HasError()) { | 76 if (result->HasError()) { |
| 77 error_ = result->error(); | 77 error_ = result->error(); |
| 78 return false; | 78 return false; |
| 79 } | 79 } |
| 80 | 80 |
| 81 SetResult(result->settings().release()); | 81 SetResult(result->settings().release()); |
| 82 return true; | 82 return true; |
| 83 } | 83 } |
| 84 | 84 |
| 85 bool SettingsFunction::UseWriteResult(ValueStore::WriteResult result) { | 85 bool SettingsFunction::UseWriteResult(ValueStore::WriteResult result) { |
| 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 87 if (result->HasError()) { | 86 if (result->HasError()) { |
| 88 error_ = result->error(); | 87 error_ = result->error(); |
| 89 return false; | 88 return false; |
| 90 } | 89 } |
| 91 | 90 |
| 92 if (!result->changes().empty()) { | 91 if (!result->changes().empty()) { |
| 93 observers_->Notify( | 92 observers_->Notify( |
| 94 &SettingsObserver::OnSettingsChanged, | 93 &SettingsObserver::OnSettingsChanged, |
| 95 extension_id(), | 94 extension_id(), |
| 96 settings_namespace_, | 95 settings_namespace_, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 119 std::vector<std::string> GetKeys(const DictionaryValue& dict) { | 118 std::vector<std::string> GetKeys(const DictionaryValue& dict) { |
| 120 std::vector<std::string> keys; | 119 std::vector<std::string> keys; |
| 121 for (DictionaryValue::key_iterator it = dict.begin_keys(); | 120 for (DictionaryValue::key_iterator it = dict.begin_keys(); |
| 122 it != dict.end_keys(); ++it) { | 121 it != dict.end_keys(); ++it) { |
| 123 keys.push_back(*it); | 122 keys.push_back(*it); |
| 124 } | 123 } |
| 125 return keys; | 124 return keys; |
| 126 } | 125 } |
| 127 | 126 |
| 128 // Creates quota heuristics for settings modification. | 127 // Creates quota heuristics for settings modification. |
| 129 static void GetModificationQuotaLimitHeuristics( | 128 void GetModificationQuotaLimitHeuristics(QuotaLimitHeuristics* heuristics) { |
| 130 QuotaLimitHeuristics* heuristics) { | |
| 131 QuotaLimitHeuristic::Config longLimitConfig = { | 129 QuotaLimitHeuristic::Config longLimitConfig = { |
| 132 // See storage.json for current value. | 130 // See storage.json for current value. |
| 133 api::storage::sync::MAX_WRITE_OPERATIONS_PER_HOUR, | 131 api::storage::sync::MAX_WRITE_OPERATIONS_PER_HOUR, |
| 134 base::TimeDelta::FromHours(1) | 132 base::TimeDelta::FromHours(1) |
| 135 }; | 133 }; |
| 136 heuristics->push_back( | 134 heuristics->push_back( |
| 137 new ExtensionsQuotaService::TimedLimit( | 135 new ExtensionsQuotaService::TimedLimit( |
| 138 longLimitConfig, new QuotaLimitHeuristic::SingletonBucketMapper())); | 136 longLimitConfig, new QuotaLimitHeuristic::SingletonBucketMapper())); |
| 139 | 137 |
| 140 // A max of 10 operations per minute, sustained over 10 minutes. | 138 // A max of 10 operations per minute, sustained over 10 minutes. |
| 141 QuotaLimitHeuristic::Config shortLimitConfig = { | 139 QuotaLimitHeuristic::Config shortLimitConfig = { |
| 142 // See storage.json for current value. | 140 // See storage.json for current value. |
| 143 api::storage::sync::MAX_SUSTAINED_WRITE_OPERATIONS_PER_MINUTE, | 141 api::storage::sync::MAX_SUSTAINED_WRITE_OPERATIONS_PER_MINUTE, |
| 144 base::TimeDelta::FromMinutes(1) | 142 base::TimeDelta::FromMinutes(1) |
| 145 }; | 143 }; |
| 146 heuristics->push_back( | 144 heuristics->push_back( |
| 147 new ExtensionsQuotaService::SustainedLimit( | 145 new ExtensionsQuotaService::SustainedLimit( |
| 148 base::TimeDelta::FromMinutes(10), | 146 base::TimeDelta::FromMinutes(10), |
| 149 shortLimitConfig, | 147 shortLimitConfig, |
| 150 new QuotaLimitHeuristic::SingletonBucketMapper())); | 148 new QuotaLimitHeuristic::SingletonBucketMapper())); |
| 151 }; | 149 }; |
| 152 | 150 |
| 153 } // namespace | 151 } // namespace |
| 154 | 152 |
| 155 bool GetSettingsFunction::RunWithStorage(ValueStore* storage) { | 153 bool GetSettingsFunction::RunWithStorage(ValueStore* storage) { |
| 156 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 157 Value* input = NULL; | 154 Value* input = NULL; |
| 158 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); | 155 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); |
| 159 | 156 |
| 160 switch (input->GetType()) { | 157 switch (input->GetType()) { |
| 161 case Value::TYPE_NULL: | 158 case Value::TYPE_NULL: |
| 162 return UseReadResult(storage->Get()); | 159 return UseReadResult(storage->Get()); |
| 163 | 160 |
| 164 case Value::TYPE_STRING: { | 161 case Value::TYPE_STRING: { |
| 165 std::string as_string; | 162 std::string as_string; |
| 166 input->GetAsString(&as_string); | 163 input->GetAsString(&as_string); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 186 ValueStore::MakeReadResult(with_default_values)); | 183 ValueStore::MakeReadResult(with_default_values)); |
| 187 } | 184 } |
| 188 | 185 |
| 189 default: | 186 default: |
| 190 return UseReadResult( | 187 return UseReadResult( |
| 191 ValueStore::MakeReadResult(kUnsupportedArgumentType)); | 188 ValueStore::MakeReadResult(kUnsupportedArgumentType)); |
| 192 } | 189 } |
| 193 } | 190 } |
| 194 | 191 |
| 195 bool GetBytesInUseSettingsFunction::RunWithStorage(ValueStore* storage) { | 192 bool GetBytesInUseSettingsFunction::RunWithStorage(ValueStore* storage) { |
| 196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 197 Value* input = NULL; | 193 Value* input = NULL; |
| 198 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); | 194 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); |
| 199 | 195 |
| 200 size_t bytes_in_use = 0; | 196 size_t bytes_in_use = 0; |
| 201 | 197 |
| 202 switch (input->GetType()) { | 198 switch (input->GetType()) { |
| 203 case Value::TYPE_NULL: | 199 case Value::TYPE_NULL: |
| 204 bytes_in_use = storage->GetBytesInUse(); | 200 bytes_in_use = storage->GetBytesInUse(); |
| 205 break; | 201 break; |
| 206 | 202 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 221 default: | 217 default: |
| 222 error_ = kUnsupportedArgumentType; | 218 error_ = kUnsupportedArgumentType; |
| 223 return false; | 219 return false; |
| 224 } | 220 } |
| 225 | 221 |
| 226 SetResult(Value::CreateIntegerValue(bytes_in_use)); | 222 SetResult(Value::CreateIntegerValue(bytes_in_use)); |
| 227 return true; | 223 return true; |
| 228 } | 224 } |
| 229 | 225 |
| 230 bool SetSettingsFunction::RunWithStorage(ValueStore* storage) { | 226 bool SetSettingsFunction::RunWithStorage(ValueStore* storage) { |
| 231 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 232 DictionaryValue* input = NULL; | 227 DictionaryValue* input = NULL; |
| 233 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &input)); | 228 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &input)); |
| 234 return UseWriteResult(storage->Set(ValueStore::DEFAULTS, *input)); | 229 return UseWriteResult(storage->Set(ValueStore::DEFAULTS, *input)); |
| 235 } | 230 } |
| 236 | 231 |
| 237 void SetSettingsFunction::GetQuotaLimitHeuristics( | 232 void SetSettingsFunction::GetQuotaLimitHeuristics( |
| 238 QuotaLimitHeuristics* heuristics) const { | 233 QuotaLimitHeuristics* heuristics) const { |
| 239 GetModificationQuotaLimitHeuristics(heuristics); | 234 GetModificationQuotaLimitHeuristics(heuristics); |
| 240 } | 235 } |
| 241 | 236 |
| 242 bool RemoveSettingsFunction::RunWithStorage(ValueStore* storage) { | 237 bool RemoveSettingsFunction::RunWithStorage(ValueStore* storage) { |
| 243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 244 Value* input = NULL; | 238 Value* input = NULL; |
| 245 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); | 239 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); |
| 246 | 240 |
| 247 switch (input->GetType()) { | 241 switch (input->GetType()) { |
| 248 case Value::TYPE_STRING: { | 242 case Value::TYPE_STRING: { |
| 249 std::string as_string; | 243 std::string as_string; |
| 250 input->GetAsString(&as_string); | 244 input->GetAsString(&as_string); |
| 251 return UseWriteResult(storage->Remove(as_string)); | 245 return UseWriteResult(storage->Remove(as_string)); |
| 252 } | 246 } |
| 253 | 247 |
| 254 case Value::TYPE_LIST: { | 248 case Value::TYPE_LIST: { |
| 255 std::vector<std::string> as_string_list; | 249 std::vector<std::string> as_string_list; |
| 256 AddAllStringValues(*static_cast<ListValue*>(input), &as_string_list); | 250 AddAllStringValues(*static_cast<ListValue*>(input), &as_string_list); |
| 257 return UseWriteResult(storage->Remove(as_string_list)); | 251 return UseWriteResult(storage->Remove(as_string_list)); |
| 258 } | 252 } |
| 259 | 253 |
| 260 default: | 254 default: |
| 261 return UseWriteResult( | 255 return UseWriteResult( |
| 262 ValueStore::MakeWriteResult(kUnsupportedArgumentType)); | 256 ValueStore::MakeWriteResult(kUnsupportedArgumentType)); |
| 263 }; | 257 }; |
| 264 } | 258 } |
| 265 | 259 |
| 266 void RemoveSettingsFunction::GetQuotaLimitHeuristics( | 260 void RemoveSettingsFunction::GetQuotaLimitHeuristics( |
| 267 QuotaLimitHeuristics* heuristics) const { | 261 QuotaLimitHeuristics* heuristics) const { |
| 268 GetModificationQuotaLimitHeuristics(heuristics); | 262 GetModificationQuotaLimitHeuristics(heuristics); |
| 269 } | 263 } |
| 270 | 264 |
| 271 bool ClearSettingsFunction::RunWithStorage(ValueStore* storage) { | 265 bool ClearSettingsFunction::RunWithStorage(ValueStore* storage) { |
| 272 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 273 return UseWriteResult(storage->Clear()); | 266 return UseWriteResult(storage->Clear()); |
| 274 } | 267 } |
| 275 | 268 |
| 276 void ClearSettingsFunction::GetQuotaLimitHeuristics( | 269 void ClearSettingsFunction::GetQuotaLimitHeuristics( |
| 277 QuotaLimitHeuristics* heuristics) const { | 270 QuotaLimitHeuristics* heuristics) const { |
| 278 GetModificationQuotaLimitHeuristics(heuristics); | 271 GetModificationQuotaLimitHeuristics(heuristics); |
| 279 } | 272 } |
| 280 | 273 |
| 281 } // namespace extensions | 274 } // namespace extensions |
| OLD | NEW |