register_protocol_handler_infobar_delegate.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
19f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson// Copyright (c) 2012 The Chromium Authors. All rights reserved.
29f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson// Use of this source code is governed by a BSD-style license that can be
39f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson// found in the LICENSE file.
49f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
59f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson#include "chrome/browser/custom_handlers/register_protocol_handler_infobar_delegate.h"
69f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
79f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson#include "base/strings/utf_string_conversions.h"
89f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
99f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson#include "chrome/browser/infobars/infobar_service.h"
109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson#include "chrome/common/url_constants.h"
119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson#include "chrome/grit/generated_resources.h"
129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson#include "components/infobars/core/infobar.h"
139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson#include "content/public/browser/user_metrics.h"
149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson#include "content/public/browser/web_contents.h"
159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson#include "grit/components_strings.h"
169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson#include "ui/base/l10n/l10n_util.h"
179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson// static
199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonvoid RegisterProtocolHandlerInfoBarDelegate::Create(
209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    InfoBarService* infobar_service,
219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    ProtocolHandlerRegistry* registry,
229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    const ProtocolHandler& handler) {
239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  content::RecordAction(
249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      base::UserMetricsAction("RegisterProtocolHandler.InfoBar_Shown"));
259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  scoped_ptr<infobars::InfoBar> infobar(
279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      ConfirmInfoBarDelegate::CreateInfoBar(scoped_ptr<ConfirmInfoBarDelegate>(
289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          new RegisterProtocolHandlerInfoBarDelegate(registry, handler))));
299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  for (size_t i = 0; i < infobar_service->infobar_count(); ++i) {
319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    infobars::InfoBar* existing_infobar = infobar_service->infobar_at(i);
329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    RegisterProtocolHandlerInfoBarDelegate* existing_delegate =
339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        existing_infobar->delegate()->
349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            AsRegisterProtocolHandlerInfoBarDelegate();
359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    if ((existing_delegate != NULL) &&
369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        existing_delegate->handler_.IsEquivalent(handler)) {
379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      infobar_service->ReplaceInfoBar(existing_infobar, infobar.Pass());
389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      return;
399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  infobar_service->AddInfoBar(infobar.Pass());
439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson}
449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse WilsonRegisterProtocolHandlerInfoBarDelegate::RegisterProtocolHandlerInfoBarDelegate(
469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    ProtocolHandlerRegistry* registry,
479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    const ProtocolHandler& handler)
489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    : ConfirmInfoBarDelegate(),
499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      registry_(registry),
509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      handler_(handler) {
519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson}
529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse WilsonRegisterProtocolHandlerInfoBarDelegate::
549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    ~RegisterProtocolHandlerInfoBarDelegate() {
559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson}
569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsoninfobars::InfoBarDelegate::InfoBarAutomationType
589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse WilsonRegisterProtocolHandlerInfoBarDelegate::GetInfoBarAutomationType() const {
599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  return RPH_INFOBAR;
609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson}
619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsoninfobars::InfoBarDelegate::Type
639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse WilsonRegisterProtocolHandlerInfoBarDelegate::GetInfoBarType() const {
649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  return PAGE_ACTION_TYPE;
659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson}
669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse WilsonRegisterProtocolHandlerInfoBarDelegate*
689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    RegisterProtocolHandlerInfoBarDelegate::
699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        AsRegisterProtocolHandlerInfoBarDelegate() {
709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  return this;
719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson}
729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonbase::string16 RegisterProtocolHandlerInfoBarDelegate::GetMessageText() const {
749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  ProtocolHandler old_handler = registry_->GetHandlerFor(handler_.protocol());
759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  return old_handler.IsEmpty() ?
769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      l10n_util::GetStringFUTF16(
779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          IDS_REGISTER_PROTOCOL_HANDLER_CONFIRM,
789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          base::UTF8ToUTF16(handler_.url().host()),
799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          GetProtocolName(handler_)) :
809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      l10n_util::GetStringFUTF16(
819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          IDS_REGISTER_PROTOCOL_HANDLER_CONFIRM_REPLACE,
829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          base::UTF8ToUTF16(handler_.url().host()),
839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          GetProtocolName(handler_),
849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          base::UTF8ToUTF16(old_handler.url().host()));
859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson}
869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonbase::string16 RegisterProtocolHandlerInfoBarDelegate::GetButtonLabel(
889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    InfoBarButton button) const {
899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  return (button == BUTTON_OK) ?
909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      l10n_util::GetStringUTF16(IDS_REGISTER_PROTOCOL_HANDLER_ACCEPT) :
919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      l10n_util::GetStringUTF16(IDS_REGISTER_PROTOCOL_HANDLER_DENY);
929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson}
939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonbool RegisterProtocolHandlerInfoBarDelegate::OKButtonTriggersUACPrompt() const {
959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  return true;
969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson}
979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonbool RegisterProtocolHandlerInfoBarDelegate::Accept() {
999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  content::RecordAction(
1009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      base::UserMetricsAction("RegisterProtocolHandler.Infobar_Accept"));
1019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  registry_->OnAcceptRegisterProtocolHandler(handler_);
1029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  return true;
1039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson}
1049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonbool RegisterProtocolHandlerInfoBarDelegate::Cancel() {
1069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  content::RecordAction(
1079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      base::UserMetricsAction("RegisterProtocolHandler.InfoBar_Deny"));
1089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  registry_->OnIgnoreRegisterProtocolHandler(handler_);
1099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  return true;
1109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson}
1119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonbase::string16 RegisterProtocolHandlerInfoBarDelegate::GetLinkText() const {
1139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
1149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson}
1159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonbool RegisterProtocolHandlerInfoBarDelegate::LinkClicked(
1179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    WindowOpenDisposition disposition) {
1189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  content::RecordAction(
1199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      base::UserMetricsAction("RegisterProtocolHandler.InfoBar_LearnMore"));
1209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  InfoBarService::WebContentsFromInfoBar(infobar())->OpenURL(
1219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      content::OpenURLParams(
1229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          GURL(chrome::kLearnMoreRegisterProtocolHandlerURL),
1239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          content::Referrer(),
1249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition,
1259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          ui::PAGE_TRANSITION_LINK, false));
1269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  return false;
1279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson}
1289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonbase::string16 RegisterProtocolHandlerInfoBarDelegate::GetProtocolName(
1309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    const ProtocolHandler& handler) const {
1319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  if (handler.protocol() == "mailto")
1329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    return l10n_util::GetStringUTF16(IDS_REGISTER_PROTOCOL_HANDLER_MAILTO_NAME);
1339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  if (handler.protocol() == "webcal")
1349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    return l10n_util::GetStringUTF16(IDS_REGISTER_PROTOCOL_HANDLER_WEBCAL_NAME);
1359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  return base::UTF8ToUTF16(handler.protocol());
1369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson}
1379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson