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.
5 #include "chrome/browser/custom_handlers/register_protocol_handler_infobar_delegate.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
9 #include "chrome/browser/infobars/infobar_service.h"
10 #include "chrome/common/url_constants.h"
11 #include "chrome/grit/generated_resources.h"
12 #include "components/infobars/core/infobar.h"
13 #include "content/public/browser/user_metrics.h"
14 #include "content/public/browser/web_contents.h"
15 #include "ui/base/l10n/l10n_util.h"
18 void RegisterProtocolHandlerInfoBarDelegate::Create(
19 InfoBarService
* infobar_service
,
20 ProtocolHandlerRegistry
* registry
,
21 const ProtocolHandler
& handler
) {
22 content::RecordAction(
23 base::UserMetricsAction("RegisterProtocolHandler.InfoBar_Shown"));
25 scoped_ptr
<infobars::InfoBar
> infobar(
26 ConfirmInfoBarDelegate::CreateInfoBar(scoped_ptr
<ConfirmInfoBarDelegate
>(
27 new RegisterProtocolHandlerInfoBarDelegate(registry
, handler
))));
29 for (size_t i
= 0; i
< infobar_service
->infobar_count(); ++i
) {
30 infobars::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());
41 infobar_service
->AddInfoBar(infobar
.Pass());
44 RegisterProtocolHandlerInfoBarDelegate::RegisterProtocolHandlerInfoBarDelegate(
45 ProtocolHandlerRegistry
* registry
,
46 const ProtocolHandler
& handler
)
47 : ConfirmInfoBarDelegate(),
52 RegisterProtocolHandlerInfoBarDelegate::
53 ~RegisterProtocolHandlerInfoBarDelegate() {
56 infobars::InfoBarDelegate::InfoBarAutomationType
57 RegisterProtocolHandlerInfoBarDelegate::GetInfoBarAutomationType() const {
61 infobars::InfoBarDelegate::Type
62 RegisterProtocolHandlerInfoBarDelegate::GetInfoBarType() const {
63 return PAGE_ACTION_TYPE
;
66 RegisterProtocolHandlerInfoBarDelegate
*
67 RegisterProtocolHandlerInfoBarDelegate::
68 AsRegisterProtocolHandlerInfoBarDelegate() {
72 base::string16
RegisterProtocolHandlerInfoBarDelegate::GetMessageText() const {
73 ProtocolHandler old_handler
= registry_
->GetHandlerFor(handler_
.protocol());
74 return old_handler
.IsEmpty() ?
75 l10n_util::GetStringFUTF16(
76 IDS_REGISTER_PROTOCOL_HANDLER_CONFIRM
,
77 base::UTF8ToUTF16(handler_
.url().host()),
78 GetProtocolName(handler_
)) :
79 l10n_util::GetStringFUTF16(
80 IDS_REGISTER_PROTOCOL_HANDLER_CONFIRM_REPLACE
,
81 base::UTF8ToUTF16(handler_
.url().host()),
82 GetProtocolName(handler_
),
83 base::UTF8ToUTF16(old_handler
.url().host()));
86 base::string16
RegisterProtocolHandlerInfoBarDelegate::GetButtonLabel(
87 InfoBarButton button
) const {
88 return (button
== BUTTON_OK
) ?
89 l10n_util::GetStringUTF16(IDS_REGISTER_PROTOCOL_HANDLER_ACCEPT
) :
90 l10n_util::GetStringUTF16(IDS_REGISTER_PROTOCOL_HANDLER_DENY
);
93 bool RegisterProtocolHandlerInfoBarDelegate::OKButtonTriggersUACPrompt() const {
97 bool RegisterProtocolHandlerInfoBarDelegate::Accept() {
98 content::RecordAction(
99 base::UserMetricsAction("RegisterProtocolHandler.Infobar_Accept"));
100 registry_
->OnAcceptRegisterProtocolHandler(handler_
);
104 bool RegisterProtocolHandlerInfoBarDelegate::Cancel() {
105 content::RecordAction(
106 base::UserMetricsAction("RegisterProtocolHandler.InfoBar_Deny"));
107 registry_
->OnIgnoreRegisterProtocolHandler(handler_
);
111 base::string16
RegisterProtocolHandlerInfoBarDelegate::GetLinkText() const {
112 return l10n_util::GetStringUTF16(IDS_LEARN_MORE
);
115 bool RegisterProtocolHandlerInfoBarDelegate::LinkClicked(
116 WindowOpenDisposition disposition
) {
117 content::RecordAction(
118 base::UserMetricsAction("RegisterProtocolHandler.InfoBar_LearnMore"));
119 InfoBarService::WebContentsFromInfoBar(infobar())->OpenURL(
120 content::OpenURLParams(
121 GURL(chrome::kLearnMoreRegisterProtocolHandlerURL
),
123 (disposition
== CURRENT_TAB
) ? NEW_FOREGROUND_TAB
: disposition
,
124 content::PAGE_TRANSITION_LINK
, false));
128 base::string16
RegisterProtocolHandlerInfoBarDelegate::GetProtocolName(
129 const ProtocolHandler
& handler
) const {
130 if (handler
.protocol() == "mailto")
131 return l10n_util::GetStringUTF16(IDS_REGISTER_PROTOCOL_HANDLER_MAILTO_NAME
);
132 if (handler
.protocol() == "webcal")
133 return l10n_util::GetStringUTF16(IDS_REGISTER_PROTOCOL_HANDLER_WEBCAL_NAME
);
134 return base::UTF8ToUTF16(handler
.protocol());