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