OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/ui/webui/options/handler_options_handler.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/bind_helpers.h" | |
11 #include "base/utf_string_conversions.h" | |
12 #include "base/values.h" | |
13 #include "chrome/browser/prefs/pref_service.h" | |
14 #include "chrome/browser/profiles/profile.h" | |
15 #include "chrome/common/chrome_notification_types.h" | |
16 #include "content/public/browser/web_ui.h" | |
17 #include "grit/generated_resources.h" | |
18 #include "ui/base/l10n/l10n_util.h" | |
19 | |
20 HandlerOptionsHandler::HandlerOptionsHandler() { | |
21 } | |
22 | |
23 HandlerOptionsHandler::~HandlerOptionsHandler() { | |
24 } | |
25 | |
26 void HandlerOptionsHandler::GetLocalizedValues( | |
27 DictionaryValue* localized_strings) { | |
28 DCHECK(localized_strings); | |
29 | |
30 static OptionsStringResource resources[] = { | |
31 { "handlers_tab_label", IDS_HANDLERS_TAB_LABEL }, | |
32 { "handlers_allow", IDS_HANDLERS_ALLOW_RADIO }, | |
33 { "handlers_block", IDS_HANDLERS_DONOTALLOW_RADIO }, | |
34 { "handlers_type_column_header", IDS_HANDLERS_TYPE_COLUMN_HEADER }, | |
35 { "handlers_site_column_header", IDS_HANDLERS_SITE_COLUMN_HEADER }, | |
36 { "handlers_remove_link", IDS_HANDLERS_REMOVE_HANDLER_LINK }, | |
37 { "handlers_none_handler", IDS_HANDLERS_NONE_HANDLER }, | |
38 { "handlers_active_heading", IDS_HANDLERS_ACTIVE_HEADING }, | |
39 { "handlers_ignored_heading", IDS_HANDLERS_IGNORED_HEADING }, | |
40 }; | |
41 RegisterTitle(localized_strings, "handlersPage", | |
42 IDS_HANDLER_OPTIONS_WINDOW_TITLE); | |
43 RegisterStrings(localized_strings, resources, arraysize(resources)); | |
44 } | |
45 | |
46 void HandlerOptionsHandler::InitializeHandler() { | |
47 UpdateHandlerList(); | |
48 notification_registrar_.Add( | |
49 this, chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED, | |
50 content::Source<Profile>(Profile::FromWebUI(web_ui()))); | |
51 } | |
52 | |
53 void HandlerOptionsHandler::RegisterMessages() { | |
54 web_ui()->RegisterMessageCallback("clearDefault", | |
55 base::Bind(&HandlerOptionsHandler::ClearDefault, | |
56 base::Unretained(this))); | |
57 web_ui()->RegisterMessageCallback("removeHandler", | |
58 base::Bind(&HandlerOptionsHandler::RemoveHandler, | |
59 base::Unretained(this))); | |
60 web_ui()->RegisterMessageCallback("setHandlersEnabled", | |
61 base::Bind(&HandlerOptionsHandler::SetHandlersEnabled, | |
62 base::Unretained(this))); | |
63 web_ui()->RegisterMessageCallback("setDefault", | |
64 base::Bind(&HandlerOptionsHandler::SetDefault, | |
65 base::Unretained(this))); | |
66 web_ui()->RegisterMessageCallback("removeIgnoredHandler", | |
67 base::Bind(&HandlerOptionsHandler::RemoveIgnoredHandler, | |
68 base::Unretained(this))); | |
69 } | |
70 | |
71 ProtocolHandlerRegistry* HandlerOptionsHandler::GetProtocolHandlerRegistry() { | |
72 return Profile::FromWebUI(web_ui())->GetProtocolHandlerRegistry(); | |
73 } | |
74 | |
75 static void GetHandlersAsListValue( | |
76 const ProtocolHandlerRegistry::ProtocolHandlerList& handlers, | |
77 ListValue* handler_list) { | |
78 ProtocolHandlerRegistry::ProtocolHandlerList::const_iterator handler; | |
79 for (handler = handlers.begin(); handler != handlers.end(); ++handler) { | |
80 ListValue* handlerValue = new ListValue(); | |
81 handlerValue->Append(Value::CreateStringValue(handler->protocol())); | |
82 handlerValue->Append(Value::CreateStringValue(handler->url().spec())); | |
83 handlerValue->Append(Value::CreateStringValue(handler->title())); | |
84 handler_list->Append(handlerValue); | |
85 } | |
86 } | |
87 | |
88 void HandlerOptionsHandler::GetHandlersForProtocol( | |
89 const std::string& protocol, | |
90 DictionaryValue* handlers_value) { | |
91 ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry(); | |
92 handlers_value->SetString("protocol", protocol); | |
93 handlers_value->SetInteger("default_handler", | |
94 registry->GetHandlerIndex(protocol)); | |
95 | |
96 ListValue* handlers_list = new ListValue(); | |
97 GetHandlersAsListValue(registry->GetHandlersFor(protocol), handlers_list); | |
98 handlers_value->Set("handlers", handlers_list); | |
99 } | |
100 | |
101 void HandlerOptionsHandler::GetIgnoredHandlers(ListValue* handlers) { | |
102 ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry(); | |
103 ProtocolHandlerRegistry::ProtocolHandlerList ignored_handlers = | |
104 registry->GetIgnoredHandlers(); | |
105 return GetHandlersAsListValue(ignored_handlers, handlers); | |
106 } | |
107 | |
108 void HandlerOptionsHandler::UpdateHandlerList() { | |
109 #if defined(ENABLE_REGISTER_PROTOCOL_HANDLER) | |
110 ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry(); | |
111 std::vector<std::string> protocols; | |
112 registry->GetRegisteredProtocols(&protocols); | |
113 | |
114 ListValue handlers; | |
115 for (std::vector<std::string>::iterator protocol = protocols.begin(); | |
116 protocol != protocols.end(); protocol++) { | |
117 DictionaryValue* handler_value = new DictionaryValue(); | |
118 GetHandlersForProtocol(*protocol, handler_value); | |
119 handlers.Append(handler_value); | |
120 } | |
121 | |
122 scoped_ptr<ListValue> ignored_handlers(new ListValue()); | |
123 GetIgnoredHandlers(ignored_handlers.get()); | |
124 web_ui()->CallJavascriptFunction("HandlerOptions.setHandlers", handlers); | |
125 web_ui()->CallJavascriptFunction("HandlerOptions.setIgnoredHandlers", | |
126 *ignored_handlers); | |
127 #endif // defined(ENABLE_REGISTER_PROTOCOL_HANDLER) | |
128 } | |
129 | |
130 void HandlerOptionsHandler::RemoveHandler(const ListValue* args) { | |
131 ListValue* list; | |
132 if (!args->GetList(0, &list)) { | |
133 NOTREACHED(); | |
134 return; | |
135 } | |
136 | |
137 ProtocolHandler handler(ParseHandlerFromArgs(list)); | |
138 GetProtocolHandlerRegistry()->RemoveHandler(handler); | |
139 | |
140 // No need to call UpdateHandlerList() - we should receive a notification | |
141 // that the ProtocolHandlerRegistry has changed and we will update the view | |
142 // then. | |
143 } | |
144 | |
145 void HandlerOptionsHandler::RemoveIgnoredHandler(const ListValue* args) { | |
146 ListValue* list; | |
147 if (!args->GetList(0, &list)) { | |
148 NOTREACHED(); | |
149 return; | |
150 } | |
151 | |
152 ProtocolHandler handler(ParseHandlerFromArgs(list)); | |
153 GetProtocolHandlerRegistry()->RemoveIgnoredHandler(handler); | |
154 } | |
155 | |
156 void HandlerOptionsHandler::SetHandlersEnabled(const ListValue* args) { | |
157 bool enabled = true; | |
158 CHECK(args->GetBoolean(0, &enabled)); | |
159 if (enabled) | |
160 GetProtocolHandlerRegistry()->Enable(); | |
161 else | |
162 GetProtocolHandlerRegistry()->Disable(); | |
163 } | |
164 | |
165 void HandlerOptionsHandler::ClearDefault(const ListValue* args) { | |
166 Value* value; | |
167 CHECK(args->Get(0, &value)); | |
168 std::string protocol_to_clear; | |
169 CHECK(value->GetAsString(&protocol_to_clear)); | |
170 GetProtocolHandlerRegistry()->ClearDefault(protocol_to_clear); | |
171 } | |
172 | |
173 void HandlerOptionsHandler::SetDefault(const ListValue* args) { | |
174 Value* value; | |
175 CHECK(args->Get(0, &value)); | |
176 ListValue* list; | |
177 CHECK(args->GetList(0, &list)); | |
178 const ProtocolHandler& handler(ParseHandlerFromArgs(list)); | |
179 CHECK(!handler.IsEmpty()); | |
180 GetProtocolHandlerRegistry()->OnAcceptRegisterProtocolHandler(handler); | |
181 } | |
182 | |
183 ProtocolHandler HandlerOptionsHandler::ParseHandlerFromArgs( | |
184 const ListValue* args) const { | |
185 string16 protocol; | |
186 string16 url; | |
187 string16 title; | |
188 bool ok = args->GetString(0, &protocol) && args->GetString(1, &url) && | |
189 args->GetString(2, &title); | |
190 if (!ok) | |
191 return ProtocolHandler::EmptyProtocolHandler(); | |
192 return ProtocolHandler::CreateProtocolHandler(UTF16ToUTF8(protocol), | |
193 GURL(UTF16ToUTF8(url)), | |
194 title); | |
195 } | |
196 | |
197 void HandlerOptionsHandler::Observe( | |
198 int type, | |
199 const content::NotificationSource& source, | |
200 const content::NotificationDetails& details) { | |
201 if (type == chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED) | |
202 UpdateHandlerList(); | |
203 else | |
204 NOTREACHED(); | |
205 } | |
OLD | NEW |