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