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