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