Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(819)

Side by Side Diff: chrome/browser/dom_ui/core_options_handler.cc

Issue 2835009: Split options page code/html into its own set of files (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 10 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/dom_ui/core_options_handler.h ('k') | chrome/browser/dom_ui/options_ui.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "chrome/common/notification_service.h" 11 #include "chrome/common/notification_service.h"
12 #include "chrome/common/notification_type.h" 12 #include "chrome/common/notification_type.h"
13 #include "chrome/browser/pref_service.h" 13 #include "chrome/browser/pref_service.h"
14 #include "chrome/browser/profile.h" 14 #include "chrome/browser/profile.h"
15 #include "grit/browser_resources.h" 15 #include "grit/browser_resources.h"
16 #include "grit/chromium_strings.h" 16 #include "grit/chromium_strings.h"
17 #include "grit/generated_resources.h" 17 #include "grit/generated_resources.h"
18 #include "grit/locale_settings.h" 18 #include "grit/locale_settings.h"
19 #include "grit/theme_resources.h" 19 #include "grit/theme_resources.h"
20 20
21 CoreOptionsHandler::CoreOptionsHandler() { 21 CoreOptionsHandler::CoreOptionsHandler() {
22 } 22 }
23 23
24 CoreOptionsHandler::~CoreOptionsHandler() {
25 // Remove registered preference change notification observers.
26 DCHECK(dom_ui_);
27 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs();
28 std::wstring last_pref;
29 for (PreferenceCallbackMap::const_iterator iter = pref_callback_map_.begin();
30 iter != pref_callback_map_.end();
31 ++iter) {
32 if (last_pref != iter->first) {
33 pref_service->RemovePrefObserver(iter->first.c_str(), this);
34 last_pref = iter->first;
35 }
36 }
37 }
38
24 void CoreOptionsHandler::GetLocalizedValues( 39 void CoreOptionsHandler::GetLocalizedValues(
25 DictionaryValue* localized_strings) { 40 DictionaryValue* localized_strings) {
26 DCHECK(localized_strings); 41 DCHECK(localized_strings);
27 // Main 42 // Main
28 localized_strings->SetString(L"title", 43 localized_strings->SetString(L"title",
29 l10n_util::GetStringF(IDS_OPTIONS_DIALOG_TITLE, 44 l10n_util::GetStringF(IDS_OPTIONS_DIALOG_TITLE,
30 l10n_util::GetString(IDS_PRODUCT_NAME))); 45 l10n_util::GetString(IDS_PRODUCT_NAME)));
31 46
32 #if defined(OS_CHROMEOS) 47 #if defined(OS_CHROMEOS)
33 localized_strings->SetString(L"systemPage", 48 localized_strings->SetString(L"systemPage",
(...skipping 14 matching lines...) Expand all
48 const NotificationSource& source, 63 const NotificationSource& source,
49 const NotificationDetails& details) { 64 const NotificationDetails& details) {
50 if (type == NotificationType::PREF_CHANGED) 65 if (type == NotificationType::PREF_CHANGED)
51 NotifyPrefChanged(Details<std::wstring>(details).ptr()); 66 NotifyPrefChanged(Details<std::wstring>(details).ptr());
52 } 67 }
53 68
54 void CoreOptionsHandler::RegisterMessages() { 69 void CoreOptionsHandler::RegisterMessages() {
55 dom_ui_->RegisterMessageCallback("fetchPrefs", 70 dom_ui_->RegisterMessageCallback("fetchPrefs",
56 NewCallback(this, &CoreOptionsHandler::HandleFetchPrefs)); 71 NewCallback(this, &CoreOptionsHandler::HandleFetchPrefs));
57 dom_ui_->RegisterMessageCallback("observePrefs", 72 dom_ui_->RegisterMessageCallback("observePrefs",
58 NewCallback(this, &CoreOptionsHandler::HandleFetchPrefs)); 73 NewCallback(this, &CoreOptionsHandler::HandleObservePefs));
59 dom_ui_->RegisterMessageCallback("setBooleanPref", 74 dom_ui_->RegisterMessageCallback("setBooleanPref",
60 NewCallback(this, &CoreOptionsHandler::HandleSetBooleanPref)); 75 NewCallback(this, &CoreOptionsHandler::HandleSetBooleanPref));
61 dom_ui_->RegisterMessageCallback("setIntegerPref", 76 dom_ui_->RegisterMessageCallback("setIntegerPref",
62 NewCallback(this, &CoreOptionsHandler::HandleSetIntegerPref)); 77 NewCallback(this, &CoreOptionsHandler::HandleSetIntegerPref));
63 dom_ui_->RegisterMessageCallback("setStringPref", 78 dom_ui_->RegisterMessageCallback("setStringPref",
64 NewCallback(this, &CoreOptionsHandler::HandleSetStringPref)); 79 NewCallback(this, &CoreOptionsHandler::HandleSetStringPref));
65 } 80 }
66 81
67 82
68 void CoreOptionsHandler::HandleFetchPrefs(const Value* value) { 83 void CoreOptionsHandler::HandleFetchPrefs(const Value* value) {
69 if (!value || !value->IsType(Value::TYPE_LIST)) 84 if (!value || !value->IsType(Value::TYPE_LIST))
70 return; 85 return;
71 86
72 const ListValue* param_values = static_cast<const ListValue*>(value); 87 const ListValue* param_values = static_cast<const ListValue*>(value);
73 88
74 // First param is name of callback function, the second one is the value of 89 // First param is name of callback function, so, there needs to be at least
75 // context that is just passed through - so, there needs to be at least one 90 // one more element for the actual preference identifier.
76 // more for the actual preference identifier. 91 const size_t kMinFetchPrefsParamCount = 2;
77 const size_t kMinFetchPrefsParamCount = 3;
78 if (param_values->GetSize() < kMinFetchPrefsParamCount) 92 if (param_values->GetSize() < kMinFetchPrefsParamCount)
79 return; 93 return;
80 94
81 size_t idx = param_values->GetSize(); 95 size_t idx = param_values->GetSize();
82 LOG(INFO) << "param_values->GetSize() = " << idx; 96 LOG(INFO) << "param_values->GetSize() = " << idx;
83 // Get callback JS function name. 97 // Get callback JS function name.
84 Value* callback; 98 Value* callback;
85 if (!param_values->Get(0, &callback) || !callback->IsType(Value::TYPE_STRING)) 99 if (!param_values->Get(0, &callback) || !callback->IsType(Value::TYPE_STRING))
86 return; 100 return;
87 101
88 std::wstring callback_function; 102 std::wstring callback_function;
89 if (!callback->GetAsString(&callback_function)) 103 if (!callback->GetAsString(&callback_function))
90 return; 104 return;
91 105
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. 106 // Get the list of name for prefs to build the response dictionary.
98 DictionaryValue result_value; 107 DictionaryValue result_value;
99 Value* list_member; 108 Value* list_member;
100 DCHECK(dom_ui_); 109 DCHECK(dom_ui_);
101 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); 110 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs();
102 111
103 for (size_t i = 2; i < param_values->GetSize(); i++) { 112 for (size_t i = 1; i < param_values->GetSize(); i++) {
104 if (!param_values->Get(i, &list_member)) 113 if (!param_values->Get(i, &list_member))
105 break; 114 break;
106 115
107 if (!list_member->IsType(Value::TYPE_STRING)) 116 if (!list_member->IsType(Value::TYPE_STRING))
108 continue; 117 continue;
109 118
110 std::wstring pref_name; 119 std::wstring pref_name;
111 if (!list_member->GetAsString(&pref_name)) 120 if (!list_member->GetAsString(&pref_name))
112 continue; 121 continue;
113 122
114 const PrefService::Preference* pref = 123 const PrefService::Preference* pref =
115 pref_service->FindPreference(pref_name.c_str()); 124 pref_service->FindPreference(pref_name.c_str());
116 result_value.Set(pref_name.c_str(), 125 result_value.Set(pref_name.c_str(),
117 pref ? pref->GetValue()->DeepCopy() : Value::CreateNullValue()); 126 pref ? pref->GetValue()->DeepCopy() : Value::CreateNullValue());
118 } 127 }
119 dom_ui_->CallJavascriptFunction(callback_function.c_str(), *context, 128 dom_ui_->CallJavascriptFunction(callback_function.c_str(), result_value);
120 result_value);
121 } 129 }
122 130
123 void CoreOptionsHandler::HandleObservePefs(const Value* value) { 131 void CoreOptionsHandler::HandleObservePefs(const Value* value) {
124 if (!value || !value->IsType(Value::TYPE_LIST)) 132 if (!value || !value->IsType(Value::TYPE_LIST))
125 return; 133 return;
126 134
127 DCHECK(dom_ui_); 135 DCHECK(dom_ui_);
128 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); 136 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs();
129 DictionaryValue result_value; 137 DictionaryValue result_value;
130 const ListValue* list_value = static_cast<const ListValue*>(value); 138 const ListValue* list_value = static_cast<const ListValue*>(value);
131 Value* list_member; 139
132 for (size_t i = 0; i < list_value->GetSize(); i++) { 140 // First param is name is JS callback function name, the rest are pref
141 // identifiers that we are observing.
142 const size_t kMinObservePrefsParamCount = 2;
143 if (list_value->GetSize() < kMinObservePrefsParamCount)
144 return;
145
146 // Get preference change callback function name.
147 std::wstring callback_func_name;
148 Value* list_member = 0;
149 if (!list_value->Get(0, &list_member) ||
150 !list_member->IsType(Value::TYPE_STRING) ||
151 !list_member->GetAsString(&callback_func_name))
152 return;
153
154 // Get all other parameters - pref identifiers.
155 for (size_t i = 1; i < list_value->GetSize(); i++) {
133 if (!list_value->Get(i, &list_member)) 156 if (!list_value->Get(i, &list_member))
134 break; 157 break;
135 158
136 if (!list_member->IsType(Value::TYPE_STRING)) 159 // Just ignore bad pref identifiers for now.
160 std::wstring pref_name;
161 if (!list_member->IsType(Value::TYPE_STRING) ||
162 !list_member->GetAsString(&pref_name))
137 continue; 163 continue;
138 164
139 std::wstring pref_name; 165 if (pref_callback_map_.find(pref_name) == pref_callback_map_.end())
140 if (!list_member->GetAsString(&pref_name)) 166 pref_service->AddPrefObserver(pref_name.c_str(), this);
141 continue;
142 167
143 pref_service->AddPrefObserver(pref_name.c_str(), this); 168 pref_callback_map_.insert(
169 PreferenceCallbackMap::value_type(pref_name, callback_func_name));
144 } 170 }
145 } 171 }
146 172
147 void CoreOptionsHandler::HandleSetBooleanPref(const Value* value) { 173 void CoreOptionsHandler::HandleSetBooleanPref(const Value* value) {
148 HandleSetPref(value, Value::TYPE_BOOLEAN); 174 HandleSetPref(value, Value::TYPE_BOOLEAN);
149 } 175 }
150 176
151 void CoreOptionsHandler::HandleSetIntegerPref(const Value* value) { 177 void CoreOptionsHandler::HandleSetIntegerPref(const Value* value) {
152 HandleSetPref(value, Value::TYPE_INTEGER); 178 HandleSetPref(value, Value::TYPE_INTEGER);
153 } 179 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 break; 220 break;
195 case Value::TYPE_STRING: 221 case Value::TYPE_STRING:
196 pref_service->SetString(pref_name.c_str(), value_string); 222 pref_service->SetString(pref_name.c_str(), value_string);
197 break; 223 break;
198 default: 224 default:
199 NOTREACHED(); 225 NOTREACHED();
200 } 226 }
201 } 227 }
202 228
203 void CoreOptionsHandler::NotifyPrefChanged(const std::wstring* pref_name) { 229 void CoreOptionsHandler::NotifyPrefChanged(const std::wstring* pref_name) {
230 DCHECK(pref_name);
204 DCHECK(dom_ui_); 231 DCHECK(dom_ui_);
205 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs(); 232 PrefService* pref_service = dom_ui_->GetProfile()->GetPrefs();
206 const PrefService::Preference* pref = 233 const PrefService::Preference* pref =
207 pref_service->FindPreference(pref_name->c_str()); 234 pref_service->FindPreference(pref_name->c_str());
208 if (pref) { 235 if (pref) {
209 DictionaryValue result_value; 236 for (PreferenceCallbackMap::const_iterator iter =
210 result_value.Set(pref_name->c_str(), pref->GetValue()->DeepCopy()); 237 pref_callback_map_.find(*pref_name);
211 dom_ui_->CallJavascriptFunction(L"prefsChanged", result_value); 238 iter != pref_callback_map_.end(); ++iter) {
239 const std::wstring& callback_function = iter->second;
240 ListValue result_value;
241 result_value.Append(Value::CreateStringValue(pref_name->c_str()));
242 result_value.Append(pref->GetValue()->DeepCopy());
243 dom_ui_->CallJavascriptFunction(callback_function, result_value);
244 }
212 } 245 }
213 } 246 }
OLDNEW
« no previous file with comments | « chrome/browser/dom_ui/core_options_handler.h ('k') | chrome/browser/dom_ui/options_ui.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698