| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/values.h" | 6 #include "base/values.h" |
| 7 #include "chrome/browser/extensions/extension_service.h" | 7 #include "chrome/browser/extensions/extension_service.h" |
| 8 #include "chrome/browser/extensions/extension_settings_api.h" | 8 #include "chrome/browser/extensions/extension_settings_api.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "content/browser/browser_thread.h" | 10 #include "content/browser/browser_thread.h" |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 const char* kUnsupportedArgumentType = "Unsupported argument type"; | 13 const char* kUnsupportedArgumentType = "Unsupported argument type"; |
| 14 } // namespace | 14 } // namespace |
| 15 | 15 |
| 16 // SettingsFunction | 16 // SettingsFunction |
| 17 | 17 |
| 18 bool SettingsFunction::RunImpl() { | 18 bool SettingsFunction::RunImpl() { |
| 19 profile()->GetExtensionService()->extension_settings_frontend()-> | 19 profile()->GetExtensionService()->extension_settings_frontend()-> |
| 20 RunWithBackend( | 20 RunWithBackend( |
| 21 base::Bind(&SettingsFunction::RunWithBackendOnFileThread, this)); | 21 base::Bind(&SettingsFunction::RunWithBackendOnFileThread, this)); |
| 22 return true; | 22 return true; |
| 23 } | 23 } |
| 24 | 24 |
| 25 void SettingsFunction::RunWithBackendOnFileThread( | 25 void SettingsFunction::RunWithBackendOnFileThread( |
| 26 ExtensionSettingsBackend* backend) { | 26 ExtensionSettingsBackend* backend) { |
| 27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 28 bool success = RunWithStorage(backend->GetStorage(extension_id())); | 28 bool success = RunWithStorage(backend, backend->GetStorage(extension_id())); |
| 29 BrowserThread::PostTask( | 29 BrowserThread::PostTask( |
| 30 BrowserThread::UI, | 30 BrowserThread::UI, |
| 31 FROM_HERE, | 31 FROM_HERE, |
| 32 base::Bind(&SettingsFunction::SendResponse, this, success)); | 32 base::Bind(&SettingsFunction::SendResponse, this, success)); |
| 33 } | 33 } |
| 34 | 34 |
| 35 bool SettingsFunction::UseResult( | 35 bool SettingsFunction::UseResult( |
| 36 ExtensionSettingsBackend* backend, |
| 36 const ExtensionSettingsStorage::Result& storage_result) { | 37 const ExtensionSettingsStorage::Result& storage_result) { |
| 38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 37 if (storage_result.HasError()) { | 39 if (storage_result.HasError()) { |
| 38 error_ = storage_result.GetError(); | 40 error_ = storage_result.GetError(); |
| 39 return false; | 41 return false; |
| 40 } | 42 } |
| 41 DictionaryValue* settings = storage_result.GetSettings(); | 43 |
| 42 result_.reset(settings == NULL ? NULL : settings->DeepCopy()); | 44 const DictionaryValue* settings = storage_result.GetSettings(); |
| 45 if (settings) { |
| 46 result_.reset(settings->DeepCopy()); |
| 47 } |
| 48 |
| 49 const std::set<std::string>* changed_keys = storage_result.GetChangedKeys(); |
| 50 if (changed_keys && !changed_keys->empty()) { |
| 51 ExtensionSettingChanges::Builder changes; |
| 52 for (std::set<std::string>::const_iterator it = changed_keys->begin(); |
| 53 it != changed_keys->end(); ++it) { |
| 54 const Value* old_value = storage_result.GetOldValue(*it); |
| 55 const Value* new_value = storage_result.GetNewValue(*it); |
| 56 changes.AppendChange( |
| 57 *it, |
| 58 old_value ? old_value->DeepCopy() : NULL, |
| 59 new_value ? new_value->DeepCopy() : NULL); |
| 60 } |
| 61 backend->TriggerOnSettingsChanged( |
| 62 profile(), extension_id(), changes.Build()); |
| 63 } |
| 64 |
| 43 return true; | 65 return true; |
| 44 } | 66 } |
| 45 | 67 |
| 46 // Concrete settings functions | 68 // Concrete settings functions |
| 47 | 69 |
| 48 // Adds all StringValues from a ListValue to a vector of strings. | 70 // Adds all StringValues from a ListValue to a vector of strings. |
| 49 static void AddAllStringValues( | 71 static void AddAllStringValues( |
| 50 const ListValue& from, std::vector<std::string>* to) { | 72 const ListValue& from, std::vector<std::string>* to) { |
| 51 DCHECK(to->empty()); | 73 DCHECK(to->empty()); |
| 52 std::string as_string; | 74 std::string as_string; |
| 53 for (ListValue::const_iterator it = from.begin(); it != from.end(); ++it) { | 75 for (ListValue::const_iterator it = from.begin(); it != from.end(); ++it) { |
| 54 if ((*it)->GetAsString(&as_string)) { | 76 if ((*it)->GetAsString(&as_string)) { |
| 55 to->push_back(as_string); | 77 to->push_back(as_string); |
| 56 } | 78 } |
| 57 } | 79 } |
| 58 } | 80 } |
| 59 | 81 |
| 60 bool GetSettingsFunction::RunWithStorage( | 82 bool GetSettingsFunction::RunWithStorage( |
| 83 ExtensionSettingsBackend* backend, |
| 61 ExtensionSettingsStorage* storage) { | 84 ExtensionSettingsStorage* storage) { |
| 85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 62 Value *input; | 86 Value *input; |
| 63 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); | 87 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); |
| 64 std::string as_string; | 88 std::string as_string; |
| 65 ListValue* as_list; | 89 ListValue* as_list; |
| 66 if (input->IsType(Value::TYPE_NULL)) { | 90 if (input->IsType(Value::TYPE_NULL)) { |
| 67 return UseResult(storage->Get()); | 91 return UseResult(backend, storage->Get()); |
| 68 } else if (input->GetAsString(&as_string)) { | 92 } else if (input->GetAsString(&as_string)) { |
| 69 return UseResult(storage->Get(as_string)); | 93 return UseResult(backend, storage->Get(as_string)); |
| 70 } else if (input->GetAsList(&as_list)) { | 94 } else if (input->GetAsList(&as_list)) { |
| 71 std::vector<std::string> string_list; | 95 std::vector<std::string> string_list; |
| 72 AddAllStringValues(*as_list, &string_list); | 96 AddAllStringValues(*as_list, &string_list); |
| 73 return UseResult(storage->Get(string_list)); | 97 return UseResult(backend, storage->Get(string_list)); |
| 74 } | 98 } |
| 75 return UseResult(ExtensionSettingsStorage::Result(kUnsupportedArgumentType)); | 99 return UseResult( |
| 100 backend, ExtensionSettingsStorage::Result(kUnsupportedArgumentType)); |
| 76 } | 101 } |
| 77 | 102 |
| 78 bool SetSettingsFunction::RunWithStorage( | 103 bool SetSettingsFunction::RunWithStorage( |
| 104 ExtensionSettingsBackend* backend, |
| 79 ExtensionSettingsStorage* storage) { | 105 ExtensionSettingsStorage* storage) { |
| 106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 80 DictionaryValue *input; | 107 DictionaryValue *input; |
| 81 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &input)); | 108 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &input)); |
| 82 return UseResult(storage->Set(*input)); | 109 return UseResult(backend, storage->Set(*input)); |
| 83 } | 110 } |
| 84 | 111 |
| 85 bool RemoveSettingsFunction::RunWithStorage( | 112 bool RemoveSettingsFunction::RunWithStorage( |
| 113 ExtensionSettingsBackend* backend, |
| 86 ExtensionSettingsStorage* storage) { | 114 ExtensionSettingsStorage* storage) { |
| 115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 87 Value *input; | 116 Value *input; |
| 88 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); | 117 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &input)); |
| 89 std::string as_string; | 118 std::string as_string; |
| 90 ListValue* as_list; | 119 ListValue* as_list; |
| 91 if (input->GetAsString(&as_string)) { | 120 if (input->GetAsString(&as_string)) { |
| 92 return UseResult(storage->Remove(as_string)); | 121 return UseResult(backend, storage->Remove(as_string)); |
| 93 } else if (input->GetAsList(&as_list)) { | 122 } else if (input->GetAsList(&as_list)) { |
| 94 std::vector<std::string> string_list; | 123 std::vector<std::string> string_list; |
| 95 AddAllStringValues(*as_list, &string_list); | 124 AddAllStringValues(*as_list, &string_list); |
| 96 return UseResult(storage->Remove(string_list)); | 125 return UseResult(backend, storage->Remove(string_list)); |
| 97 } | 126 } |
| 98 return UseResult(ExtensionSettingsStorage::Result(kUnsupportedArgumentType)); | 127 return UseResult( |
| 128 backend, ExtensionSettingsStorage::Result(kUnsupportedArgumentType)); |
| 99 } | 129 } |
| 100 | 130 |
| 101 bool ClearSettingsFunction::RunWithStorage( | 131 bool ClearSettingsFunction::RunWithStorage( |
| 132 ExtensionSettingsBackend* backend, |
| 102 ExtensionSettingsStorage* storage) { | 133 ExtensionSettingsStorage* storage) { |
| 103 return UseResult(storage->Clear()); | 134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 135 return UseResult(backend, storage->Clear()); |
| 104 } | 136 } |
| OLD | NEW |