OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/dom_ui/core_options_handler.h" |
| 6 |
| 7 #include "app/l10n_util.h" |
| 8 #include "app/resource_bundle.h" |
| 9 #include "base/utf_string_conversions.h" |
| 10 #include "base/values.h" |
| 11 #include "chrome/common/notification_service.h" |
| 12 #include "chrome/common/notification_type.h" |
| 13 #include "chrome/browser/pref_service.h" |
| 14 #include "chrome/browser/profile.h" |
| 15 #include "grit/browser_resources.h" |
| 16 #include "grit/chromium_strings.h" |
| 17 #include "grit/generated_resources.h" |
| 18 #include "grit/locale_settings.h" |
| 19 #include "grit/theme_resources.h" |
| 20 |
| 21 CoreOptionsHandler::CoreOptionsHandler() { |
| 22 } |
| 23 |
| 24 void CoreOptionsHandler::GetLocalizedValues( |
| 25 DictionaryValue* localized_strings) { |
| 26 DCHECK(localized_strings); |
| 27 // Main |
| 28 localized_strings->SetString(L"title", |
| 29 l10n_util::GetStringF(IDS_OPTIONS_DIALOG_TITLE, |
| 30 l10n_util::GetString(IDS_PRODUCT_NAME))); |
| 31 |
| 32 #if defined(OS_CHROMEOS) |
| 33 localized_strings->SetString(L"systemPage", |
| 34 l10n_util::GetString(IDS_OPTIONS_SYSTEM_TAB_LABEL)); |
| 35 localized_strings->SetString(L"internetPage", |
| 36 l10n_util::GetString(IDS_OPTIONS_INTERNET_TAB_LABEL)); |
| 37 #endif |
| 38 |
| 39 localized_strings->SetString(L"basicsPage", |
| 40 l10n_util::GetString(IDS_OPTIONS_GENERAL_TAB_LABEL)); |
| 41 localized_strings->SetString(L"personalStuffPage", |
| 42 l10n_util::GetString(IDS_OPTIONS_CONTENT_TAB_LABEL)); |
| 43 localized_strings->SetString(L"underHoodPage", |
| 44 l10n_util::GetString(IDS_OPTIONS_ADVANCED_TAB_LABEL)); |
| 45 } |
| 46 |
| 47 void CoreOptionsHandler::Observe(NotificationType type, |
| 48 const NotificationSource& source, |
| 49 const NotificationDetails& details) { |
| 50 if (type == NotificationType::PREF_CHANGED) |
| 51 NotifyPrefChanged(Details<std::wstring>(details).ptr()); |
| 52 } |
| 53 |
| 54 void CoreOptionsHandler::RegisterMessages() { |
| 55 dom_ui_->RegisterMessageCallback("fetchPrefs", |
| 56 NewCallback(this, &CoreOptionsHandler::HandleFetchPrefs)); |
| 57 dom_ui_->RegisterMessageCallback("observePrefs", |
| 58 NewCallback(this, &CoreOptionsHandler::HandleFetchPrefs)); |
| 59 dom_ui_->RegisterMessageCallback("setBooleanPref", |
| 60 NewCallback(this, &CoreOptionsHandler::HandleSetBooleanPref)); |
| 61 dom_ui_->RegisterMessageCallback("setIntegerPref", |
| 62 NewCallback(this, &CoreOptionsHandler::HandleSetIntegerPref)); |
| 63 dom_ui_->RegisterMessageCallback("setStringPref", |
| 64 NewCallback(this, &CoreOptionsHandler::HandleSetStringPref)); |
| 65 } |
| 66 |
| 67 |
| 68 void CoreOptionsHandler::HandleFetchPrefs(const Value* value) { |
| 69 if (!value || !value->IsType(Value::TYPE_LIST)) |
| 70 return; |
| 71 |
| 72 const ListValue* param_values = static_cast<const ListValue*>(value); |
| 73 |
| 74 // First param is name of callback function, the second one is the value of |
| 75 // context that is just passed through - so, there needs to be at least one |
| 76 // more for the actual preference identifier. |
| 77 const size_t kMinFetchPrefsParamCount = 3; |
| 78 if (param_values->GetSize() < kMinFetchPrefsParamCount) |
| 79 return; |
| 80 |
| 81 size_t idx = param_values->GetSize(); |
| 82 LOG(INFO) << "param_values->GetSize() = " << idx; |
| 83 // Get callback JS function name. |
| 84 Value* callback; |
| 85 if (!param_values->Get(0, &callback) || !callback->IsType(Value::TYPE_STRING)) |
| 86 return; |
| 87 |
| 88 std::wstring callback_function; |
| 89 if (!callback->GetAsString(&callback_function)) |
| 90 return; |
| 91 |
| 92 // Get context param (just passthrough value) |
| 93 Value* context; |
| 94 if (!param_values->Get(1, &context) || !context) |
| 95 return; |
| 96 |
| 97 // Get the list of name for prefs to build the response dictionary. |
| 98 DictionaryValue result_value; |
| 99 Value* list_member; |
| 100 DCHECK(dom_ui_); |
| 101 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); |
| 102 |
| 103 for (size_t i = 2; i < param_values->GetSize(); i++) { |
| 104 if (!param_values->Get(i, &list_member)) |
| 105 break; |
| 106 |
| 107 if (!list_member->IsType(Value::TYPE_STRING)) |
| 108 continue; |
| 109 |
| 110 std::wstring pref_name; |
| 111 if (!list_member->GetAsString(&pref_name)) |
| 112 continue; |
| 113 |
| 114 const PrefService::Preference* pref = |
| 115 pref_service->FindPreference(pref_name.c_str()); |
| 116 result_value.Set(pref_name.c_str(), |
| 117 pref ? pref->GetValue()->DeepCopy() : Value::CreateNullValue()); |
| 118 } |
| 119 dom_ui_->CallJavascriptFunction(callback_function.c_str(), *context, |
| 120 result_value); |
| 121 } |
| 122 |
| 123 void CoreOptionsHandler::HandleObservePefs(const Value* value) { |
| 124 if (!value || !value->IsType(Value::TYPE_LIST)) |
| 125 return; |
| 126 |
| 127 DCHECK(dom_ui_); |
| 128 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); |
| 129 DictionaryValue result_value; |
| 130 const ListValue* list_value = static_cast<const ListValue*>(value); |
| 131 Value* list_member; |
| 132 for (size_t i = 0; i < list_value->GetSize(); i++) { |
| 133 if (!list_value->Get(i, &list_member)) |
| 134 break; |
| 135 |
| 136 if (!list_member->IsType(Value::TYPE_STRING)) |
| 137 continue; |
| 138 |
| 139 std::wstring pref_name; |
| 140 if (!list_member->GetAsString(&pref_name)) |
| 141 continue; |
| 142 |
| 143 pref_service->AddPrefObserver(pref_name.c_str(), this); |
| 144 } |
| 145 } |
| 146 |
| 147 void CoreOptionsHandler::HandleSetBooleanPref(const Value* value) { |
| 148 HandleSetPref(value, Value::TYPE_BOOLEAN); |
| 149 } |
| 150 |
| 151 void CoreOptionsHandler::HandleSetIntegerPref(const Value* value) { |
| 152 HandleSetPref(value, Value::TYPE_INTEGER); |
| 153 } |
| 154 |
| 155 void CoreOptionsHandler::HandleSetStringPref(const Value* value) { |
| 156 HandleSetPref(value, Value::TYPE_STRING); |
| 157 } |
| 158 |
| 159 void CoreOptionsHandler::HandleSetPref(const Value* value, |
| 160 Value::ValueType type) { |
| 161 if (!value || !value->IsType(Value::TYPE_LIST)) |
| 162 return; |
| 163 const ListValue* param_values = static_cast<const ListValue*>(value); |
| 164 size_t size = param_values->GetSize(); |
| 165 LOG(INFO) << "Array size = " << size; |
| 166 if (param_values->GetSize() != 2) |
| 167 return; |
| 168 |
| 169 DCHECK(dom_ui_); |
| 170 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); |
| 171 |
| 172 Value* name_element; |
| 173 std::wstring pref_name; |
| 174 if (!param_values->Get(0, &name_element) || |
| 175 !name_element->IsType(Value::TYPE_STRING) || |
| 176 !name_element->GetAsString(&pref_name)) |
| 177 return; |
| 178 |
| 179 Value* value_element; |
| 180 std::string value_string; |
| 181 if (!param_values->Get(1, &value_element) || |
| 182 !value_element->IsType(Value::TYPE_STRING) || |
| 183 !value_element->GetAsString(&value_string)) |
| 184 return; |
| 185 |
| 186 switch (type) { |
| 187 case Value::TYPE_BOOLEAN: |
| 188 pref_service->SetBoolean(pref_name.c_str(), value_string == "true"); |
| 189 break; |
| 190 case Value::TYPE_INTEGER: |
| 191 int int_value; |
| 192 if (StringToInt(value_string, &int_value)) |
| 193 pref_service->SetInteger(pref_name.c_str(), int_value); |
| 194 break; |
| 195 case Value::TYPE_STRING: |
| 196 pref_service->SetString(pref_name.c_str(), UTF8ToWide(value_string)); |
| 197 break; |
| 198 default: |
| 199 NOTREACHED(); |
| 200 } |
| 201 } |
| 202 |
| 203 void CoreOptionsHandler::NotifyPrefChanged(const std::wstring* pref_name) { |
| 204 DCHECK(dom_ui_); |
| 205 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); |
| 206 const PrefService::Preference* pref = |
| 207 pref_service->FindPreference(pref_name->c_str()); |
| 208 if (pref) { |
| 209 DictionaryValue result_value; |
| 210 result_value.Set(pref_name->c_str(), pref->GetValue()->DeepCopy()); |
| 211 dom_ui_->CallJavascriptFunction(L"prefsChanged", result_value); |
| 212 } |
| 213 } |
OLD | NEW |