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/profiles/profile.h"
15 #include "chrome/browser/ssl/common_name_mismatch_handler.h"
16 #include "chrome/browser/ssl/ssl_cert_reporter.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "content/public/browser/web_contents_observer.h"
20 #include "content/public/browser/web_contents_user_data.h"
21 #include "net/ssl/ssl_info.h"
25 class CommonNameMismatchHandler
;
32 // This class is responsible for deciding whether to show an SSL warning or a
33 // captive portal error page. It makes this decision by delaying the display of
34 // SSL interstitial for a few seconds (2 by default), and waiting for a captive
35 // portal result to arrive during this window. If a captive portal detected
36 // result arrives in this window, a captive portal error page is shown.
37 // Otherwise, an SSL interstitial is shown.
39 // An SSLErrorHandler is associated with a particular WebContents, and is
40 // deleted if the WebContents is destroyed, or an interstitial is displayed.
41 // It should only be used on the UI thread because its implementation uses
42 // captive_portal::CaptivePortalService which can only be accessed on the UI
44 class SSLErrorHandler
: public content::WebContentsUserData
<SSLErrorHandler
>,
45 public content::WebContentsObserver
,
46 public content::NotificationObserver
{
48 // Type of the delay to display the SSL interstitial.
49 enum InterstitialDelayType
{
50 NORMAL
, // Default interstitial timer delay used in production.
51 NONE
, // No interstitial timer delay (i.e. zero), used in tests.
52 LONG
// Very long interstitial timer delay (ie. an hour), used in tests.
55 static void HandleSSLError(content::WebContents
* web_contents
,
57 const net::SSLInfo
& ssl_info
,
58 const GURL
& request_url
,
60 scoped_ptr
<SSLCertReporter
> ssl_cert_reporter
,
61 const base::Callback
<void(bool)>& callback
);
63 static void SetInterstitialDelayTypeForTest(InterstitialDelayType delay
);
65 typedef base::Callback
<void(content::WebContents
*)> TimerStartedCallback
;
66 static void SetInterstitialTimerStartedCallbackForTest(
67 TimerStartedCallback
* callback
);
69 // Gets the result of whether the suggested URL is valid. Displays
70 // common name mismatch interstitial or ssl interstitial accordingly.
71 void CommonNameMismatchHandlerCallback(
72 const CommonNameMismatchHandler::SuggestedUrlCheckResult
& result
,
73 const GURL
& suggested_url
);
76 SSLErrorHandler(content::WebContents
* web_contents
,
78 const net::SSLInfo
& ssl_info
,
79 const GURL
& request_url
,
81 scoped_ptr
<SSLCertReporter
> ssl_cert_reporter
,
82 const base::Callback
<void(bool)>& callback
);
84 ~SSLErrorHandler() override
;
86 // Called when an SSL cert error is encountered. Triggers a captive portal
87 // check and fires a one shot timer to wait for a "captive portal detected"
89 void StartHandlingError();
90 const base::OneShotTimer
<SSLErrorHandler
>& get_timer() const {
95 // Callback for the one-shot timer. When the timer expires, an SSL error is
96 // immediately displayed.
97 void OnTimerExpired();
99 // These are virtual for tests:
100 virtual void CheckForCaptivePortal();
101 virtual void ShowCaptivePortalInterstitial(const GURL
& landing_url
);
102 virtual void ShowSSLInterstitial();
103 virtual bool GetSuggestedUrl(const std::vector
<std::string
>& dns_names
,
104 GURL
* suggested_url
) const;
105 virtual void CheckSuggestedUrl(const GURL
& suggested_url
);
106 virtual void NavigateToSuggestedURL(const GURL
& suggested_url
);
107 virtual bool IsErrorOverridable() const;
109 // content::NotificationObserver:
112 const content::NotificationSource
& source
,
113 const content::NotificationDetails
& details
) override
;
115 // content::WebContentsObserver:
116 void DidStartNavigationToPendingEntry(
118 content::NavigationController::ReloadType reload_type
) override
;
120 // content::WebContentsObserver:
121 void NavigationStopped() override
;
123 // Deletes the SSLErrorHandler. This method is called when the page
124 // load stops or when there is a new navigation.
125 void DeleteSSLErrorHandler();
127 content::WebContents
* web_contents_
;
128 const int cert_error_
;
129 const net::SSLInfo ssl_info_
;
130 const GURL request_url_
;
131 const int options_mask_
;
132 base::Callback
<void(bool)> callback_
;
133 Profile
* const profile_
;
135 content::NotificationRegistrar registrar_
;
136 base::OneShotTimer
<SSLErrorHandler
> timer_
;
138 scoped_ptr
<CommonNameMismatchHandler
> common_name_mismatch_handler_
;
140 scoped_ptr
<SSLCertReporter
> ssl_cert_reporter_
;
142 DISALLOW_COPY_AND_ASSIGN(SSLErrorHandler
);
145 #endif // CHROME_BROWSER_SSL_SSL_ERROR_HANDLER_H_