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 handler; |
| 72 for (handler = handlers->begin(); handler != handlers->end(); ++handler) { |
| 73 ListValue* handlerValue = new ListValue(); |
| 74 handlerValue->Append(Value::CreateStringValue(handler->url().spec())); |
| 75 handlerValue->Append(Value::CreateStringValue(handler->title())); |
| 76 handler_list->Append(handlerValue); |
| 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 protocol = protocols.begin(); |
| 89 protocol != protocols.end(); protocol++) { |
| 90 handlers.Append(GetHandlersForProtocol(*protocol)); |
| 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 CHECK(args->GetBoolean(0, &enabled)); |
| 114 if (enabled) |
| 115 GetProtocolHandlerRegistry()->Enable(); |
| 116 else |
| 117 GetProtocolHandlerRegistry()->Disable(); |
| 118 } |
| 119 |
| 120 void HandlerOptionsHandler::SetDefault(const ListValue* args) { |
| 121 Value* value; |
| 122 CHECK(args->Get(0, &value)); |
| 123 ListValue* list; |
| 124 CHECK(args->GetList(0, &list)); |
| 125 const ProtocolHandler& handler(ParseHandlerFromArgs(list)); |
| 126 CHECK(!handler.IsEmpty()); |
| 127 GetProtocolHandlerRegistry()->SetDefault(handler); |
| 128 } |
| 129 |
| 130 ProtocolHandler HandlerOptionsHandler::ParseHandlerFromArgs( |
| 131 const ListValue* args) const { |
| 132 string16 protocol; |
| 133 string16 url; |
| 134 string16 title; |
| 135 bool ok = true; |
| 136 ok = ok && args->GetString(0, &protocol); |
| 137 ok = ok && args->GetString(1, &url); |
| 138 ok = ok && args->GetString(2, &title); |
| 139 if (!ok) |
| 140 return ProtocolHandler::EmptyProtocolHandler(); |
| 141 return ProtocolHandler::CreateProtocolHandler(UTF16ToUTF8(protocol), |
| 142 GURL(UTF16ToUTF8(url)), |
| 143 title); |
| 144 } |
| 145 |
| 146 void HandlerOptionsHandler::Observe(NotificationType type, |
| 147 const NotificationSource& source, |
| 148 const NotificationDetails& details) { |
| 149 if (type == NotificationType::PROTOCOL_HANDLER_REGISTRY_CHANGED) |
| 150 UpdateHandlerList(); |
| 151 else |
| 152 NOTREACHED(); |
| 153 } |
OLD | NEW |