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