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