Chromium Code Reviews| Index: chrome/browser/extensions/extension_settings_api.cc |
| diff --git a/chrome/browser/extensions/extension_settings_api.cc b/chrome/browser/extensions/extension_settings_api.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ba20438925a71a3a69e671bd00791ffa37648c18 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/extension_settings_api.cc |
| @@ -0,0 +1,108 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/values.h" |
| +#include "chrome/browser/extensions/extension_service.h" |
| +#include "chrome/browser/extensions/extension_settings_api.h" |
| +#include "chrome/browser/profiles/profile.h" |
| + |
| +// GetStorageCallback |
| + |
| +SettingsFunction::GetStorageCallback::GetStorageCallback( |
| + SettingsFunction* settings_function) |
| + : settings_function_(settings_function) { |
| +} |
| + |
| +SettingsFunction::GetStorageCallback::~GetStorageCallback() { |
| +} |
| + |
| +void SettingsFunction::GetStorageCallback::Run( |
| + ExtensionSettingsStorage* storage) { |
| + // Mimic how RunImpl() is handled in extensions code. |
| + if (!settings_function_->RunWithStorage(storage)) { |
| + settings_function_->SendResponse(false); |
| + } |
| +} |
| + |
| +// StorageResultCallback |
| + |
| +SettingsFunction::StorageResultCallback::StorageResultCallback( |
| + SettingsFunction* settings_function) |
| + : settings_function_(settings_function) { |
| +} |
| + |
| +SettingsFunction::StorageResultCallback::~StorageResultCallback() { |
| +} |
| + |
| +void SettingsFunction::StorageResultCallback::OnSuccess( |
| + DictionaryValue* settings) { |
| + settings_function_->result_.reset(settings); |
| + settings_function_->SendResponse(true); |
| +} |
| + |
| +void SettingsFunction::StorageResultCallback::OnFailure( |
| + const std::string& message) { |
| + settings_function_->error_ = message; |
| + settings_function_->SendResponse(false); |
| +} |
| + |
| +// SettingsFunction |
| + |
| +bool SettingsFunction::RunImpl() { |
| + profile()->GetExtensionService()->extension_settings()->GetStorage( |
| + extension_id(), new GetStorageCallback(this)); |
| + return true; |
| +} |
| + |
| +// Concrete settings functions |
| + |
| +bool GetSettingsFunction::RunWithStorage(ExtensionSettingsStorage* storage) { |
| + Value *input; |
| + EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); |
| + |
| + std::string as_string; |
| + ListValue* as_list; |
| + if (input->IsType(Value::TYPE_NULL)) { |
| + storage->Get(new StorageResultCallback(this)); |
| + } else if (input->GetAsString(&as_string)) { |
| + storage->Get(as_string, new StorageResultCallback(this)); |
| + } else if (input->GetAsList(&as_list)) { |
| + storage->Get(*as_list, new StorageResultCallback(this)); |
| + } else { |
| + error_ = "Unsupported argument type"; |
|
Mihai Parparita -not on Chrome
2011/06/21 23:40:11
Errors are generally put in constants.
not at google - send to devlin
2011/06/22 09:40:38
Done.
|
| + return false; |
| + } |
| + |
| + return true; |
| +} |
| + |
| +bool SetSettingsFunction::RunWithStorage(ExtensionSettingsStorage* storage) { |
| + DictionaryValue *input; |
| + EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &input)); |
| + storage->Set(*input, new StorageResultCallback(this)); |
| + return true; |
| +} |
| + |
| +bool RemoveSettingsFunction::RunWithStorage(ExtensionSettingsStorage* storage) { |
| + Value *input; |
| + EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); |
| + |
| + std::string as_string; |
| + ListValue* as_list; |
| + if (input->GetAsString(&as_string)) { |
| + storage->Remove(as_string, new StorageResultCallback(this)); |
| + } else if (input->GetAsList(&as_list)) { |
| + storage->Remove(*as_list, new StorageResultCallback(this)); |
| + } else { |
| + error_ = "Unsupported argument type"; |
| + return false; |
| + } |
| + |
| + return true; |
| +} |
| + |
| +bool ClearSettingsFunction::RunWithStorage(ExtensionSettingsStorage* storage) { |
| + storage->Clear(new StorageResultCallback(this)); |
| + return true; |
| +} |