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 "chrome/common/url_constants.h"
18 #include "chrome/grit/generated_resources.h"
19 #include "components/google/core/browser/google_util.h"
20 #include "content/public/browser/web_ui.h"
24 HandlerOptionsHandler::HandlerOptionsHandler() {
27 HandlerOptionsHandler::~HandlerOptionsHandler() {
30 void HandlerOptionsHandler::GetLocalizedValues(
31 base::DictionaryValue
* localized_strings
) {
32 DCHECK(localized_strings
);
34 static OptionsStringResource resources
[] = {
35 { "handlersTabLabel", IDS_HANDLERS_TAB_LABEL
},
36 { "handlersAllow", IDS_HANDLERS_ALLOW_RADIO
},
37 { "handlersBlock", IDS_HANDLERS_DONOTALLOW_RADIO
},
38 { "handlersTypeColumnHeader", IDS_HANDLERS_TYPE_COLUMN_HEADER
},
39 { "handlersSiteColumnHeader", IDS_HANDLERS_SITE_COLUMN_HEADER
},
40 { "handlersRemoveLink", IDS_HANDLERS_REMOVE_HANDLER_LINK
},
41 { "handlersNoneHandler", IDS_HANDLERS_NONE_HANDLER
},
42 { "handlersActiveHeading", IDS_HANDLERS_ACTIVE_HEADING
},
43 { "handlersIgnoredHeading", IDS_HANDLERS_IGNORED_HEADING
},
45 RegisterTitle(localized_strings
, "handlersPage",
46 IDS_HANDLER_OPTIONS_WINDOW_TITLE
);
47 RegisterStrings(localized_strings
, resources
, arraysize(resources
));
49 localized_strings
->SetString("handlersLearnMoreUrl",
50 chrome::kLearnMoreRegisterProtocolHandlerURL
);
53 void HandlerOptionsHandler::InitializeHandler() {
54 notification_registrar_
.Add(
55 this, chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED
,
56 content::Source
<Profile
>(Profile::FromWebUI(web_ui())));
59 void HandlerOptionsHandler::InitializePage() {
63 void HandlerOptionsHandler::RegisterMessages() {
64 web_ui()->RegisterMessageCallback("clearDefault",
65 base::Bind(&HandlerOptionsHandler::ClearDefault
,
66 base::Unretained(this)));
67 web_ui()->RegisterMessageCallback("removeHandler",
68 base::Bind(&HandlerOptionsHandler::RemoveHandler
,
69 base::Unretained(this)));
70 web_ui()->RegisterMessageCallback("setHandlersEnabled",
71 base::Bind(&HandlerOptionsHandler::SetHandlersEnabled
,
72 base::Unretained(this)));
73 web_ui()->RegisterMessageCallback("setDefault",
74 base::Bind(&HandlerOptionsHandler::SetDefault
,
75 base::Unretained(this)));
76 web_ui()->RegisterMessageCallback("removeIgnoredHandler",
77 base::Bind(&HandlerOptionsHandler::RemoveIgnoredHandler
,
78 base::Unretained(this)));
81 ProtocolHandlerRegistry
* HandlerOptionsHandler::GetProtocolHandlerRegistry() {
82 return ProtocolHandlerRegistryFactory::GetForBrowserContext(
83 Profile::FromWebUI(web_ui()));
86 static void GetHandlersAsListValue(
87 const ProtocolHandlerRegistry::ProtocolHandlerList
& handlers
,
88 base::ListValue
* handler_list
) {
89 ProtocolHandlerRegistry::ProtocolHandlerList::const_iterator handler
;
90 for (handler
= handlers
.begin(); handler
!= handlers
.end(); ++handler
) {
91 base::ListValue
* handlerValue
= new base::ListValue();
92 handlerValue
->Append(new base::StringValue(handler
->protocol()));
93 handlerValue
->Append(new base::StringValue(handler
->url().spec()));
94 handlerValue
->Append(new base::StringValue(handler
->url().host()));
95 handler_list
->Append(handlerValue
);
99 void HandlerOptionsHandler::GetHandlersForProtocol(
100 const std::string
& protocol
,
101 base::DictionaryValue
* handlers_value
) {
102 ProtocolHandlerRegistry
* registry
= GetProtocolHandlerRegistry();
103 // The items which are to be written into |handlers_value| are also described
104 // in chrome/browser/resources/options/handler_options.js in @typedef
105 // for Handlers. Please update them whenever you add or remove any keys here.
106 handlers_value
->SetString("protocol", protocol
);
107 handlers_value
->SetInteger("default_handler",
108 registry
->GetHandlerIndex(protocol
));
109 handlers_value
->SetBoolean(
110 "is_default_handler_set_by_user",
111 registry
->IsRegisteredByUser(registry
->GetHandlerFor(protocol
)));
112 handlers_value
->SetBoolean("has_policy_recommendations",
113 registry
->HasPolicyRegisteredHandler(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 bool ok
= args
->GetString(0, &protocol
) && args
->GetString(1, &url
);
204 return ProtocolHandler::EmptyProtocolHandler();
205 return ProtocolHandler::CreateProtocolHandler(base::UTF16ToUTF8(protocol
),
206 GURL(base::UTF16ToUTF8(url
)));
209 void HandlerOptionsHandler::Observe(
211 const content::NotificationSource
& source
,
212 const content::NotificationDetails
& details
) {
213 if (type
== chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED
)
219 } // namespace options