OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/ui/webui/options/managed_user_settings_handler.h" | 5 #include "chrome/browser/ui/webui/options/managed_user_settings_handler.h" |
6 | 6 |
7 #include <vector> | |
8 | |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/command_line.h" | 10 #include "base/command_line.h" |
9 #include "base/logging.h" | 11 #include "base/logging.h" |
10 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
11 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
12 #include "base/time.h" | 14 #include "base/time.h" |
13 #include "base/values.h" | 15 #include "base/values.h" |
14 #include "chrome/browser/first_run/first_run.h" | 16 #include "chrome/browser/first_run/first_run.h" |
17 #include "chrome/browser/managed_mode/managed_user_service.h" | |
18 #include "chrome/browser/managed_mode/managed_user_service_factory.h" | |
15 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
17 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
18 #include "content/public/browser/user_metrics.h" | 22 #include "content/public/browser/user_metrics.h" |
19 #include "content/public/browser/web_ui.h" | 23 #include "content/public/browser/web_ui.h" |
24 #include "googleurl/src/url_canon.h" | |
20 #include "grit/chromium_strings.h" | 25 #include "grit/chromium_strings.h" |
21 #include "grit/generated_resources.h" | 26 #include "grit/generated_resources.h" |
22 #include "grit/google_chrome_strings.h" | 27 #include "grit/google_chrome_strings.h" |
23 #include "grit/locale_settings.h" | 28 #include "grit/locale_settings.h" |
24 | 29 |
25 using content::UserMetricsAction; | 30 using content::UserMetricsAction; |
26 | 31 |
32 namespace { | |
33 | |
34 const char* kSetting = "setting"; | |
35 const char* kPattern = "pattern"; | |
36 | |
37 // Takes the |host| string and tries to get the canonical version. Returns | |
38 // whether the operation succeeded, and if it did and the |output_string| is | |
39 // not NULL writes the canonical version in |output_string|. | |
40 bool CheckHostname(std::string host, std::string* output_string) { | |
Bernhard Bauer
2013/04/02 11:08:36
This method has too many side effects for my taste
Sergiu
2013/04/02 14:55:38
Renamed to CanonicalizeHost, as the other function
Bernhard Bauer
2013/04/02 15:07:56
It's not? It has a wiktionary entry: http://en.wik
| |
41 url_parse::Component in_comp(0, host.length()); | |
42 url_parse::Component out_comp; | |
43 std::string output; | |
44 | |
45 url_canon::StdStringCanonOutput canon_output(&output); | |
46 url_canon::CanonHostInfo host_info; | |
47 | |
48 bool is_valid = url_canon::CanonicalizeHost( | |
49 host.c_str(), in_comp, &canon_output, &out_comp); | |
50 canon_output.Complete(); | |
51 if (is_valid && output_string) | |
52 *output_string = output; | |
53 return is_valid; | |
54 } | |
55 | |
56 // Create a DictionaryValue that will act as a row in the pattern list. | |
57 // Ownership of the pointer is passed to the caller. | |
58 DictionaryValue* GetEntryForPattern(const std::string pattern, | |
59 const std::string setting) { | |
60 base::DictionaryValue* entry = new base::DictionaryValue(); | |
61 entry->SetString(kPattern, pattern); | |
62 entry->SetString(kSetting, setting); | |
63 return entry; | |
64 } | |
65 | |
66 // Read the manual url and host lists from the current profile and add them to | |
67 // the list of |entries|. | |
68 void AddCurrentURLEntries(content::WebUI* web_ui, ListValue* entries) { | |
69 Profile* profile = Profile::FromWebUI(web_ui); | |
70 const DictionaryValue* dict = | |
71 profile->GetPrefs()->GetDictionary(prefs::kManagedModeManualHosts); | |
72 for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { | |
73 bool allow = false; | |
74 bool result = it.value().GetAsBoolean(&allow); | |
75 DCHECK(result); | |
76 entries->Append( | |
77 GetEntryForPattern(it.key(), allow ? "allow" : "block")); | |
78 } | |
79 | |
80 dict = profile->GetPrefs()->GetDictionary(prefs::kManagedModeManualURLs); | |
81 for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { | |
82 bool allow = false; | |
83 bool result = it.value().GetAsBoolean(&allow); | |
84 DCHECK(result); | |
85 entries->Append( | |
86 GetEntryForPattern(it.key(), allow ? "allow" : "block")); | |
87 } | |
88 } | |
89 | |
90 } // namespace | |
91 | |
27 namespace options { | 92 namespace options { |
28 | 93 |
29 ManagedUserSettingsHandler::ManagedUserSettingsHandler() { | 94 ManagedUserSettingsHandler::ManagedUserSettingsHandler() { |
30 } | 95 } |
31 | 96 |
32 ManagedUserSettingsHandler::~ManagedUserSettingsHandler() { | 97 ManagedUserSettingsHandler::~ManagedUserSettingsHandler() { |
33 } | 98 } |
34 | 99 |
35 void ManagedUserSettingsHandler::InitializeHandler() { | 100 void ManagedUserSettingsHandler::InitializeHandler() { |
36 pref_change_registrar_.Init(Profile::FromWebUI(web_ui())->GetPrefs()); | 101 pref_change_registrar_.Init(Profile::FromWebUI(web_ui())->GetPrefs()); |
37 pref_change_registrar_.Add( | 102 pref_change_registrar_.Add( |
38 prefs::kManagedModeLocalPassphrase, | 103 prefs::kManagedModeLocalPassphrase, |
39 base::Bind(&ManagedUserSettingsHandler::OnLocalPassphraseChanged, | 104 base::Bind(&ManagedUserSettingsHandler::OnLocalPassphraseChanged, |
40 base::Unretained(this))); | 105 base::Unretained(this))); |
41 } | 106 } |
42 | 107 |
43 void ManagedUserSettingsHandler::InitializePage() { | 108 void ManagedUserSettingsHandler::InitializePage() { |
44 if (CommandLine::ForCurrentProcess()->HasSwitch( | 109 if (CommandLine::ForCurrentProcess()->HasSwitch( |
45 switches::kEnableManagedUsers)) { | 110 switches::kEnableManagedUsers)) { |
46 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); | 111 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); |
47 base::FundamentalValue is_passphrase_set(!pref_service->GetString( | 112 base::FundamentalValue is_passphrase_set(!pref_service->GetString( |
48 prefs::kManagedModeLocalPassphrase).empty()); | 113 prefs::kManagedModeLocalPassphrase).empty()); |
49 web_ui()->CallJavascriptFunction( | 114 web_ui()->CallJavascriptFunction( |
50 "ManagedUserSettings.passphraseChanged", | 115 "ManagedUserSettings.passphraseChanged", |
51 is_passphrase_set); | 116 is_passphrase_set); |
52 } | 117 } |
118 | |
119 // Populate the list. | |
120 UpdateViewFromModel(); | |
53 } | 121 } |
54 | 122 |
55 void ManagedUserSettingsHandler::HandlePageOpened(const base::ListValue* args) { | 123 void ManagedUserSettingsHandler::HandlePageOpened(const base::ListValue* args) { |
56 start_time_ = base::TimeTicks::Now(); | 124 start_time_ = base::TimeTicks::Now(); |
57 content::RecordAction(UserMetricsAction("ManagedMode_OpenSettings")); | 125 content::RecordAction(UserMetricsAction("ManagedMode_OpenSettings")); |
58 } | 126 } |
59 | 127 |
60 void ManagedUserSettingsHandler::GetLocalizedValues( | 128 void ManagedUserSettingsHandler::GetLocalizedValues( |
61 base::DictionaryValue* localized_strings) { | 129 base::DictionaryValue* localized_strings) { |
62 DCHECK(localized_strings); | 130 DCHECK(localized_strings); |
63 | 131 |
64 static OptionsStringResource resources[] = { | 132 static OptionsStringResource resources[] = { |
65 // Unlock the settings page to allow editing. | 133 // Unlock the settings page to allow editing. |
66 { "unlockSettings", IDS_UNLOCK_PASSPHRASE_BUTTON }, | 134 { "unlockSettings", IDS_UNLOCK_PASSPHRASE_BUTTON }, |
67 { "lockSettings", IDS_LOCK_MANAGED_USER_BUTTON }, | 135 { "lockSettings", IDS_LOCK_MANAGED_USER_BUTTON }, |
136 // Manual exception view. | |
137 { "allowException", IDS_EXCEPTIONS_ALLOW_BUTTON }, | |
138 { "blockException", IDS_EXCEPTIONS_BLOCK_BUTTON }, | |
139 { "addNewExceptionInstructions", IDS_EXCEPTIONS_ADD_NEW_INSTRUCTIONS }, | |
140 { "manageExceptions", IDS_EXCEPTIONS_MANAGE }, | |
141 { "exceptionPatternHeader", IDS_EXCEPTIONS_PATTERN_HEADER }, | |
142 { "exceptionBehaviorHeader", IDS_EXCEPTIONS_ACTION_HEADER }, | |
68 // Installed content packs. | 143 // Installed content packs. |
69 { "installedContentPacks", IDS_INSTALLED_CONTENT_PACKS_LABEL }, | 144 { "manualExceptionsHeader", IDS_MANUAL_EXCEPTION_HEADER }, |
145 { "manualExceptionsTabTitle", IDS_MANUAL_EXCEPTION_TAB_TITLE }, | |
146 { "contentPacksTabLabel", IDS_CONTENT_PACKS_TAB_LABEL }, | |
70 { "getContentPacks", IDS_GET_CONTENT_PACKS_BUTTON }, | 147 { "getContentPacks", IDS_GET_CONTENT_PACKS_BUTTON }, |
71 { "getContentPacksURL", IDS_GET_CONTENT_PACKS_URL }, | 148 { "getContentPacksURL", IDS_GET_CONTENT_PACKS_URL }, |
72 // Content pack restriction options. | 149 // Content pack restriction options. |
73 { "contentPackSettings", IDS_CONTENT_PACK_SETTINGS_LABEL }, | 150 { "contentPackSettings", IDS_CONTENT_PACK_SETTINGS_LABEL }, |
74 { "outsideContentPacksAllow", IDS_OUTSIDE_CONTENT_PACKS_ALLOW_RADIO }, | 151 { "outsideContentPacksAllow", IDS_OUTSIDE_CONTENT_PACKS_ALLOW_RADIO }, |
75 { "outsideContentPacksWarn", IDS_OUTSIDE_CONTENT_PACKS_WARN_RADIO }, | 152 { "outsideContentPacksWarn", IDS_OUTSIDE_CONTENT_PACKS_WARN_RADIO }, |
76 { "outsideContentPacksBlock", IDS_OUTSIDE_CONTENT_PACKS_BLOCK_RADIO }, | 153 { "outsideContentPacksBlock", IDS_OUTSIDE_CONTENT_PACKS_BLOCK_RADIO }, |
77 // Other managed user settings | 154 // Other managed user settings |
78 { "advancedManagedUserSettings", IDS_ADVANCED_MANAGED_USER_LABEL }, | 155 { "advancedManagedUserSettings", IDS_ADVANCED_MANAGED_USER_LABEL }, |
79 { "enableSafeSearch", IDS_SAFE_SEARCH_ENABLED }, | 156 { "enableSafeSearch", IDS_SAFE_SEARCH_ENABLED }, |
80 { "allowSignIn", IDS_SIGNIN_SYNC_ALLOWED }, | 157 { "allowSignIn", IDS_SIGNIN_SYNC_ALLOWED }, |
81 { "disableHistoryDeletion", IDS_HISTORY_DELETION_DISABLED }, | 158 { "disableHistoryDeletion", IDS_HISTORY_DELETION_DISABLED }, |
82 { "usePassphrase", IDS_USE_PASSPHRASE_LABEL }, | 159 { "usePassphrase", IDS_USE_PASSPHRASE_LABEL }, |
83 { "setPassphrase", IDS_SET_PASSPHRASE_BUTTON } | 160 { "setPassphrase", IDS_SET_PASSPHRASE_BUTTON } |
84 }; | 161 }; |
85 | 162 |
86 RegisterStrings(localized_strings, resources, arraysize(resources)); | 163 RegisterStrings(localized_strings, resources, arraysize(resources)); |
87 RegisterTitle(localized_strings, "managedUserSettingsPage", | 164 RegisterTitle(localized_strings, "managedUserSettingsPage", |
88 IDS_MANAGED_USER_SETTINGS_TITLE); | 165 IDS_MANAGED_USER_SETTINGS_TITLE); |
89 | 166 RegisterTitle(localized_strings, "manualExceptions", |
167 IDS_MANUAL_EXCEPTION_HEADER); | |
90 localized_strings->SetBoolean( | 168 localized_strings->SetBoolean( |
91 "managedUsersEnabled", | 169 "managedUsersEnabled", |
92 CommandLine::ForCurrentProcess()->HasSwitch( | 170 CommandLine::ForCurrentProcess()->HasSwitch( |
93 switches::kEnableManagedUsers)); | 171 switches::kEnableManagedUsers)); |
94 } | 172 } |
95 | 173 |
96 void ManagedUserSettingsHandler::RegisterMessages() { | 174 void ManagedUserSettingsHandler::RegisterMessages() { |
97 web_ui()->RegisterMessageCallback( | 175 web_ui()->RegisterMessageCallback( |
98 "confirmManagedUserSettings", | 176 "confirmManagedUserSettings", |
99 base::Bind(&ManagedUserSettingsHandler::SaveMetrics, | 177 base::Bind(&ManagedUserSettingsHandler::SaveMetrics, |
100 base::Unretained(this))); | 178 base::Unretained(this))); |
101 web_ui()->RegisterMessageCallback( | 179 web_ui()->RegisterMessageCallback( |
102 "settingsPageOpened", | 180 "settingsPageOpened", |
103 base::Bind(&ManagedUserSettingsHandler::HandlePageOpened, | 181 base::Bind(&ManagedUserSettingsHandler::HandlePageOpened, |
104 base::Unretained(this))); | 182 base::Unretained(this))); |
183 web_ui()->RegisterMessageCallback("removeManualException", | |
184 base::Bind(&ManagedUserSettingsHandler::RemoveManualException, | |
185 base::Unretained(this))); | |
186 web_ui()->RegisterMessageCallback("setManualException", | |
187 base::Bind(&ManagedUserSettingsHandler::SetManualException, | |
188 base::Unretained(this))); | |
189 web_ui()->RegisterMessageCallback("checkManualExceptionValidity", | |
190 base::Bind( | |
191 &ManagedUserSettingsHandler::CheckManualExceptionValidity, | |
192 base::Unretained(this))); | |
105 } | 193 } |
106 | 194 |
107 void ManagedUserSettingsHandler::SaveMetrics(const ListValue* args) { | 195 void ManagedUserSettingsHandler::SaveMetrics(const ListValue* args) { |
108 if (first_run::IsChromeFirstRun()) { | 196 if (first_run::IsChromeFirstRun()) { |
109 UMA_HISTOGRAM_LONG_TIMES("ManagedMode.UserSettingsFirstRunTime", | 197 UMA_HISTOGRAM_LONG_TIMES("ManagedMode.UserSettingsFirstRunTime", |
110 base::TimeTicks::Now() - start_time_); | 198 base::TimeTicks::Now() - start_time_); |
111 } else { | 199 } else { |
112 UMA_HISTOGRAM_LONG_TIMES("ManagedMode.UserSettingsModifyTime", | 200 UMA_HISTOGRAM_LONG_TIMES("ManagedMode.UserSettingsModifyTime", |
113 base::TimeTicks::Now() - start_time_); | 201 base::TimeTicks::Now() - start_time_); |
114 } | 202 } |
115 } | 203 } |
116 | 204 |
117 void ManagedUserSettingsHandler::OnLocalPassphraseChanged() { | 205 void ManagedUserSettingsHandler::OnLocalPassphraseChanged() { |
118 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); | 206 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); |
119 base::FundamentalValue is_passphrase_set(!pref_service->GetString( | 207 base::FundamentalValue is_passphrase_set(!pref_service->GetString( |
120 prefs::kManagedModeLocalPassphrase).empty()); | 208 prefs::kManagedModeLocalPassphrase).empty()); |
121 web_ui()->CallJavascriptFunction("ManagedUserSettings.passphraseChanged", | 209 web_ui()->CallJavascriptFunction("ManagedUserSettings.passphraseChanged", |
122 is_passphrase_set); | 210 is_passphrase_set); |
123 } | 211 } |
124 | 212 |
213 void ManagedUserSettingsHandler::RemoveManualException( | |
214 const ListValue* args) { | |
215 size_t arg_i = 0; | |
216 std::string pattern; | |
217 CHECK(args->GetString(arg_i++, &pattern)); | |
218 | |
219 UpdateManualBehavior(pattern, ManagedUserService::MANUAL_NONE); | |
220 | |
221 UpdateViewFromModel(); | |
222 } | |
223 | |
224 void ManagedUserSettingsHandler::UpdateManualBehavior( | |
225 std::string pattern, | |
226 ManagedUserService::ManualBehavior behavior) { | |
227 ManagedUserService* service = | |
228 ManagedUserServiceFactory::GetForProfile(Profile::FromWebUI(web_ui())); | |
229 GURL url(pattern); | |
230 if (!url.is_valid()) { | |
231 // This is a host, get the canonical version of the string. | |
232 std::vector<std::string> to_update; | |
233 std::string canonical_host; | |
234 DCHECK(CheckHostname(pattern, &canonical_host)); | |
Bernhard Bauer
2013/04/02 11:08:36
This will leave |canonical_host| empty in a non-DC
Sergiu
2013/04/02 14:55:38
Ah, got it.
| |
235 to_update.push_back(canonical_host); | |
236 service->SetManualBehaviorForHosts(to_update, behavior); | |
237 } else { | |
238 // A specific URL. The GURL generates the canonical form when being built. | |
239 std::vector<GURL> to_update; | |
240 to_update.push_back(url); | |
241 service->SetManualBehaviorForURLs(to_update, behavior); | |
242 } | |
243 } | |
244 | |
245 void ManagedUserSettingsHandler::SetManualException( | |
246 const ListValue* args) { | |
247 size_t arg_i = 0; | |
248 std::string pattern; | |
249 CHECK(args->GetString(arg_i++, &pattern)); | |
250 std::string setting; | |
251 CHECK(args->GetString(arg_i++, &setting)); | |
252 bool needs_update; | |
253 CHECK(args->GetBoolean(arg_i++, &needs_update)); | |
254 | |
255 DCHECK(setting == "allow" || setting == "block"); | |
256 ManagedUserService::ManualBehavior behavior = | |
257 (setting == "allow") ? ManagedUserService::MANUAL_ALLOW | |
258 : ManagedUserService::MANUAL_BLOCK; | |
259 UpdateManualBehavior(pattern, behavior); | |
260 | |
261 if (needs_update) | |
262 UpdateViewFromModel(); | |
263 } | |
264 | |
265 void ManagedUserSettingsHandler::CheckManualExceptionValidity( | |
266 const ListValue* args) { | |
267 size_t arg_i = 0; | |
268 std::string pattern_string; | |
269 CHECK(args->GetString(arg_i++, &pattern_string)); | |
270 bool is_valid = false; | |
271 | |
272 // First, try to see if it is a valid URL. | |
273 GURL url(pattern_string); | |
274 is_valid = url.is_valid() && url.IsStandard(); | |
275 | |
276 // If it's not valid, try to put a http:// schema to it. If it was a host, it | |
277 // should be valid now, but check that the path, query and ref are empty. | |
278 if (!is_valid) | |
279 is_valid = CheckHostname(pattern_string, NULL); | |
Bernhard Bauer
2013/04/02 11:08:36
I think you could move the declaration of |is_vali
Sergiu
2013/04/02 14:55:38
True, done.
| |
280 | |
281 web_ui()->CallJavascriptFunction( | |
282 "ManagedUserSettings.patternValidityCheckComplete", | |
283 base::StringValue(pattern_string), | |
284 base::FundamentalValue(is_valid)); | |
285 } | |
286 | |
287 void ManagedUserSettingsHandler::UpdateViewFromModel() { | |
288 ListValue entries; | |
289 AddCurrentURLEntries(web_ui(), &entries); | |
290 | |
291 web_ui()->CallJavascriptFunction( | |
292 "ManagedUserSettings.setManualExceptions", | |
293 entries); | |
294 } | |
295 | |
125 } // namespace options | 296 } // namespace options |
OLD | NEW |