Home | History | Annotate | Download | only in infobars
      1 // Copyright (c) 2011 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/infobars/insecure_content_infobar_delegate.h"
      6 
      7 #include "base/metrics/histogram.h"
      8 #include "chrome/browser/google/google_util.h"
      9 #include "chrome/browser/infobars/infobar_service.h"
     10 #include "chrome/common/render_messages.h"
     11 #include "content/public/browser/render_view_host.h"
     12 #include "content/public/browser/web_contents.h"
     13 #include "content/public/common/page_transition_types.h"
     14 #include "grit/generated_resources.h"
     15 #include "ui/base/l10n/l10n_util.h"
     16 
     17 
     18 // static
     19 void InsecureContentInfoBarDelegate::Create(InfoBarService* infobar_service,
     20                                             InfoBarType type) {
     21   scoped_ptr<InfoBarDelegate> new_infobar(
     22       new InsecureContentInfoBarDelegate(infobar_service, type));
     23 
     24   // Only supsersede an existing insecure content infobar if we are upgrading
     25   // from DISPLAY to RUN.
     26   for (size_t i = 0; i < infobar_service->infobar_count(); ++i) {
     27     InsecureContentInfoBarDelegate* delegate =
     28         infobar_service->infobar_at(i)->AsInsecureContentInfoBarDelegate();
     29     if (delegate != NULL) {
     30       if ((type == RUN) && (delegate->type_ == DISPLAY))
     31         return;
     32       infobar_service->ReplaceInfoBar(delegate, new_infobar.Pass());
     33       break;
     34     }
     35   }
     36   if (new_infobar.get())
     37     infobar_service->AddInfoBar(new_infobar.Pass());
     38 
     39   UMA_HISTOGRAM_ENUMERATION("InsecureContentInfoBarDelegateV2",
     40       (type == DISPLAY) ? DISPLAY_INFOBAR_SHOWN : RUN_INFOBAR_SHOWN,
     41       NUM_EVENTS);
     42 }
     43 
     44 InsecureContentInfoBarDelegate::InsecureContentInfoBarDelegate(
     45     InfoBarService* infobar_service,
     46     InfoBarType type)
     47     : ConfirmInfoBarDelegate(infobar_service),
     48       type_(type) {
     49 }
     50 
     51 InsecureContentInfoBarDelegate::~InsecureContentInfoBarDelegate() {
     52 }
     53 
     54 void InsecureContentInfoBarDelegate::InfoBarDismissed() {
     55   UMA_HISTOGRAM_ENUMERATION("InsecureContentInfoBarDelegateV2",
     56       (type_ == DISPLAY) ? DISPLAY_INFOBAR_DISMISSED : RUN_INFOBAR_DISMISSED,
     57       NUM_EVENTS);
     58   ConfirmInfoBarDelegate::InfoBarDismissed();
     59 }
     60 
     61 InsecureContentInfoBarDelegate*
     62     InsecureContentInfoBarDelegate::AsInsecureContentInfoBarDelegate() {
     63   return this;
     64 }
     65 
     66 string16 InsecureContentInfoBarDelegate::GetMessageText() const {
     67   return l10n_util::GetStringUTF16(IDS_BLOCKED_DISPLAYING_INSECURE_CONTENT);
     68 }
     69 
     70 string16 InsecureContentInfoBarDelegate::GetButtonLabel(
     71     InfoBarButton button) const {
     72   return l10n_util::GetStringUTF16(button == BUTTON_OK ?
     73       IDS_BLOCK_INSECURE_CONTENT_BUTTON : IDS_ALLOW_INSECURE_CONTENT_BUTTON);
     74 }
     75 
     76 // OK button is labelled "don't load".  It triggers Accept(), but really
     77 // means stay secure, so do nothing but count the event and dismiss.
     78 bool InsecureContentInfoBarDelegate::Accept() {
     79   UMA_HISTOGRAM_ENUMERATION("InsecureContentInfoBarDelegateV2",
     80       (type_ == DISPLAY) ? DISPLAY_USER_DID_NOT_LOAD : RUN_USER_DID_NOT_LOAD,
     81       NUM_EVENTS);
     82   return true;
     83 }
     84 
     85 
     86 // Cancel button is labelled "load anyways".  It triggers Cancel(), but really
     87 // means become insecure, so do the work of reloading the page.
     88 bool InsecureContentInfoBarDelegate::Cancel() {
     89   UMA_HISTOGRAM_ENUMERATION("InsecureContentInfoBarDelegateV2",
     90       (type_ == DISPLAY) ? DISPLAY_USER_OVERRIDE : RUN_USER_OVERRIDE,
     91       NUM_EVENTS);
     92 
     93   if (web_contents()) {
     94     int32 routing_id = web_contents()->GetRoutingID();
     95     web_contents()->Send((type_ == DISPLAY) ?
     96         static_cast<IPC::Message*>(
     97             new ChromeViewMsg_SetAllowDisplayingInsecureContent(routing_id,
     98                                                                 true)) :
     99         new ChromeViewMsg_SetAllowRunningInsecureContent(routing_id, true));
    100   }
    101   return true;
    102 }
    103 
    104 string16 InsecureContentInfoBarDelegate::GetLinkText() const {
    105   return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
    106 }
    107 
    108 bool InsecureContentInfoBarDelegate::LinkClicked(
    109     WindowOpenDisposition disposition) {
    110   web_contents()->OpenURL(content::OpenURLParams(
    111       google_util::AppendGoogleLocaleParam(GURL("https://www.google.com/"
    112           "support/chrome/bin/answer.py?answer=1342714")),
    113       content::Referrer(),
    114       (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition,
    115       content::PAGE_TRANSITION_LINK, false));
    116   return false;
    117 }
    118