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 // Decides between showing an SSL warning, showing a captive portal interstitial
33 // or redirecting to a name-mismatch suggested URL. This is done by delaying the
34 // display of the interstitial for a few seconds (2 by default), and waiting for
35 // name-mismatch suggested URL or a captive portal result to arrive during this
36 // window. If there is a name mismatch error and a corresponding suggested URL
37 // available result arrives in this window, the user is redirected to the
38 // suggested URL. Failing that, if a captive portal detected result arrives in
39 // the same time window, a captive portal error page is shown. Otherwise, an
40 // SSL interstitial is shown.
42 // This class should only be used on the UI thread because its implementation
43 // uses captive_portal::CaptivePortalService which can only be accessed on the
45 class SSLErrorHandler
: public content::WebContentsUserData
<SSLErrorHandler
>,
46 public content::WebContentsObserver
,
47 public content::NotificationObserver
{
49 typedef base::Callback
<void(content::WebContents
*)> TimerStartedCallback
;
51 // Entry point for the class. The parameters are the same as SSLBlockingPage
53 static void HandleSSLError(content::WebContents
* web_contents
,
55 const net::SSLInfo
& ssl_info
,
56 const GURL
& request_url
,
58 scoped_ptr
<SSLCertReporter
> ssl_cert_reporter
,
59 const base::Callback
<void(bool)>& callback
);
61 static void SetInterstitialDelayForTest(base::TimeDelta delay
);
63 // The callback pointer must remain valid for the duration of error handling.
64 static void SetInterstitialTimerStartedCallbackForTest(
65 TimerStartedCallback
* callback
);
68 // The parameters are the same as SSLBlockingPage's constructor.
69 SSLErrorHandler(content::WebContents
* web_contents
,
71 const net::SSLInfo
& ssl_info
,
72 const GURL
& request_url
,
74 scoped_ptr
<SSLCertReporter
> ssl_cert_reporter
,
75 const base::Callback
<void(bool)>& callback
);
77 ~SSLErrorHandler() override
;
79 // Called when an SSL cert error is encountered. Triggers a captive portal
80 // check and fires a one shot timer to wait for a "captive portal detected"
82 void StartHandlingError();
83 const base::OneShotTimer
<SSLErrorHandler
>& get_timer() const {
87 // These are virtual for tests:
88 virtual void CheckForCaptivePortal();
89 virtual bool GetSuggestedUrl(const std::vector
<std::string
>& dns_names
,
90 GURL
* suggested_url
) const;
91 virtual void CheckSuggestedUrl(const GURL
& suggested_url
);
92 virtual void NavigateToSuggestedURL(const GURL
& suggested_url
);
93 virtual bool IsErrorOverridable() const;
94 virtual void ShowCaptivePortalInterstitial(const GURL
& landing_url
);
95 virtual void ShowSSLInterstitial();
97 // Gets the result of whether the suggested URL is valid. Displays
98 // common name mismatch interstitial or ssl interstitial accordingly.
99 void CommonNameMismatchHandlerCallback(
100 const CommonNameMismatchHandler::SuggestedUrlCheckResult
& result
,
101 const GURL
& suggested_url
);
104 // content::NotificationObserver:
107 const content::NotificationSource
& source
,
108 const content::NotificationDetails
& details
) override
;
110 // content::WebContentsObserver:
111 void DidStartNavigationToPendingEntry(
113 content::NavigationController::ReloadType reload_type
) override
;
115 // content::WebContentsObserver:
116 void NavigationStopped() override
;
118 // Deletes the SSLErrorHandler. This method is called when the page
119 // load stops or when there is a new navigation.
120 void DeleteSSLErrorHandler();
122 content::WebContents
* web_contents_
;
123 const int cert_error_
;
124 const net::SSLInfo ssl_info_
;
125 const GURL request_url_
;
126 const int options_mask_
;
127 base::Callback
<void(bool)> callback_
;
128 Profile
* const profile_
;
130 content::NotificationRegistrar registrar_
;
131 base::OneShotTimer
<SSLErrorHandler
> timer_
;
133 scoped_ptr
<CommonNameMismatchHandler
> common_name_mismatch_handler_
;
135 scoped_ptr
<SSLCertReporter
> ssl_cert_reporter_
;
137 DISALLOW_COPY_AND_ASSIGN(SSLErrorHandler
);
140 #endif // CHROME_BROWSER_SSL_SSL_ERROR_HANDLER_H_