| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2014 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/ssl/ssl_error_handler.h" |
| 6 |
| 7 #include "base/metrics/histogram.h" |
| 8 #include "base/time/time.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/ssl/ssl_blocking_page.h" |
| 11 #include "content/public/browser/notification_service.h" |
| 12 #include "content/public/browser/notification_source.h" |
| 13 #include "content/public/browser/web_contents.h" |
| 14 |
| 15 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
| 16 #include "chrome/browser/captive_portal/captive_portal_service.h" |
| 17 #include "chrome/browser/captive_portal/captive_portal_service_factory.h" |
| 18 #include "chrome/browser/captive_portal/captive_portal_tab_helper.h" |
| 19 #include "chrome/browser/ssl/captive_portal_blocking_page.h" |
| 20 #endif |
| 21 |
| 22 namespace { |
| 23 |
| 24 // The delay before displaying the SSL interstitial for cert errors. |
| 25 // - If a "captive portal detected" result arrives in this many seconds, |
| 26 // a captive portal interstitial is displayed. |
| 27 // - Otherwise, an SSL interstitial is displayed. |
| 28 const int kDefaultInterstitialDisplayDelay = 2; |
| 29 |
| 30 // Events for UMA. |
| 31 enum SSLErrorHandlerEvent { |
| 32 HANDLE_ALL, |
| 33 SHOW_CAPTIVE_PORTAL_INTERSTITIAL, |
| 34 SHOW_CAPTIVE_PORTAL_INTERSTITIAL_OVERRIDABLE, |
| 35 SHOW_SSL_INTERSTITIAL, |
| 36 SHOW_SSL_INTERSTITIAL_OVERRIDABLE, |
| 37 SSL_ERROR_HANDLER_EVENT_COUNT |
| 38 }; |
| 39 |
| 40 void RecordUMA(SSLErrorHandlerEvent event) { |
| 41 UMA_HISTOGRAM_ENUMERATION("interstitial.ssl_error_handler", |
| 42 event, |
| 43 SSL_ERROR_HANDLER_EVENT_COUNT); |
| 44 } |
| 45 |
| 46 } // namespace |
| 47 |
| 48 |
| 49 base::TimeDelta SSLErrorHandler::interstitial_display_delay_ = |
| 50 base::TimeDelta::FromSeconds(kDefaultInterstitialDisplayDelay); |
| 51 |
| 52 SSLErrorHandler::SSLErrorHandler(content::WebContents* web_contents, |
| 53 int cert_error, |
| 54 const net::SSLInfo& ssl_info, |
| 55 const GURL& request_url, |
| 56 const int options_mask, |
| 57 const base::Callback<void(bool)>& callback) |
| 58 : content::WebContentsObserver(web_contents), |
| 59 cert_error_(cert_error), |
| 60 ssl_info_(ssl_info), |
| 61 request_url_(request_url), |
| 62 options_mask_(options_mask), |
| 63 callback_(callback) { |
| 64 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
| 65 Profile* profile = Profile::FromBrowserContext( |
| 66 web_contents->GetBrowserContext()); |
| 67 registrar_.Add(this, |
| 68 chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT, |
| 69 content::Source<Profile>(profile)); |
| 70 #endif |
| 71 } |
| 72 |
| 73 SSLErrorHandler::~SSLErrorHandler() { |
| 74 } |
| 75 |
| 76 void SSLErrorHandler::HandleSSLError( |
| 77 content::WebContents* web_contents, |
| 78 int cert_error, |
| 79 const net::SSLInfo& ssl_info, |
| 80 const GURL& request_url, |
| 81 int options_mask, |
| 82 const base::Callback<void(bool)>& callback) { |
| 83 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
| 84 CaptivePortalTabHelper* captive_portal_tab_helper = |
| 85 CaptivePortalTabHelper::FromWebContents(web_contents); |
| 86 if (captive_portal_tab_helper) { |
| 87 captive_portal_tab_helper->OnSSLCertError(ssl_info); |
| 88 } |
| 89 #endif |
| 90 (new SSLErrorHandler(web_contents, cert_error, ssl_info, request_url, |
| 91 options_mask, callback))->StartHandlingError(); |
| 92 } |
| 93 |
| 94 void SSLErrorHandler::StartHandlingError() { |
| 95 RecordUMA(HANDLE_ALL); |
| 96 |
| 97 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
| 98 CheckForCaptivePortal(); |
| 99 timer_.Start(FROM_HERE, interstitial_display_delay_, |
| 100 base::Bind(&SSLErrorHandler::OnTimerExpired, |
| 101 base::Unretained(this))); |
| 102 content::NotificationService* service = |
| 103 content::NotificationService::current(); |
| 104 service->Notify(chrome::NOTIFICATION_SSL_INTERSTITIAL_TIMER_FIRED, |
| 105 content::Source<content::WebContents>(web_contents()), |
| 106 content::NotificationService::NoDetails()); |
| 107 return; |
| 108 #endif |
| 109 // Display an SSL interstitial. |
| 110 ShowSSLInterstitial(); |
| 111 } |
| 112 |
| 113 void SSLErrorHandler::OnTimerExpired() { |
| 114 ShowSSLInterstitial(); |
| 115 } |
| 116 |
| 117 void SSLErrorHandler::CheckForCaptivePortal() { |
| 118 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
| 119 Profile* profile = Profile::FromBrowserContext( |
| 120 web_contents()->GetBrowserContext()); |
| 121 CaptivePortalService* captive_portal_service = |
| 122 CaptivePortalServiceFactory::GetForProfile(profile); |
| 123 captive_portal_service->DetectCaptivePortal(); |
| 124 #else |
| 125 NOTREACHED(); |
| 126 #endif |
| 127 } |
| 128 |
| 129 void SSLErrorHandler::ShowCaptivePortalInterstitial() { |
| 130 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
| 131 // Show captive portal blocking page. The interstitial owns the blocking page. |
| 132 RecordUMA(SSLBlockingPage::IsOverridable(options_mask_) ? |
| 133 SHOW_CAPTIVE_PORTAL_INTERSTITIAL_OVERRIDABLE : |
| 134 SHOW_CAPTIVE_PORTAL_INTERSTITIAL); |
| 135 (new CaptivePortalBlockingPage(web_contents(), request_url_))->Show(); |
| 136 delete this; |
| 137 #else |
| 138 NOTREACHED(); |
| 139 #endif |
| 140 } |
| 141 |
| 142 void SSLErrorHandler::ShowSSLInterstitial() { |
| 143 // Show SSL blocking page. The interstitial owns the blocking page. |
| 144 RecordUMA(SSLBlockingPage::IsOverridable(options_mask_) ? |
| 145 SHOW_SSL_INTERSTITIAL_OVERRIDABLE : |
| 146 SHOW_SSL_INTERSTITIAL); |
| 147 (new SSLBlockingPage(web_contents(), cert_error_, ssl_info_, request_url_, |
| 148 options_mask_, callback_))->Show(); |
| 149 delete this; |
| 150 } |
| 151 |
| 152 void SSLErrorHandler::Observe( |
| 153 int type, |
| 154 const content::NotificationSource& source, |
| 155 const content::NotificationDetails& details) { |
| 156 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
| 157 if (type == chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT) { |
| 158 timer_.Stop(); |
| 159 CaptivePortalService::Results* results = |
| 160 content::Details<CaptivePortalService::Results>(details).ptr(); |
| 161 if (results->result == captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL) |
| 162 ShowCaptivePortalInterstitial(); |
| 163 else |
| 164 ShowSSLInterstitial(); |
| 165 } |
| 166 #endif |
| 167 } |
| 168 |
| 169 void SSLErrorHandler::DidStartNavigationToPendingEntry( |
| 170 const GURL& url, |
| 171 content::NavigationController::ReloadType reload_type) { |
| 172 delete this; |
| 173 } |
| 174 |
| 175 void SSLErrorHandler::DidStopLoading( |
| 176 content::RenderViewHost* render_view_host) { |
| 177 delete this; |
| 178 } |
| 179 |
| 180 void SSLErrorHandler::WebContentsDestroyed() { |
| 181 delete this; |
| 182 } |
| OLD | NEW |