Index: chrome/browser/ssl/ssl_error_handler.cc |
diff --git a/chrome/browser/ssl/ssl_error_handler.cc b/chrome/browser/ssl/ssl_error_handler.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9283c80dcefc4ee53ae69f2cb6622e932fb22c5c |
--- /dev/null |
+++ b/chrome/browser/ssl/ssl_error_handler.cc |
@@ -0,0 +1,195 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/ssl/ssl_error_handler.h" |
+ |
+#include "base/time/time.h" |
+#include "base/timer/timer.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/ssl/ssl_blocking_page.h" |
+#include "chrome/common/chrome_version_info.h" |
+#include "content/public/browser/notification_service.h" |
+#include "content/public/browser/notification_source.h" |
+#include "content/public/browser/web_contents.h" |
+ |
+#if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
+#include "chrome/browser/captive_portal/captive_portal_service.h" |
+#include "chrome/browser/captive_portal/captive_portal_service_factory.h" |
+#include "chrome/browser/captive_portal/captive_portal_tab_helper.h" |
+#include "chrome/browser/ssl/captive_portal_blocking_page.h" |
+#endif |
+ |
+namespace { |
+ |
+// Events for UMA. |
+enum SSLErrorHandlerEvent { |
+ HANDLE_ALL, |
+ CAPTIVE_PORTAL_CHECK, |
+ CAPTIVE_PORTAL_DETECTED_ALREADY_HANDLED, |
+ CAPTIVE_PORTAL_DETECTED_BEFORE_TIMER_TRIGGERED, |
+ CAPTIVE_PORTAL_DETECTED_AFTER_TIMER_EXPIRED, |
+ SHOW_CAPTIVE_PORTAL_INTERSTITIAL, |
+ SHOW_CAPTIVE_PORTAL_INTERSTITIAL_OVERRIDABLE, |
+ SHOW_SSL_INTERSTITIAL, |
+ SHOW_SSL_INTERSTITIAL_OVERRIDABLE, |
+ SSL_ERROR_HANDLER_EVENT_COUNT |
+}; |
+ |
+void RecordUMA(SSLErrorHandlerEvent event) { |
+ UMA_HISTOGRAM_ENUMERATION("interstitial.ssl_error_handler", |
+ event, |
+ SSL_ERROR_HANDLER_EVENT_COUNT); |
+} |
+ |
+#if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
+bool IsCaptivePortalInterstitialEnabled() { |
+ chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel(); |
+ return channel <= chrome::VersionInfo::CHANNEL_DEV; |
+} |
+#endif |
+ |
+} // namespace |
+ |
+SSLErrorHandler::SSLErrorHandler(content::WebContents* web_contents, |
+ int cert_error, |
+ const net::SSLInfo& ssl_info, |
+ const GURL& request_url, |
+ const int options_mask, |
+ const base::TimeDelta ssl_error_delay, |
+ const base::Callback<void(bool)>& callback) |
+ : content::WebContentsObserver(web_contents), |
+ web_contents_(web_contents), |
+ cert_error_(cert_error), |
+ ssl_info_(ssl_info), |
+ request_url_(request_url), |
+ options_mask_(options_mask), |
+ callback_(callback), |
+ handled_(false), |
+ ssl_interstitial_display_delay_(ssl_error_delay), |
+ timer_expired_(false) { |
+#if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
+ Profile* profile = Profile::FromBrowserContext( |
+ web_contents_->GetBrowserContext()); |
+ registrar_.Add(this, |
+ chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT, |
+ content::Source<Profile>(profile)); |
+#endif |
+} |
+ |
+SSLErrorHandler::~SSLErrorHandler() { |
+} |
+ |
+void SSLErrorHandler::HandleSSLError( |
+ content::WebContents* web_contents, |
+ int cert_error, |
+ const net::SSLInfo& ssl_info, |
+ const GURL& request_url, |
+ int options_mask, |
+ const base::Callback<void(bool)>& callback) { |
+ // SSL interstitials aren't delayed if captive portal detection is disabled. |
+ base::TimeDelta ssl_error_delay; |
+#if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
+ CaptivePortalTabHelper* captive_portal_tab_helper = |
+ CaptivePortalTabHelper::FromWebContents(web_contents); |
+ if (captive_portal_tab_helper) { |
+ captive_portal_tab_helper->OnSSLCertError(ssl_info); |
+ ssl_error_delay = captive_portal_tab_helper->GetSSLErrorDelay(); |
+ } |
+#endif |
+ (new SSLErrorHandler(web_contents, cert_error, ssl_info, request_url, |
+ options_mask, ssl_error_delay, callback))->Handle(); |
+} |
+ |
+void SSLErrorHandler::Handle() { |
+ RecordUMA(HANDLE_ALL); |
+ if (handled_) |
+ return; |
mmenke
2014/11/07 15:45:30
Since this can't happen, we shouldn't check for it
meacer
2014/11/07 20:30:31
Done.
|
+ |
+#if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
+ if (IsCaptivePortalInterstitialEnabled()) { |
+ CheckForCaptivePortal(); |
+ timer_.Start(FROM_HERE, ssl_interstitial_display_delay_, |
+ base::Bind(&SSLErrorHandler::OnTimerExpired, |
+ base::Unretained(this))); |
+ return; |
+ } |
+#endif |
+ // Display an SSL interstitial. |
+ ShowSSLInterstitial(); |
+ handled_ = true; |
mmenke
2014/11/07 15:45:30
I think it makes more sense to move this line into
meacer
2014/11/07 20:30:31
Done.
|
+} |
+ |
+void SSLErrorHandler::CheckForCaptivePortal() { |
+#if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
+ RecordUMA(CAPTIVE_PORTAL_CHECK); |
+ Profile* profile = Profile::FromBrowserContext( |
+ web_contents_->GetBrowserContext()); |
+ CaptivePortalService* captive_portal_service = |
+ CaptivePortalServiceFactory::GetForProfile(profile); |
+ captive_portal_service->DetectCaptivePortal(); |
+#endif |
+} |
+ |
+void SSLErrorHandler::ShowCaptivePortalInterstitial() { |
mmenke
2014/11/07 15:45:30
DCHECK(!handled_);?
meacer
2014/11/07 20:30:31
Done.
|
+#if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
+ // Show captive portal blocking page. The interstitial owns the blocking page. |
+ RecordUMA(SSLBlockingPage::IsOverridable(options_mask_) ? |
+ SHOW_CAPTIVE_PORTAL_INTERSTITIAL_OVERRIDABLE : |
+ SHOW_CAPTIVE_PORTAL_INTERSTITIAL); |
+ (new CaptivePortalBlockingPage(web_contents_, request_url_))->Show(); |
+#endif |
+} |
+ |
+void SSLErrorHandler::ShowSSLInterstitial() { |
mmenke
2014/11/07 15:45:30
DCHECK(!handled_);?
meacer
2014/11/07 20:30:31
Done.
|
+ // Show SSL blocking page. The interstitial owns the blocking page. |
+ RecordUMA(SSLBlockingPage::IsOverridable(options_mask_) ? |
+ SHOW_SSL_INTERSTITIAL_OVERRIDABLE : |
+ SHOW_SSL_INTERSTITIAL); |
+ (new SSLBlockingPage(web_contents_, cert_error_, ssl_info_, request_url_, |
+ options_mask_, callback_))->Show(); |
+} |
+ |
+void SSLErrorHandler::OnTimerExpired() { |
+ timer_expired_ = true; |
+ if (handled_) { |
+ return; |
+ } |
mmenke
2014/11/07 15:45:30
nit: Don't use braces when the condition and body
meacer
2014/11/07 20:30:31
Done.
|
+ ShowSSLInterstitial(); |
+ handled_ = true; |
+} |
+ |
+void SSLErrorHandler::Observe( |
+ int type, |
+ const content::NotificationSource& source, |
+ const content::NotificationDetails& details) { |
+#if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
+ if (type == chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT) { |
+ CaptivePortalService::Results* results = |
+ content::Details<CaptivePortalService::Results>(details).ptr(); |
+ if (results->result != captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL) |
+ return; |
+ if (handled_) { |
+ RecordUMA(CAPTIVE_PORTAL_DETECTED_ALREADY_HANDLED); |
mmenke
2014/11/06 22:12:40
This histogram seems in need of improvement.
If a
meacer
2014/11/06 23:23:19
I think I'm going to remove that histogram too. As
meacer
2014/11/07 20:30:31
Removed CAPTIVE_PORTAL_DETECTED_ALREADY_HANDLED
|
+ return; |
+ } |
+ if (!timer_.IsRunning()) { |
+ RecordUMA(timer_expired_ ? |
+ CAPTIVE_PORTAL_DETECTED_AFTER_TIMER_EXPIRED : |
+ CAPTIVE_PORTAL_DETECTED_BEFORE_TIMER_TRIGGERED); |
+ } |
+ timer_.Stop(); |
+ ShowCaptivePortalInterstitial(); |
+ handled_ = true; |
+ } |
+#endif |
+} |
+ |
+void SSLErrorHandler::DidStopLoading( |
+ content::RenderViewHost* render_view_host) { |
+ delete this; |
+} |
+ |
+void SSLErrorHandler::WebContentsDestroyed() { |
+ delete this; |
+} |