15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/webui/options/handler_options_handler.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind_helpers.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h"
147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/chrome_notification_types.h"
15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/grit/generated_resources.h"
186d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "components/google/core/browser/google_util.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_ui.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace options {
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
23effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochnamespace {
24effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
25effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochconst char kHandlersLearnMoreUrl[] =
26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    "https://support.google.com/chrome/answer/1382847";
27effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
28effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}  // namespace
29effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)HandlerOptionsHandler::HandlerOptionsHandler() {
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)HandlerOptionsHandler::~HandlerOptionsHandler() {
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HandlerOptionsHandler::GetLocalizedValues(
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::DictionaryValue* localized_strings) {
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(localized_strings);
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static OptionsStringResource resources[] = {
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      { "handlers_tab_label", IDS_HANDLERS_TAB_LABEL },
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      { "handlers_allow", IDS_HANDLERS_ALLOW_RADIO },
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      { "handlers_block", IDS_HANDLERS_DONOTALLOW_RADIO },
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      { "handlers_type_column_header", IDS_HANDLERS_TYPE_COLUMN_HEADER },
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      { "handlers_site_column_header", IDS_HANDLERS_SITE_COLUMN_HEADER },
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      { "handlers_remove_link", IDS_HANDLERS_REMOVE_HANDLER_LINK },
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      { "handlers_none_handler", IDS_HANDLERS_NONE_HANDLER },
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      { "handlers_active_heading", IDS_HANDLERS_ACTIVE_HEADING },
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      { "handlers_ignored_heading", IDS_HANDLERS_IGNORED_HEADING },
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RegisterTitle(localized_strings, "handlersPage",
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                IDS_HANDLER_OPTIONS_WINDOW_TITLE);
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RegisterStrings(localized_strings, resources, arraysize(resources));
54effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
55f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  localized_strings->SetString("handlers_learn_more_url",
56f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                               kHandlersLearnMoreUrl);
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HandlerOptionsHandler::InitializeHandler() {
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  notification_registrar_.Add(
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      this, chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED,
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::Source<Profile>(Profile::FromWebUI(web_ui())));
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HandlerOptionsHandler::InitializePage() {
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  UpdateHandlerList();
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HandlerOptionsHandler::RegisterMessages() {
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  web_ui()->RegisterMessageCallback("clearDefault",
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(&HandlerOptionsHandler::ClearDefault,
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 base::Unretained(this)));
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  web_ui()->RegisterMessageCallback("removeHandler",
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(&HandlerOptionsHandler::RemoveHandler,
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 base::Unretained(this)));
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  web_ui()->RegisterMessageCallback("setHandlersEnabled",
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(&HandlerOptionsHandler::SetHandlersEnabled,
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 base::Unretained(this)));
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  web_ui()->RegisterMessageCallback("setDefault",
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(&HandlerOptionsHandler::SetDefault,
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 base::Unretained(this)));
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  web_ui()->RegisterMessageCallback("removeIgnoredHandler",
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(&HandlerOptionsHandler::RemoveIgnoredHandler,
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 base::Unretained(this)));
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProtocolHandlerRegistry* HandlerOptionsHandler::GetProtocolHandlerRegistry() {
885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return ProtocolHandlerRegistryFactory::GetForBrowserContext(
89c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      Profile::FromWebUI(web_ui()));
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void GetHandlersAsListValue(
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const ProtocolHandlerRegistry::ProtocolHandlerList& handlers,
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::ListValue* handler_list) {
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProtocolHandlerRegistry::ProtocolHandlerList::const_iterator handler;
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (handler = handlers.begin(); handler != handlers.end(); ++handler) {
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::ListValue* handlerValue = new base::ListValue();
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    handlerValue->Append(new base::StringValue(handler->protocol()));
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    handlerValue->Append(new base::StringValue(handler->url().spec()));
100cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    handlerValue->Append(new base::StringValue(handler->url().host()));
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    handler_list->Append(handlerValue);
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HandlerOptionsHandler::GetHandlersForProtocol(
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& protocol,
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::DictionaryValue* handlers_value) {
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry();
1091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // The items which are to be written into |handlers_value| are also described
1101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // in chrome/browser/resources/options/handler_options.js in @typedef
1111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // for Handlers. Please update them whenever you add or remove any keys here.
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  handlers_value->SetString("protocol", protocol);
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  handlers_value->SetInteger("default_handler",
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      registry->GetHandlerIndex(protocol));
1151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  handlers_value->SetBoolean(
1161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      "is_default_handler_set_by_user",
1171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      registry->IsRegisteredByUser(registry->GetHandlerFor(protocol)));
1181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  handlers_value->SetBoolean("has_policy_recommendations",
1191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                             registry->HasPolicyRegisteredHandler(protocol));
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::ListValue* handlers_list = new base::ListValue();
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GetHandlersAsListValue(registry->GetHandlersFor(protocol), handlers_list);
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  handlers_value->Set("handlers", handlers_list);
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void HandlerOptionsHandler::GetIgnoredHandlers(base::ListValue* handlers) {
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry();
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProtocolHandlerRegistry::ProtocolHandlerList ignored_handlers =
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      registry->GetIgnoredHandlers();
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return GetHandlersAsListValue(ignored_handlers, handlers);
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HandlerOptionsHandler::UpdateHandlerList() {
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProtocolHandlerRegistry* registry = GetProtocolHandlerRegistry();
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<std::string> protocols;
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  registry->GetRegisteredProtocols(&protocols);
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::ListValue handlers;
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (std::vector<std::string>::iterator protocol = protocols.begin();
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       protocol != protocols.end(); protocol++) {
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::DictionaryValue* handler_value = new base::DictionaryValue();
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GetHandlersForProtocol(*protocol, handler_value);
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    handlers.Append(handler_value);
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<base::ListValue> ignored_handlers(new base::ListValue());
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GetIgnoredHandlers(ignored_handlers.get());
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  web_ui()->CallJavascriptFunction("HandlerOptions.setHandlers", handlers);
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  web_ui()->CallJavascriptFunction("HandlerOptions.setIgnoredHandlers",
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   *ignored_handlers);
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void HandlerOptionsHandler::RemoveHandler(const base::ListValue* args) {
1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const base::ListValue* list;
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!args->GetList(0, &list)) {
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NOTREACHED();
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProtocolHandler handler(ParseHandlerFromArgs(list));
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GetProtocolHandlerRegistry()->RemoveHandler(handler);
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // No need to call UpdateHandlerList() - we should receive a notification
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // that the ProtocolHandlerRegistry has changed and we will update the view
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // then.
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void HandlerOptionsHandler::RemoveIgnoredHandler(const base::ListValue* args) {
1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const base::ListValue* list;
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!args->GetList(0, &list)) {
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NOTREACHED();
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProtocolHandler handler(ParseHandlerFromArgs(list));
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GetProtocolHandlerRegistry()->RemoveIgnoredHandler(handler);
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void HandlerOptionsHandler::SetHandlersEnabled(const base::ListValue* args) {
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool enabled = true;
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(args->GetBoolean(0, &enabled));
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (enabled)
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GetProtocolHandlerRegistry()->Enable();
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  else
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GetProtocolHandlerRegistry()->Disable();
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void HandlerOptionsHandler::ClearDefault(const base::ListValue* args) {
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const base::Value* value;
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(args->Get(0, &value));
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string protocol_to_clear;
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(value->GetAsString(&protocol_to_clear));
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GetProtocolHandlerRegistry()->ClearDefault(protocol_to_clear);
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void HandlerOptionsHandler::SetDefault(const base::ListValue* args) {
1975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const base::ListValue* list;
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(args->GetList(0, &list));
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const ProtocolHandler& handler(ParseHandlerFromArgs(list));
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK(!handler.IsEmpty());
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GetProtocolHandlerRegistry()->OnAcceptRegisterProtocolHandler(handler);
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ProtocolHandler HandlerOptionsHandler::ParseHandlerFromArgs(
2055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::ListValue* args) const {
206a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 protocol;
207a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 url;
208cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  bool ok = args->GetString(0, &protocol) && args->GetString(1, &url);
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!ok)
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return ProtocolHandler::EmptyProtocolHandler();
2115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return ProtocolHandler::CreateProtocolHandler(base::UTF16ToUTF8(protocol),
212cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                                GURL(base::UTF16ToUTF8(url)));
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void HandlerOptionsHandler::Observe(
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int type,
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const content::NotificationSource& source,
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const content::NotificationDetails& details) {
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (type == chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED)
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    UpdateHandlerList();
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  else
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NOTREACHED();
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace options
226