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.
5 #ifndef CHROME_BROWSER_SSL_SSL_ERROR_HANDLER_H_
6 #define CHROME_BROWSER_SSL_SSL_ERROR_HANDLER_H_
10 #include "base/callback_forward.h"
11 #include "base/macros.h"
12 #include "base/timer/timer.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/ssl/ssl_cert_reporter.h"
15 #include "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h"
17 #include "content/public/browser/web_contents_observer.h"
18 #include "content/public/browser/web_contents_user_data.h"
19 #include "net/ssl/ssl_info.h"
27 // This class is responsible for deciding whether to show an SSL warning or a
28 // captive portal error page. It makes this decision by delaying the display of
29 // SSL interstitial for a few seconds (2 by default), and waiting for a captive
30 // portal result to arrive during this window. If a captive portal detected
31 // result arrives in this window, a captive portal error page is shown.
32 // Otherwise, an SSL interstitial is shown.
34 // An SSLErrorHandler is associated with a particular WebContents, and is
35 // deleted if the WebContents is destroyed, or an interstitial is displayed.
36 // It should only be used on the UI thread because its implementation uses
37 // captive_portal::CaptivePortalService which can only be accessed on the UI
39 class SSLErrorHandler
: public content::WebContentsUserData
<SSLErrorHandler
>,
40 public content::WebContentsObserver
,
41 public content::NotificationObserver
{
43 // Type of the delay to display the SSL interstitial.
44 enum InterstitialDelayType
{
45 NORMAL
, // Default interstitial timer delay used in production.
46 NONE
, // No interstitial timer delay (i.e. zero), used in tests.
47 LONG
// Very long interstitial timer delay (ie. an hour), used in tests.
50 static void HandleSSLError(content::WebContents
* web_contents
,
52 const net::SSLInfo
& ssl_info
,
53 const GURL
& request_url
,
55 scoped_ptr
<SSLCertReporter
> ssl_cert_reporter
,
56 const base::Callback
<void(bool)>& callback
);
58 static void SetInterstitialDelayTypeForTest(InterstitialDelayType delay
);
60 typedef base::Callback
<void(content::WebContents
*)> TimerStartedCallback
;
61 static void SetInterstitialTimerStartedCallbackForTest(
62 TimerStartedCallback
* callback
);
65 SSLErrorHandler(content::WebContents
* web_contents
,
67 const net::SSLInfo
& ssl_info
,
68 const GURL
& request_url
,
70 scoped_ptr
<SSLCertReporter
> ssl_cert_reporter
,
71 const base::Callback
<void(bool)>& callback
);
73 ~SSLErrorHandler() override
;
75 // Called when an SSL cert error is encountered. Triggers a captive portal
76 // check and fires a one shot timer to wait for a "captive portal detected"
78 void StartHandlingError();
79 const base::OneShotTimer
<SSLErrorHandler
>& get_timer() const {
84 // Callback for the one-shot timer. When the timer expires, an SSL error is
85 // immediately displayed.
86 void OnTimerExpired();
88 // These are virtual for tests:
89 virtual void CheckForCaptivePortal();
90 virtual void ShowCaptivePortalInterstitial(const GURL
& landing_url
);
91 virtual void ShowSSLInterstitial();
93 // content::NotificationObserver:
96 const content::NotificationSource
& source
,
97 const content::NotificationDetails
& details
) override
;
99 // content::WebContentsObserver:
100 void DidStartNavigationToPendingEntry(
102 content::NavigationController::ReloadType reload_type
) override
;
104 content::WebContents
* web_contents_
;
105 const int cert_error_
;
106 const net::SSLInfo ssl_info_
;
107 const GURL request_url_
;
108 const int options_mask_
;
109 base::Callback
<void(bool)> callback_
;
111 content::NotificationRegistrar registrar_
;
112 base::OneShotTimer
<SSLErrorHandler
> timer_
;
114 scoped_ptr
<SSLCertReporter
> ssl_cert_reporter_
;
116 DISALLOW_COPY_AND_ASSIGN(SSLErrorHandler
);
119 #endif // CHROME_BROWSER_SSL_SSL_ERROR_HANDLER_H_