register_protocol_handler_infobar_delegate.cc revision a02191e04bc25c4935f804f2c080ae28663d096d
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/strings/utf_string_conversions.h" 8#include "chrome/browser/custom_handlers/protocol_handler_registry.h" 9#include "chrome/browser/infobars/infobar.h" 10#include "chrome/browser/infobars/infobar_service.h" 11#include "chrome/common/url_constants.h" 12#include "content/public/browser/user_metrics.h" 13#include "content/public/browser/web_contents.h" 14#include "grit/generated_resources.h" 15#include "ui/base/l10n/l10n_util.h" 16 17// static 18void RegisterProtocolHandlerInfoBarDelegate::Create( 19 InfoBarService* infobar_service, 20 ProtocolHandlerRegistry* registry, 21 const ProtocolHandler& handler) { 22 content::RecordAction( 23 base::UserMetricsAction("RegisterProtocolHandler.InfoBar_Shown")); 24 25 scoped_ptr<InfoBar> infobar(ConfirmInfoBarDelegate::CreateInfoBar( 26 scoped_ptr<ConfirmInfoBarDelegate>( 27 new RegisterProtocolHandlerInfoBarDelegate(registry, handler)))); 28 29 for (size_t i = 0; i < infobar_service->infobar_count(); ++i) { 30 InfoBar* existing_infobar = infobar_service->infobar_at(i); 31 RegisterProtocolHandlerInfoBarDelegate* existing_delegate = 32 existing_infobar->delegate()-> 33 AsRegisterProtocolHandlerInfoBarDelegate(); 34 if ((existing_delegate != NULL) && 35 existing_delegate->handler_.IsEquivalent(handler)) { 36 infobar_service->ReplaceInfoBar(existing_infobar, infobar.Pass()); 37 return; 38 } 39 } 40 41 infobar_service->AddInfoBar(infobar.Pass()); 42} 43 44RegisterProtocolHandlerInfoBarDelegate::RegisterProtocolHandlerInfoBarDelegate( 45 ProtocolHandlerRegistry* registry, 46 const ProtocolHandler& handler) 47 : ConfirmInfoBarDelegate(), 48 registry_(registry), 49 handler_(handler) { 50} 51 52RegisterProtocolHandlerInfoBarDelegate:: 53 ~RegisterProtocolHandlerInfoBarDelegate() { 54} 55 56InfoBarDelegate::InfoBarAutomationType 57 RegisterProtocolHandlerInfoBarDelegate::GetInfoBarAutomationType() const { 58 return RPH_INFOBAR; 59} 60 61InfoBarDelegate::Type 62 RegisterProtocolHandlerInfoBarDelegate::GetInfoBarType() const { 63 return PAGE_ACTION_TYPE; 64} 65 66RegisterProtocolHandlerInfoBarDelegate* 67 RegisterProtocolHandlerInfoBarDelegate:: 68 AsRegisterProtocolHandlerInfoBarDelegate() { 69 return this; 70} 71 72base::string16 RegisterProtocolHandlerInfoBarDelegate::GetMessageText() const { 73 ProtocolHandler old_handler = registry_->GetHandlerFor(handler_.protocol()); 74 return old_handler.IsEmpty() ? 75 l10n_util::GetStringFUTF16(IDS_REGISTER_PROTOCOL_HANDLER_CONFIRM, 76 handler_.title(), base::UTF8ToUTF16(handler_.url().host()), 77 GetProtocolName(handler_)) : 78 l10n_util::GetStringFUTF16(IDS_REGISTER_PROTOCOL_HANDLER_CONFIRM_REPLACE, 79 handler_.title(), base::UTF8ToUTF16(handler_.url().host()), 80 GetProtocolName(handler_), old_handler.title()); 81} 82 83base::string16 RegisterProtocolHandlerInfoBarDelegate::GetButtonLabel( 84 InfoBarButton button) const { 85 return (button == BUTTON_OK) ? 86 l10n_util::GetStringFUTF16(IDS_REGISTER_PROTOCOL_HANDLER_ACCEPT, 87 handler_.title()) : 88 l10n_util::GetStringUTF16(IDS_REGISTER_PROTOCOL_HANDLER_DENY); 89} 90 91bool RegisterProtocolHandlerInfoBarDelegate::NeedElevation( 92 InfoBarButton button) const { 93 return button == BUTTON_OK; 94} 95 96bool RegisterProtocolHandlerInfoBarDelegate::Accept() { 97 content::RecordAction( 98 base::UserMetricsAction("RegisterProtocolHandler.Infobar_Accept")); 99 registry_->OnAcceptRegisterProtocolHandler(handler_); 100 return true; 101} 102 103bool RegisterProtocolHandlerInfoBarDelegate::Cancel() { 104 content::RecordAction( 105 base::UserMetricsAction("RegisterProtocolHandler.InfoBar_Deny")); 106 registry_->OnIgnoreRegisterProtocolHandler(handler_); 107 return true; 108} 109 110base::string16 RegisterProtocolHandlerInfoBarDelegate::GetLinkText() const { 111 return l10n_util::GetStringUTF16(IDS_LEARN_MORE); 112} 113 114bool RegisterProtocolHandlerInfoBarDelegate::LinkClicked( 115 WindowOpenDisposition disposition) { 116 content::RecordAction( 117 base::UserMetricsAction("RegisterProtocolHandler.InfoBar_LearnMore")); 118 InfoBarService::WebContentsFromInfoBar(infobar())->OpenURL( 119 content::OpenURLParams( 120 GURL(chrome::kLearnMoreRegisterProtocolHandlerURL), 121 content::Referrer(), 122 (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition, 123 content::PAGE_TRANSITION_LINK, false)); 124 return false; 125} 126 127base::string16 RegisterProtocolHandlerInfoBarDelegate::GetProtocolName( 128 const ProtocolHandler& handler) const { 129 if (handler.protocol() == "mailto") 130 return l10n_util::GetStringUTF16(IDS_REGISTER_PROTOCOL_HANDLER_MAILTO_NAME); 131 if (handler.protocol() == "webcal") 132 return l10n_util::GetStringUTF16(IDS_REGISTER_PROTOCOL_HANDLER_WEBCAL_NAME); 133 return base::UTF8ToUTF16(handler.protocol()); 134} 135