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"
24 class CommonNameMismatchHandler
;
36 // This class is responsible for deciding what type of interstitial to show for
37 // an SSL validation error. The display of the interstitial might be delayed by
38 // a few seconds (2 by default) while trying to determine the cause of the
39 // error. During this window, the class will: check for a clock error, wait for
40 // a name-mismatch suggested URL, or wait for a captive portal result to arrive.
41 // If there is a name mismatch error and a corresponding suggested URL
42 // result arrives in this window, the user is redirected to the suggested URL.
43 // Failing that, if a captive portal detected result arrives in the time window,
44 // a captive portal error page is shown. If none of these potential error
45 // causes match, an SSL interstitial is shown.
47 // This class should only be used on the UI thread because its implementation
48 // uses captive_portal::CaptivePortalService which can only be accessed on the
50 class SSLErrorHandler
: public content::WebContentsUserData
<SSLErrorHandler
>,
51 public content::WebContentsObserver
,
52 public content::NotificationObserver
{
54 typedef base::Callback
<void(content::WebContents
*)> TimerStartedCallback
;
56 // Entry point for the class. The parameters are the same as SSLBlockingPage
58 static void HandleSSLError(content::WebContents
* web_contents
,
60 const net::SSLInfo
& ssl_info
,
61 const GURL
& request_url
,
63 scoped_ptr
<SSLCertReporter
> ssl_cert_reporter
,
64 const base::Callback
<void(bool)>& callback
);
67 static void SetInterstitialDelayForTest(base::TimeDelta delay
);
68 // The callback pointer must remain valid for the duration of error handling.
69 static void SetInterstitialTimerStartedCallbackForTest(
70 TimerStartedCallback
* callback
);
71 static void SetClockForTest(base::Clock
* testing_clock
);
74 // The parameters are the same as SSLBlockingPage's constructor.
75 SSLErrorHandler(content::WebContents
* web_contents
,
77 const net::SSLInfo
& ssl_info
,
78 const GURL
& request_url
,
80 scoped_ptr
<SSLCertReporter
> ssl_cert_reporter
,
81 const base::Callback
<void(bool)>& callback
);
83 ~SSLErrorHandler() override
;
85 // Called when an SSL cert error is encountered. Triggers a captive portal
86 // check and fires a one shot timer to wait for a "captive portal detected"
88 void StartHandlingError();
89 const base::OneShotTimer
<SSLErrorHandler
>& get_timer() const {
93 // These are virtual for tests:
94 virtual void CheckForCaptivePortal();
95 virtual bool GetSuggestedUrl(const std::vector
<std::string
>& dns_names
,
96 GURL
* suggested_url
) const;
97 virtual void CheckSuggestedUrl(const GURL
& suggested_url
);
98 virtual void NavigateToSuggestedURL(const GURL
& suggested_url
);
99 virtual bool IsErrorOverridable() const;
100 virtual void ShowCaptivePortalInterstitial(const GURL
& landing_url
);
101 virtual void ShowSSLInterstitial();
103 void ShowBadClockInterstitial(const base::Time
& now
);
105 // Gets the result of whether the suggested URL is valid. Displays
106 // common name mismatch interstitial or ssl interstitial accordingly.
107 void CommonNameMismatchHandlerCallback(
108 const CommonNameMismatchHandler::SuggestedUrlCheckResult
& result
,
109 const GURL
& suggested_url
);
112 // content::NotificationObserver:
115 const content::NotificationSource
& source
,
116 const content::NotificationDetails
& details
) override
;
118 // content::WebContentsObserver:
119 void DidStartNavigationToPendingEntry(
121 content::NavigationController::ReloadType reload_type
) override
;
123 // content::WebContentsObserver:
124 void NavigationStopped() override
;
126 // Deletes the SSLErrorHandler. This method is called when the page
127 // load stops or when there is a new navigation.
128 void DeleteSSLErrorHandler();
130 content::WebContents
* web_contents_
;
131 const int cert_error_
;
132 const net::SSLInfo ssl_info_
;
133 const GURL request_url_
;
134 const int options_mask_
;
135 base::Callback
<void(bool)> callback_
;
136 Profile
* const profile_
;
138 content::NotificationRegistrar registrar_
;
139 base::OneShotTimer
<SSLErrorHandler
> timer_
;
141 scoped_ptr
<CommonNameMismatchHandler
> common_name_mismatch_handler_
;
143 scoped_ptr
<SSLCertReporter
> ssl_cert_reporter_
;
145 DISALLOW_COPY_AND_ASSIGN(SSLErrorHandler
);
148 #endif // CHROME_BROWSER_SSL_SSL_ERROR_HANDLER_H_