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/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 "grit/generated_resources.h" |
| 17 #include "ui/base/l10n/l10n_util.h" |
| 18 |
| 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::Initialize() { |
| 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 DCHECK(web_ui_); |
| 55 web_ui_->RegisterMessageCallback("clearDefault", |
| 56 base::Bind(&HandlerOptionsHandler::ClearDefault, |
| 57 base::Unretained(this))); |
| 58 web_ui_->RegisterMessageCallback("removeHandler", |
| 59 base::Bind(&HandlerOptionsHandler::RemoveHandler, |
| 60 base::Unretained(this))); |
| 61 web_ui_->RegisterMessageCallback("setHandlersEnabled", |
| 62 base::Bind(&HandlerOptionsHandler::SetHandlersEnabled, |
| 63 base::Unretained(this))); |
| 64 web_ui_->RegisterMessageCallback("setDefault", |
| 65 base::Bind(&HandlerOptionsHandler::SetDefault, |
| 66 base::Unretained(this))); |
| 67 web_ui_->RegisterMessageCallback("removeIgnoredHandler", |
| 68 base::Bind(&HandlerOptionsHandler::RemoveIgnoredHandler, |
| 69 base::Unretained(this))); |
| 70 } |
| 71 |
| 72 ProtocolHandlerRegistry* HandlerOptionsHandler::GetProtocolHandlerRegistry() { |
| 73 DCHECK(web_ui_); |
| 74 return Profile::FromWebUI(web_ui_)->GetProtocolHandlerRegistry(); |
| 75 } |
| 76 |
| 77 static void GetHandlersAsListValue( |
| 78 const ProtocolHandlerRegistry::ProtocolHandlerList& handlers, |
| 79 ListValue* handler_list) { |
| 80 ProtocolHandlerRegistry::ProtocolHandlerList::const_iterator handler; |
| 81 for (handler = handlers.begin(); handler != handlers.end(); ++handler) { |
| 82 ListValue* handlerValue = new ListValue(); |
| 83 handlerValue->Append(Value::CreateStringValue(handler->protocol())); |
| 84 handlerValue->Append(Value::CreateStringValue(handler->url().spec())); |
| 85 handlerValue->Append(Value::CreateStringValue(handler->title())); |
| 86 handler_list->Append(handlerValue); |
| 87 } |
| 88 } |
| 89 |
| 90 void HandlerOptionsHandler::GetHandlersForProtocol( |
| 91 const std::string& protocol, |
| 92 DictionaryValue* handlers_value) { |
| 93 ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry(); |
| 94 handlers_value->SetString("protocol", protocol); |
| 95 handlers_value->SetInteger("default_handler", |
| 96 registry->GetHandlerIndex(protocol)); |
| 97 |
| 98 ListValue* handlers_list = new ListValue(); |
| 99 GetHandlersAsListValue(registry->GetHandlersFor(protocol), handlers_list); |
| 100 handlers_value->Set("handlers", handlers_list); |
| 101 } |
| 102 |
| 103 void HandlerOptionsHandler::GetIgnoredHandlers(ListValue* handlers) { |
| 104 ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry(); |
| 105 ProtocolHandlerRegistry::ProtocolHandlerList ignored_handlers = |
| 106 registry->GetIgnoredHandlers(); |
| 107 return GetHandlersAsListValue(ignored_handlers, handlers); |
| 108 } |
| 109 |
| 110 void HandlerOptionsHandler::UpdateHandlerList() { |
| 111 #if defined(ENABLE_REGISTER_PROTOCOL_HANDLER) |
| 112 ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry(); |
| 113 std::vector<std::string> protocols; |
| 114 registry->GetRegisteredProtocols(&protocols); |
| 115 |
| 116 ListValue handlers; |
| 117 for (std::vector<std::string>::iterator protocol = protocols.begin(); |
| 118 protocol != protocols.end(); protocol++) { |
| 119 DictionaryValue* handler_value = new DictionaryValue(); |
| 120 GetHandlersForProtocol(*protocol, handler_value); |
| 121 handlers.Append(handler_value); |
| 122 } |
| 123 |
| 124 scoped_ptr<ListValue> ignored_handlers(new ListValue()); |
| 125 GetIgnoredHandlers(ignored_handlers.get()); |
| 126 web_ui_->CallJavascriptFunction("HandlerOptions.setHandlers", handlers); |
| 127 web_ui_->CallJavascriptFunction("HandlerOptions.setIgnoredHandlers", |
| 128 *ignored_handlers); |
| 129 #endif // defined(ENABLE_REGISTER_PROTOCOL_HANDLER) |
| 130 } |
| 131 |
| 132 void HandlerOptionsHandler::RemoveHandler(const ListValue* args) { |
| 133 ListValue* list; |
| 134 if (!args->GetList(0, &list)) { |
| 135 NOTREACHED(); |
| 136 return; |
| 137 } |
| 138 |
| 139 ProtocolHandler handler(ParseHandlerFromArgs(list)); |
| 140 GetProtocolHandlerRegistry()->RemoveHandler(handler); |
| 141 |
| 142 // No need to call UpdateHandlerList() - we should receive a notification |
| 143 // that the ProtocolHandlerRegistry has changed and we will update the view |
| 144 // then. |
| 145 } |
| 146 |
| 147 void HandlerOptionsHandler::RemoveIgnoredHandler(const ListValue* args) { |
| 148 ListValue* list; |
| 149 if (!args->GetList(0, &list)) { |
| 150 NOTREACHED(); |
| 151 return; |
| 152 } |
| 153 |
| 154 ProtocolHandler handler(ParseHandlerFromArgs(list)); |
| 155 GetProtocolHandlerRegistry()->RemoveIgnoredHandler(handler); |
| 156 } |
| 157 |
| 158 void HandlerOptionsHandler::SetHandlersEnabled(const ListValue* args) { |
| 159 bool enabled = true; |
| 160 CHECK(args->GetBoolean(0, &enabled)); |
| 161 if (enabled) |
| 162 GetProtocolHandlerRegistry()->Enable(); |
| 163 else |
| 164 GetProtocolHandlerRegistry()->Disable(); |
| 165 } |
| 166 |
| 167 void HandlerOptionsHandler::ClearDefault(const ListValue* args) { |
| 168 Value* value; |
| 169 CHECK(args->Get(0, &value)); |
| 170 std::string protocol_to_clear; |
| 171 CHECK(value->GetAsString(&protocol_to_clear)); |
| 172 GetProtocolHandlerRegistry()->ClearDefault(protocol_to_clear); |
| 173 } |
| 174 |
| 175 void HandlerOptionsHandler::SetDefault(const ListValue* args) { |
| 176 Value* value; |
| 177 CHECK(args->Get(0, &value)); |
| 178 ListValue* list; |
| 179 CHECK(args->GetList(0, &list)); |
| 180 const ProtocolHandler& handler(ParseHandlerFromArgs(list)); |
| 181 CHECK(!handler.IsEmpty()); |
| 182 GetProtocolHandlerRegistry()->OnAcceptRegisterProtocolHandler(handler); |
| 183 } |
| 184 |
| 185 ProtocolHandler HandlerOptionsHandler::ParseHandlerFromArgs( |
| 186 const ListValue* args) const { |
| 187 string16 protocol; |
| 188 string16 url; |
| 189 string16 title; |
| 190 bool ok = args->GetString(0, &protocol) && args->GetString(1, &url) && |
| 191 args->GetString(2, &title); |
| 192 if (!ok) |
| 193 return ProtocolHandler::EmptyProtocolHandler(); |
| 194 return ProtocolHandler::CreateProtocolHandler(UTF16ToUTF8(protocol), |
| 195 GURL(UTF16ToUTF8(url)), |
| 196 title); |
| 197 } |
| 198 |
| 199 void HandlerOptionsHandler::Observe( |
| 200 int type, |
| 201 const content::NotificationSource& source, |
| 202 const content::NotificationDetails& details) { |
| 203 if (type == chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED) |
| 204 UpdateHandlerList(); |
| 205 else |
| 206 NOTREACHED(); |
| 207 } |
OLD | NEW |