Chromium Code Reviews| 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/field_trial.h" | |
| 8 #include "base/metrics/histogram.h" | |
| 9 #include "base/time/time.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "chrome/browser/ssl/ssl_blocking_page.h" | |
| 12 #include "content/public/browser/notification_service.h" | |
| 13 #include "content/public/browser/notification_source.h" | |
| 14 #include "content/public/browser/web_contents.h" | |
| 15 | |
| 16 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) | |
| 17 #include "chrome/browser/captive_portal/captive_portal_service.h" | |
| 18 #include "chrome/browser/captive_portal/captive_portal_service_factory.h" | |
| 19 #include "chrome/browser/captive_portal/captive_portal_tab_helper.h" | |
| 20 #include "chrome/browser/ssl/captive_portal_blocking_page.h" | |
| 21 #endif | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 // The delay before displaying the SSL interstitial for cert errors. | |
| 26 // - If a "captive portal detected" result arrives in this many seconds, | |
| 27 // a captive portal interstitial is displayed. | |
| 28 // - Otherwise, an SSL interstitial is displayed. | |
| 29 const int kDefaultInterstitialDisplayDelayInSeconds = 2; | |
| 30 | |
| 31 // Time to wait before displaying the SSL interstitial. If a captive portal | |
| 32 // arrives before this, the captive portal interstitial is displayed instead. | |
| 33 // Can be changed for testing. | |
| 34 static base::TimeDelta interstitial_display_delay = | |
|
Bernhard Bauer
2014/12/19 10:14:25
Could you name these with a g_ prefix to make it c
Bernhard Bauer
2014/12/19 10:14:25
Are we sure this won't create a static initializer
meacer
2014/12/19 19:04:23
Done.
meacer
2014/12/19 19:04:24
Since there are only three different values, I mad
| |
| 35 base::TimeDelta::FromSeconds(kDefaultInterstitialDisplayDelayInSeconds); | |
| 36 | |
| 37 // Callback to call when the interstitial timer is fired. Used for testing. | |
| 38 static SSLErrorHandler::TimerFiredCallback* timer_fired_callback = nullptr; | |
| 39 | |
| 40 // Events for UMA. | |
| 41 enum SSLErrorHandlerEvent { | |
| 42 HANDLE_ALL, | |
| 43 SHOW_CAPTIVE_PORTAL_INTERSTITIAL, | |
| 44 SHOW_CAPTIVE_PORTAL_INTERSTITIAL_OVERRIDABLE, | |
| 45 SHOW_SSL_INTERSTITIAL, | |
| 46 SHOW_SSL_INTERSTITIAL_OVERRIDABLE, | |
| 47 SSL_ERROR_HANDLER_EVENT_COUNT | |
| 48 }; | |
| 49 | |
| 50 void RecordUMA(SSLErrorHandlerEvent event) { | |
| 51 UMA_HISTOGRAM_ENUMERATION("interstitial.ssl_error_handler", | |
| 52 event, | |
| 53 SSL_ERROR_HANDLER_EVENT_COUNT); | |
| 54 } | |
| 55 | |
| 56 } // namespace | |
| 57 | |
| 58 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SSLErrorHandler); | |
| 59 | |
| 60 SSLErrorHandler::~SSLErrorHandler() { | |
| 61 } | |
| 62 | |
| 63 void SSLErrorHandler::HandleSSLError( | |
| 64 content::WebContents* web_contents, | |
| 65 int cert_error, | |
| 66 const net::SSLInfo& ssl_info, | |
| 67 const GURL& request_url, | |
| 68 int options_mask, | |
| 69 const base::Callback<void(bool)>& callback) { | |
| 70 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) | |
| 71 CaptivePortalTabHelper* captive_portal_tab_helper = | |
| 72 CaptivePortalTabHelper::FromWebContents(web_contents); | |
| 73 if (captive_portal_tab_helper) { | |
| 74 captive_portal_tab_helper->OnSSLCertError(ssl_info); | |
| 75 } | |
| 76 #endif | |
| 77 SSLErrorHandler::CreateForWebContents(web_contents, cert_error, ssl_info, | |
| 78 request_url, options_mask, callback); | |
| 79 SSLErrorHandler* error_handler = | |
| 80 SSLErrorHandler::FromWebContents(web_contents); | |
| 81 error_handler->StartHandlingError(); | |
| 82 } | |
| 83 | |
| 84 // static | |
| 85 void SSLErrorHandler::SetInterstitialDisplayDelayForTest( | |
| 86 base::TimeDelta delay) { | |
| 87 interstitial_display_delay = delay; | |
| 88 } | |
| 89 | |
| 90 // static | |
| 91 void SSLErrorHandler::SetInterstitialTimerFiredCallbackForTest( | |
| 92 TimerFiredCallback* callback) { | |
| 93 if (timer_fired_callback) { | |
|
Bernhard Bauer
2014/12/19 10:14:25
Uh, I don't think this does what you think it does
meacer
2014/12/19 19:04:23
Oh wow, good catch. I don't know what I was thinki
| |
| 94 DCHECK(!timer_fired_callback->is_null()); | |
| 95 return; | |
| 96 } | |
| 97 timer_fired_callback = callback; | |
| 98 } | |
| 99 | |
| 100 SSLErrorHandler::SSLErrorHandler(content::WebContents* web_contents, | |
| 101 int cert_error, | |
| 102 const net::SSLInfo& ssl_info, | |
| 103 const GURL& request_url, | |
| 104 int options_mask, | |
| 105 const base::Callback<void(bool)>& callback) | |
| 106 : web_contents_(web_contents), | |
| 107 cert_error_(cert_error), | |
| 108 ssl_info_(ssl_info), | |
| 109 request_url_(request_url), | |
| 110 options_mask_(options_mask), | |
| 111 callback_(callback) { | |
| 112 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) | |
| 113 Profile* profile = Profile::FromBrowserContext( | |
| 114 web_contents->GetBrowserContext()); | |
| 115 registrar_.Add(this, | |
| 116 chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT, | |
| 117 content::Source<Profile>(profile)); | |
| 118 #endif | |
| 119 } | |
| 120 | |
| 121 void SSLErrorHandler::StartHandlingError() { | |
| 122 RecordUMA(HANDLE_ALL); | |
| 123 | |
| 124 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) | |
| 125 CheckForCaptivePortal(); | |
| 126 timer_.Start(FROM_HERE, interstitial_display_delay, | |
| 127 base::Bind(&SSLErrorHandler::OnTimerExpired, | |
| 128 base::Unretained(this))); | |
| 129 if (timer_fired_callback) | |
| 130 timer_fired_callback->Run(web_contents_); | |
| 131 #else | |
| 132 // Display an SSL interstitial. | |
| 133 ShowSSLInterstitial(); | |
| 134 #endif | |
| 135 } | |
| 136 | |
| 137 // static | |
| 138 void SSLErrorHandler::CreateForWebContents( | |
| 139 content::WebContents* web_contents, | |
| 140 int cert_error, | |
| 141 const net::SSLInfo& ssl_info, | |
| 142 const GURL& request_url, | |
| 143 int options_mask, | |
| 144 const base::Callback<void(bool)>& callback) { | |
| 145 DCHECK(!FromWebContents(web_contents)); | |
| 146 web_contents->SetUserData(UserDataKey(), | |
| 147 new SSLErrorHandler(web_contents, cert_error, | |
| 148 ssl_info, request_url, | |
| 149 options_mask, callback)); | |
| 150 } | |
| 151 | |
| 152 void SSLErrorHandler::OnTimerExpired() { | |
| 153 ShowSSLInterstitial(); | |
| 154 } | |
| 155 | |
| 156 void SSLErrorHandler::CheckForCaptivePortal() { | |
| 157 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) | |
| 158 Profile* profile = Profile::FromBrowserContext( | |
| 159 web_contents_->GetBrowserContext()); | |
| 160 CaptivePortalService* captive_portal_service = | |
| 161 CaptivePortalServiceFactory::GetForProfile(profile); | |
| 162 captive_portal_service->DetectCaptivePortal(); | |
| 163 #else | |
| 164 NOTREACHED(); | |
| 165 #endif | |
| 166 } | |
| 167 | |
| 168 void SSLErrorHandler::ShowCaptivePortalInterstitial() { | |
| 169 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) | |
| 170 // Show captive portal blocking page. The interstitial owns the blocking page. | |
| 171 RecordUMA(SSLBlockingPage::IsOverridable(options_mask_) ? | |
| 172 SHOW_CAPTIVE_PORTAL_INTERSTITIAL_OVERRIDABLE : | |
| 173 SHOW_CAPTIVE_PORTAL_INTERSTITIAL); | |
| 174 (new CaptivePortalBlockingPage(web_contents_, request_url_))->Show(); | |
| 175 // Once an interstitial is displayed, no need to keep the handler around. | |
| 176 // This is the equivalent of "delete this". | |
| 177 web_contents_->RemoveUserData(UserDataKey()); | |
| 178 #else | |
| 179 NOTREACHED(); | |
| 180 #endif | |
| 181 } | |
| 182 | |
| 183 void SSLErrorHandler::ShowSSLInterstitial() { | |
| 184 // Show SSL blocking page. The interstitial owns the blocking page. | |
| 185 RecordUMA(SSLBlockingPage::IsOverridable(options_mask_) ? | |
| 186 SHOW_SSL_INTERSTITIAL_OVERRIDABLE : | |
| 187 SHOW_SSL_INTERSTITIAL); | |
| 188 (new SSLBlockingPage(web_contents_, cert_error_, ssl_info_, request_url_, | |
| 189 options_mask_, callback_))->Show(); | |
| 190 // Once an interstitial is displayed, no need to keep the handler around. | |
| 191 // This is the equivalent of "delete this". | |
| 192 web_contents_->RemoveUserData(UserDataKey()); | |
| 193 } | |
| 194 | |
| 195 void SSLErrorHandler::Observe( | |
| 196 int type, | |
| 197 const content::NotificationSource& source, | |
| 198 const content::NotificationDetails& details) { | |
| 199 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) | |
| 200 if (type == chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT) { | |
| 201 timer_.Stop(); | |
| 202 CaptivePortalService::Results* results = | |
| 203 content::Details<CaptivePortalService::Results>(details).ptr(); | |
| 204 if (results->result == captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL) | |
| 205 ShowCaptivePortalInterstitial(); | |
| 206 else | |
| 207 ShowSSLInterstitial(); | |
| 208 } | |
| 209 #endif | |
| 210 } | |
| OLD | NEW |