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..07afc3a5e9e597f33447f71eebbd1a6d746e185a 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,6 +14,8 @@ |
#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" |
@@ -24,6 +28,47 @@ |
using content::UserMetricsAction; |
+namespace { |
+ |
+const char* kSetting = "setting"; |
+const char* kPattern = "pattern"; |
+ |
+// 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 +95,9 @@ void ManagedUserSettingsHandler::InitializePage() { |
"ManagedUserSettings.passphraseChanged", |
is_passphrase_set); |
} |
+ |
+ // Populate the list. |
+ UpdateViewFromModel(); |
} |
void ManagedUserSettingsHandler::HandlePageOpened(const base::ListValue* args) { |
@@ -65,8 +113,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 +143,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 +160,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 +190,88 @@ 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 pattern. |
+ std::vector<std::string> to_update; |
+ to_update.push_back(pattern); |
+ service->SetManualBehaviorForHosts(to_update, behavior); |
+ } else { |
+ // A specific URL. |
+ 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) { |
+ url = GURL("http://" + pattern_string); |
+ is_valid = url.is_valid() && url.path() == "/" && !url.has_query() && |
+ !url.has_ref(); |
Bernhard Bauer
2013/03/27 14:41:11
How about using CanonicalizeHost() (from gurl.h)?
Sergiu
2013/03/27 15:26:46
I've changed it to that (the one from url_canon.h)
Bernhard Bauer
2013/03/27 15:55:12
By escaped you mean punycode? We also use that in
|
+ } |
+ |
+ 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 |