blob: 292ecbfd8d5eded407d5a92c27b12ff1588d9597 [file] [log] [blame]
[email protected]ebbbb9f2011-03-09 13:16:141// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]3b073b22009-01-16 03:29:035#include "chrome/browser/ssl/ssl_blocking_page.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]7cf1b6ce2010-03-20 06:37:017#include "base/i18n/rtl.h"
[email protected]835d7c82010-10-14 04:38:388#include "base/metrics/histogram.h"
initial.commit09911bf2008-07-26 23:55:299#include "base/string_piece.h"
[email protected]99922662010-08-17 16:24:2510#include "base/utf_string_conversions.h"
[email protected]bfd04a62009-02-01 18:16:5611#include "base/values.h"
initial.commit09911bf2008-07-26 23:55:2912#include "chrome/browser/dom_operation_notification_details.h"
[email protected]3b073b22009-01-16 03:29:0313#include "chrome/browser/ssl/ssl_error_info.h"
[email protected]8ec26472011-06-06 16:52:4514#include "chrome/browser/tab_contents/tab_util.h"
initial.commit09911bf2008-07-26 23:55:2915#include "chrome/common/jstemplate_builder.h"
[email protected]a01efd22011-03-01 00:38:3216#include "content/browser/cert_store.h"
17#include "content/browser/renderer_host/render_process_host.h"
18#include "content/browser/renderer_host/render_view_host.h"
[email protected]74b962a2011-06-03 21:22:5419#include "content/browser/ssl/ssl_cert_error_handler.h"
[email protected]a01efd22011-03-01 00:38:3220#include "content/browser/tab_contents/navigation_controller.h"
21#include "content/browser/tab_contents/navigation_entry.h"
22#include "content/browser/tab_contents/tab_contents.h"
[email protected]ebbbb9f2011-03-09 13:16:1423#include "content/common/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1624#include "content/public/browser/notification_types.h"
[email protected]34ac8f32009-02-22 23:03:2725#include "grit/browser_resources.h"
26#include "grit/generated_resources.h"
[email protected]c051a1b2011-01-21 23:30:1727#include "ui/base/l10n/l10n_util.h"
[email protected]42ce29d2011-01-20 23:19:4628#include "ui/base/resource/resource_bundle.h"
initial.commit09911bf2008-07-26 23:55:2929
[email protected]49f3d552009-05-01 00:03:4930namespace {
31
32enum SSLBlockingPageEvent {
33 SHOW,
34 PROCEED,
35 DONT_PROCEED,
[email protected]2753b392009-12-28 06:59:5236 UNUSED_ENUM,
[email protected]49f3d552009-05-01 00:03:4937};
38
39void RecordSSLBlockingPageStats(SSLBlockingPageEvent event) {
[email protected]2753b392009-12-28 06:59:5240 UMA_HISTOGRAM_ENUMERATION("interstial.ssl", event, UNUSED_ENUM);
[email protected]49f3d552009-05-01 00:03:4941}
42
43} // namespace
44
[email protected]cbab76d2008-10-13 22:42:4745// Note that we always create a navigation entry with SSL errors.
46// No error happening loading a sub-resource triggers an interstitial so far.
[email protected]848dd042011-06-04 18:24:0347SSLBlockingPage::SSLBlockingPage(
48 SSLCertErrorHandler* handler,
49 bool overridable,
[email protected]8a27abf2011-09-30 21:59:5850 const base::Callback<void(SSLCertErrorHandler*, bool)>& callback)
[email protected]8ec26472011-06-06 16:52:4551 : ChromeInterstitialPage(
52 tab_util::GetTabContentsByID(
53 handler->render_process_host_id(), handler->tab_contents_id()),
54 true,
55 handler->request_url()),
[email protected]1d89a82f2009-05-14 05:46:2456 handler_(handler),
[email protected]848dd042011-06-04 18:24:0357 callback_(callback),
58 overridable_(overridable) {
[email protected]49f3d552009-05-01 00:03:4959 RecordSSLBlockingPageStats(SHOW);
initial.commit09911bf2008-07-26 23:55:2960}
61
62SSLBlockingPage::~SSLBlockingPage() {
[email protected]8a27abf2011-09-30 21:59:5863 if (!callback_.is_null()) {
initial.commit09911bf2008-07-26 23:55:2964 // The page is closed without the user having chosen what to do, default to
65 // deny.
66 NotifyDenyCertificate();
67 }
68}
69
[email protected]cbab76d2008-10-13 22:42:4770std::string SSLBlockingPage::GetHTMLContents() {
initial.commit09911bf2008-07-26 23:55:2971 // Let's build the html error page.
72 DictionaryValue strings;
[email protected]848dd042011-06-04 18:24:0373 SSLErrorInfo error_info = SSLErrorInfo::CreateError(
74 SSLErrorInfo::NetErrorToErrorType(handler_->cert_error()),
75 handler_->ssl_info().cert, handler_->request_url());
76
[email protected]42197a22010-12-28 23:29:4277 strings.SetString("headLine", error_info.title());
78 strings.SetString("description", error_info.details());
initial.commit09911bf2008-07-26 23:55:2979
[email protected]99922662010-08-17 16:24:2580 strings.SetString("moreInfoTitle",
81 l10n_util::GetStringUTF16(IDS_CERT_ERROR_EXTRA_INFO_TITLE));
initial.commit09911bf2008-07-26 23:55:2982 SetExtraInfo(&strings, error_info.extra_information());
83
[email protected]faf9cd62010-04-20 19:17:5884 int resource_id;
[email protected]848dd042011-06-04 18:24:0385 if (overridable_) {
[email protected]faf9cd62010-04-20 19:17:5886 resource_id = IDR_SSL_ROAD_BLOCK_HTML;
[email protected]99922662010-08-17 16:24:2587 strings.SetString("title",
88 l10n_util::GetStringUTF16(IDS_SSL_BLOCKING_PAGE_TITLE));
89 strings.SetString("proceed",
90 l10n_util::GetStringUTF16(IDS_SSL_BLOCKING_PAGE_PROCEED));
91 strings.SetString("exit",
92 l10n_util::GetStringUTF16(IDS_SSL_BLOCKING_PAGE_EXIT));
[email protected]faf9cd62010-04-20 19:17:5893 } else {
[email protected]faf9cd62010-04-20 19:17:5894 resource_id = IDR_SSL_ERROR_HTML;
[email protected]99922662010-08-17 16:24:2595 strings.SetString("title",
96 l10n_util::GetStringUTF16(IDS_SSL_ERROR_PAGE_TITLE));
97 strings.SetString("back",
98 l10n_util::GetStringUTF16(IDS_SSL_ERROR_PAGE_BACK));
[email protected]faf9cd62010-04-20 19:17:5899 }
initial.commit09911bf2008-07-26 23:55:29100
[email protected]99922662010-08-17 16:24:25101 strings.SetString("textdirection", base::i18n::IsRTL() ? "rtl" : "ltr");
initial.commit09911bf2008-07-26 23:55:29102
[email protected]faf9cd62010-04-20 19:17:58103 base::StringPiece html(
104 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id));
initial.commit09911bf2008-07-26 23:55:29105
[email protected]7cd22a52009-07-14 00:40:25106 return jstemplate_builder::GetI18nTemplateHtml(html, &strings);
[email protected]cbab76d2008-10-13 22:42:47107}
initial.commit09911bf2008-07-26 23:55:29108
[email protected]cbab76d2008-10-13 22:42:47109void SSLBlockingPage::UpdateEntry(NavigationEntry* entry) {
[email protected]1d89a82f2009-05-14 05:46:24110 const net::SSLInfo& ssl_info = handler_->ssl_info();
[email protected]8e8bb6d2010-12-13 08:18:55111 int cert_id = CertStore::GetInstance()->StoreCert(
[email protected]76543b92009-08-31 17:27:45112 ssl_info.cert, tab()->render_view_host()->process()->id());
[email protected]ecd9d8702008-08-28 22:10:17113
[email protected]e9ba4472008-09-14 15:42:43114 entry->ssl().set_security_style(SECURITY_STYLE_AUTHENTICATION_BROKEN);
115 entry->ssl().set_cert_id(cert_id);
116 entry->ssl().set_cert_status(ssl_info.cert_status);
117 entry->ssl().set_security_bits(ssl_info.security_bits);
118 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27119 content::NOTIFICATION_SSL_VISIBLE_STATE_CHANGED,
[email protected]57c6a652009-05-04 07:58:34120 Source<NavigationController>(&tab()->controller()),
[email protected]e9ba4472008-09-14 15:42:43121 NotificationService::NoDetails());
initial.commit09911bf2008-07-26 23:55:29122}
123
[email protected]cbab76d2008-10-13 22:42:47124void SSLBlockingPage::CommandReceived(const std::string& command) {
125 if (command == "1") {
126 Proceed();
127 } else {
128 DontProceed();
initial.commit09911bf2008-07-26 23:55:29129 }
130}
131
132void SSLBlockingPage::Proceed() {
[email protected]49f3d552009-05-01 00:03:49133 RecordSSLBlockingPageStats(PROCEED);
134
initial.commit09911bf2008-07-26 23:55:29135 // Accepting the certificate resumes the loading of the page.
136 NotifyAllowCertificate();
[email protected]a3a1d142008-12-19 00:42:30137
138 // This call hides and deletes the interstitial.
139 InterstitialPage::Proceed();
initial.commit09911bf2008-07-26 23:55:29140}
141
142void SSLBlockingPage::DontProceed() {
[email protected]49f3d552009-05-01 00:03:49143 RecordSSLBlockingPageStats(DONT_PROCEED);
144
initial.commit09911bf2008-07-26 23:55:29145 NotifyDenyCertificate();
[email protected]cbab76d2008-10-13 22:42:47146 InterstitialPage::DontProceed();
initial.commit09911bf2008-07-26 23:55:29147}
148
initial.commit09911bf2008-07-26 23:55:29149void SSLBlockingPage::NotifyDenyCertificate() {
[email protected]0d5f33072011-09-03 05:03:23150 // It's possible that callback_ may not exist if the user clicks "Proceed"
151 // followed by pressing the back button before the interstitial is hidden.
152 // In that case the certificate will still be treated as allowed.
[email protected]8a27abf2011-09-30 21:59:58153 if (callback_.is_null())
[email protected]0d5f33072011-09-03 05:03:23154 return;
initial.commit09911bf2008-07-26 23:55:29155
[email protected]8a27abf2011-09-30 21:59:58156 callback_.Run(handler_, false);
157 callback_.Reset();
initial.commit09911bf2008-07-26 23:55:29158}
159
160void SSLBlockingPage::NotifyAllowCertificate() {
[email protected]8a27abf2011-09-30 21:59:58161 DCHECK(!callback_.is_null());
initial.commit09911bf2008-07-26 23:55:29162
[email protected]8a27abf2011-09-30 21:59:58163 callback_.Run(handler_, true);
164 callback_.Reset();
initial.commit09911bf2008-07-26 23:55:29165}
166
167// static
initial.commit09911bf2008-07-26 23:55:29168void SSLBlockingPage::SetExtraInfo(
169 DictionaryValue* strings,
[email protected]42197a22010-12-28 23:29:42170 const std::vector<string16>& extra_info) {
initial.commit09911bf2008-07-26 23:55:29171 DCHECK(extra_info.size() < 5); // We allow 5 paragraphs max.
[email protected]99922662010-08-17 16:24:25172 const char* keys[5] = {
173 "moreInfo1", "moreInfo2", "moreInfo3", "moreInfo4", "moreInfo5"
initial.commit09911bf2008-07-26 23:55:29174 };
175 int i;
176 for (i = 0; i < static_cast<int>(extra_info.size()); i++) {
[email protected]42197a22010-12-28 23:29:42177 strings->SetString(keys[i], extra_info[i]);
initial.commit09911bf2008-07-26 23:55:29178 }
[email protected]99922662010-08-17 16:24:25179 for (; i < 5; i++) {
180 strings->SetString(keys[i], "");
initial.commit09911bf2008-07-26 23:55:29181 }
182}