Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / options / handler_options_handler.cc
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/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"
21
22 namespace options {
23
24 namespace {
25
26 const char kHandlersLearnMoreUrl[] =
27     "https://support.google.com/chromebook/answer/1382847";
28
29 }  // namespace
30
31 HandlerOptionsHandler::HandlerOptionsHandler() {
32 }
33
34 HandlerOptionsHandler::~HandlerOptionsHandler() {
35 }
36
37 void HandlerOptionsHandler::GetLocalizedValues(
38     base::DictionaryValue* localized_strings) {
39   DCHECK(localized_strings);
40
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 },
51   };
52   RegisterTitle(localized_strings, "handlersPage",
53                 IDS_HANDLER_OPTIONS_WINDOW_TITLE);
54   RegisterStrings(localized_strings, resources, arraysize(resources));
55
56   localized_strings->SetString(
57       "handlers_learn_more_url",
58       google_util::StringAppendGoogleLocaleParam(kHandlersLearnMoreUrl));
59 }
60
61 void HandlerOptionsHandler::InitializeHandler() {
62   notification_registrar_.Add(
63       this, chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED,
64       content::Source<Profile>(Profile::FromWebUI(web_ui())));
65 }
66
67 void HandlerOptionsHandler::InitializePage() {
68   UpdateHandlerList();
69 }
70
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)));
87 }
88
89 ProtocolHandlerRegistry* HandlerOptionsHandler::GetProtocolHandlerRegistry() {
90   return ProtocolHandlerRegistryFactory::GetForProfile(
91       Profile::FromWebUI(web_ui()));
92 }
93
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);
104   }
105 }
106
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));
114
115   base::ListValue* handlers_list = new base::ListValue();
116   GetHandlersAsListValue(registry->GetHandlersFor(protocol), handlers_list);
117   handlers_value->Set("handlers", handlers_list);
118 }
119
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);
125 }
126
127 void HandlerOptionsHandler::UpdateHandlerList() {
128   ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry();
129   std::vector<std::string> protocols;
130   registry->GetRegisteredProtocols(&protocols);
131
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);
138   }
139
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",
144                                    *ignored_handlers);
145 }
146
147 void HandlerOptionsHandler::RemoveHandler(const base::ListValue* args) {
148   const base::ListValue* list;
149   if (!args->GetList(0, &list)) {
150     NOTREACHED();
151     return;
152   }
153
154   ProtocolHandler handler(ParseHandlerFromArgs(list));
155   GetProtocolHandlerRegistry()->RemoveHandler(handler);
156
157   // No need to call UpdateHandlerList() - we should receive a notification
158   // that the ProtocolHandlerRegistry has changed and we will update the view
159   // then.
160 }
161
162 void HandlerOptionsHandler::RemoveIgnoredHandler(const base::ListValue* args) {
163   const base::ListValue* list;
164   if (!args->GetList(0, &list)) {
165     NOTREACHED();
166     return;
167   }
168
169   ProtocolHandler handler(ParseHandlerFromArgs(list));
170   GetProtocolHandlerRegistry()->RemoveIgnoredHandler(handler);
171 }
172
173 void HandlerOptionsHandler::SetHandlersEnabled(const base::ListValue* args) {
174   bool enabled = true;
175   CHECK(args->GetBoolean(0, &enabled));
176   if (enabled)
177     GetProtocolHandlerRegistry()->Enable();
178   else
179     GetProtocolHandlerRegistry()->Disable();
180 }
181
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);
188 }
189
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);
196 }
197
198 ProtocolHandler HandlerOptionsHandler::ParseHandlerFromArgs(
199     const base::ListValue* args) const {
200   base::string16 protocol;
201   base::string16 url;
202   base::string16 title;
203   bool ok = args->GetString(0, &protocol) && args->GetString(1, &url) &&
204     args->GetString(2, &title);
205   if (!ok)
206     return ProtocolHandler::EmptyProtocolHandler();
207   return ProtocolHandler::CreateProtocolHandler(base::UTF16ToUTF8(protocol),
208                                                 GURL(base::UTF16ToUTF8(url)),
209                                                 title);
210 }
211
212 void HandlerOptionsHandler::Observe(
213     int type,
214     const content::NotificationSource& source,
215     const content::NotificationDetails& details) {
216   if (type == chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED)
217     UpdateHandlerList();
218   else
219     NOTREACHED();
220 }
221
222 }  // namespace options