Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/options/handler_options_handler.h" | 5 #include "chrome/browser/ui/webui/settings/settings_handler_options_handler.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "chrome/browser/chrome_notification_types.h" | 16 #include "chrome/browser/chrome_notification_types.h" |
| 17 #include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h" | 17 #include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
| 20 #include "chrome/grit/generated_resources.h" | 20 #include "chrome/grit/generated_resources.h" |
| 21 #include "components/google/core/browser/google_util.h" | 21 #include "components/google/core/browser/google_util.h" |
| 22 #include "components/prefs/pref_service.h" | 22 #include "components/prefs/pref_service.h" |
| 23 #include "content/public/browser/web_ui.h" | 23 #include "content/public/browser/web_ui.h" |
| 24 | 24 |
| 25 namespace options { | 25 namespace settings { |
| 26 | 26 |
| 27 HandlerOptionsHandler::HandlerOptionsHandler() { | 27 HandlerOptionsHandler::HandlerOptionsHandler() { |
| 28 } | 28 } |
| 29 | 29 |
| 30 HandlerOptionsHandler::~HandlerOptionsHandler() { | 30 HandlerOptionsHandler::~HandlerOptionsHandler() { |
| 31 } | 31 } |
| 32 | 32 |
| 33 void HandlerOptionsHandler::GetLocalizedValues( | 33 void HandlerOptionsHandler::OnJavascriptAllowed() { |
| 34 base::DictionaryValue* localized_strings) { | |
| 35 DCHECK(localized_strings); | |
| 36 | |
| 37 static OptionsStringResource resources[] = { | |
| 38 { "handlersTabLabel", IDS_HANDLERS_TAB_LABEL }, | |
| 39 { "handlersAllow", IDS_HANDLERS_ALLOW_RADIO }, | |
| 40 { "handlersBlock", IDS_HANDLERS_DONOTALLOW_RADIO }, | |
| 41 { "handlersTypeColumnHeader", IDS_HANDLERS_TYPE_COLUMN_HEADER }, | |
| 42 { "handlersSiteColumnHeader", IDS_HANDLERS_SITE_COLUMN_HEADER }, | |
| 43 { "handlersRemoveLink", IDS_HANDLERS_REMOVE_HANDLER_LINK }, | |
| 44 { "handlersNoneHandler", IDS_HANDLERS_NONE_HANDLER }, | |
| 45 { "handlersActiveHeading", IDS_HANDLERS_ACTIVE_HEADING }, | |
| 46 { "handlersIgnoredHeading", IDS_HANDLERS_IGNORED_HEADING }, | |
| 47 }; | |
| 48 RegisterTitle(localized_strings, "handlersPage", | |
| 49 IDS_HANDLER_OPTIONS_WINDOW_TITLE); | |
| 50 RegisterStrings(localized_strings, resources, arraysize(resources)); | |
| 51 | |
| 52 localized_strings->SetString("handlersLearnMoreUrl", | |
| 53 chrome::kLearnMoreRegisterProtocolHandlerURL); | |
| 54 } | |
| 55 | |
| 56 void HandlerOptionsHandler::InitializeHandler() { | |
| 57 notification_registrar_.Add( | 34 notification_registrar_.Add( |
| 58 this, chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED, | 35 this, chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED, |
| 59 content::Source<Profile>(Profile::FromWebUI(web_ui()))); | 36 content::Source<Profile>(Profile::FromWebUI(web_ui()))); |
| 60 } | 37 } |
| 61 | 38 |
| 62 void HandlerOptionsHandler::InitializePage() { | 39 void HandlerOptionsHandler::OnJavascriptDisallowed() { |
| 63 UpdateHandlerList(); | 40 |
| 64 } | 41 } |
| 65 | 42 |
| 66 void HandlerOptionsHandler::RegisterMessages() { | 43 void HandlerOptionsHandler::RegisterMessages() { |
| 44 web_ui()->RegisterMessageCallback("initializeProtocolHandlerList", | |
| 45 base::Bind(&HandlerOptionsHandler::InitializeProtocolHandlerList, | |
| 46 base::Unretained(this))); | |
| 67 web_ui()->RegisterMessageCallback("clearDefault", | 47 web_ui()->RegisterMessageCallback("clearDefault", |
| 68 base::Bind(&HandlerOptionsHandler::ClearDefault, | 48 base::Bind(&HandlerOptionsHandler::ClearDefault, |
| 69 base::Unretained(this))); | 49 base::Unretained(this))); |
| 70 web_ui()->RegisterMessageCallback("removeHandler", | 50 web_ui()->RegisterMessageCallback("removeHandler", |
| 71 base::Bind(&HandlerOptionsHandler::RemoveHandler, | 51 base::Bind(&HandlerOptionsHandler::RemoveHandler, |
| 72 base::Unretained(this))); | 52 base::Unretained(this))); |
| 73 web_ui()->RegisterMessageCallback("setHandlersEnabled", | 53 web_ui()->RegisterMessageCallback("setHandlersEnabled", |
| 74 base::Bind(&HandlerOptionsHandler::SetHandlersEnabled, | 54 base::Bind(&HandlerOptionsHandler::SetHandlersEnabled, |
| 75 base::Unretained(this))); | 55 base::Unretained(this))); |
| 76 web_ui()->RegisterMessageCallback("setDefault", | 56 web_ui()->RegisterMessageCallback("setDefault", |
| 77 base::Bind(&HandlerOptionsHandler::SetDefault, | 57 base::Bind(&HandlerOptionsHandler::SetDefault, |
| 78 base::Unretained(this))); | 58 base::Unretained(this))); |
| 79 web_ui()->RegisterMessageCallback("removeIgnoredHandler", | 59 web_ui()->RegisterMessageCallback("removeIgnoredHandler", |
| 80 base::Bind(&HandlerOptionsHandler::RemoveIgnoredHandler, | 60 base::Bind(&HandlerOptionsHandler::RemoveIgnoredHandler, |
| 81 base::Unretained(this))); | 61 base::Unretained(this))); |
| 82 } | 62 } |
| 83 | 63 |
| 84 ProtocolHandlerRegistry* HandlerOptionsHandler::GetProtocolHandlerRegistry() { | 64 ProtocolHandlerRegistry* HandlerOptionsHandler::GetProtocolHandlerRegistry() { |
| 85 return ProtocolHandlerRegistryFactory::GetForBrowserContext( | 65 return ProtocolHandlerRegistryFactory::GetForBrowserContext( |
| 86 Profile::FromWebUI(web_ui())); | 66 Profile::FromWebUI(web_ui())); |
| 87 } | 67 } |
| 88 | 68 |
| 89 static void GetHandlersAsListValue( | 69 static void GetHandlersAsListValue( |
| 90 const ProtocolHandlerRegistry::ProtocolHandlerList& handlers, | 70 const ProtocolHandlerRegistry::ProtocolHandlerList& handlers, |
| 91 base::ListValue* handler_list) { | 71 base::ListValue* handler_list) { |
| 92 ProtocolHandlerRegistry::ProtocolHandlerList::const_iterator handler; | 72 ProtocolHandlerRegistry::ProtocolHandlerList::const_iterator handler; |
| 93 for (handler = handlers.begin(); handler != handlers.end(); ++handler) { | 73 for (handler = handlers.begin(); handler != handlers.end(); ++handler) { |
| 94 std::unique_ptr<base::ListValue> handler_value(new base::ListValue()); | 74 std::unique_ptr<base::DictionaryValue> handler_value( |
| 95 handler_value->AppendString(handler->protocol()); | 75 new base::DictionaryValue()); |
| 96 handler_value->AppendString(handler->url().spec()); | 76 handler_value->SetString("protocol", handler->protocol()); |
| 97 handler_value->AppendString(handler->url().host()); | 77 handler_value->SetString("spec", handler->url().spec()); |
| 78 handler_value->SetString("host", handler->url().host()); | |
| 98 handler_list->Append(std::move(handler_value)); | 79 handler_list->Append(std::move(handler_value)); |
| 99 } | 80 } |
| 100 } | 81 } |
| 101 | 82 |
| 102 void HandlerOptionsHandler::GetHandlersForProtocol( | 83 void HandlerOptionsHandler::GetHandlersForProtocol( |
| 103 const std::string& protocol, | 84 const std::string& protocol, |
| 104 base::DictionaryValue* handlers_value) { | 85 base::DictionaryValue* handlers_value) { |
| 105 ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry(); | 86 ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry(); |
| 106 // The items which are to be written into |handlers_value| are also described | 87 // The items which are to be written into |handlers_value| are also described |
| 107 // in chrome/browser/resources/options/handler_options.js in @typedef | 88 // in chrome/browser/resources/options/handler_options.js in @typedef |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 121 } | 102 } |
| 122 | 103 |
| 123 void HandlerOptionsHandler::GetIgnoredHandlers(base::ListValue* handlers) { | 104 void HandlerOptionsHandler::GetIgnoredHandlers(base::ListValue* handlers) { |
| 124 ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry(); | 105 ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry(); |
| 125 ProtocolHandlerRegistry::ProtocolHandlerList ignored_handlers = | 106 ProtocolHandlerRegistry::ProtocolHandlerList ignored_handlers = |
| 126 registry->GetIgnoredHandlers(); | 107 registry->GetIgnoredHandlers(); |
| 127 return GetHandlersAsListValue(ignored_handlers, handlers); | 108 return GetHandlersAsListValue(ignored_handlers, handlers); |
| 128 } | 109 } |
| 129 | 110 |
| 130 void HandlerOptionsHandler::UpdateHandlerList() { | 111 void HandlerOptionsHandler::UpdateHandlerList() { |
| 112 AllowJavascript(); | |
|
michaelpg
2016/07/08 20:56:00
there's some circular logic with having AllowJavas
Finnur
2016/07/10 00:40:17
Done.
| |
| 113 | |
| 131 ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry(); | 114 ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry(); |
| 132 std::vector<std::string> protocols; | 115 std::vector<std::string> protocols; |
| 133 registry->GetRegisteredProtocols(&protocols); | 116 registry->GetRegisteredProtocols(&protocols); |
| 134 | 117 |
| 135 base::ListValue handlers; | 118 base::ListValue handlers; |
| 136 for (std::vector<std::string>::iterator protocol = protocols.begin(); | 119 for (std::vector<std::string>::iterator protocol = protocols.begin(); |
| 137 protocol != protocols.end(); protocol++) { | 120 protocol != protocols.end(); protocol++) { |
| 138 std::unique_ptr<base::DictionaryValue> handler_value( | 121 std::unique_ptr<base::DictionaryValue> handler_value( |
| 139 new base::DictionaryValue()); | 122 new base::DictionaryValue()); |
| 140 GetHandlersForProtocol(*protocol, handler_value.get()); | 123 GetHandlersForProtocol(*protocol, handler_value.get()); |
| 141 handlers.Append(std::move(handler_value)); | 124 handlers.Append(std::move(handler_value)); |
| 142 } | 125 } |
| 143 | 126 |
| 144 std::unique_ptr<base::ListValue> ignored_handlers(new base::ListValue()); | 127 std::unique_ptr<base::ListValue> ignored_handlers(new base::ListValue()); |
| 145 GetIgnoredHandlers(ignored_handlers.get()); | 128 GetIgnoredHandlers(ignored_handlers.get()); |
| 146 web_ui()->CallJavascriptFunctionUnsafe("HandlerOptions.setHandlers", | 129 CallJavascriptFunction("cr.webUIListenerCallback", |
| 147 handlers); | 130 base::StringValue("setProtocolHandlers"), |
| 148 web_ui()->CallJavascriptFunctionUnsafe("HandlerOptions.setIgnoredHandlers", | 131 handlers); |
| 149 *ignored_handlers); | 132 CallJavascriptFunction("cr.webUIListenerCallback", |
| 133 base::StringValue("setIgnoredProtocolHandlers"), | |
| 134 *ignored_handlers); | |
| 135 } | |
| 136 | |
| 137 void HandlerOptionsHandler::InitializeProtocolHandlerList( | |
| 138 const base::ListValue* args) { | |
| 139 SendHandlersEnabledValue(); | |
| 140 UpdateHandlerList(); | |
| 141 } | |
| 142 | |
| 143 void HandlerOptionsHandler::SendHandlersEnabledValue() { | |
| 144 AllowJavascript(); | |
| 145 CallJavascriptFunction("cr.webUIListenerCallback", | |
| 146 base::StringValue("setHandlersEnabled"), | |
| 147 base::FundamentalValue( | |
| 148 GetProtocolHandlerRegistry()->enabled())); | |
|
michaelpg
2016/07/08 20:56:00
indent off
Finnur
2016/07/10 00:40:17
Wow. I'm impressed. Did you really catch this by e
michaelpg
2016/07/10 17:54:43
No, I only looked closely because it looked differ
| |
| 150 } | 149 } |
| 151 | 150 |
| 152 void HandlerOptionsHandler::RemoveHandler(const base::ListValue* args) { | 151 void HandlerOptionsHandler::RemoveHandler(const base::ListValue* args) { |
| 153 const base::ListValue* list; | 152 const base::ListValue* list; |
| 154 if (!args->GetList(0, &list)) { | 153 if (!args->GetList(0, &list)) { |
| 155 NOTREACHED(); | 154 NOTREACHED(); |
| 156 return; | 155 return; |
| 157 } | 156 } |
| 158 | 157 |
| 159 ProtocolHandler handler(ParseHandlerFromArgs(list)); | 158 ProtocolHandler handler(ParseHandlerFromArgs(list)); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 209 return ProtocolHandler::EmptyProtocolHandler(); | 208 return ProtocolHandler::EmptyProtocolHandler(); |
| 210 return ProtocolHandler::CreateProtocolHandler(base::UTF16ToUTF8(protocol), | 209 return ProtocolHandler::CreateProtocolHandler(base::UTF16ToUTF8(protocol), |
| 211 GURL(base::UTF16ToUTF8(url))); | 210 GURL(base::UTF16ToUTF8(url))); |
| 212 } | 211 } |
| 213 | 212 |
| 214 void HandlerOptionsHandler::Observe( | 213 void HandlerOptionsHandler::Observe( |
| 215 int type, | 214 int type, |
| 216 const content::NotificationSource& source, | 215 const content::NotificationSource& source, |
| 217 const content::NotificationDetails& details) { | 216 const content::NotificationDetails& details) { |
| 218 DCHECK_EQ(chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED, type); | 217 DCHECK_EQ(chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED, type); |
| 218 SendHandlersEnabledValue(); | |
| 219 UpdateHandlerList(); | 219 UpdateHandlerList(); |
| 220 } | 220 } |
| 221 | 221 |
| 222 } // namespace options | 222 } // namespace settings |
| OLD | NEW |