Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/bind.h" | |
| 6 #include "base/values.h" | |
| 7 #include "chrome/browser/extensions/extension_service.h" | |
| 8 #include "chrome/browser/extensions/extension_settings_api.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | |
| 10 | |
| 11 namespace { | |
| 12 const char* kUnsupportedArgumentType = "Unsupported argument type"; | |
| 13 } // namespace | |
| 14 | |
| 15 // GetStorageCallback | |
| 16 | |
| 17 SettingsFunction::GetStorageCallback::GetStorageCallback( | |
| 18 SettingsFunction* settings_function) | |
| 19 : settings_function_(settings_function) { | |
| 20 } | |
| 21 | |
| 22 void SettingsFunction::GetStorageCallback::Run( | |
|
Matt Perry
2011/06/23 19:14:11
why not just make this a method of SettingsFunctio
not at google - send to devlin
2011/06/27 08:51:02
Done.
| |
| 23 ExtensionSettingsStorage* storage) { | |
| 24 // Mimic how RunImpl() is handled in extensions code. | |
| 25 if (!settings_function_->RunWithStorage(storage)) { | |
| 26 settings_function_->SendResponse(false); | |
| 27 } | |
| 28 delete this; | |
| 29 } | |
| 30 | |
| 31 // StorageResultCallback | |
| 32 | |
| 33 SettingsFunction::StorageResultCallback::StorageResultCallback( | |
| 34 SettingsFunction* settings_function) | |
| 35 : settings_function_(settings_function) { | |
| 36 } | |
| 37 | |
| 38 SettingsFunction::StorageResultCallback::~StorageResultCallback() { | |
| 39 } | |
| 40 | |
| 41 void SettingsFunction::StorageResultCallback::OnSuccess( | |
| 42 DictionaryValue* settings) { | |
| 43 settings_function_->result_.reset(settings); | |
| 44 settings_function_->SendResponse(true); | |
| 45 } | |
| 46 | |
| 47 void SettingsFunction::StorageResultCallback::OnFailure( | |
| 48 const std::string& message) { | |
| 49 settings_function_->error_ = message; | |
| 50 settings_function_->SendResponse(false); | |
| 51 } | |
| 52 | |
| 53 // SettingsFunction | |
| 54 | |
| 55 bool SettingsFunction::RunImpl() { | |
| 56 profile()->GetExtensionService()->extension_settings()->GetStorage( | |
| 57 extension_id(), | |
| 58 base::Bind( | |
| 59 &GetStorageCallback::Run, | |
|
Matt Perry
2011/06/23 19:14:11
nit: indent +2
not at google - send to devlin
2011/06/27 08:51:02
Done.
not at google - send to devlin
2011/06/27 08:51:02
Done.
| |
| 60 base::Unretained(new GetStorageCallback(this)))); | |
| 61 return true; | |
| 62 } | |
| 63 | |
| 64 // Concrete settings functions | |
| 65 | |
| 66 bool GetSettingsFunction::RunWithStorage(ExtensionSettingsStorage* storage) { | |
| 67 Value *input; | |
| 68 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); | |
| 69 | |
| 70 std::string as_string; | |
| 71 ListValue* as_list; | |
| 72 if (input->IsType(Value::TYPE_NULL)) { | |
| 73 storage->Get(new StorageResultCallback(this)); | |
| 74 } else if (input->GetAsString(&as_string)) { | |
| 75 storage->Get(as_string, new StorageResultCallback(this)); | |
| 76 } else if (input->GetAsList(&as_list)) { | |
| 77 storage->Get(*as_list, new StorageResultCallback(this)); | |
| 78 } else { | |
| 79 error_ = kUnsupportedArgumentType; | |
| 80 return false; | |
| 81 } | |
| 82 | |
| 83 return true; | |
| 84 } | |
| 85 | |
| 86 bool SetSettingsFunction::RunWithStorage(ExtensionSettingsStorage* storage) { | |
| 87 DictionaryValue *input; | |
| 88 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &input)); | |
| 89 storage->Set(*input, new StorageResultCallback(this)); | |
| 90 return true; | |
| 91 } | |
| 92 | |
| 93 bool RemoveSettingsFunction::RunWithStorage(ExtensionSettingsStorage* storage) { | |
| 94 Value *input; | |
| 95 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); | |
| 96 | |
| 97 std::string as_string; | |
| 98 ListValue* as_list; | |
| 99 if (input->GetAsString(&as_string)) { | |
| 100 storage->Remove(as_string, new StorageResultCallback(this)); | |
| 101 } else if (input->GetAsList(&as_list)) { | |
| 102 storage->Remove(*as_list, new StorageResultCallback(this)); | |
| 103 } else { | |
| 104 error_ = kUnsupportedArgumentType; | |
| 105 return false; | |
| 106 } | |
| 107 | |
| 108 return true; | |
| 109 } | |
| 110 | |
| 111 bool ClearSettingsFunction::RunWithStorage(ExtensionSettingsStorage* storage) { | |
| 112 storage->Clear(new StorageResultCallback(this)); | |
| 113 return true; | |
| 114 } | |
| OLD | NEW |