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 // StorageResultCallback |
| 16 |
| 17 SettingsFunction::StorageResultCallback::StorageResultCallback( |
| 18 SettingsFunction* settings_function) |
| 19 : settings_function_(settings_function) { |
| 20 } |
| 21 |
| 22 SettingsFunction::StorageResultCallback::~StorageResultCallback() { |
| 23 } |
| 24 |
| 25 void SettingsFunction::StorageResultCallback::OnSuccess( |
| 26 DictionaryValue* settings) { |
| 27 settings_function_->result_.reset(settings); |
| 28 settings_function_->SendResponse(true); |
| 29 } |
| 30 |
| 31 void SettingsFunction::StorageResultCallback::OnFailure( |
| 32 const std::string& message) { |
| 33 settings_function_->error_ = message; |
| 34 settings_function_->SendResponse(false); |
| 35 } |
| 36 |
| 37 // SettingsFunction |
| 38 |
| 39 bool SettingsFunction::RunImpl() { |
| 40 profile()->GetExtensionService()->extension_settings()->GetStorage( |
| 41 extension_id(), |
| 42 base::Bind(&SettingsFunction::RunWithStorage, this)); |
| 43 return true; |
| 44 } |
| 45 |
| 46 void SettingsFunction::RunWithStorage(ExtensionSettingsStorage* storage) { |
| 47 // Mimic how RunImpl() is handled in extensions code. |
| 48 if (!RunWithStorageImpl(storage)) { |
| 49 SendResponse(false); |
| 50 } |
| 51 } |
| 52 |
| 53 // Concrete settings functions |
| 54 |
| 55 bool GetSettingsFunction::RunWithStorageImpl( |
| 56 ExtensionSettingsStorage* storage) { |
| 57 Value *input; |
| 58 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); |
| 59 |
| 60 std::string as_string; |
| 61 ListValue* as_list; |
| 62 if (input->IsType(Value::TYPE_NULL)) { |
| 63 storage->Get(new StorageResultCallback(this)); |
| 64 } else if (input->GetAsString(&as_string)) { |
| 65 storage->Get(as_string, new StorageResultCallback(this)); |
| 66 } else if (input->GetAsList(&as_list)) { |
| 67 storage->Get(*as_list, new StorageResultCallback(this)); |
| 68 } else { |
| 69 error_ = kUnsupportedArgumentType; |
| 70 return false; |
| 71 } |
| 72 |
| 73 return true; |
| 74 } |
| 75 |
| 76 bool SetSettingsFunction::RunWithStorageImpl( |
| 77 ExtensionSettingsStorage* storage) { |
| 78 DictionaryValue *input; |
| 79 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &input)); |
| 80 storage->Set(*input, new StorageResultCallback(this)); |
| 81 return true; |
| 82 } |
| 83 |
| 84 bool RemoveSettingsFunction::RunWithStorageImpl( |
| 85 ExtensionSettingsStorage* storage) { |
| 86 Value *input; |
| 87 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); |
| 88 |
| 89 std::string as_string; |
| 90 ListValue* as_list; |
| 91 if (input->GetAsString(&as_string)) { |
| 92 storage->Remove(as_string, new StorageResultCallback(this)); |
| 93 } else if (input->GetAsList(&as_list)) { |
| 94 storage->Remove(*as_list, new StorageResultCallback(this)); |
| 95 } else { |
| 96 error_ = kUnsupportedArgumentType; |
| 97 return false; |
| 98 } |
| 99 |
| 100 return true; |
| 101 } |
| 102 |
| 103 bool ClearSettingsFunction::RunWithStorageImpl( |
| 104 ExtensionSettingsStorage* storage) { |
| 105 storage->Clear(new StorageResultCallback(this)); |
| 106 return true; |
| 107 } |
OLD | NEW |