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; | |
Bernhard Bauer
2014/12/18 18:10:06
Add an "S" or "Seconds" suffix to clarify the unit
meacer
2014/12/19 02:42:24
Done.
| |
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 // static | |
49 base::TimeDelta SSLErrorHandler::interstitial_display_delay_ = | |
Bernhard Bauer
2014/12/18 18:10:06
You could probably put these into the anonymous na
meacer
2014/12/19 02:42:24
Done.
| |
50 base::TimeDelta::FromSeconds(kDefaultInterstitialDisplayDelay); | |
51 | |
52 // static | |
53 SSLErrorHandler::TimerFiredCallback* SSLErrorHandler::timer_fired_callback_ = | |
54 NULL; | |
Bernhard Bauer
2014/12/18 18:10:06
nullptr
meacer
2014/12/19 02:42:24
Done.
| |
55 | |
56 SSLErrorHandler::SSLErrorHandler(content::WebContents* web_contents, | |
57 int cert_error, | |
58 const net::SSLInfo& ssl_info, | |
59 const GURL& request_url, | |
60 const int options_mask, | |
61 const base::Callback<void(bool)>& callback) | |
62 : content::WebContentsObserver(web_contents), | |
63 cert_error_(cert_error), | |
64 ssl_info_(ssl_info), | |
65 request_url_(request_url), | |
66 options_mask_(options_mask), | |
67 callback_(callback) { | |
68 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) | |
69 Profile* profile = Profile::FromBrowserContext( | |
70 web_contents->GetBrowserContext()); | |
71 registrar_.Add(this, | |
72 chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT, | |
73 content::Source<Profile>(profile)); | |
74 #endif | |
75 } | |
76 | |
77 SSLErrorHandler::~SSLErrorHandler() { | |
78 } | |
79 | |
80 void SSLErrorHandler::HandleSSLError( | |
81 content::WebContents* web_contents, | |
82 int cert_error, | |
83 const net::SSLInfo& ssl_info, | |
84 const GURL& request_url, | |
85 int options_mask, | |
86 const base::Callback<void(bool)>& callback) { | |
87 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) | |
88 CaptivePortalTabHelper* captive_portal_tab_helper = | |
89 CaptivePortalTabHelper::FromWebContents(web_contents); | |
90 if (captive_portal_tab_helper) { | |
91 captive_portal_tab_helper->OnSSLCertError(ssl_info); | |
92 } | |
93 #endif | |
94 (new SSLErrorHandler(web_contents, cert_error, ssl_info, request_url, | |
95 options_mask, callback))->StartHandlingError(); | |
96 } | |
97 | |
98 void SSLErrorHandler::StartHandlingError() { | |
99 RecordUMA(HANDLE_ALL); | |
100 | |
101 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) | |
102 CheckForCaptivePortal(); | |
103 timer_.Start(FROM_HERE, interstitial_display_delay_, | |
104 base::Bind(&SSLErrorHandler::OnTimerExpired, | |
105 base::Unretained(this))); | |
106 if (timer_fired_callback_ && !timer_fired_callback_->is_null()) | |
Bernhard Bauer
2014/12/18 18:10:06
Couldn't we simply make it an error to set |timer_
meacer
2014/12/19 02:42:24
Added the check to SetInterstitialTimerFiredCallba
| |
107 timer_fired_callback_->Run(web_contents()); | |
108 return; | |
109 #endif | |
Bernhard Bauer
2014/12/18 18:10:06
You could put the ShowSSLInterstitial() call into
meacer
2014/12/19 02:42:24
Hmm, there was some other logic inside the #if and
| |
110 // Display an SSL interstitial. | |
111 ShowSSLInterstitial(); | |
112 } | |
113 | |
114 void SSLErrorHandler::OnTimerExpired() { | |
115 ShowSSLInterstitial(); | |
116 } | |
117 | |
118 void SSLErrorHandler::CheckForCaptivePortal() { | |
119 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) | |
120 Profile* profile = Profile::FromBrowserContext( | |
121 web_contents()->GetBrowserContext()); | |
122 CaptivePortalService* captive_portal_service = | |
123 CaptivePortalServiceFactory::GetForProfile(profile); | |
124 captive_portal_service->DetectCaptivePortal(); | |
125 #else | |
126 NOTREACHED(); | |
127 #endif | |
128 } | |
129 | |
130 void SSLErrorHandler::ShowCaptivePortalInterstitial() { | |
131 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) | |
132 // Show captive portal blocking page. The interstitial owns the blocking page. | |
133 RecordUMA(SSLBlockingPage::IsOverridable(options_mask_) ? | |
134 SHOW_CAPTIVE_PORTAL_INTERSTITIAL_OVERRIDABLE : | |
135 SHOW_CAPTIVE_PORTAL_INTERSTITIAL); | |
136 (new CaptivePortalBlockingPage(web_contents(), request_url_))->Show(); | |
137 delete this; | |
138 #else | |
139 NOTREACHED(); | |
140 #endif | |
141 } | |
142 | |
143 void SSLErrorHandler::ShowSSLInterstitial() { | |
144 // Show SSL blocking page. The interstitial owns the blocking page. | |
145 RecordUMA(SSLBlockingPage::IsOverridable(options_mask_) ? | |
146 SHOW_SSL_INTERSTITIAL_OVERRIDABLE : | |
147 SHOW_SSL_INTERSTITIAL); | |
148 (new SSLBlockingPage(web_contents(), cert_error_, ssl_info_, request_url_, | |
149 options_mask_, callback_))->Show(); | |
150 delete this; | |
151 } | |
152 | |
153 void SSLErrorHandler::Observe( | |
154 int type, | |
155 const content::NotificationSource& source, | |
156 const content::NotificationDetails& details) { | |
157 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION) | |
158 if (type == chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT) { | |
159 timer_.Stop(); | |
160 CaptivePortalService::Results* results = | |
161 content::Details<CaptivePortalService::Results>(details).ptr(); | |
162 if (results->result == captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL) | |
163 ShowCaptivePortalInterstitial(); | |
164 else | |
165 ShowSSLInterstitial(); | |
166 } | |
167 #endif | |
168 } | |
169 | |
170 void SSLErrorHandler::DidStartNavigationToPendingEntry( | |
171 const GURL& url, | |
172 content::NavigationController::ReloadType reload_type) { | |
173 delete this; | |
174 } | |
175 | |
176 void SSLErrorHandler::DidStopLoading( | |
177 content::RenderViewHost* render_view_host) { | |
178 delete this; | |
179 } | |
180 | |
181 void SSLErrorHandler::WebContentsDestroyed() { | |
182 delete this; | |
Bernhard Bauer
2014/12/18 18:10:06
Another idea: If this class's lifetime is tied to
meacer
2014/12/19 02:42:24
I thought of this before but never did it. Done no
| |
183 } | |
OLD | NEW |