| 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/protocol_handlers_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 ProtocolHandlersHandler::ProtocolHandlersHandler() { |
| 28 } | 28 } |
| 29 | 29 |
| 30 HandlerOptionsHandler::~HandlerOptionsHandler() { | 30 ProtocolHandlersHandler::~ProtocolHandlersHandler() { |
| 31 } | 31 } |
| 32 | 32 |
| 33 void HandlerOptionsHandler::GetLocalizedValues( | 33 void ProtocolHandlersHandler::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 ProtocolHandlersHandler::OnJavascriptDisallowed() { |
| 63 UpdateHandlerList(); | 40 notification_registrar_.RemoveAll(); |
| 64 } | 41 } |
| 65 | 42 |
| 66 void HandlerOptionsHandler::RegisterMessages() { | 43 void ProtocolHandlersHandler::RegisterMessages() { |
| 44 web_ui()->RegisterMessageCallback("initializeProtocolHandlerList", |
| 45 base::Bind(&ProtocolHandlersHandler::InitializeProtocolHandlerList, |
| 46 base::Unretained(this))); |
| 67 web_ui()->RegisterMessageCallback("clearDefault", | 47 web_ui()->RegisterMessageCallback("clearDefault", |
| 68 base::Bind(&HandlerOptionsHandler::ClearDefault, | 48 base::Bind(&ProtocolHandlersHandler::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(&ProtocolHandlersHandler::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(&ProtocolHandlersHandler::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(&ProtocolHandlersHandler::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(&ProtocolHandlersHandler::RemoveIgnoredHandler, |
| 81 base::Unretained(this))); | 61 base::Unretained(this))); |
| 82 } | 62 } |
| 83 | 63 |
| 84 ProtocolHandlerRegistry* HandlerOptionsHandler::GetProtocolHandlerRegistry() { | 64 ProtocolHandlerRegistry* ProtocolHandlersHandler::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 ProtocolHandlersHandler::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 |
| 108 // for Handlers. Please update them whenever you add or remove any keys here. | 89 // for Handlers. Please update them whenever you add or remove any keys here. |
| 109 handlers_value->SetString("protocol", protocol); | 90 handlers_value->SetString("protocol", protocol); |
| 110 handlers_value->SetInteger("default_handler", | 91 handlers_value->SetInteger("default_handler", |
| 111 registry->GetHandlerIndex(protocol)); | 92 registry->GetHandlerIndex(protocol)); |
| 112 handlers_value->SetBoolean( | 93 handlers_value->SetBoolean( |
| 113 "is_default_handler_set_by_user", | 94 "is_default_handler_set_by_user", |
| 114 registry->IsRegisteredByUser(registry->GetHandlerFor(protocol))); | 95 registry->IsRegisteredByUser(registry->GetHandlerFor(protocol))); |
| 115 handlers_value->SetBoolean("has_policy_recommendations", | 96 handlers_value->SetBoolean("has_policy_recommendations", |
| 116 registry->HasPolicyRegisteredHandler(protocol)); | 97 registry->HasPolicyRegisteredHandler(protocol)); |
| 117 | 98 |
| 118 base::ListValue* handlers_list = new base::ListValue(); | 99 base::ListValue* handlers_list = new base::ListValue(); |
| 119 GetHandlersAsListValue(registry->GetHandlersFor(protocol), handlers_list); | 100 GetHandlersAsListValue(registry->GetHandlersFor(protocol), handlers_list); |
| 120 handlers_value->Set("handlers", handlers_list); | 101 handlers_value->Set("handlers", handlers_list); |
| 121 } | 102 } |
| 122 | 103 |
| 123 void HandlerOptionsHandler::GetIgnoredHandlers(base::ListValue* handlers) { | 104 void ProtocolHandlersHandler::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 ProtocolHandlersHandler::UpdateHandlerList() { |
| 131 ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry(); | 112 ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry(); |
| 132 std::vector<std::string> protocols; | 113 std::vector<std::string> protocols; |
| 133 registry->GetRegisteredProtocols(&protocols); | 114 registry->GetRegisteredProtocols(&protocols); |
| 134 | 115 |
| 135 base::ListValue handlers; | 116 base::ListValue handlers; |
| 136 for (std::vector<std::string>::iterator protocol = protocols.begin(); | 117 for (std::vector<std::string>::iterator protocol = protocols.begin(); |
| 137 protocol != protocols.end(); protocol++) { | 118 protocol != protocols.end(); protocol++) { |
| 138 std::unique_ptr<base::DictionaryValue> handler_value( | 119 std::unique_ptr<base::DictionaryValue> handler_value( |
| 139 new base::DictionaryValue()); | 120 new base::DictionaryValue()); |
| 140 GetHandlersForProtocol(*protocol, handler_value.get()); | 121 GetHandlersForProtocol(*protocol, handler_value.get()); |
| 141 handlers.Append(std::move(handler_value)); | 122 handlers.Append(std::move(handler_value)); |
| 142 } | 123 } |
| 143 | 124 |
| 144 std::unique_ptr<base::ListValue> ignored_handlers(new base::ListValue()); | 125 std::unique_ptr<base::ListValue> ignored_handlers(new base::ListValue()); |
| 145 GetIgnoredHandlers(ignored_handlers.get()); | 126 GetIgnoredHandlers(ignored_handlers.get()); |
| 146 web_ui()->CallJavascriptFunctionUnsafe("HandlerOptions.setHandlers", | 127 CallJavascriptFunction("cr.webUIListenerCallback", |
| 147 handlers); | 128 base::StringValue("setProtocolHandlers"), |
| 148 web_ui()->CallJavascriptFunctionUnsafe("HandlerOptions.setIgnoredHandlers", | 129 handlers); |
| 149 *ignored_handlers); | 130 CallJavascriptFunction("cr.webUIListenerCallback", |
| 131 base::StringValue("setIgnoredProtocolHandlers"), |
| 132 *ignored_handlers); |
| 150 } | 133 } |
| 151 | 134 |
| 152 void HandlerOptionsHandler::RemoveHandler(const base::ListValue* args) { | 135 void ProtocolHandlersHandler::InitializeProtocolHandlerList( |
| 136 const base::ListValue* args) { |
| 137 AllowJavascript(); |
| 138 SendHandlersEnabledValue(); |
| 139 UpdateHandlerList(); |
| 140 } |
| 141 |
| 142 void ProtocolHandlersHandler::SendHandlersEnabledValue() { |
| 143 CallJavascriptFunction("cr.webUIListenerCallback", |
| 144 base::StringValue("setHandlersEnabled"), |
| 145 base::FundamentalValue( |
| 146 GetProtocolHandlerRegistry()->enabled())); |
| 147 } |
| 148 |
| 149 void ProtocolHandlersHandler::RemoveHandler(const base::ListValue* args) { |
| 153 const base::ListValue* list; | 150 const base::ListValue* list; |
| 154 if (!args->GetList(0, &list)) { | 151 if (!args->GetList(0, &list)) { |
| 155 NOTREACHED(); | 152 NOTREACHED(); |
| 156 return; | 153 return; |
| 157 } | 154 } |
| 158 | 155 |
| 159 ProtocolHandler handler(ParseHandlerFromArgs(list)); | 156 ProtocolHandler handler(ParseHandlerFromArgs(list)); |
| 160 GetProtocolHandlerRegistry()->RemoveHandler(handler); | 157 GetProtocolHandlerRegistry()->RemoveHandler(handler); |
| 161 | 158 |
| 162 // No need to call UpdateHandlerList() - we should receive a notification | 159 // No need to call UpdateHandlerList() - we should receive a notification |
| 163 // that the ProtocolHandlerRegistry has changed and we will update the view | 160 // that the ProtocolHandlerRegistry has changed and we will update the view |
| 164 // then. | 161 // then. |
| 165 } | 162 } |
| 166 | 163 |
| 167 void HandlerOptionsHandler::RemoveIgnoredHandler(const base::ListValue* args) { | 164 void ProtocolHandlersHandler::RemoveIgnoredHandler( |
| 165 const base::ListValue* args) { |
| 168 const base::ListValue* list; | 166 const base::ListValue* list; |
| 169 if (!args->GetList(0, &list)) { | 167 if (!args->GetList(0, &list)) { |
| 170 NOTREACHED(); | 168 NOTREACHED(); |
| 171 return; | 169 return; |
| 172 } | 170 } |
| 173 | 171 |
| 174 ProtocolHandler handler(ParseHandlerFromArgs(list)); | 172 ProtocolHandler handler(ParseHandlerFromArgs(list)); |
| 175 GetProtocolHandlerRegistry()->RemoveIgnoredHandler(handler); | 173 GetProtocolHandlerRegistry()->RemoveIgnoredHandler(handler); |
| 176 } | 174 } |
| 177 | 175 |
| 178 void HandlerOptionsHandler::SetHandlersEnabled(const base::ListValue* args) { | 176 void ProtocolHandlersHandler::SetHandlersEnabled(const base::ListValue* args) { |
| 179 bool enabled = true; | 177 bool enabled = true; |
| 180 CHECK(args->GetBoolean(0, &enabled)); | 178 CHECK(args->GetBoolean(0, &enabled)); |
| 181 if (enabled) | 179 if (enabled) |
| 182 GetProtocolHandlerRegistry()->Enable(); | 180 GetProtocolHandlerRegistry()->Enable(); |
| 183 else | 181 else |
| 184 GetProtocolHandlerRegistry()->Disable(); | 182 GetProtocolHandlerRegistry()->Disable(); |
| 185 } | 183 } |
| 186 | 184 |
| 187 void HandlerOptionsHandler::ClearDefault(const base::ListValue* args) { | 185 void ProtocolHandlersHandler::ClearDefault(const base::ListValue* args) { |
| 188 const base::Value* value; | 186 const base::Value* value; |
| 189 CHECK(args->Get(0, &value)); | 187 CHECK(args->Get(0, &value)); |
| 190 std::string protocol_to_clear; | 188 std::string protocol_to_clear; |
| 191 CHECK(value->GetAsString(&protocol_to_clear)); | 189 CHECK(value->GetAsString(&protocol_to_clear)); |
| 192 GetProtocolHandlerRegistry()->ClearDefault(protocol_to_clear); | 190 GetProtocolHandlerRegistry()->ClearDefault(protocol_to_clear); |
| 193 } | 191 } |
| 194 | 192 |
| 195 void HandlerOptionsHandler::SetDefault(const base::ListValue* args) { | 193 void ProtocolHandlersHandler::SetDefault(const base::ListValue* args) { |
| 196 const base::ListValue* list; | 194 const base::ListValue* list; |
| 197 CHECK(args->GetList(0, &list)); | 195 CHECK(args->GetList(0, &list)); |
| 198 const ProtocolHandler& handler(ParseHandlerFromArgs(list)); | 196 const ProtocolHandler& handler(ParseHandlerFromArgs(list)); |
| 199 CHECK(!handler.IsEmpty()); | 197 CHECK(!handler.IsEmpty()); |
| 200 GetProtocolHandlerRegistry()->OnAcceptRegisterProtocolHandler(handler); | 198 GetProtocolHandlerRegistry()->OnAcceptRegisterProtocolHandler(handler); |
| 201 } | 199 } |
| 202 | 200 |
| 203 ProtocolHandler HandlerOptionsHandler::ParseHandlerFromArgs( | 201 ProtocolHandler ProtocolHandlersHandler::ParseHandlerFromArgs( |
| 204 const base::ListValue* args) const { | 202 const base::ListValue* args) const { |
| 205 base::string16 protocol; | 203 base::string16 protocol; |
| 206 base::string16 url; | 204 base::string16 url; |
| 207 bool ok = args->GetString(0, &protocol) && args->GetString(1, &url); | 205 bool ok = args->GetString(0, &protocol) && args->GetString(1, &url); |
| 208 if (!ok) | 206 if (!ok) |
| 209 return ProtocolHandler::EmptyProtocolHandler(); | 207 return ProtocolHandler::EmptyProtocolHandler(); |
| 210 return ProtocolHandler::CreateProtocolHandler(base::UTF16ToUTF8(protocol), | 208 return ProtocolHandler::CreateProtocolHandler(base::UTF16ToUTF8(protocol), |
| 211 GURL(base::UTF16ToUTF8(url))); | 209 GURL(base::UTF16ToUTF8(url))); |
| 212 } | 210 } |
| 213 | 211 |
| 214 void HandlerOptionsHandler::Observe( | 212 void ProtocolHandlersHandler::Observe( |
| 215 int type, | 213 int type, |
| 216 const content::NotificationSource& source, | 214 const content::NotificationSource& source, |
| 217 const content::NotificationDetails& details) { | 215 const content::NotificationDetails& details) { |
| 218 DCHECK_EQ(chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED, type); | 216 DCHECK_EQ(chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED, type); |
| 217 SendHandlersEnabledValue(); |
| 219 UpdateHandlerList(); | 218 UpdateHandlerList(); |
| 220 } | 219 } |
| 221 | 220 |
| 222 } // namespace options | 221 } // namespace settings |
| OLD | NEW |