OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 "base/utf_string_conversions.h" | |
8 #include "base/values.h" | |
9 #include "chrome/browser/prefs/pref_service.h" | |
10 #include "chrome/browser/profiles/profile.h" | |
11 #include "grit/generated_resources.h" | |
12 #include "ui/base/l10n/l10n_util.h" | |
13 | |
14 | |
15 HandlerOptionsHandler::HandlerOptionsHandler() { | |
16 } | |
17 | |
18 HandlerOptionsHandler::~HandlerOptionsHandler() { | |
19 } | |
20 | |
21 void HandlerOptionsHandler::GetLocalizedValues( | |
22 DictionaryValue* localized_strings) { | |
23 DCHECK(localized_strings); | |
24 | |
25 static OptionsStringResource resources[] = { | |
26 { "handlers_tab_label", IDS_HANDLERS_TAB_LABEL }, | |
27 { "handlers_allow", IDS_HANDLERS_ALLOW_RADIO }, | |
28 { "handlers_block", IDS_HANDLERS_DONOTALLOW_RADIO }, | |
29 { "handlers_type_column_header", IDS_HANDLERS_TYPE_COLUMN_HEADER }, | |
30 { "handlers_site_column_header", IDS_HANDLERS_SITE_COLUMN_HEADER }, | |
31 { "handlers_remove_link", IDS_HANDLERS_REMOVE_HANDLER_LINK }, | |
32 }; | |
33 RegisterTitle(localized_strings, "handlersPage", | |
34 IDS_HANDLER_OPTIONS_WINDOW_TITLE); | |
35 RegisterStrings(localized_strings, resources, arraysize(resources)); | |
36 } | |
37 | |
38 void HandlerOptionsHandler::Initialize() { | |
39 UpdateHandlerList(); | |
40 notification_registrar_.Add( | |
41 this, NotificationType::PROTOCOL_HANDLER_REGISTRY_CHANGED, | |
42 NotificationService::AllSources()); | |
43 } | |
44 | |
45 void HandlerOptionsHandler::RegisterMessages() { | |
46 DCHECK(web_ui_); | |
47 web_ui_->RegisterMessageCallback("removeHandler", | |
48 NewCallback(this, &HandlerOptionsHandler::RemoveHandler)); | |
49 web_ui_->RegisterMessageCallback("setHandlersEnabled", | |
50 NewCallback(this, &HandlerOptionsHandler::SetHandlersEnabled)); | |
51 web_ui_->RegisterMessageCallback("setDefault", | |
52 NewCallback(this, &HandlerOptionsHandler::SetDefault)); | |
53 } | |
54 | |
55 ProtocolHandlerRegistry* HandlerOptionsHandler::GetProtocolHandlerRegistry() { | |
56 DCHECK(web_ui_); | |
57 return web_ui_->GetProfile()->GetProtocolHandlerRegistry(); | |
58 } | |
59 | |
60 DictionaryValue* HandlerOptionsHandler::GetHandlersForProtocol( | |
61 const std::string& protocol) { | |
62 ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry(); | |
63 DictionaryValue* handlers_value = new DictionaryValue(); | |
64 handlers_value->SetString("protocol", protocol); | |
65 handlers_value->SetInteger("default_handler", | |
66 registry->GetHandlerIndex(protocol)); | |
67 | |
68 ListValue* handler_list = new ListValue(); | |
69 const ProtocolHandlerRegistry::ProtocolHandlerList* handlers = | |
70 registry->GetHandlersFor(protocol); | |
71 ProtocolHandlerRegistry::ProtocolHandlerList::const_iterator p; | |
Evan Stade
2011/05/19 17:08:42
variable names should be descriptive (p is not) (h
koz (OOO until 15th September)
2011/05/19 21:07:52
Done.
| |
72 for (p = handlers->begin(); p != handlers->end(); ++p) { | |
73 ListValue* handler = new ListValue(); | |
74 handler->Append(Value::CreateStringValue(p->url().spec())); | |
75 handler->Append(Value::CreateStringValue(p->title())); | |
76 handler_list->Append(handler); | |
77 } | |
78 handlers_value->Set("handlers", handler_list); | |
79 return handlers_value; | |
80 } | |
81 | |
82 void HandlerOptionsHandler::UpdateHandlerList() { | |
83 ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry(); | |
84 std::vector<std::string> protocols; | |
85 registry->GetHandledProtocols(&protocols); | |
86 | |
87 ListValue handlers; | |
88 for (std::vector<std::string>::iterator p = protocols.begin(); | |
89 p != protocols.end(); p++) { | |
Evan Stade
2011/05/19 17:08:42
one more space of indent
koz (OOO until 15th September)
2011/05/19 21:07:52
Done.
| |
90 handlers.Append(GetHandlersForProtocol(*p)); | |
91 } | |
92 | |
93 web_ui_->CallJavascriptFunction("HandlerOptions.setHandlers", handlers); | |
94 } | |
95 | |
96 void HandlerOptionsHandler::RemoveHandler(const ListValue* args) { | |
97 ListValue* list; | |
98 if (!args->GetList(0, &list)) { | |
99 NOTREACHED(); | |
100 return; | |
101 } | |
102 | |
103 ProtocolHandler handler(ParseHandlerFromArgs(list)); | |
104 GetProtocolHandlerRegistry()->RemoveHandler(handler); | |
105 | |
106 // No need to call UpdateHandlerList() - we should receive a notification | |
107 // that the ProtocolHandlerRegistry has changed and we will update the view | |
108 // then. | |
109 } | |
110 | |
111 void HandlerOptionsHandler::SetHandlersEnabled(const ListValue* args) { | |
112 bool enabled = true; | |
113 if (!args->GetBoolean(0, &enabled)) { | |
Evan Stade
2011/05/19 17:08:42
prefer CHECK (here and elsewhere)
koz (OOO until 15th September)
2011/05/19 21:07:52
Done.
| |
114 NOTREACHED(); | |
115 return; | |
116 } | |
117 if (enabled) { | |
Evan Stade
2011/05/19 17:08:42
no curlies (here and elsewhere)
koz (OOO until 15th September)
2011/05/19 21:07:52
Done.
| |
118 GetProtocolHandlerRegistry()->Enable(); | |
119 } else { | |
120 GetProtocolHandlerRegistry()->Disable(); | |
121 } | |
122 } | |
123 | |
124 void HandlerOptionsHandler::SetDefault(const ListValue* args) { | |
125 Value* value; | |
126 args->Get(0, &value); | |
127 ListValue* list; | |
128 if (!args->GetList(0, &list)) { | |
129 NOTREACHED(); | |
130 return; | |
131 } | |
132 const ProtocolHandler& handler(ParseHandlerFromArgs(list)); | |
133 if (handler.IsEmpty()) { | |
134 NOTREACHED(); | |
135 return; | |
136 } | |
137 GetProtocolHandlerRegistry()->SetDefault(handler); | |
138 } | |
139 | |
140 ProtocolHandler HandlerOptionsHandler::ParseHandlerFromArgs( | |
141 const ListValue* args) const { | |
142 string16 protocol; | |
143 string16 url; | |
144 string16 title; | |
145 bool ok = true; | |
146 ok = ok && args->GetString(0, &protocol); | |
147 ok = ok && args->GetString(1, &url); | |
148 ok = ok && args->GetString(2, &title); | |
149 if (!ok) { | |
150 return ProtocolHandler::EmptyProtocolHandler(); | |
151 } | |
152 return ProtocolHandler::CreateProtocolHandler(UTF16ToUTF8(protocol), | |
153 GURL(UTF16ToUTF8(url)), | |
154 title); | |
155 } | |
156 | |
157 void HandlerOptionsHandler::Observe(NotificationType type, | |
158 const NotificationSource& source, | |
159 const NotificationDetails& details) { | |
160 if (type == NotificationType::PROTOCOL_HANDLER_REGISTRY_CHANGED) { | |
161 UpdateHandlerList(); | |
162 } else { | |
163 NOTREACHED(); | |
164 } | |
165 } | |
OLD | NEW |