| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/dom_ui/core_options_handler.h" | 5 #include "chrome/browser/dom_ui/core_options_handler.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 if (type == NotificationType::PREF_CHANGED) | 65 if (type == NotificationType::PREF_CHANGED) |
| 66 NotifyPrefChanged(Details<std::wstring>(details).ptr()); | 66 NotifyPrefChanged(Details<std::wstring>(details).ptr()); |
| 67 } | 67 } |
| 68 | 68 |
| 69 void CoreOptionsHandler::RegisterMessages() { | 69 void CoreOptionsHandler::RegisterMessages() { |
| 70 dom_ui_->RegisterMessageCallback("coreOptionsInitialize", | 70 dom_ui_->RegisterMessageCallback("coreOptionsInitialize", |
| 71 NewCallback(this, &CoreOptionsHandler::HandleInitialize)); | 71 NewCallback(this, &CoreOptionsHandler::HandleInitialize)); |
| 72 dom_ui_->RegisterMessageCallback("fetchPrefs", | 72 dom_ui_->RegisterMessageCallback("fetchPrefs", |
| 73 NewCallback(this, &CoreOptionsHandler::HandleFetchPrefs)); | 73 NewCallback(this, &CoreOptionsHandler::HandleFetchPrefs)); |
| 74 dom_ui_->RegisterMessageCallback("observePrefs", | 74 dom_ui_->RegisterMessageCallback("observePrefs", |
| 75 NewCallback(this, &CoreOptionsHandler::HandleObservePefs)); | 75 NewCallback(this, &CoreOptionsHandler::HandleObservePrefs)); |
| 76 dom_ui_->RegisterMessageCallback("setBooleanPref", | 76 dom_ui_->RegisterMessageCallback("setBooleanPref", |
| 77 NewCallback(this, &CoreOptionsHandler::HandleSetBooleanPref)); | 77 NewCallback(this, &CoreOptionsHandler::HandleSetBooleanPref)); |
| 78 dom_ui_->RegisterMessageCallback("setIntegerPref", | 78 dom_ui_->RegisterMessageCallback("setIntegerPref", |
| 79 NewCallback(this, &CoreOptionsHandler::HandleSetIntegerPref)); | 79 NewCallback(this, &CoreOptionsHandler::HandleSetIntegerPref)); |
| 80 dom_ui_->RegisterMessageCallback("setStringPref", | 80 dom_ui_->RegisterMessageCallback("setStringPref", |
| 81 NewCallback(this, &CoreOptionsHandler::HandleSetStringPref)); | 81 NewCallback(this, &CoreOptionsHandler::HandleSetStringPref)); |
| 82 dom_ui_->RegisterMessageCallback("setObjectPref", |
| 83 NewCallback(this, &CoreOptionsHandler::HandleSetObjectPref)); |
| 82 } | 84 } |
| 83 | 85 |
| 84 void CoreOptionsHandler::HandleInitialize(const Value* value) { | 86 void CoreOptionsHandler::HandleInitialize(const Value* value) { |
| 85 (static_cast<OptionsUI*>(dom_ui_))->InitializeHandlers(); | 87 (static_cast<OptionsUI*>(dom_ui_))->InitializeHandlers(); |
| 86 } | 88 } |
| 87 | 89 |
| 90 Value* CoreOptionsHandler::FetchPref(const std::wstring& pref_name) { |
| 91 DCHECK(dom_ui_); |
| 92 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); |
| 93 |
| 94 const PrefService::Preference* pref = |
| 95 pref_service->FindPreference(pref_name.c_str()); |
| 96 |
| 97 return pref ? pref->GetValue()->DeepCopy() : Value::CreateNullValue(); |
| 98 } |
| 99 |
| 100 void CoreOptionsHandler::ObservePref(const std::wstring& pref_name) { |
| 101 DCHECK(dom_ui_); |
| 102 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); |
| 103 |
| 104 pref_service->AddPrefObserver(pref_name.c_str(), this); |
| 105 } |
| 106 |
| 107 void CoreOptionsHandler::SetPref(const std::wstring& pref_name, |
| 108 Value::ValueType pref_type, |
| 109 const std::string& value_string) { |
| 110 DCHECK(dom_ui_); |
| 111 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); |
| 112 |
| 113 switch (pref_type) { |
| 114 case Value::TYPE_BOOLEAN: |
| 115 pref_service->SetBoolean(pref_name.c_str(), value_string == "true"); |
| 116 break; |
| 117 case Value::TYPE_INTEGER: |
| 118 int int_value; |
| 119 if (StringToInt(value_string, &int_value)) |
| 120 pref_service->SetInteger(pref_name.c_str(), int_value); |
| 121 break; |
| 122 case Value::TYPE_STRING: |
| 123 pref_service->SetString(pref_name.c_str(), value_string); |
| 124 break; |
| 125 default: |
| 126 NOTREACHED(); |
| 127 } |
| 128 } |
| 129 |
| 88 void CoreOptionsHandler::HandleFetchPrefs(const Value* value) { | 130 void CoreOptionsHandler::HandleFetchPrefs(const Value* value) { |
| 89 if (!value || !value->IsType(Value::TYPE_LIST)) | 131 if (!value || !value->IsType(Value::TYPE_LIST)) |
| 90 return; | 132 return; |
| 91 | 133 |
| 92 const ListValue* param_values = static_cast<const ListValue*>(value); | 134 const ListValue* param_values = static_cast<const ListValue*>(value); |
| 93 | 135 |
| 94 // First param is name of callback function, so, there needs to be at least | 136 // First param is name of callback function, so, there needs to be at least |
| 95 // one more element for the actual preference identifier. | 137 // one more element for the actual preference identifier. |
| 96 const size_t kMinFetchPrefsParamCount = 2; | 138 const size_t kMinFetchPrefsParamCount = 2; |
| 97 if (param_values->GetSize() < kMinFetchPrefsParamCount) | 139 if (param_values->GetSize() < kMinFetchPrefsParamCount) |
| 98 return; | 140 return; |
| 99 | 141 |
| 100 size_t idx = param_values->GetSize(); | 142 size_t idx = param_values->GetSize(); |
| 101 LOG(INFO) << "param_values->GetSize() = " << idx; | 143 LOG(INFO) << "param_values->GetSize() = " << idx; |
| 102 // Get callback JS function name. | 144 // Get callback JS function name. |
| 103 Value* callback; | 145 Value* callback; |
| 104 if (!param_values->Get(0, &callback) || !callback->IsType(Value::TYPE_STRING)) | 146 if (!param_values->Get(0, &callback) || !callback->IsType(Value::TYPE_STRING)) |
| 105 return; | 147 return; |
| 106 | 148 |
| 107 std::wstring callback_function; | 149 std::wstring callback_function; |
| 108 if (!callback->GetAsString(&callback_function)) | 150 if (!callback->GetAsString(&callback_function)) |
| 109 return; | 151 return; |
| 110 | 152 |
| 111 // Get the list of name for prefs to build the response dictionary. | 153 // Get the list of name for prefs to build the response dictionary. |
| 112 DictionaryValue result_value; | 154 DictionaryValue result_value; |
| 113 Value* list_member; | 155 Value* list_member; |
| 114 DCHECK(dom_ui_); | |
| 115 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); | |
| 116 | 156 |
| 117 for (size_t i = 1; i < param_values->GetSize(); i++) { | 157 for (size_t i = 1; i < param_values->GetSize(); i++) { |
| 118 if (!param_values->Get(i, &list_member)) | 158 if (!param_values->Get(i, &list_member)) |
| 119 break; | 159 break; |
| 120 | 160 |
| 121 if (!list_member->IsType(Value::TYPE_STRING)) | 161 if (!list_member->IsType(Value::TYPE_STRING)) |
| 122 continue; | 162 continue; |
| 123 | 163 |
| 124 std::wstring pref_name; | 164 std::wstring pref_name; |
| 125 if (!list_member->GetAsString(&pref_name)) | 165 if (!list_member->GetAsString(&pref_name)) |
| 126 continue; | 166 continue; |
| 127 | 167 |
| 128 const PrefService::Preference* pref = | 168 result_value.Set(pref_name.c_str(), FetchPref(pref_name)); |
| 129 pref_service->FindPreference(pref_name.c_str()); | |
| 130 result_value.Set(pref_name.c_str(), | |
| 131 pref ? pref->GetValue()->DeepCopy() : Value::CreateNullValue()); | |
| 132 } | 169 } |
| 133 dom_ui_->CallJavascriptFunction(callback_function.c_str(), result_value); | 170 dom_ui_->CallJavascriptFunction(callback_function.c_str(), result_value); |
| 134 } | 171 } |
| 135 | 172 |
| 136 void CoreOptionsHandler::HandleObservePefs(const Value* value) { | 173 void CoreOptionsHandler::HandleObservePrefs(const Value* value) { |
| 137 if (!value || !value->IsType(Value::TYPE_LIST)) | 174 if (!value || !value->IsType(Value::TYPE_LIST)) |
| 138 return; | 175 return; |
| 139 | 176 |
| 140 DCHECK(dom_ui_); | |
| 141 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); | |
| 142 DictionaryValue result_value; | 177 DictionaryValue result_value; |
| 143 const ListValue* list_value = static_cast<const ListValue*>(value); | 178 const ListValue* list_value = static_cast<const ListValue*>(value); |
| 144 | 179 |
| 145 // First param is name is JS callback function name, the rest are pref | 180 // First param is name is JS callback function name, the rest are pref |
| 146 // identifiers that we are observing. | 181 // identifiers that we are observing. |
| 147 const size_t kMinObservePrefsParamCount = 2; | 182 const size_t kMinObservePrefsParamCount = 2; |
| 148 if (list_value->GetSize() < kMinObservePrefsParamCount) | 183 if (list_value->GetSize() < kMinObservePrefsParamCount) |
| 149 return; | 184 return; |
| 150 | 185 |
| 151 // Get preference change callback function name. | 186 // Get preference change callback function name. |
| 152 std::wstring callback_func_name; | 187 std::wstring callback_func_name; |
| 153 Value* list_member = 0; | 188 if (!list_value->GetString(0, &callback_func_name)) |
| 154 if (!list_value->Get(0, &list_member) || | |
| 155 !list_member->IsType(Value::TYPE_STRING) || | |
| 156 !list_member->GetAsString(&callback_func_name)) | |
| 157 return; | 189 return; |
| 158 | 190 |
| 159 // Get all other parameters - pref identifiers. | 191 // Get all other parameters - pref identifiers. |
| 160 for (size_t i = 1; i < list_value->GetSize(); i++) { | 192 for (size_t i = 1; i < list_value->GetSize(); i++) { |
| 193 Value* list_member; |
| 161 if (!list_value->Get(i, &list_member)) | 194 if (!list_value->Get(i, &list_member)) |
| 162 break; | 195 break; |
| 163 | 196 |
| 164 // Just ignore bad pref identifiers for now. | 197 // Just ignore bad pref identifiers for now. |
| 165 std::wstring pref_name; | 198 std::wstring pref_name; |
| 166 if (!list_member->IsType(Value::TYPE_STRING) || | 199 if (!list_member->IsType(Value::TYPE_STRING) || |
| 167 !list_member->GetAsString(&pref_name)) | 200 !list_member->GetAsString(&pref_name)) |
| 168 continue; | 201 continue; |
| 169 | 202 |
| 170 if (pref_callback_map_.find(pref_name) == pref_callback_map_.end()) | 203 if (pref_callback_map_.find(pref_name) == pref_callback_map_.end()) |
| 171 pref_service->AddPrefObserver(pref_name.c_str(), this); | 204 ObservePref(pref_name); |
| 172 | 205 |
| 173 pref_callback_map_.insert( | 206 pref_callback_map_.insert( |
| 174 PreferenceCallbackMap::value_type(pref_name, callback_func_name)); | 207 PreferenceCallbackMap::value_type(pref_name, callback_func_name)); |
| 175 } | 208 } |
| 176 } | 209 } |
| 177 | 210 |
| 178 void CoreOptionsHandler::HandleSetBooleanPref(const Value* value) { | 211 void CoreOptionsHandler::HandleSetBooleanPref(const Value* value) { |
| 179 HandleSetPref(value, Value::TYPE_BOOLEAN); | 212 HandleSetPref(value, Value::TYPE_BOOLEAN); |
| 180 } | 213 } |
| 181 | 214 |
| 182 void CoreOptionsHandler::HandleSetIntegerPref(const Value* value) { | 215 void CoreOptionsHandler::HandleSetIntegerPref(const Value* value) { |
| 183 HandleSetPref(value, Value::TYPE_INTEGER); | 216 HandleSetPref(value, Value::TYPE_INTEGER); |
| 184 } | 217 } |
| 185 | 218 |
| 186 void CoreOptionsHandler::HandleSetStringPref(const Value* value) { | 219 void CoreOptionsHandler::HandleSetStringPref(const Value* value) { |
| 187 HandleSetPref(value, Value::TYPE_STRING); | 220 HandleSetPref(value, Value::TYPE_STRING); |
| 188 } | 221 } |
| 189 | 222 |
| 223 void CoreOptionsHandler::HandleSetObjectPref(const Value* value) { |
| 224 HandleSetPref(value, Value::TYPE_NULL); |
| 225 } |
| 226 |
| 190 void CoreOptionsHandler::HandleSetPref(const Value* value, | 227 void CoreOptionsHandler::HandleSetPref(const Value* value, |
| 191 Value::ValueType type) { | 228 Value::ValueType type) { |
| 192 if (!value || !value->IsType(Value::TYPE_LIST)) | 229 if (!value || !value->IsType(Value::TYPE_LIST)) |
| 193 return; | 230 return; |
| 194 const ListValue* param_values = static_cast<const ListValue*>(value); | 231 const ListValue* param_values = static_cast<const ListValue*>(value); |
| 195 size_t size = param_values->GetSize(); | 232 size_t size = param_values->GetSize(); |
| 196 LOG(INFO) << "Array size = " << size; | 233 LOG(INFO) << "Array size = " << size; |
| 197 if (param_values->GetSize() != 2) | 234 if (param_values->GetSize() != 2) |
| 198 return; | 235 return; |
| 199 | 236 |
| 200 DCHECK(dom_ui_); | |
| 201 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); | |
| 202 | |
| 203 Value* name_element; | |
| 204 std::wstring pref_name; | 237 std::wstring pref_name; |
| 205 if (!param_values->Get(0, &name_element) || | 238 if (!param_values->GetString(0, &pref_name)) |
| 206 !name_element->IsType(Value::TYPE_STRING) || | |
| 207 !name_element->GetAsString(&pref_name)) | |
| 208 return; | 239 return; |
| 209 | 240 |
| 210 Value* value_element; | |
| 211 std::string value_string; | 241 std::string value_string; |
| 212 if (!param_values->Get(1, &value_element) || | 242 if (!param_values->GetString(1, &value_string)) |
| 213 !value_element->IsType(Value::TYPE_STRING) || | |
| 214 !value_element->GetAsString(&value_string)) | |
| 215 return; | 243 return; |
| 216 | 244 |
| 217 switch (type) { | 245 SetPref(pref_name, type, value_string); |
| 218 case Value::TYPE_BOOLEAN: | |
| 219 pref_service->SetBoolean(pref_name.c_str(), value_string == "true"); | |
| 220 break; | |
| 221 case Value::TYPE_INTEGER: | |
| 222 int int_value; | |
| 223 if (StringToInt(value_string, &int_value)) | |
| 224 pref_service->SetInteger(pref_name.c_str(), int_value); | |
| 225 break; | |
| 226 case Value::TYPE_STRING: | |
| 227 pref_service->SetString(pref_name.c_str(), value_string); | |
| 228 break; | |
| 229 default: | |
| 230 NOTREACHED(); | |
| 231 } | |
| 232 } | 246 } |
| 233 | 247 |
| 234 void CoreOptionsHandler::NotifyPrefChanged(const std::wstring* pref_name) { | 248 void CoreOptionsHandler::NotifyPrefChanged(const std::wstring* pref_name) { |
| 235 DCHECK(pref_name); | 249 DCHECK(pref_name); |
| 236 DCHECK(dom_ui_); | 250 DCHECK(dom_ui_); |
| 237 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); | 251 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); |
| 238 const PrefService::Preference* pref = | 252 const PrefService::Preference* pref = |
| 239 pref_service->FindPreference(pref_name->c_str()); | 253 pref_service->FindPreference(pref_name->c_str()); |
| 240 if (pref) { | 254 if (pref) { |
| 241 for (PreferenceCallbackMap::const_iterator iter = | 255 for (PreferenceCallbackMap::const_iterator iter = |
| 242 pref_callback_map_.find(*pref_name); | 256 pref_callback_map_.find(*pref_name); |
| 243 iter != pref_callback_map_.end(); ++iter) { | 257 iter != pref_callback_map_.end(); ++iter) { |
| 244 const std::wstring& callback_function = iter->second; | 258 const std::wstring& callback_function = iter->second; |
| 245 ListValue result_value; | 259 ListValue result_value; |
| 246 result_value.Append(Value::CreateStringValue(pref_name->c_str())); | 260 result_value.Append(Value::CreateStringValue(pref_name->c_str())); |
| 247 result_value.Append(pref->GetValue()->DeepCopy()); | 261 result_value.Append(pref->GetValue()->DeepCopy()); |
| 248 dom_ui_->CallJavascriptFunction(callback_function, result_value); | 262 dom_ui_->CallJavascriptFunction(callback_function, result_value); |
| 249 } | 263 } |
| 250 } | 264 } |
| 251 } | 265 } |
| OLD | NEW |