Chromium Code Reviews| Index: chrome/browser/ui/webui/options/handler_options_handler.cc |
| diff --git a/chrome/browser/ui/webui/options/handler_options_handler.cc b/chrome/browser/ui/webui/options/handler_options_handler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..74064c42c43fdb82e29e523d45e8cde3d831e590 |
| --- /dev/null |
| +++ b/chrome/browser/ui/webui/options/handler_options_handler.cc |
| @@ -0,0 +1,165 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/webui/options/handler_options_handler.h" |
| + |
| +#include "base/utf_string_conversions.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/prefs/pref_service.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "grit/generated_resources.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| + |
| + |
| +HandlerOptionsHandler::HandlerOptionsHandler() { |
| +} |
| + |
| +HandlerOptionsHandler::~HandlerOptionsHandler() { |
| +} |
| + |
| +void HandlerOptionsHandler::GetLocalizedValues( |
| + DictionaryValue* localized_strings) { |
| + DCHECK(localized_strings); |
| + |
| + static OptionsStringResource resources[] = { |
| + { "handlers_tab_label", IDS_HANDLERS_TAB_LABEL }, |
| + { "handlers_allow", IDS_HANDLERS_ALLOW_RADIO }, |
| + { "handlers_block", IDS_HANDLERS_DONOTALLOW_RADIO }, |
| + { "handlers_type_column_header", IDS_HANDLERS_TYPE_COLUMN_HEADER }, |
| + { "handlers_site_column_header", IDS_HANDLERS_SITE_COLUMN_HEADER }, |
| + { "handlers_remove_link", IDS_HANDLERS_REMOVE_HANDLER_LINK }, |
| + }; |
| + RegisterTitle(localized_strings, "handlersPage", |
| + IDS_HANDLER_OPTIONS_WINDOW_TITLE); |
| + RegisterStrings(localized_strings, resources, arraysize(resources)); |
| +} |
| + |
| +void HandlerOptionsHandler::Initialize() { |
| + UpdateHandlerList(); |
| + notification_registrar_.Add( |
| + this, NotificationType::PROTOCOL_HANDLER_REGISTRY_CHANGED, |
| + NotificationService::AllSources()); |
| +} |
| + |
| +void HandlerOptionsHandler::RegisterMessages() { |
| + DCHECK(web_ui_); |
| + web_ui_->RegisterMessageCallback("removeHandler", |
| + NewCallback(this, &HandlerOptionsHandler::RemoveHandler)); |
| + web_ui_->RegisterMessageCallback("setHandlersEnabled", |
| + NewCallback(this, &HandlerOptionsHandler::SetHandlersEnabled)); |
| + web_ui_->RegisterMessageCallback("setDefault", |
| + NewCallback(this, &HandlerOptionsHandler::SetDefault)); |
| +} |
| + |
| +ProtocolHandlerRegistry* HandlerOptionsHandler::GetProtocolHandlerRegistry() { |
| + DCHECK(web_ui_); |
| + return web_ui_->GetProfile()->GetProtocolHandlerRegistry(); |
| +} |
| + |
| +DictionaryValue* HandlerOptionsHandler::GetHandlersForProtocol( |
| + const std::string& protocol) { |
| + ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry(); |
| + DictionaryValue* handlers_value = new DictionaryValue(); |
| + handlers_value->SetString("protocol", protocol); |
| + handlers_value->SetInteger("default_handler", |
| + registry->GetHandlerIndex(protocol)); |
| + |
| + ListValue* handler_list = new ListValue(); |
| + const ProtocolHandlerRegistry::ProtocolHandlerList* handlers = |
| + registry->GetHandlersFor(protocol); |
| + ProtocolHandlerRegistry::ProtocolHandlerList::const_iterator p; |
|
Evan Stade
2011/05/19 17:08:42
variable names should be descriptive (p is not) (h
koz (OOO until 15th September)
2011/05/19 21:07:52
Done.
|
| + for (p = handlers->begin(); p != handlers->end(); ++p) { |
| + ListValue* handler = new ListValue(); |
| + handler->Append(Value::CreateStringValue(p->url().spec())); |
| + handler->Append(Value::CreateStringValue(p->title())); |
| + handler_list->Append(handler); |
| + } |
| + handlers_value->Set("handlers", handler_list); |
| + return handlers_value; |
| +} |
| + |
| +void HandlerOptionsHandler::UpdateHandlerList() { |
| + ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry(); |
| + std::vector<std::string> protocols; |
| + registry->GetHandledProtocols(&protocols); |
| + |
| + ListValue handlers; |
| + for (std::vector<std::string>::iterator p = protocols.begin(); |
| + p != protocols.end(); p++) { |
|
Evan Stade
2011/05/19 17:08:42
one more space of indent
koz (OOO until 15th September)
2011/05/19 21:07:52
Done.
|
| + handlers.Append(GetHandlersForProtocol(*p)); |
| + } |
| + |
| + web_ui_->CallJavascriptFunction("HandlerOptions.setHandlers", handlers); |
| +} |
| + |
| +void HandlerOptionsHandler::RemoveHandler(const ListValue* args) { |
| + ListValue* list; |
| + if (!args->GetList(0, &list)) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + |
| + ProtocolHandler handler(ParseHandlerFromArgs(list)); |
| + GetProtocolHandlerRegistry()->RemoveHandler(handler); |
| + |
| + // No need to call UpdateHandlerList() - we should receive a notification |
| + // that the ProtocolHandlerRegistry has changed and we will update the view |
| + // then. |
| +} |
| + |
| +void HandlerOptionsHandler::SetHandlersEnabled(const ListValue* args) { |
| + bool enabled = true; |
| + if (!args->GetBoolean(0, &enabled)) { |
|
Evan Stade
2011/05/19 17:08:42
prefer CHECK (here and elsewhere)
koz (OOO until 15th September)
2011/05/19 21:07:52
Done.
|
| + NOTREACHED(); |
| + return; |
| + } |
| + if (enabled) { |
|
Evan Stade
2011/05/19 17:08:42
no curlies (here and elsewhere)
koz (OOO until 15th September)
2011/05/19 21:07:52
Done.
|
| + GetProtocolHandlerRegistry()->Enable(); |
| + } else { |
| + GetProtocolHandlerRegistry()->Disable(); |
| + } |
| +} |
| + |
| +void HandlerOptionsHandler::SetDefault(const ListValue* args) { |
| + Value* value; |
| + args->Get(0, &value); |
| + ListValue* list; |
| + if (!args->GetList(0, &list)) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + const ProtocolHandler& handler(ParseHandlerFromArgs(list)); |
| + if (handler.IsEmpty()) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + GetProtocolHandlerRegistry()->SetDefault(handler); |
| +} |
| + |
| +ProtocolHandler HandlerOptionsHandler::ParseHandlerFromArgs( |
| + const ListValue* args) const { |
| + string16 protocol; |
| + string16 url; |
| + string16 title; |
| + bool ok = true; |
| + ok = ok && args->GetString(0, &protocol); |
| + ok = ok && args->GetString(1, &url); |
| + ok = ok && args->GetString(2, &title); |
| + if (!ok) { |
| + return ProtocolHandler::EmptyProtocolHandler(); |
| + } |
| + return ProtocolHandler::CreateProtocolHandler(UTF16ToUTF8(protocol), |
| + GURL(UTF16ToUTF8(url)), |
| + title); |
| +} |
| + |
| +void HandlerOptionsHandler::Observe(NotificationType type, |
| + const NotificationSource& source, |
| + const NotificationDetails& details) { |
| + if (type == NotificationType::PROTOCOL_HANDLER_REGISTRY_CHANGED) { |
| + UpdateHandlerList(); |
| + } else { |
| + NOTREACHED(); |
| + } |
| +} |