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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/webui/options/handler_options_handler.h"
10 #include "base/bind_helpers.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "components/google/core/browser/google_util.h"
18 #include "content/public/browser/web_ui.h"
19 #include "grit/generated_resources.h"
25 const char kHandlersLearnMoreUrl[] =
26 "https://support.google.com/chrome/answer/1382847";
30 HandlerOptionsHandler::HandlerOptionsHandler() {
33 HandlerOptionsHandler::~HandlerOptionsHandler() {
36 void HandlerOptionsHandler::GetLocalizedValues(
37 base::DictionaryValue* localized_strings) {
38 DCHECK(localized_strings);
40 static OptionsStringResource resources[] = {
41 { "handlers_tab_label", IDS_HANDLERS_TAB_LABEL },
42 { "handlers_allow", IDS_HANDLERS_ALLOW_RADIO },
43 { "handlers_block", IDS_HANDLERS_DONOTALLOW_RADIO },
44 { "handlers_type_column_header", IDS_HANDLERS_TYPE_COLUMN_HEADER },
45 { "handlers_site_column_header", IDS_HANDLERS_SITE_COLUMN_HEADER },
46 { "handlers_remove_link", IDS_HANDLERS_REMOVE_HANDLER_LINK },
47 { "handlers_none_handler", IDS_HANDLERS_NONE_HANDLER },
48 { "handlers_active_heading", IDS_HANDLERS_ACTIVE_HEADING },
49 { "handlers_ignored_heading", IDS_HANDLERS_IGNORED_HEADING },
51 RegisterTitle(localized_strings, "handlersPage",
52 IDS_HANDLER_OPTIONS_WINDOW_TITLE);
53 RegisterStrings(localized_strings, resources, arraysize(resources));
55 localized_strings->SetString("handlers_learn_more_url",
56 kHandlersLearnMoreUrl);
59 void HandlerOptionsHandler::InitializeHandler() {
60 notification_registrar_.Add(
61 this, chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED,
62 content::Source<Profile>(Profile::FromWebUI(web_ui())));
65 void HandlerOptionsHandler::InitializePage() {
69 void HandlerOptionsHandler::RegisterMessages() {
70 web_ui()->RegisterMessageCallback("clearDefault",
71 base::Bind(&HandlerOptionsHandler::ClearDefault,
72 base::Unretained(this)));
73 web_ui()->RegisterMessageCallback("removeHandler",
74 base::Bind(&HandlerOptionsHandler::RemoveHandler,
75 base::Unretained(this)));
76 web_ui()->RegisterMessageCallback("setHandlersEnabled",
77 base::Bind(&HandlerOptionsHandler::SetHandlersEnabled,
78 base::Unretained(this)));
79 web_ui()->RegisterMessageCallback("setDefault",
80 base::Bind(&HandlerOptionsHandler::SetDefault,
81 base::Unretained(this)));
82 web_ui()->RegisterMessageCallback("removeIgnoredHandler",
83 base::Bind(&HandlerOptionsHandler::RemoveIgnoredHandler,
84 base::Unretained(this)));
87 ProtocolHandlerRegistry* HandlerOptionsHandler::GetProtocolHandlerRegistry() {
88 return ProtocolHandlerRegistryFactory::GetForBrowserContext(
89 Profile::FromWebUI(web_ui()));
92 static void GetHandlersAsListValue(
93 const ProtocolHandlerRegistry::ProtocolHandlerList& handlers,
94 base::ListValue* handler_list) {
95 ProtocolHandlerRegistry::ProtocolHandlerList::const_iterator handler;
96 for (handler = handlers.begin(); handler != handlers.end(); ++handler) {
97 base::ListValue* handlerValue = new base::ListValue();
98 handlerValue->Append(new base::StringValue(handler->protocol()));
99 handlerValue->Append(new base::StringValue(handler->url().spec()));
100 handlerValue->Append(new base::StringValue(handler->url().host()));
101 handler_list->Append(handlerValue);
105 void HandlerOptionsHandler::GetHandlersForProtocol(
106 const std::string& protocol,
107 base::DictionaryValue* handlers_value) {
108 ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry();
109 handlers_value->SetString("protocol", protocol);
110 handlers_value->SetInteger("default_handler",
111 registry->GetHandlerIndex(protocol));
113 base::ListValue* handlers_list = new base::ListValue();
114 GetHandlersAsListValue(registry->GetHandlersFor(protocol), handlers_list);
115 handlers_value->Set("handlers", handlers_list);
118 void HandlerOptionsHandler::GetIgnoredHandlers(base::ListValue* handlers) {
119 ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry();
120 ProtocolHandlerRegistry::ProtocolHandlerList ignored_handlers =
121 registry->GetIgnoredHandlers();
122 return GetHandlersAsListValue(ignored_handlers, handlers);
125 void HandlerOptionsHandler::UpdateHandlerList() {
126 ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry();
127 std::vector<std::string> protocols;
128 registry->GetRegisteredProtocols(&protocols);
130 base::ListValue handlers;
131 for (std::vector<std::string>::iterator protocol = protocols.begin();
132 protocol != protocols.end(); protocol++) {
133 base::DictionaryValue* handler_value = new base::DictionaryValue();
134 GetHandlersForProtocol(*protocol, handler_value);
135 handlers.Append(handler_value);
138 scoped_ptr<base::ListValue> ignored_handlers(new base::ListValue());
139 GetIgnoredHandlers(ignored_handlers.get());
140 web_ui()->CallJavascriptFunction("HandlerOptions.setHandlers", handlers);
141 web_ui()->CallJavascriptFunction("HandlerOptions.setIgnoredHandlers",
145 void HandlerOptionsHandler::RemoveHandler(const base::ListValue* args) {
146 const base::ListValue* list;
147 if (!args->GetList(0, &list)) {
152 ProtocolHandler handler(ParseHandlerFromArgs(list));
153 GetProtocolHandlerRegistry()->RemoveHandler(handler);
155 // No need to call UpdateHandlerList() - we should receive a notification
156 // that the ProtocolHandlerRegistry has changed and we will update the view
160 void HandlerOptionsHandler::RemoveIgnoredHandler(const base::ListValue* args) {
161 const base::ListValue* list;
162 if (!args->GetList(0, &list)) {
167 ProtocolHandler handler(ParseHandlerFromArgs(list));
168 GetProtocolHandlerRegistry()->RemoveIgnoredHandler(handler);
171 void HandlerOptionsHandler::SetHandlersEnabled(const base::ListValue* args) {
173 CHECK(args->GetBoolean(0, &enabled));
175 GetProtocolHandlerRegistry()->Enable();
177 GetProtocolHandlerRegistry()->Disable();
180 void HandlerOptionsHandler::ClearDefault(const base::ListValue* args) {
181 const base::Value* value;
182 CHECK(args->Get(0, &value));
183 std::string protocol_to_clear;
184 CHECK(value->GetAsString(&protocol_to_clear));
185 GetProtocolHandlerRegistry()->ClearDefault(protocol_to_clear);
188 void HandlerOptionsHandler::SetDefault(const base::ListValue* args) {
189 const base::ListValue* list;
190 CHECK(args->GetList(0, &list));
191 const ProtocolHandler& handler(ParseHandlerFromArgs(list));
192 CHECK(!handler.IsEmpty());
193 GetProtocolHandlerRegistry()->OnAcceptRegisterProtocolHandler(handler);
196 ProtocolHandler HandlerOptionsHandler::ParseHandlerFromArgs(
197 const base::ListValue* args) const {
198 base::string16 protocol;
200 bool ok = args->GetString(0, &protocol) && args->GetString(1, &url);
202 return ProtocolHandler::EmptyProtocolHandler();
203 return ProtocolHandler::CreateProtocolHandler(base::UTF16ToUTF8(protocol),
204 GURL(base::UTF16ToUTF8(url)));
207 void HandlerOptionsHandler::Observe(
209 const content::NotificationSource& source,
210 const content::NotificationDetails& details) {
211 if (type == chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED)
217 } // namespace options