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/google/google_util.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "content/public/browser/web_ui.h"
19 #include "grit/generated_resources.h"
20 #include "ui/base/l10n/l10n_util.h"
26 const char kHandlersLearnMoreUrl[] =
27 "https://support.google.com/chromebook/answer/1382847";
31 HandlerOptionsHandler::HandlerOptionsHandler() {
34 HandlerOptionsHandler::~HandlerOptionsHandler() {
37 void HandlerOptionsHandler::GetLocalizedValues(
38 base::DictionaryValue* localized_strings) {
39 DCHECK(localized_strings);
41 static OptionsStringResource resources[] = {
42 { "handlers_tab_label", IDS_HANDLERS_TAB_LABEL },
43 { "handlers_allow", IDS_HANDLERS_ALLOW_RADIO },
44 { "handlers_block", IDS_HANDLERS_DONOTALLOW_RADIO },
45 { "handlers_type_column_header", IDS_HANDLERS_TYPE_COLUMN_HEADER },
46 { "handlers_site_column_header", IDS_HANDLERS_SITE_COLUMN_HEADER },
47 { "handlers_remove_link", IDS_HANDLERS_REMOVE_HANDLER_LINK },
48 { "handlers_none_handler", IDS_HANDLERS_NONE_HANDLER },
49 { "handlers_active_heading", IDS_HANDLERS_ACTIVE_HEADING },
50 { "handlers_ignored_heading", IDS_HANDLERS_IGNORED_HEADING },
52 RegisterTitle(localized_strings, "handlersPage",
53 IDS_HANDLER_OPTIONS_WINDOW_TITLE);
54 RegisterStrings(localized_strings, resources, arraysize(resources));
56 localized_strings->SetString(
57 "handlers_learn_more_url",
58 google_util::StringAppendGoogleLocaleParam(kHandlersLearnMoreUrl));
61 void HandlerOptionsHandler::InitializeHandler() {
62 notification_registrar_.Add(
63 this, chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED,
64 content::Source<Profile>(Profile::FromWebUI(web_ui())));
67 void HandlerOptionsHandler::InitializePage() {
71 void HandlerOptionsHandler::RegisterMessages() {
72 web_ui()->RegisterMessageCallback("clearDefault",
73 base::Bind(&HandlerOptionsHandler::ClearDefault,
74 base::Unretained(this)));
75 web_ui()->RegisterMessageCallback("removeHandler",
76 base::Bind(&HandlerOptionsHandler::RemoveHandler,
77 base::Unretained(this)));
78 web_ui()->RegisterMessageCallback("setHandlersEnabled",
79 base::Bind(&HandlerOptionsHandler::SetHandlersEnabled,
80 base::Unretained(this)));
81 web_ui()->RegisterMessageCallback("setDefault",
82 base::Bind(&HandlerOptionsHandler::SetDefault,
83 base::Unretained(this)));
84 web_ui()->RegisterMessageCallback("removeIgnoredHandler",
85 base::Bind(&HandlerOptionsHandler::RemoveIgnoredHandler,
86 base::Unretained(this)));
89 ProtocolHandlerRegistry* HandlerOptionsHandler::GetProtocolHandlerRegistry() {
90 return ProtocolHandlerRegistryFactory::GetForProfile(
91 Profile::FromWebUI(web_ui()));
94 static void GetHandlersAsListValue(
95 const ProtocolHandlerRegistry::ProtocolHandlerList& handlers,
96 base::ListValue* handler_list) {
97 ProtocolHandlerRegistry::ProtocolHandlerList::const_iterator handler;
98 for (handler = handlers.begin(); handler != handlers.end(); ++handler) {
99 base::ListValue* handlerValue = new base::ListValue();
100 handlerValue->Append(new base::StringValue(handler->protocol()));
101 handlerValue->Append(new base::StringValue(handler->url().spec()));
102 handlerValue->Append(new base::StringValue(handler->title()));
103 handler_list->Append(handlerValue);
107 void HandlerOptionsHandler::GetHandlersForProtocol(
108 const std::string& protocol,
109 base::DictionaryValue* handlers_value) {
110 ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry();
111 handlers_value->SetString("protocol", protocol);
112 handlers_value->SetInteger("default_handler",
113 registry->GetHandlerIndex(protocol));
115 base::ListValue* handlers_list = new base::ListValue();
116 GetHandlersAsListValue(registry->GetHandlersFor(protocol), handlers_list);
117 handlers_value->Set("handlers", handlers_list);
120 void HandlerOptionsHandler::GetIgnoredHandlers(base::ListValue* handlers) {
121 ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry();
122 ProtocolHandlerRegistry::ProtocolHandlerList ignored_handlers =
123 registry->GetIgnoredHandlers();
124 return GetHandlersAsListValue(ignored_handlers, handlers);
127 void HandlerOptionsHandler::UpdateHandlerList() {
128 ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry();
129 std::vector<std::string> protocols;
130 registry->GetRegisteredProtocols(&protocols);
132 base::ListValue handlers;
133 for (std::vector<std::string>::iterator protocol = protocols.begin();
134 protocol != protocols.end(); protocol++) {
135 base::DictionaryValue* handler_value = new base::DictionaryValue();
136 GetHandlersForProtocol(*protocol, handler_value);
137 handlers.Append(handler_value);
140 scoped_ptr<base::ListValue> ignored_handlers(new base::ListValue());
141 GetIgnoredHandlers(ignored_handlers.get());
142 web_ui()->CallJavascriptFunction("HandlerOptions.setHandlers", handlers);
143 web_ui()->CallJavascriptFunction("HandlerOptions.setIgnoredHandlers",
147 void HandlerOptionsHandler::RemoveHandler(const base::ListValue* args) {
148 const base::ListValue* list;
149 if (!args->GetList(0, &list)) {
154 ProtocolHandler handler(ParseHandlerFromArgs(list));
155 GetProtocolHandlerRegistry()->RemoveHandler(handler);
157 // No need to call UpdateHandlerList() - we should receive a notification
158 // that the ProtocolHandlerRegistry has changed and we will update the view
162 void HandlerOptionsHandler::RemoveIgnoredHandler(const base::ListValue* args) {
163 const base::ListValue* list;
164 if (!args->GetList(0, &list)) {
169 ProtocolHandler handler(ParseHandlerFromArgs(list));
170 GetProtocolHandlerRegistry()->RemoveIgnoredHandler(handler);
173 void HandlerOptionsHandler::SetHandlersEnabled(const base::ListValue* args) {
175 CHECK(args->GetBoolean(0, &enabled));
177 GetProtocolHandlerRegistry()->Enable();
179 GetProtocolHandlerRegistry()->Disable();
182 void HandlerOptionsHandler::ClearDefault(const base::ListValue* args) {
183 const base::Value* value;
184 CHECK(args->Get(0, &value));
185 std::string protocol_to_clear;
186 CHECK(value->GetAsString(&protocol_to_clear));
187 GetProtocolHandlerRegistry()->ClearDefault(protocol_to_clear);
190 void HandlerOptionsHandler::SetDefault(const base::ListValue* args) {
191 const base::ListValue* list;
192 CHECK(args->GetList(0, &list));
193 const ProtocolHandler& handler(ParseHandlerFromArgs(list));
194 CHECK(!handler.IsEmpty());
195 GetProtocolHandlerRegistry()->OnAcceptRegisterProtocolHandler(handler);
198 ProtocolHandler HandlerOptionsHandler::ParseHandlerFromArgs(
199 const base::ListValue* args) const {
200 base::string16 protocol;
202 base::string16 title;
203 bool ok = args->GetString(0, &protocol) && args->GetString(1, &url) &&
204 args->GetString(2, &title);
206 return ProtocolHandler::EmptyProtocolHandler();
207 return ProtocolHandler::CreateProtocolHandler(base::UTF16ToUTF8(protocol),
208 GURL(base::UTF16ToUTF8(url)),
212 void HandlerOptionsHandler::Observe(
214 const content::NotificationSource& source,
215 const content::NotificationDetails& details) {
216 if (type == chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED)
222 } // namespace options