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/message_loop/message_loop.h"
10 #include "chrome/browser/captive_portal/captive_portal_service.h"
11 #include "chrome/browser/captive_portal/captive_portal_service_factory.h"
12 #include "content/public/browser/interstitial_page.h"
13 #include "content/public/browser/navigation_controller.h"
14 #include "content/public/browser/navigation_entry.h"
15 #include "content/public/browser/web_contents.h"
16 #include "net/base/net_errors.h"
18 using captive_portal::CaptivePortalResult
;
22 // The time to wait for a slow loading SSL page before triggering a captive
24 const int kDefaultSlowSSLTimeSeconds
= 30;
26 // Returns true if an SSL request resulting in |error| may indicate a captive
28 bool SslNetErrorMayImplyCaptivePortal(int error
) {
29 // May be returned when a captive portal silently blocks an SSL request.
30 if (error
== net::ERR_CONNECTION_TIMED_OUT
)
33 // May be returned when a captive portal lets SSL requests connect, but
34 // disconnects Chrome after Chrome starts SSL negotiation, or sends an
36 if (error
== net::ERR_SSL_PROTOCOL_ERROR
)
44 CaptivePortalTabReloader::CaptivePortalTabReloader(
46 content::WebContents
* web_contents
,
47 const OpenLoginTabCallback
& open_login_tab_callback
)
49 web_contents_(web_contents
),
51 provisional_main_frame_load_(false),
52 ssl_url_in_redirect_chain_(false),
54 base::TimeDelta::FromSeconds(kDefaultSlowSSLTimeSeconds
)),
55 open_login_tab_callback_(open_login_tab_callback
),
59 CaptivePortalTabReloader::~CaptivePortalTabReloader() {
62 void CaptivePortalTabReloader::OnLoadStart(bool is_ssl
) {
63 provisional_main_frame_load_
= true;
64 ssl_url_in_redirect_chain_
= is_ssl
;
68 // Start the slow load timer for SSL pages.
69 // TODO(mmenke): Should this look at the port instead? The reason the
70 // request never connects is because of the port, not the
73 SetState(STATE_TIMER_RUNNING
);
76 void CaptivePortalTabReloader::OnLoadCommitted(int net_error
) {
77 provisional_main_frame_load_
= false;
78 ssl_url_in_redirect_chain_
= false;
80 if (state_
== STATE_NONE
)
83 // If |net_error| is not an error code that could indicate there's a captive
84 // portal, reset the state.
85 if (!SslNetErrorMayImplyCaptivePortal(net_error
)) {
86 // TODO(mmenke): If the new URL is the same as the old broken URL, and the
87 // request succeeds, should probably trigger another
88 // captive portal check.
93 // The page returned an error out before the timer triggered. Go ahead and
94 // try to detect a portal now, rather than waiting for the timer.
95 if (state_
== STATE_TIMER_RUNNING
) {
100 // If the tab needs to reload, do so asynchronously, to avoid reentrancy
102 if (state_
== STATE_NEEDS_RELOAD
) {
103 base::MessageLoop::current()->PostTask(
105 base::Bind(&CaptivePortalTabReloader::ReloadTabIfNeeded
,
106 weak_factory_
.GetWeakPtr()));
110 void CaptivePortalTabReloader::OnAbort() {
111 provisional_main_frame_load_
= false;
112 ssl_url_in_redirect_chain_
= false;
114 SetState(STATE_NONE
);
117 void CaptivePortalTabReloader::OnRedirect(bool is_ssl
) {
118 SetState(STATE_NONE
);
121 // Only start the SSL timer running if no SSL URL has been seen in the current
122 // redirect chain. If we've already successfully downloaded one SSL URL,
123 // assume we're not behind a captive portal.
124 if (!ssl_url_in_redirect_chain_
)
125 SetState(STATE_TIMER_RUNNING
);
126 ssl_url_in_redirect_chain_
= true;
129 void CaptivePortalTabReloader::OnCaptivePortalResults(
130 CaptivePortalResult previous_result
,
131 CaptivePortalResult result
) {
132 if (result
== captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL
) {
133 if (state_
== STATE_MAYBE_BROKEN_BY_PORTAL
) {
134 SetState(STATE_BROKEN_BY_PORTAL
);
135 MaybeOpenCaptivePortalLoginTab();
141 case STATE_MAYBE_BROKEN_BY_PORTAL
:
142 case STATE_TIMER_RUNNING
:
143 // If the previous result was BEHIND_CAPTIVE_PORTAL, and the state is
144 // either STATE_MAYBE_BROKEN_BY_PORTAL or STATE_TIMER_RUNNING, reload the
145 // tab. In the latter case, the tab has yet to commit, but is an SSL
146 // page, so if the page ends up at an error caused by a captive portal, it
147 // will be reloaded. If not, the state will just be reset. The helps in
148 // the case that a user tries to reload a tab, and then quickly logs in.
149 if (previous_result
== captive_portal::RESULT_BEHIND_CAPTIVE_PORTAL
) {
150 SetState(STATE_NEEDS_RELOAD
);
153 SetState(STATE_NONE
);
156 case STATE_BROKEN_BY_PORTAL
:
157 // Either reload the tab now, if an error page has already been committed
158 // or an interstitial is being displayed, or reload it if and when a
160 SetState(STATE_NEEDS_RELOAD
);
163 case STATE_NEEDS_RELOAD
:
165 // If the tab needs to reload or is in STATE_NONE, do nothing. The reload
166 // case shouldn't be very common, since it only lasts until a tab times
167 // out, but it's still possible.
175 void CaptivePortalTabReloader::OnSSLCertError(const net::SSLInfo
& ssl_info
) {
176 // TODO(mmenke): Figure out if any cert errors should be ignored. The
177 // most common errors when behind captive portals are likely
178 // ERR_CERT_COMMON_NAME_INVALID and ERR_CERT_AUTHORITY_INVALID. It's unclear
179 // if captive portals cause any others.
180 if (state_
== STATE_TIMER_RUNNING
)
181 SetState(STATE_MAYBE_BROKEN_BY_PORTAL
);
184 void CaptivePortalTabReloader::OnSlowSSLConnect() {
185 SetState(STATE_MAYBE_BROKEN_BY_PORTAL
);
188 void CaptivePortalTabReloader::SetState(State new_state
) {
189 // Stop the timer even when old and new states are the same.
190 if (state_
== STATE_TIMER_RUNNING
) {
191 slow_ssl_load_timer_
.Stop();
193 DCHECK(!slow_ssl_load_timer_
.IsRunning());
196 // Check for unexpected state transitions.
199 DCHECK(new_state
== STATE_NONE
||
200 new_state
== STATE_TIMER_RUNNING
);
202 case STATE_TIMER_RUNNING
:
203 DCHECK(new_state
== STATE_NONE
||
204 new_state
== STATE_MAYBE_BROKEN_BY_PORTAL
||
205 new_state
== STATE_NEEDS_RELOAD
);
207 case STATE_MAYBE_BROKEN_BY_PORTAL
:
208 DCHECK(new_state
== STATE_NONE
||
209 new_state
== STATE_BROKEN_BY_PORTAL
||
210 new_state
== STATE_NEEDS_RELOAD
);
212 case STATE_BROKEN_BY_PORTAL
:
213 DCHECK(new_state
== STATE_NONE
||
214 new_state
== STATE_NEEDS_RELOAD
);
216 case STATE_NEEDS_RELOAD
:
217 DCHECK_EQ(STATE_NONE
, new_state
);
227 case STATE_TIMER_RUNNING
:
228 slow_ssl_load_timer_
.Start(
232 &CaptivePortalTabReloader::OnSlowSSLConnect
);
235 case STATE_MAYBE_BROKEN_BY_PORTAL
:
236 CheckForCaptivePortal();
239 case STATE_NEEDS_RELOAD
:
240 // Try to reload the tab now.
249 void CaptivePortalTabReloader::ReloadTabIfNeeded() {
250 // If the page no longer needs to be reloaded, do nothing.
251 if (state_
!= STATE_NEEDS_RELOAD
)
254 // If there's still a provisional load going, do nothing unless there's an
255 // interstitial page. Reloading when displaying an interstitial page will
256 // reload the underlying page, even if it hasn't yet committed.
257 if (provisional_main_frame_load_
&&
258 !content::InterstitialPage::GetInterstitialPage(web_contents_
)) {
262 SetState(STATE_NONE
);
266 void CaptivePortalTabReloader::ReloadTab() {
267 content::NavigationController
* controller
= &web_contents_
->GetController();
268 if (!controller
->GetActiveEntry()->GetHasPostData())
269 controller
->Reload(true);
272 void CaptivePortalTabReloader::MaybeOpenCaptivePortalLoginTab() {
273 open_login_tab_callback_
.Run();
276 void CaptivePortalTabReloader::CheckForCaptivePortal() {
277 CaptivePortalService
* service
=
278 CaptivePortalServiceFactory::GetForProfile(profile_
);
280 service
->DetectCaptivePortal();