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/string16.h" |
9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/metrics/user_metrics.h" |
| 13 #include "chrome/browser/pref_service.h" |
| 14 #include "chrome/browser/profile.h" |
12 #include "chrome/common/notification_service.h" | 15 #include "chrome/common/notification_service.h" |
13 #include "chrome/common/notification_type.h" | 16 #include "chrome/common/notification_type.h" |
14 #include "chrome/browser/pref_service.h" | |
15 #include "chrome/browser/profile.h" | |
16 #include "grit/browser_resources.h" | 17 #include "grit/browser_resources.h" |
17 #include "grit/chromium_strings.h" | 18 #include "grit/chromium_strings.h" |
18 #include "grit/generated_resources.h" | 19 #include "grit/generated_resources.h" |
19 #include "grit/locale_settings.h" | 20 #include "grit/locale_settings.h" |
20 #include "grit/theme_resources.h" | 21 #include "grit/theme_resources.h" |
21 | 22 |
22 CoreOptionsHandler::CoreOptionsHandler() { | 23 CoreOptionsHandler::CoreOptionsHandler() { |
23 } | 24 } |
24 | 25 |
25 CoreOptionsHandler::~CoreOptionsHandler() { | 26 CoreOptionsHandler::~CoreOptionsHandler() { |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 dom_ui_->RegisterMessageCallback("observePrefs", | 104 dom_ui_->RegisterMessageCallback("observePrefs", |
104 NewCallback(this, &CoreOptionsHandler::HandleObservePrefs)); | 105 NewCallback(this, &CoreOptionsHandler::HandleObservePrefs)); |
105 dom_ui_->RegisterMessageCallback("setBooleanPref", | 106 dom_ui_->RegisterMessageCallback("setBooleanPref", |
106 NewCallback(this, &CoreOptionsHandler::HandleSetBooleanPref)); | 107 NewCallback(this, &CoreOptionsHandler::HandleSetBooleanPref)); |
107 dom_ui_->RegisterMessageCallback("setIntegerPref", | 108 dom_ui_->RegisterMessageCallback("setIntegerPref", |
108 NewCallback(this, &CoreOptionsHandler::HandleSetIntegerPref)); | 109 NewCallback(this, &CoreOptionsHandler::HandleSetIntegerPref)); |
109 dom_ui_->RegisterMessageCallback("setStringPref", | 110 dom_ui_->RegisterMessageCallback("setStringPref", |
110 NewCallback(this, &CoreOptionsHandler::HandleSetStringPref)); | 111 NewCallback(this, &CoreOptionsHandler::HandleSetStringPref)); |
111 dom_ui_->RegisterMessageCallback("setObjectPref", | 112 dom_ui_->RegisterMessageCallback("setObjectPref", |
112 NewCallback(this, &CoreOptionsHandler::HandleSetObjectPref)); | 113 NewCallback(this, &CoreOptionsHandler::HandleSetObjectPref)); |
| 114 dom_ui_->RegisterMessageCallback("coreOptionsUserMetricsAction", |
| 115 NewCallback(this, &CoreOptionsHandler::HandleUserMetricsAction)); |
113 } | 116 } |
114 | 117 |
115 void CoreOptionsHandler::HandleInitialize(const ListValue* args) { | 118 void CoreOptionsHandler::HandleInitialize(const ListValue* args) { |
116 static_cast<OptionsUI*>(dom_ui_)->InitializeHandlers(); | 119 static_cast<OptionsUI*>(dom_ui_)->InitializeHandlers(); |
117 } | 120 } |
118 | 121 |
119 Value* CoreOptionsHandler::FetchPref(const std::string& pref_name) { | 122 Value* CoreOptionsHandler::FetchPref(const std::string& pref_name) { |
120 DCHECK(dom_ui_); | 123 DCHECK(dom_ui_); |
121 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); | 124 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); |
122 | 125 |
(...skipping 13 matching lines...) Expand all Loading... |
136 } | 139 } |
137 | 140 |
138 void CoreOptionsHandler::ObservePref(const std::string& pref_name) { | 141 void CoreOptionsHandler::ObservePref(const std::string& pref_name) { |
139 DCHECK(dom_ui_); | 142 DCHECK(dom_ui_); |
140 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); | 143 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); |
141 pref_service->AddPrefObserver(pref_name.c_str(), this); | 144 pref_service->AddPrefObserver(pref_name.c_str(), this); |
142 } | 145 } |
143 | 146 |
144 void CoreOptionsHandler::SetPref(const std::string& pref_name, | 147 void CoreOptionsHandler::SetPref(const std::string& pref_name, |
145 Value::ValueType pref_type, | 148 Value::ValueType pref_type, |
146 const std::string& value_string) { | 149 const std::string& value_string, |
| 150 const std::string& metric) { |
147 DCHECK(dom_ui_); | 151 DCHECK(dom_ui_); |
148 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); | 152 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); |
149 | 153 |
150 switch (pref_type) { | 154 switch (pref_type) { |
151 case Value::TYPE_BOOLEAN: | 155 case Value::TYPE_BOOLEAN: |
152 pref_service->SetBoolean(pref_name.c_str(), value_string == "true"); | 156 pref_service->SetBoolean(pref_name.c_str(), value_string == "true"); |
153 break; | 157 break; |
154 case Value::TYPE_INTEGER: | 158 case Value::TYPE_INTEGER: |
155 int int_value; | 159 int int_value; |
156 if (base::StringToInt(value_string, &int_value)) | 160 if (base::StringToInt(value_string, &int_value)) |
157 pref_service->SetInteger(pref_name.c_str(), int_value); | 161 pref_service->SetInteger(pref_name.c_str(), int_value); |
158 break; | 162 break; |
159 case Value::TYPE_STRING: | 163 case Value::TYPE_STRING: |
160 pref_service->SetString(pref_name.c_str(), value_string); | 164 pref_service->SetString(pref_name.c_str(), value_string); |
161 break; | 165 break; |
162 default: | 166 default: |
163 NOTREACHED(); | 167 NOTREACHED(); |
164 } | 168 } |
| 169 |
| 170 ProcessUserMetric(pref_type, value_string, metric); |
| 171 } |
| 172 |
| 173 void CoreOptionsHandler::ProcessUserMetric(Value::ValueType pref_type, |
| 174 const std::string& value_string, |
| 175 const std::string& metric) { |
| 176 if (metric.empty()) |
| 177 return; |
| 178 |
| 179 std::string metric_string = metric; |
| 180 if (pref_type == Value::TYPE_BOOLEAN) |
| 181 metric_string += (value_string == "true" ? "_Enable" : "_Disable"); |
| 182 |
| 183 UserMetricsRecordAction(UserMetricsAction(metric_string.c_str()), |
| 184 dom_ui_->GetProfile()->GetPrefs()); |
165 } | 185 } |
166 | 186 |
167 void CoreOptionsHandler::HandleFetchPrefs(const ListValue* args) { | 187 void CoreOptionsHandler::HandleFetchPrefs(const ListValue* args) { |
168 // First param is name of callback function, so, there needs to be at least | 188 // First param is name of callback function, so, there needs to be at least |
169 // one more element for the actual preference identifier. | 189 // one more element for the actual preference identifier. |
170 const size_t kMinFetchPrefsParamCount = 2; | 190 const size_t kMinFetchPrefsParamCount = 2; |
171 if (args->GetSize() < kMinFetchPrefsParamCount) | 191 if (args->GetSize() < kMinFetchPrefsParamCount) |
172 return; | 192 return; |
173 | 193 |
174 // Get callback JS function name. | 194 // Get callback JS function name. |
(...skipping 20 matching lines...) Expand all Loading... |
195 if (!list_member->GetAsString(&pref_name)) | 215 if (!list_member->GetAsString(&pref_name)) |
196 continue; | 216 continue; |
197 | 217 |
198 result_value.Set(pref_name.c_str(), FetchPref(pref_name)); | 218 result_value.Set(pref_name.c_str(), FetchPref(pref_name)); |
199 } | 219 } |
200 dom_ui_->CallJavascriptFunction(UTF16ToWideHack(callback_function).c_str(), | 220 dom_ui_->CallJavascriptFunction(UTF16ToWideHack(callback_function).c_str(), |
201 result_value); | 221 result_value); |
202 } | 222 } |
203 | 223 |
204 void CoreOptionsHandler::HandleObservePrefs(const ListValue* args) { | 224 void CoreOptionsHandler::HandleObservePrefs(const ListValue* args) { |
205 DictionaryValue result_value; | |
206 | |
207 // First param is name is JS callback function name, the rest are pref | 225 // First param is name is JS callback function name, the rest are pref |
208 // identifiers that we are observing. | 226 // identifiers that we are observing. |
209 const size_t kMinObservePrefsParamCount = 2; | 227 const size_t kMinObservePrefsParamCount = 2; |
210 if (args->GetSize() < kMinObservePrefsParamCount) | 228 if (args->GetSize() < kMinObservePrefsParamCount) |
211 return; | 229 return; |
212 | 230 |
213 // Get preference change callback function name. | 231 // Get preference change callback function name. |
214 string16 callback_func_name; | 232 string16 callback_func_name; |
215 if (!args->GetString(0, &callback_func_name)) | 233 if (!args->GetString(0, &callback_func_name)) |
216 return; | 234 return; |
(...skipping 30 matching lines...) Expand all Loading... |
247 void CoreOptionsHandler::HandleSetStringPref(const ListValue* args) { | 265 void CoreOptionsHandler::HandleSetStringPref(const ListValue* args) { |
248 HandleSetPref(args, Value::TYPE_STRING); | 266 HandleSetPref(args, Value::TYPE_STRING); |
249 } | 267 } |
250 | 268 |
251 void CoreOptionsHandler::HandleSetObjectPref(const ListValue* args) { | 269 void CoreOptionsHandler::HandleSetObjectPref(const ListValue* args) { |
252 HandleSetPref(args, Value::TYPE_NULL); | 270 HandleSetPref(args, Value::TYPE_NULL); |
253 } | 271 } |
254 | 272 |
255 void CoreOptionsHandler::HandleSetPref(const ListValue* args, | 273 void CoreOptionsHandler::HandleSetPref(const ListValue* args, |
256 Value::ValueType type) { | 274 Value::ValueType type) { |
257 if (args->GetSize() != 2) | 275 if (args->GetSize() < 2) |
258 return; | 276 return; |
259 | 277 |
260 std::string pref_name; | 278 std::string pref_name; |
261 if (!args->GetString(0, &pref_name)) | 279 if (!args->GetString(0, &pref_name)) |
262 return; | 280 return; |
263 | 281 |
264 std::string value_string; | 282 std::string value_string; |
265 if (!args->GetString(1, &value_string)) | 283 if (!args->GetString(1, &value_string)) |
266 return; | 284 return; |
267 | 285 |
268 SetPref(pref_name, type, value_string); | 286 std::string metric; |
| 287 if (args->GetSize() > 2) |
| 288 args->GetString(2, &metric); |
| 289 |
| 290 SetPref(pref_name, type, value_string, metric); |
| 291 } |
| 292 |
| 293 void CoreOptionsHandler::HandleUserMetricsAction(const ListValue* args) { |
| 294 std::string metric = WideToUTF8(ExtractStringValue(args)); |
| 295 if (!metric.empty()) |
| 296 UserMetricsRecordAction(UserMetricsAction(metric.c_str()), NULL); |
269 } | 297 } |
270 | 298 |
271 void CoreOptionsHandler::NotifyPrefChanged(const std::string* pref_name) { | 299 void CoreOptionsHandler::NotifyPrefChanged(const std::string* pref_name) { |
272 DCHECK(pref_name); | 300 DCHECK(pref_name); |
273 DCHECK(dom_ui_); | 301 DCHECK(dom_ui_); |
274 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); | 302 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); |
275 const PrefService::Preference* pref = | 303 const PrefService::Preference* pref = |
276 pref_service->FindPreference(pref_name->c_str()); | 304 pref_service->FindPreference(pref_name->c_str()); |
277 if (pref) { | 305 if (pref) { |
278 for (PreferenceCallbackMap::const_iterator iter = | 306 for (PreferenceCallbackMap::const_iterator iter = |
279 pref_callback_map_.find(*pref_name); | 307 pref_callback_map_.find(*pref_name); |
280 iter != pref_callback_map_.end(); ++iter) { | 308 iter != pref_callback_map_.end(); ++iter) { |
281 const std::wstring& callback_function = iter->second; | 309 const std::wstring& callback_function = iter->second; |
282 ListValue result_value; | 310 ListValue result_value; |
283 result_value.Append(Value::CreateStringValue(pref_name->c_str())); | 311 result_value.Append(Value::CreateStringValue(pref_name->c_str())); |
284 | 312 |
285 DictionaryValue* dict = new DictionaryValue; | 313 DictionaryValue* dict = new DictionaryValue; |
286 dict->Set("value", pref->GetValue()->DeepCopy()); | 314 dict->Set("value", pref->GetValue()->DeepCopy()); |
287 dict->SetBoolean("managed", pref->IsManaged()); | 315 dict->SetBoolean("managed", pref->IsManaged()); |
288 result_value.Append(dict); | 316 result_value.Append(dict); |
289 | 317 |
290 dom_ui_->CallJavascriptFunction(callback_function, result_value); | 318 dom_ui_->CallJavascriptFunction(callback_function, result_value); |
291 } | 319 } |
292 } | 320 } |
293 } | 321 } |
294 | 322 |
OLD | NEW |