Home | History | Annotate | Download | only in custom_handlers
      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_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"
     17 
     18 // static
     19 void RegisterProtocolHandlerInfoBarDelegate::Create(
     20     InfoBarService* infobar_service,
     21     ProtocolHandlerRegistry* registry,
     22     const ProtocolHandler& handler) {
     23   content::RecordAction(
     24       base::UserMetricsAction("RegisterProtocolHandler.InfoBar_Shown"));
     25 
     26   scoped_ptr<infobars::InfoBar> infobar(
     27       ConfirmInfoBarDelegate::CreateInfoBar(scoped_ptr<ConfirmInfoBarDelegate>(
     28           new RegisterProtocolHandlerInfoBarDelegate(registry, handler))));
     29 
     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());
     38       return;
     39     }
     40   }
     41 
     42   infobar_service->AddInfoBar(infobar.Pass());
     43 }
     44 
     45 RegisterProtocolHandlerInfoBarDelegate::RegisterProtocolHandlerInfoBarDelegate(
     46     ProtocolHandlerRegistry* registry,
     47     const ProtocolHandler& handler)
     48     : ConfirmInfoBarDelegate(),
     49       registry_(registry),
     50       handler_(handler) {
     51 }
     52 
     53 RegisterProtocolHandlerInfoBarDelegate::
     54     ~RegisterProtocolHandlerInfoBarDelegate() {
     55 }
     56 
     57 infobars::InfoBarDelegate::InfoBarAutomationType
     58 RegisterProtocolHandlerInfoBarDelegate::GetInfoBarAutomationType() const {
     59   return RPH_INFOBAR;
     60 }
     61 
     62 infobars::InfoBarDelegate::Type
     63 RegisterProtocolHandlerInfoBarDelegate::GetInfoBarType() const {
     64   return PAGE_ACTION_TYPE;
     65 }
     66 
     67 RegisterProtocolHandlerInfoBarDelegate*
     68     RegisterProtocolHandlerInfoBarDelegate::
     69         AsRegisterProtocolHandlerInfoBarDelegate() {
     70   return this;
     71 }
     72 
     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()));
     85 }
     86 
     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);
     92 }
     93 
     94 bool RegisterProtocolHandlerInfoBarDelegate::OKButtonTriggersUACPrompt() const {
     95   return true;
     96 }
     97 
     98 bool RegisterProtocolHandlerInfoBarDelegate::Accept() {
     99   content::RecordAction(
    100       base::UserMetricsAction("RegisterProtocolHandler.Infobar_Accept"));
    101   registry_->OnAcceptRegisterProtocolHandler(handler_);
    102   return true;
    103 }
    104 
    105 bool RegisterProtocolHandlerInfoBarDelegate::Cancel() {
    106   content::RecordAction(
    107       base::UserMetricsAction("RegisterProtocolHandler.InfoBar_Deny"));
    108   registry_->OnIgnoreRegisterProtocolHandler(handler_);
    109   return true;
    110 }
    111 
    112 base::string16 RegisterProtocolHandlerInfoBarDelegate::GetLinkText() const {
    113   return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
    114 }
    115 
    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),
    123           content::Referrer(),
    124           (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition,
    125           ui::PAGE_TRANSITION_LINK, false));
    126   return false;
    127 }
    128 
    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());
    136 }
    137