Chromium Code Reviews| 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..d39cd0171e0bd86477e857b89fe4f49842bdf658 |
| --- /dev/null |
| +++ b/chrome/browser/ssl/ssl_error_handler.cc |
| @@ -0,0 +1,159 @@ |
| +// Copyright (c) 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/metrics/histogram.h" |
| +#include "base/time/time.h" |
| +#include "base/timer/timer.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/ssl/captive_portal_blocking_page.h" |
| +#include "chrome/browser/ssl/ssl_blocking_page.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" |
| +#endif |
| + |
| +namespace { |
| +// The delay before displaying the SSL interstitial, during which we wait for a |
| +// captive portal result. |
| +const int kDefaultSSLInterstitialDisplayDelay = 2; |
| + |
| +// Events for UMA. |
| +enum SSLErrorHandlerEvent { |
| + HANDLE_ALL, |
| + CAPTIVE_PORTAL_CHECK, |
| + CAPTIVE_PORTAL_RESULT_ALREADY_HANDLED, |
| + CAPTIVE_PORTAL_RESULT_BEFORE_TIMER, |
| + SSL_WARNING_ALREADY_HANDLED, |
| + SHOW_CAPTIVE_PORTAL_INTERSTITIAL, |
| + SHOW_SSL_INTERSTITIAL, |
| + SHOW_SSL_INTERSTITIAL_WITHOUT_CAPTIVE_PORTAL_CHECK, |
| + SSL_ERROR_HANDLER_EVENT_COUNT |
| +}; |
| + |
| +void RecordUMA(SSLErrorHandlerEvent event) { |
| + UMA_HISTOGRAM_ENUMERATION("interstitial.ssl_error_handler", |
| + event, |
| + SSL_ERROR_HANDLER_EVENT_COUNT); |
| +} |
| + |
| +} // namespace |
| + |
| +SSLErrorHandler::SSLErrorHandler(content::WebContents* web_contents, |
| + int cert_error, |
| + const net::SSLInfo& ssl_info, |
| + const GURL& request_url, |
| + bool overridable, |
| + bool strict_enforcement, |
| + 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), |
| + overridable_(overridable), |
| + strict_enforcement_(strict_enforcement), |
| + callback_(callback), |
| + handled_(false), |
| + ssl_interstitial_display_delay_( |
| + base::TimeDelta::FromSeconds(kDefaultSSLInterstitialDisplayDelay)) { |
| +} |
| + |
| +SSLErrorHandler::~SSLErrorHandler() { |
| +} |
| + |
| +void SSLErrorHandler::Handle() { |
| + RecordUMA(HANDLE_ALL); |
| +#if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
| + CheckForCaptivePortal(); |
| + timer_.Start(FROM_HERE, ssl_interstitial_display_delay_, |
| + base::Bind(&SSLErrorHandler::OnTimerExpired, |
| + base::Unretained(this))); |
| +#else |
| + RecordUMA(SHOW_SSL_INTERSTITIAL_WITHOUT_CAPTIVE_PORTAL_CHECK); |
| + ShowSSLInterstitial(); |
| + handled_ = true; |
| +#endif |
| +} |
| + |
| +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(); |
| + registrar_.Add(this, |
| + chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT, |
| + content::Source<Profile>(profile)); |
| +#endif |
| +} |
| + |
| +void SSLErrorHandler::Observe( |
| + int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) { |
| + // TODO(meacer): Refactor this method in SSLBlockingPage. |
| +#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) |
| + OnCaptivePortalResult(); |
| + } |
| +#endif |
| +} |
| + |
| +void SSLErrorHandler::OnCaptivePortalResult() { |
| + if (!timer_.IsRunning()) { |
| + RecordUMA(CAPTIVE_PORTAL_RESULT_BEFORE_TIMER); |
| + return; |
| + } |
| + if (handled_) { |
| + RecordUMA(CAPTIVE_PORTAL_RESULT_ALREADY_HANDLED); |
| + return; |
| + } |
| + timer_.Stop(); |
| + ShowCaptivePortalInterstitial(); |
| + handled_ = true; |
|
mmenke
2014/06/20 16:07:09
We just keep the handler around until some random
meacer
2014/06/20 23:28:15
Once we show the captive portal interstitial, the
|
| +} |
| + |
| +void SSLErrorHandler::OnTimerExpired() { |
| + if (handled_) { |
| + RecordUMA(SSL_WARNING_ALREADY_HANDLED); |
| + return; |
| + } |
| + ShowSSLInterstitial(); |
| + handled_ = true; |
| +} |
| + |
| +void SSLErrorHandler::ShowCaptivePortalInterstitial() { |
| + // Show captive portal blocking page. The interstitial owns the blocking page. |
| + RecordUMA(SHOW_CAPTIVE_PORTAL_INTERSTITIAL); |
| + new CaptivePortalBlockingPage(web_contents_, request_url_); |
| +} |
| + |
| +void SSLErrorHandler::ShowSSLInterstitial() { |
| + // Show SSL blocking page. The interstitial owns the blocking page. |
| + RecordUMA(SHOW_SSL_INTERSTITIAL); |
| + new SSLBlockingPage(web_contents_, cert_error_, ssl_info_, request_url_, |
| + overridable_, strict_enforcement_, callback_); |
| +} |
| + |
| +void SSLErrorHandler::DidStopLoading( |
|
mmenke
2014/06/20 16:07:09
Should also destroy the handler on a new load. No
mmenke
2014/06/20 16:07:09
What if the user presses the stop button?
meacer
2014/06/20 23:28:15
In which case do we get a new load notification on
meacer
2014/06/20 23:28:15
In that case we'll get a DidStopLoading and delete
|
| + content::RenderViewHost* render_view_host) { |
| + timer_.Stop(); |
|
mmenke
2014/06/20 16:07:09
Not need to stop the timer. Destroying ourselves
meacer
2014/06/20 23:28:15
Done.
|
| + delete this; |
| +} |
| + |
| +void SSLErrorHandler::WebContentsDestroyed() { |
| + timer_.Stop(); |
| + delete this; |
| +} |