Chromium Code Reviews| 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 }, | |
|
tony
2011/05/23 21:42:00
Nit: I think we normally 4 space indent stuff like
koz (OOO until 15th September)
2011/05/24 08:47:49
Done.
| |
| 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 ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry(); | |
| 86 std::vector<std::string> protocols; | |
| 87 registry->GetHandledProtocols(&protocols); | |
| 88 | |
| 89 ListValue handlers; | |
| 90 for (std::vector<std::string>::iterator protocol = protocols.begin(); | |
| 91 protocol != protocols.end(); protocol++) { | |
| 92 handlers.Append(GetHandlersForProtocol(*protocol)); | |
| 93 } | |
| 94 | |
| 95 web_ui_->CallJavascriptFunction("HandlerOptions.setHandlers", handlers); | |
| 96 } | |
| 97 | |
| 98 void HandlerOptionsHandler::RemoveHandler(const ListValue* args) { | |
| 99 ListValue* list; | |
| 100 if (!args->GetList(0, &list)) { | |
| 101 NOTREACHED(); | |
| 102 return; | |
| 103 } | |
| 104 | |
| 105 ProtocolHandler handler(ParseHandlerFromArgs(list)); | |
| 106 GetProtocolHandlerRegistry()->RemoveHandler(handler); | |
| 107 | |
| 108 // No need to call UpdateHandlerList() - we should receive a notification | |
| 109 // that the ProtocolHandlerRegistry has changed and we will update the view | |
| 110 // then. | |
| 111 } | |
| 112 | |
| 113 void HandlerOptionsHandler::SetHandlersEnabled(const ListValue* args) { | |
| 114 bool enabled = true; | |
| 115 CHECK(args->GetBoolean(0, &enabled)); | |
| 116 if (enabled) | |
| 117 GetProtocolHandlerRegistry()->Enable(); | |
| 118 else | |
| 119 GetProtocolHandlerRegistry()->Disable(); | |
| 120 } | |
| 121 | |
| 122 void HandlerOptionsHandler::SetDefault(const ListValue* args) { | |
| 123 Value* value; | |
| 124 CHECK(args->Get(0, &value)); | |
| 125 ListValue* list; | |
| 126 CHECK(args->GetList(0, &list)); | |
| 127 const ProtocolHandler& handler(ParseHandlerFromArgs(list)); | |
| 128 CHECK(!handler.IsEmpty()); | |
| 129 GetProtocolHandlerRegistry()->SetDefault(handler); | |
| 130 } | |
| 131 | |
| 132 ProtocolHandler HandlerOptionsHandler::ParseHandlerFromArgs( | |
| 133 const ListValue* args) const { | |
| 134 string16 protocol; | |
| 135 string16 url; | |
| 136 string16 title; | |
| 137 bool ok = true; | |
| 138 ok = ok && args->GetString(0, &protocol); | |
| 139 ok = ok && args->GetString(1, &url); | |
| 140 ok = ok && args->GetString(2, &title); | |
|
tony
2011/05/23 21:42:00
This could just be:
bool ok = args->GetString(0, &
koz (OOO until 15th September)
2011/05/24 08:47:49
Done.
| |
| 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 |