register_protocol_handler_infobar_delegate.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
1// Copyright (c) 2012 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/custom_handlers/register_protocol_handler_infobar_delegate.h" 6 7#include "base/utf_string_conversions.h" 8#include "chrome/browser/api/infobars/infobar_service.h" 9#include "chrome/browser/custom_handlers/protocol_handler_registry.h" 10#include "chrome/common/url_constants.h" 11#include "content/public/browser/user_metrics.h" 12#include "content/public/browser/web_contents.h" 13#include "grit/generated_resources.h" 14#include "ui/base/l10n/l10n_util.h" 15 16using content::OpenURLParams; 17using content::Referrer; 18using content::UserMetricsAction; 19 20RegisterProtocolHandlerInfoBarDelegate::RegisterProtocolHandlerInfoBarDelegate( 21 InfoBarService* infobar_service, 22 ProtocolHandlerRegistry* registry, 23 const ProtocolHandler& handler) 24 : ConfirmInfoBarDelegate(infobar_service), 25 registry_(registry), 26 handler_(handler) { 27} 28 29InfoBarDelegate::InfoBarAutomationType 30 RegisterProtocolHandlerInfoBarDelegate::GetInfoBarAutomationType() const { 31 return RPH_INFOBAR; 32} 33 34InfoBarDelegate::Type 35RegisterProtocolHandlerInfoBarDelegate::GetInfoBarType() const { 36 return PAGE_ACTION_TYPE; 37} 38 39string16 RegisterProtocolHandlerInfoBarDelegate::GetMessageText() const { 40 ProtocolHandler old_handler = registry_->GetHandlerFor(handler_.protocol()); 41 return !old_handler.IsEmpty() ? 42 l10n_util::GetStringFUTF16(IDS_REGISTER_PROTOCOL_HANDLER_CONFIRM_REPLACE, 43 handler_.title(), UTF8ToUTF16(handler_.url().host()), 44 GetProtocolName(handler_), old_handler.title()) : 45 l10n_util::GetStringFUTF16(IDS_REGISTER_PROTOCOL_HANDLER_CONFIRM, 46 handler_.title(), UTF8ToUTF16(handler_.url().host()), 47 GetProtocolName(handler_)); 48} 49 50string16 RegisterProtocolHandlerInfoBarDelegate::GetProtocolName( 51 const ProtocolHandler& handler) const { 52 if (handler.protocol() == "mailto") 53 return l10n_util::GetStringUTF16(IDS_REGISTER_PROTOCOL_HANDLER_MAILTO_NAME); 54 if (handler.protocol() == "webcal") 55 return l10n_util::GetStringUTF16(IDS_REGISTER_PROTOCOL_HANDLER_WEBCAL_NAME); 56 return UTF8ToUTF16(handler.protocol()); 57} 58 59string16 RegisterProtocolHandlerInfoBarDelegate::GetButtonLabel( 60 InfoBarButton button) const { 61 return (button == BUTTON_OK) ? 62 l10n_util::GetStringFUTF16(IDS_REGISTER_PROTOCOL_HANDLER_ACCEPT, 63 handler_.title()) : 64 l10n_util::GetStringUTF16(IDS_REGISTER_PROTOCOL_HANDLER_DENY); 65} 66 67bool RegisterProtocolHandlerInfoBarDelegate::NeedElevation( 68 InfoBarButton button) const { 69 return button == BUTTON_OK; 70} 71 72bool RegisterProtocolHandlerInfoBarDelegate::Accept() { 73 content::RecordAction( 74 UserMetricsAction("RegisterProtocolHandler.Infobar_Accept")); 75 registry_->OnAcceptRegisterProtocolHandler(handler_); 76 return true; 77} 78 79bool RegisterProtocolHandlerInfoBarDelegate::Cancel() { 80 content::RecordAction( 81 UserMetricsAction("RegisterProtocolHandler.InfoBar_Deny")); 82 registry_->OnIgnoreRegisterProtocolHandler(handler_); 83 return true; 84} 85 86string16 RegisterProtocolHandlerInfoBarDelegate::GetLinkText() const { 87 return l10n_util::GetStringUTF16(IDS_LEARN_MORE); 88} 89 90bool RegisterProtocolHandlerInfoBarDelegate::LinkClicked( 91 WindowOpenDisposition disposition) { 92 content::RecordAction( 93 UserMetricsAction("RegisterProtocolHandler.InfoBar_LearnMore")); 94 OpenURLParams params( 95 GURL(chrome::kLearnMoreRegisterProtocolHandlerURL), 96 Referrer(), 97 (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition, 98 content::PAGE_TRANSITION_LINK, 99 false); 100 owner()->GetWebContents()->OpenURL(params); 101 return false; 102} 103 104bool RegisterProtocolHandlerInfoBarDelegate::IsReplacedBy( 105 RegisterProtocolHandlerInfoBarDelegate* delegate) { 106 return handler_.IsEquivalent(delegate->handler_); 107} 108 109RegisterProtocolHandlerInfoBarDelegate* 110 RegisterProtocolHandlerInfoBarDelegate:: 111 AsRegisterProtocolHandlerInfoBarDelegate() { 112 return this; 113} 114