Chromium Code Reviews| Index: chrome/browser/ui/webui/options/managed_user_settings_handler.cc |
| diff --git a/chrome/browser/ui/webui/options/managed_user_settings_handler.cc b/chrome/browser/ui/webui/options/managed_user_settings_handler.cc |
| index 450ed7b8a8fee118ee65909c97322143f55b462c..44f22dc8c628f6affdaf2c050edb46302e3ac9f1 100644 |
| --- a/chrome/browser/ui/webui/options/managed_user_settings_handler.cc |
| +++ b/chrome/browser/ui/webui/options/managed_user_settings_handler.cc |
| @@ -4,6 +4,8 @@ |
| #include "chrome/browser/ui/webui/options/managed_user_settings_handler.h" |
| +#include <vector> |
| + |
| #include "base/bind.h" |
| #include "base/command_line.h" |
| #include "base/logging.h" |
| @@ -12,11 +14,14 @@ |
| #include "base/time.h" |
| #include "base/values.h" |
| #include "chrome/browser/first_run/first_run.h" |
| +#include "chrome/browser/managed_mode/managed_user_service.h" |
| +#include "chrome/browser/managed_mode/managed_user_service_factory.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/common/chrome_switches.h" |
| #include "chrome/common/pref_names.h" |
| #include "content/public/browser/user_metrics.h" |
| #include "content/public/browser/web_ui.h" |
| +#include "googleurl/src/url_canon.h" |
| #include "grit/chromium_strings.h" |
| #include "grit/generated_resources.h" |
| #include "grit/google_chrome_strings.h" |
| @@ -24,6 +29,66 @@ |
| using content::UserMetricsAction; |
| +namespace { |
| + |
| +const char* kSetting = "setting"; |
| +const char* kPattern = "pattern"; |
| + |
| +// Takes the |host| string and tries to get the canonical version. Returns |
| +// whether the operation succeeded, and if it did and the |output_string| is |
| +// not NULL writes the canonical version in |output_string|. |
| +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
|
| + url_parse::Component in_comp(0, host.length()); |
| + url_parse::Component out_comp; |
| + std::string output; |
| + |
| + url_canon::StdStringCanonOutput canon_output(&output); |
| + url_canon::CanonHostInfo host_info; |
| + |
| + bool is_valid = url_canon::CanonicalizeHost( |
| + host.c_str(), in_comp, &canon_output, &out_comp); |
| + canon_output.Complete(); |
| + if (is_valid && output_string) |
| + *output_string = output; |
| + return is_valid; |
| +} |
| + |
| +// Create a DictionaryValue that will act as a row in the pattern list. |
| +// Ownership of the pointer is passed to the caller. |
| +DictionaryValue* GetEntryForPattern(const std::string pattern, |
| + const std::string setting) { |
| + base::DictionaryValue* entry = new base::DictionaryValue(); |
| + entry->SetString(kPattern, pattern); |
| + entry->SetString(kSetting, setting); |
| + return entry; |
| +} |
| + |
| +// Read the manual url and host lists from the current profile and add them to |
| +// the list of |entries|. |
| +void AddCurrentURLEntries(content::WebUI* web_ui, ListValue* entries) { |
| + Profile* profile = Profile::FromWebUI(web_ui); |
| + const DictionaryValue* dict = |
| + profile->GetPrefs()->GetDictionary(prefs::kManagedModeManualHosts); |
| + for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { |
| + bool allow = false; |
| + bool result = it.value().GetAsBoolean(&allow); |
| + DCHECK(result); |
| + entries->Append( |
| + GetEntryForPattern(it.key(), allow ? "allow" : "block")); |
| + } |
| + |
| + dict = profile->GetPrefs()->GetDictionary(prefs::kManagedModeManualURLs); |
| + for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { |
| + bool allow = false; |
| + bool result = it.value().GetAsBoolean(&allow); |
| + DCHECK(result); |
| + entries->Append( |
| + GetEntryForPattern(it.key(), allow ? "allow" : "block")); |
| + } |
| +} |
| + |
| +} // namespace |
| + |
| namespace options { |
| ManagedUserSettingsHandler::ManagedUserSettingsHandler() { |
| @@ -50,6 +115,9 @@ void ManagedUserSettingsHandler::InitializePage() { |
| "ManagedUserSettings.passphraseChanged", |
| is_passphrase_set); |
| } |
| + |
| + // Populate the list. |
| + UpdateViewFromModel(); |
| } |
| void ManagedUserSettingsHandler::HandlePageOpened(const base::ListValue* args) { |
| @@ -65,8 +133,17 @@ void ManagedUserSettingsHandler::GetLocalizedValues( |
| // Unlock the settings page to allow editing. |
| { "unlockSettings", IDS_UNLOCK_PASSPHRASE_BUTTON }, |
| { "lockSettings", IDS_LOCK_MANAGED_USER_BUTTON }, |
| + // Manual exception view. |
| + { "allowException", IDS_EXCEPTIONS_ALLOW_BUTTON }, |
| + { "blockException", IDS_EXCEPTIONS_BLOCK_BUTTON }, |
| + { "addNewExceptionInstructions", IDS_EXCEPTIONS_ADD_NEW_INSTRUCTIONS }, |
| + { "manageExceptions", IDS_EXCEPTIONS_MANAGE }, |
| + { "exceptionPatternHeader", IDS_EXCEPTIONS_PATTERN_HEADER }, |
| + { "exceptionBehaviorHeader", IDS_EXCEPTIONS_ACTION_HEADER }, |
| // Installed content packs. |
| - { "installedContentPacks", IDS_INSTALLED_CONTENT_PACKS_LABEL }, |
| + { "manualExceptionsHeader", IDS_MANUAL_EXCEPTION_HEADER }, |
| + { "manualExceptionsTabTitle", IDS_MANUAL_EXCEPTION_TAB_TITLE }, |
| + { "contentPacksTabLabel", IDS_CONTENT_PACKS_TAB_LABEL }, |
| { "getContentPacks", IDS_GET_CONTENT_PACKS_BUTTON }, |
| { "getContentPacksURL", IDS_GET_CONTENT_PACKS_URL }, |
| // Content pack restriction options. |
| @@ -86,7 +163,8 @@ void ManagedUserSettingsHandler::GetLocalizedValues( |
| RegisterStrings(localized_strings, resources, arraysize(resources)); |
| RegisterTitle(localized_strings, "managedUserSettingsPage", |
| IDS_MANAGED_USER_SETTINGS_TITLE); |
| - |
| + RegisterTitle(localized_strings, "manualExceptions", |
| + IDS_MANUAL_EXCEPTION_HEADER); |
| localized_strings->SetBoolean( |
| "managedUsersEnabled", |
| CommandLine::ForCurrentProcess()->HasSwitch( |
| @@ -102,6 +180,16 @@ void ManagedUserSettingsHandler::RegisterMessages() { |
| "settingsPageOpened", |
| base::Bind(&ManagedUserSettingsHandler::HandlePageOpened, |
| base::Unretained(this))); |
| + web_ui()->RegisterMessageCallback("removeManualException", |
| + base::Bind(&ManagedUserSettingsHandler::RemoveManualException, |
| + base::Unretained(this))); |
| + web_ui()->RegisterMessageCallback("setManualException", |
| + base::Bind(&ManagedUserSettingsHandler::SetManualException, |
| + base::Unretained(this))); |
| + web_ui()->RegisterMessageCallback("checkManualExceptionValidity", |
| + base::Bind( |
| + &ManagedUserSettingsHandler::CheckManualExceptionValidity, |
| + base::Unretained(this))); |
| } |
| void ManagedUserSettingsHandler::SaveMetrics(const ListValue* args) { |
| @@ -122,4 +210,87 @@ void ManagedUserSettingsHandler::OnLocalPassphraseChanged() { |
| is_passphrase_set); |
| } |
| +void ManagedUserSettingsHandler::RemoveManualException( |
| + const ListValue* args) { |
| + size_t arg_i = 0; |
| + std::string pattern; |
| + CHECK(args->GetString(arg_i++, &pattern)); |
| + |
| + UpdateManualBehavior(pattern, ManagedUserService::MANUAL_NONE); |
| + |
| + UpdateViewFromModel(); |
| +} |
| + |
| +void ManagedUserSettingsHandler::UpdateManualBehavior( |
| + std::string pattern, |
| + ManagedUserService::ManualBehavior behavior) { |
| + ManagedUserService* service = |
| + ManagedUserServiceFactory::GetForProfile(Profile::FromWebUI(web_ui())); |
| + GURL url(pattern); |
| + if (!url.is_valid()) { |
| + // This is a host, get the canonical version of the string. |
| + std::vector<std::string> to_update; |
| + std::string canonical_host; |
| + 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.
|
| + to_update.push_back(canonical_host); |
| + service->SetManualBehaviorForHosts(to_update, behavior); |
| + } else { |
| + // A specific URL. The GURL generates the canonical form when being built. |
| + std::vector<GURL> to_update; |
| + to_update.push_back(url); |
| + service->SetManualBehaviorForURLs(to_update, behavior); |
| + } |
| +} |
| + |
| +void ManagedUserSettingsHandler::SetManualException( |
| + const ListValue* args) { |
| + size_t arg_i = 0; |
| + std::string pattern; |
| + CHECK(args->GetString(arg_i++, &pattern)); |
| + std::string setting; |
| + CHECK(args->GetString(arg_i++, &setting)); |
| + bool needs_update; |
| + CHECK(args->GetBoolean(arg_i++, &needs_update)); |
| + |
| + DCHECK(setting == "allow" || setting == "block"); |
| + ManagedUserService::ManualBehavior behavior = |
| + (setting == "allow") ? ManagedUserService::MANUAL_ALLOW |
| + : ManagedUserService::MANUAL_BLOCK; |
| + UpdateManualBehavior(pattern, behavior); |
| + |
| + if (needs_update) |
| + UpdateViewFromModel(); |
| +} |
| + |
| +void ManagedUserSettingsHandler::CheckManualExceptionValidity( |
| + const ListValue* args) { |
| + size_t arg_i = 0; |
| + std::string pattern_string; |
| + CHECK(args->GetString(arg_i++, &pattern_string)); |
| + bool is_valid = false; |
| + |
| + // First, try to see if it is a valid URL. |
| + GURL url(pattern_string); |
| + is_valid = url.is_valid() && url.IsStandard(); |
| + |
| + // If it's not valid, try to put a http:// schema to it. If it was a host, it |
| + // should be valid now, but check that the path, query and ref are empty. |
| + if (!is_valid) |
| + 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.
|
| + |
| + web_ui()->CallJavascriptFunction( |
| + "ManagedUserSettings.patternValidityCheckComplete", |
| + base::StringValue(pattern_string), |
| + base::FundamentalValue(is_valid)); |
| +} |
| + |
| +void ManagedUserSettingsHandler::UpdateViewFromModel() { |
| + ListValue entries; |
| + AddCurrentURLEntries(web_ui(), &entries); |
| + |
| + web_ui()->CallJavascriptFunction( |
| + "ManagedUserSettings.setManualExceptions", |
| + entries); |
| +} |
| + |
| } // namespace options |