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" |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 for (PreferenceCallbackMap::const_iterator iter = pref_callback_map_.begin(); | 81 for (PreferenceCallbackMap::const_iterator iter = pref_callback_map_.begin(); |
82 iter != pref_callback_map_.end(); | 82 iter != pref_callback_map_.end(); |
83 ++iter) { | 83 ++iter) { |
84 if (last_pref != iter->first) { | 84 if (last_pref != iter->first) { |
85 StopObservingPref(iter->first); | 85 StopObservingPref(iter->first); |
86 last_pref = iter->first; | 86 last_pref = iter->first; |
87 } | 87 } |
88 } | 88 } |
89 } | 89 } |
90 | 90 |
| 91 DOMMessageHandler* CoreOptionsHandler::Attach(DOMUI* dom_ui) { |
| 92 DOMMessageHandler* result = DOMMessageHandler::Attach(dom_ui); |
| 93 DCHECK(dom_ui_); |
| 94 registrar_.Init(dom_ui_->GetProfile()->GetPrefs()); |
| 95 return result; |
| 96 } |
| 97 |
91 void CoreOptionsHandler::Observe(NotificationType type, | 98 void CoreOptionsHandler::Observe(NotificationType type, |
92 const NotificationSource& source, | 99 const NotificationSource& source, |
93 const NotificationDetails& details) { | 100 const NotificationDetails& details) { |
94 if (type == NotificationType::PREF_CHANGED) | 101 if (type == NotificationType::PREF_CHANGED) |
95 NotifyPrefChanged(Details<std::string>(details).ptr()); | 102 NotifyPrefChanged(Details<std::string>(details).ptr()); |
96 } | 103 } |
97 | 104 |
98 void CoreOptionsHandler::RegisterMessages() { | 105 void CoreOptionsHandler::RegisterMessages() { |
99 dom_ui_->RegisterMessageCallback("coreOptionsInitialize", | 106 dom_ui_->RegisterMessageCallback("coreOptionsInitialize", |
100 NewCallback(this, &CoreOptionsHandler::HandleInitialize)); | 107 NewCallback(this, &CoreOptionsHandler::HandleInitialize)); |
(...skipping 30 matching lines...) Expand all Loading... |
131 dict->Set("value", pref->GetValue()->DeepCopy()); | 138 dict->Set("value", pref->GetValue()->DeepCopy()); |
132 dict->SetBoolean("managed", pref->IsManaged()); | 139 dict->SetBoolean("managed", pref->IsManaged()); |
133 return_value = dict; | 140 return_value = dict; |
134 } else { | 141 } else { |
135 return_value = Value::CreateNullValue(); | 142 return_value = Value::CreateNullValue(); |
136 } | 143 } |
137 return return_value; | 144 return return_value; |
138 } | 145 } |
139 | 146 |
140 void CoreOptionsHandler::ObservePref(const std::string& pref_name) { | 147 void CoreOptionsHandler::ObservePref(const std::string& pref_name) { |
141 DCHECK(dom_ui_); | 148 registrar_.Add(pref_name.c_str(), this); |
142 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); | |
143 pref_service->AddPrefObserver(pref_name.c_str(), this); | |
144 } | 149 } |
145 | 150 |
146 void CoreOptionsHandler::SetPref(const std::string& pref_name, | 151 void CoreOptionsHandler::SetPref(const std::string& pref_name, |
147 Value::ValueType pref_type, | 152 Value::ValueType pref_type, |
148 const std::string& value_string, | 153 const std::string& value_string, |
149 const std::string& metric) { | 154 const std::string& metric) { |
150 DCHECK(dom_ui_); | 155 DCHECK(dom_ui_); |
151 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); | 156 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); |
152 | 157 |
153 switch (pref_type) { | 158 switch (pref_type) { |
(...skipping 24 matching lines...) Expand all Loading... |
178 return; | 183 return; |
179 | 184 |
180 std::string metric_string = metric; | 185 std::string metric_string = metric; |
181 if (pref_type == Value::TYPE_BOOLEAN) | 186 if (pref_type == Value::TYPE_BOOLEAN) |
182 metric_string += (value_string == "true" ? "_Enable" : "_Disable"); | 187 metric_string += (value_string == "true" ? "_Enable" : "_Disable"); |
183 | 188 |
184 UserMetricsRecordAction(UserMetricsAction(metric_string.c_str())); | 189 UserMetricsRecordAction(UserMetricsAction(metric_string.c_str())); |
185 } | 190 } |
186 | 191 |
187 void CoreOptionsHandler::StopObservingPref(const std::string& path) { | 192 void CoreOptionsHandler::StopObservingPref(const std::string& path) { |
188 DCHECK(dom_ui_); | 193 registrar_.Remove(path.c_str(), this); |
189 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); | |
190 pref_service->RemovePrefObserver(path.c_str(), this); | |
191 } | 194 } |
192 | 195 |
193 void CoreOptionsHandler::HandleFetchPrefs(const ListValue* args) { | 196 void CoreOptionsHandler::HandleFetchPrefs(const ListValue* args) { |
194 // First param is name of callback function, so, there needs to be at least | 197 // First param is name of callback function, so, there needs to be at least |
195 // one more element for the actual preference identifier. | 198 // one more element for the actual preference identifier. |
196 const size_t kMinFetchPrefsParamCount = 2; | 199 const size_t kMinFetchPrefsParamCount = 2; |
197 if (args->GetSize() < kMinFetchPrefsParamCount) | 200 if (args->GetSize() < kMinFetchPrefsParamCount) |
198 return; | 201 return; |
199 | 202 |
200 // Get callback JS function name. | 203 // Get callback JS function name. |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 DictionaryValue* dict = new DictionaryValue; | 322 DictionaryValue* dict = new DictionaryValue; |
320 dict->Set("value", pref->GetValue()->DeepCopy()); | 323 dict->Set("value", pref->GetValue()->DeepCopy()); |
321 dict->SetBoolean("managed", pref->IsManaged()); | 324 dict->SetBoolean("managed", pref->IsManaged()); |
322 result_value.Append(dict); | 325 result_value.Append(dict); |
323 | 326 |
324 dom_ui_->CallJavascriptFunction(callback_function, result_value); | 327 dom_ui_->CallJavascriptFunction(callback_function, result_value); |
325 } | 328 } |
326 } | 329 } |
327 } | 330 } |
328 | 331 |
OLD | NEW |