| 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 "base/string16.h" |
| 8 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 9 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 10 #include "base/values.h" | 11 #include "base/values.h" |
| 11 #include "chrome/common/notification_service.h" | 12 #include "chrome/common/notification_service.h" |
| 12 #include "chrome/common/notification_type.h" | 13 #include "chrome/common/notification_type.h" |
| 13 #include "chrome/browser/pref_service.h" | 14 #include "chrome/browser/pref_service.h" |
| 14 #include "chrome/browser/profile.h" | 15 #include "chrome/browser/profile.h" |
| 15 #include "grit/browser_resources.h" | 16 #include "grit/browser_resources.h" |
| 16 #include "grit/chromium_strings.h" | 17 #include "grit/chromium_strings.h" |
| 17 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 // one more element for the actual preference identifier. | 164 // one more element for the actual preference identifier. |
| 164 const size_t kMinFetchPrefsParamCount = 2; | 165 const size_t kMinFetchPrefsParamCount = 2; |
| 165 if (param_values->GetSize() < kMinFetchPrefsParamCount) | 166 if (param_values->GetSize() < kMinFetchPrefsParamCount) |
| 166 return; | 167 return; |
| 167 | 168 |
| 168 // Get callback JS function name. | 169 // Get callback JS function name. |
| 169 Value* callback; | 170 Value* callback; |
| 170 if (!param_values->Get(0, &callback) || !callback->IsType(Value::TYPE_STRING)) | 171 if (!param_values->Get(0, &callback) || !callback->IsType(Value::TYPE_STRING)) |
| 171 return; | 172 return; |
| 172 | 173 |
| 173 std::wstring callback_function; | 174 string16 callback_function; |
| 174 if (!callback->GetAsString(&callback_function)) | 175 if (!callback->GetAsString(&callback_function)) |
| 175 return; | 176 return; |
| 176 | 177 |
| 177 // Get the list of name for prefs to build the response dictionary. | 178 // Get the list of name for prefs to build the response dictionary. |
| 178 DictionaryValue result_value; | 179 DictionaryValue result_value; |
| 179 Value* list_member; | 180 Value* list_member; |
| 180 | 181 |
| 181 for (size_t i = 1; i < param_values->GetSize(); i++) { | 182 for (size_t i = 1; i < param_values->GetSize(); i++) { |
| 182 if (!param_values->Get(i, &list_member)) | 183 if (!param_values->Get(i, &list_member)) |
| 183 break; | 184 break; |
| 184 | 185 |
| 185 if (!list_member->IsType(Value::TYPE_STRING)) | 186 if (!list_member->IsType(Value::TYPE_STRING)) |
| 186 continue; | 187 continue; |
| 187 | 188 |
| 188 std::string pref_name; | 189 std::string pref_name; |
| 189 if (!list_member->GetAsString(&pref_name)) | 190 if (!list_member->GetAsString(&pref_name)) |
| 190 continue; | 191 continue; |
| 191 | 192 |
| 192 result_value.Set(pref_name.c_str(), FetchPref(pref_name)); | 193 result_value.Set(pref_name.c_str(), FetchPref(pref_name)); |
| 193 } | 194 } |
| 194 dom_ui_->CallJavascriptFunction(callback_function.c_str(), result_value); | 195 dom_ui_->CallJavascriptFunction(UTF16ToWideHack(callback_function).c_str(), |
| 196 result_value); |
| 195 } | 197 } |
| 196 | 198 |
| 197 void CoreOptionsHandler::HandleObservePrefs(const Value* value) { | 199 void CoreOptionsHandler::HandleObservePrefs(const Value* value) { |
| 198 if (!value || !value->IsType(Value::TYPE_LIST)) | 200 if (!value || !value->IsType(Value::TYPE_LIST)) |
| 199 return; | 201 return; |
| 200 | 202 |
| 201 DictionaryValue result_value; | 203 DictionaryValue result_value; |
| 202 const ListValue* list_value = static_cast<const ListValue*>(value); | 204 const ListValue* list_value = static_cast<const ListValue*>(value); |
| 203 | 205 |
| 204 // First param is name is JS callback function name, the rest are pref | 206 // First param is name is JS callback function name, the rest are pref |
| 205 // identifiers that we are observing. | 207 // identifiers that we are observing. |
| 206 const size_t kMinObservePrefsParamCount = 2; | 208 const size_t kMinObservePrefsParamCount = 2; |
| 207 if (list_value->GetSize() < kMinObservePrefsParamCount) | 209 if (list_value->GetSize() < kMinObservePrefsParamCount) |
| 208 return; | 210 return; |
| 209 | 211 |
| 210 // Get preference change callback function name. | 212 // Get preference change callback function name. |
| 211 std::wstring callback_func_name; | 213 string16 callback_func_name; |
| 212 if (!list_value->GetString(0, &callback_func_name)) | 214 if (!list_value->GetString(0, &callback_func_name)) |
| 213 return; | 215 return; |
| 214 | 216 |
| 215 // Get all other parameters - pref identifiers. | 217 // Get all other parameters - pref identifiers. |
| 216 for (size_t i = 1; i < list_value->GetSize(); i++) { | 218 for (size_t i = 1; i < list_value->GetSize(); i++) { |
| 217 Value* list_member; | 219 Value* list_member; |
| 218 if (!list_value->Get(i, &list_member)) | 220 if (!list_value->Get(i, &list_member)) |
| 219 break; | 221 break; |
| 220 | 222 |
| 221 // Just ignore bad pref identifiers for now. | 223 // Just ignore bad pref identifiers for now. |
| 222 std::string pref_name; | 224 std::string pref_name; |
| 223 if (!list_member->IsType(Value::TYPE_STRING) || | 225 if (!list_member->IsType(Value::TYPE_STRING) || |
| 224 !list_member->GetAsString(&pref_name)) | 226 !list_member->GetAsString(&pref_name)) |
| 225 continue; | 227 continue; |
| 226 | 228 |
| 227 if (pref_callback_map_.find(pref_name) == pref_callback_map_.end()) | 229 if (pref_callback_map_.find(pref_name) == pref_callback_map_.end()) |
| 228 ObservePref(pref_name); | 230 ObservePref(pref_name); |
| 229 | 231 |
| 230 pref_callback_map_.insert( | 232 pref_callback_map_.insert( |
| 231 PreferenceCallbackMap::value_type(pref_name, callback_func_name)); | 233 PreferenceCallbackMap::value_type(pref_name, |
| 234 UTF16ToWideHack(callback_func_name))); |
| 232 } | 235 } |
| 233 } | 236 } |
| 234 | 237 |
| 235 void CoreOptionsHandler::HandleSetBooleanPref(const Value* value) { | 238 void CoreOptionsHandler::HandleSetBooleanPref(const Value* value) { |
| 236 HandleSetPref(value, Value::TYPE_BOOLEAN); | 239 HandleSetPref(value, Value::TYPE_BOOLEAN); |
| 237 } | 240 } |
| 238 | 241 |
| 239 void CoreOptionsHandler::HandleSetIntegerPref(const Value* value) { | 242 void CoreOptionsHandler::HandleSetIntegerPref(const Value* value) { |
| 240 HandleSetPref(value, Value::TYPE_INTEGER); | 243 HandleSetPref(value, Value::TYPE_INTEGER); |
| 241 } | 244 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 pref_callback_map_.find(*pref_name); | 281 pref_callback_map_.find(*pref_name); |
| 279 iter != pref_callback_map_.end(); ++iter) { | 282 iter != pref_callback_map_.end(); ++iter) { |
| 280 const std::wstring& callback_function = iter->second; | 283 const std::wstring& callback_function = iter->second; |
| 281 ListValue result_value; | 284 ListValue result_value; |
| 282 result_value.Append(Value::CreateStringValue(pref_name->c_str())); | 285 result_value.Append(Value::CreateStringValue(pref_name->c_str())); |
| 283 result_value.Append(pref->GetValue()->DeepCopy()); | 286 result_value.Append(pref->GetValue()->DeepCopy()); |
| 284 dom_ui_->CallJavascriptFunction(callback_function, result_value); | 287 dom_ui_->CallJavascriptFunction(callback_function, result_value); |
| 285 } | 288 } |
| 286 } | 289 } |
| 287 } | 290 } |
| OLD | NEW |