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