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 "grit/components_strings.h"
16 #include "ui/base/l10n/l10n_util.h"
19 void RegisterProtocolHandlerInfoBarDelegate::Create(
20 InfoBarService
* infobar_service
,
21 ProtocolHandlerRegistry
* registry
,
22 const ProtocolHandler
& handler
) {
23 content::RecordAction(
24 base::UserMetricsAction("RegisterProtocolHandler.InfoBar_Shown"));
26 scoped_ptr
<infobars::InfoBar
> infobar(
27 infobar_service
->CreateConfirmInfoBar(scoped_ptr
<ConfirmInfoBarDelegate
>(
28 new RegisterProtocolHandlerInfoBarDelegate(registry
, handler
))));
30 for (size_t i
= 0; i
< infobar_service
->infobar_count(); ++i
) {
31 infobars::InfoBar
* existing_infobar
= infobar_service
->infobar_at(i
);
32 RegisterProtocolHandlerInfoBarDelegate
* existing_delegate
=
33 existing_infobar
->delegate()->
34 AsRegisterProtocolHandlerInfoBarDelegate();
35 if ((existing_delegate
!= NULL
) &&
36 existing_delegate
->handler_
.IsEquivalent(handler
)) {
37 infobar_service
->ReplaceInfoBar(existing_infobar
, infobar
.Pass());
42 infobar_service
->AddInfoBar(infobar
.Pass());
45 RegisterProtocolHandlerInfoBarDelegate::RegisterProtocolHandlerInfoBarDelegate(
46 ProtocolHandlerRegistry
* registry
,
47 const ProtocolHandler
& handler
)
48 : ConfirmInfoBarDelegate(),
53 RegisterProtocolHandlerInfoBarDelegate::
54 ~RegisterProtocolHandlerInfoBarDelegate() {
57 infobars::InfoBarDelegate::Type
58 RegisterProtocolHandlerInfoBarDelegate::GetInfoBarType() const {
59 return PAGE_ACTION_TYPE
;
62 infobars::InfoBarDelegate::InfoBarAutomationType
63 RegisterProtocolHandlerInfoBarDelegate::GetInfoBarAutomationType() const {
67 RegisterProtocolHandlerInfoBarDelegate
*
68 RegisterProtocolHandlerInfoBarDelegate::
69 AsRegisterProtocolHandlerInfoBarDelegate() {
73 base::string16
RegisterProtocolHandlerInfoBarDelegate::GetMessageText() const {
74 ProtocolHandler old_handler
= registry_
->GetHandlerFor(handler_
.protocol());
75 return old_handler
.IsEmpty() ?
76 l10n_util::GetStringFUTF16(
77 IDS_REGISTER_PROTOCOL_HANDLER_CONFIRM
,
78 base::UTF8ToUTF16(handler_
.url().host()),
79 GetProtocolName(handler_
)) :
80 l10n_util::GetStringFUTF16(
81 IDS_REGISTER_PROTOCOL_HANDLER_CONFIRM_REPLACE
,
82 base::UTF8ToUTF16(handler_
.url().host()),
83 GetProtocolName(handler_
),
84 base::UTF8ToUTF16(old_handler
.url().host()));
87 base::string16
RegisterProtocolHandlerInfoBarDelegate::GetButtonLabel(
88 InfoBarButton button
) const {
89 return (button
== BUTTON_OK
) ?
90 l10n_util::GetStringUTF16(IDS_REGISTER_PROTOCOL_HANDLER_ACCEPT
) :
91 l10n_util::GetStringUTF16(IDS_REGISTER_PROTOCOL_HANDLER_DENY
);
94 bool RegisterProtocolHandlerInfoBarDelegate::OKButtonTriggersUACPrompt() const {
98 bool RegisterProtocolHandlerInfoBarDelegate::Accept() {
99 content::RecordAction(
100 base::UserMetricsAction("RegisterProtocolHandler.Infobar_Accept"));
101 registry_
->OnAcceptRegisterProtocolHandler(handler_
);
105 bool RegisterProtocolHandlerInfoBarDelegate::Cancel() {
106 content::RecordAction(
107 base::UserMetricsAction("RegisterProtocolHandler.InfoBar_Deny"));
108 registry_
->OnIgnoreRegisterProtocolHandler(handler_
);
112 base::string16
RegisterProtocolHandlerInfoBarDelegate::GetLinkText() const {
113 return l10n_util::GetStringUTF16(IDS_LEARN_MORE
);
116 bool RegisterProtocolHandlerInfoBarDelegate::LinkClicked(
117 WindowOpenDisposition disposition
) {
118 content::RecordAction(
119 base::UserMetricsAction("RegisterProtocolHandler.InfoBar_LearnMore"));
120 InfoBarService::WebContentsFromInfoBar(infobar())->OpenURL(
121 content::OpenURLParams(
122 GURL(chrome::kLearnMoreRegisterProtocolHandlerURL
),
124 (disposition
== CURRENT_TAB
) ? NEW_FOREGROUND_TAB
: disposition
,
125 ui::PAGE_TRANSITION_LINK
, false));
129 base::string16
RegisterProtocolHandlerInfoBarDelegate::GetProtocolName(
130 const ProtocolHandler
& handler
) const {
131 if (handler
.protocol() == "mailto")
132 return l10n_util::GetStringUTF16(IDS_REGISTER_PROTOCOL_HANDLER_MAILTO_NAME
);
133 if (handler
.protocol() == "webcal")
134 return l10n_util::GetStringUTF16(IDS_REGISTER_PROTOCOL_HANDLER_WEBCAL_NAME
);
135 return base::UTF8ToUTF16(handler
.protocol());