1 // Copyright (c) 2012 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 #include "chrome/browser/captive_portal/captive_portal_tab_reloader.h"
8 #include "base/callback.h"
9 #include "base/location.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "chrome/browser/captive_portal/captive_portal_service.h"
13 #include "chrome/browser/captive_portal/captive_portal_service_factory.h"
14 #include "content/public/browser/interstitial_page.h"
15 #include "content/public/browser/navigation_controller.h"
16 #include "content/public/browser/navigation_entry.h"
17 #include "content/public/browser/web_contents.h"
18 #include "net/base/net_errors.h"
20 using captive_portal::CaptivePortalResult
;
24 // The time to wait for a slow loading SSL page before triggering a captive
26 const int kDefaultSlowSSLTimeSeconds
= 30;
28 // Returns true if an SSL request resulting in |error| may indicate a captive
30 bool SslNetErrorMayImplyCaptivePortal(int error
) {
31 // May be returned when a captive portal silently blocks an SSL request.
32 if (error
== net::ERR_CONNECTION_TIMED_OUT
)
35 // May be returned when a captive portal lets SSL requests connect, but
36 // disconnects Chrome after Chrome starts SSL negotiation, or sends an
38 if (error
== net::ERR_SSL_PROTOCOL_ERROR
)
46 CaptivePortalTabReloader::CaptivePortalTabReloader(
48 content::WebContents
* web_contents
,
49 const OpenLoginTabCallback
& open_login_tab_callback
)
51 web_contents_(web_contents
),
53 provisional_main_frame_load_(false),
54 ssl_url_in_redirect_chain_(false),
56 base::TimeDelta::FromSeconds(kDefaultSlowSSLTimeSeconds
)),
57 open_login_tab_callback_(open_login_tab_callback
),
61 CaptivePortalTabReloader::~CaptivePortalTabReloader() {
64 void CaptivePortalTabReloader::OnLoadStart(bool is_ssl
) {
65 provisional_main_frame_load_
= true;
66 ssl_url_in_redirect_chain_
= is_ssl
;
70 // Start the slow load timer for SSL pages.
71 // TODO(mmenke): Should this look at the port instead? The reason the
72 // request never connects is because of the port, not the
75 SetState(STATE_TIMER_RUNNING
);
78 void CaptivePortalTabReloader::OnLoadCommitted(int net_error
) {
79 provisional_main_frame_load_
= false;
80 ssl_url_in_redirect_chain_
= false;
82 if (state_
== STATE_NONE
)
85 // If |net_error| is not an error code that could indicate there's a captive
86 // portal, reset the state.
87 if (!SslNetErrorMayImplyCaptivePortal(net_error
)) {
88 // TODO(mmenke): If the new URL is the same as the old broken URL, and the
89 // request succeeds, should probably trigger another
90 // captive portal check.
95 // The page returned an error out before the timer triggered. Go ahead and
96 // try to detect a portal now, rather than waiting for the timer.
97 if (state_
== STATE_TIMER_RUNNING
) {
102 // If the tab needs to reload, do so asynchronously, to avoid reentrancy
104 if (state_
== STATE_NEEDS_RELOAD
) {
105 base::ThreadTaskRunnerHandle::Get()->PostTask(
106 FROM_HERE
, base::Bind(&CaptivePortalTabReloader::ReloadTabIfNeeded
,
107 weak_factory_
.GetWeakPtr()));
111 void CaptivePortalTabReloader::OnAbort() {
112 provisional_main_frame_load_
= false;
113 ssl_url_in_redirect_chain_
= false;
115 SetState(STATE_NONE
);
118 void CaptivePortalTabReloader::OnRedirect(bool is_ssl
) {
119 SetState(STATE_NONE
);
122 // Only start the SSL timer running if no SSL URL has been seen in the current
123 // redirect chain. If we've already successfully downloaded one SSL URL,
124 // assume we're not behind a captive portal.
125 if (!ssl_url_in_redirect_chain_
)
126 SetState(STATE_TIMER_RUNNING
);
127 ssl_url_in_redirect_chain_
= true;
130 void CaptivePortalTabReloader::OnCaptivePortalResults(
131 CaptivePortalResult previous_result
,
132 CaptivePortalResult result
) {
133 if (result
== captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL
) {
134 if (state_
== STATE_MAYBE_BROKEN_BY_PORTAL
) {
135 SetState(STATE_BROKEN_BY_PORTAL
);
136 MaybeOpenCaptivePortalLoginTab();
142 case STATE_MAYBE_BROKEN_BY_PORTAL
:
143 case STATE_TIMER_RUNNING
:
144 // If the previous result was BEHIND_CAPTIVE_PORTAL, and the state is
145 // either STATE_MAYBE_BROKEN_BY_PORTAL or STATE_TIMER_RUNNING, reload the
146 // tab. In the latter case, the tab has yet to commit, but is an SSL
147 // page, so if the page ends up at an error caused by a captive portal, it
148 // will be reloaded. If not, the state will just be reset. The helps in
149 // the case that a user tries to reload a tab, and then quickly logs in.
150 if (previous_result
== captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL
) {
151 SetState(STATE_NEEDS_RELOAD
);
154 SetState(STATE_NONE
);
157 case STATE_BROKEN_BY_PORTAL
:
158 // Either reload the tab now, if an error page has already been committed
159 // or an interstitial is being displayed, or reload it if and when a
161 SetState(STATE_NEEDS_RELOAD
);
164 case STATE_NEEDS_RELOAD
:
166 // If the tab needs to reload or is in STATE_NONE, do nothing. The reload
167 // case shouldn't be very common, since it only lasts until a tab times
168 // out, but it's still possible.
176 void CaptivePortalTabReloader::OnSSLCertError(const net::SSLInfo
& ssl_info
) {
177 // TODO(mmenke): Figure out if any cert errors should be ignored. The
178 // most common errors when behind captive portals are likely
179 // ERR_CERT_COMMON_NAME_INVALID and ERR_CERT_AUTHORITY_INVALID. It's unclear
180 // if captive portals cause any others.
181 if (state_
== STATE_TIMER_RUNNING
)
182 SetState(STATE_MAYBE_BROKEN_BY_PORTAL
);
185 void CaptivePortalTabReloader::OnSlowSSLConnect() {
186 SetState(STATE_MAYBE_BROKEN_BY_PORTAL
);
189 void CaptivePortalTabReloader::SetState(State new_state
) {
190 // Stop the timer even when old and new states are the same.
191 if (state_
== STATE_TIMER_RUNNING
) {
192 slow_ssl_load_timer_
.Stop();
194 DCHECK(!slow_ssl_load_timer_
.IsRunning());
197 // Check for unexpected state transitions.
200 DCHECK(new_state
== STATE_NONE
||
201 new_state
== STATE_TIMER_RUNNING
);
203 case STATE_TIMER_RUNNING
:
204 DCHECK(new_state
== STATE_NONE
||
205 new_state
== STATE_MAYBE_BROKEN_BY_PORTAL
||
206 new_state
== STATE_NEEDS_RELOAD
);
208 case STATE_MAYBE_BROKEN_BY_PORTAL
:
209 DCHECK(new_state
== STATE_NONE
||
210 new_state
== STATE_BROKEN_BY_PORTAL
||
211 new_state
== STATE_NEEDS_RELOAD
);
213 case STATE_BROKEN_BY_PORTAL
:
214 DCHECK(new_state
== STATE_NONE
||
215 new_state
== STATE_NEEDS_RELOAD
);
217 case STATE_NEEDS_RELOAD
:
218 DCHECK_EQ(STATE_NONE
, new_state
);
228 case STATE_TIMER_RUNNING
:
229 slow_ssl_load_timer_
.Start(
233 &CaptivePortalTabReloader::OnSlowSSLConnect
);
236 case STATE_MAYBE_BROKEN_BY_PORTAL
:
237 CheckForCaptivePortal();
240 case STATE_NEEDS_RELOAD
:
241 // Try to reload the tab now.
250 void CaptivePortalTabReloader::ReloadTabIfNeeded() {
251 // If the page no longer needs to be reloaded, do nothing.
252 if (state_
!= STATE_NEEDS_RELOAD
)
255 // If there's still a provisional load going, do nothing unless there's an
256 // interstitial page. Reloading when displaying an interstitial page will
257 // reload the underlying page, even if it hasn't yet committed.
258 if (provisional_main_frame_load_
&&
259 !content::InterstitialPage::GetInterstitialPage(web_contents_
)) {
263 SetState(STATE_NONE
);
267 void CaptivePortalTabReloader::ReloadTab() {
268 content::NavigationController
* controller
= &web_contents_
->GetController();
269 if (!controller
->GetActiveEntry()->GetHasPostData())
270 controller
->Reload(true);
273 void CaptivePortalTabReloader::MaybeOpenCaptivePortalLoginTab() {
274 open_login_tab_callback_
.Run();
277 void CaptivePortalTabReloader::CheckForCaptivePortal() {
278 CaptivePortalService
* service
=
279 CaptivePortalServiceFactory::GetForProfile(profile_
);
281 service
->DetectCaptivePortal();