Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / captive_portal / captive_portal_tab_reloader.cc
blobd6f38fa58eadeb7d159e911fba25c769531ee981
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"
7 #include "base/bind.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;
22 namespace {
24 // The time to wait for a slow loading SSL page before triggering a captive
25 // portal check.
26 const int kDefaultSlowSSLTimeSeconds = 30;
28 // Returns true if an SSL request resulting in |error| may indicate a captive
29 // portal.
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)
33 return true;
35 // May be returned when a captive portal lets SSL requests connect, but
36 // disconnects Chrome after Chrome starts SSL negotiation, or sends an
37 // HTTP response.
38 if (error == net::ERR_SSL_PROTOCOL_ERROR)
39 return true;
41 return false;
44 } // namespace
46 CaptivePortalTabReloader::CaptivePortalTabReloader(
47 Profile* profile,
48 content::WebContents* web_contents,
49 const OpenLoginTabCallback& open_login_tab_callback)
50 : profile_(profile),
51 web_contents_(web_contents),
52 state_(STATE_NONE),
53 provisional_main_frame_load_(false),
54 ssl_url_in_redirect_chain_(false),
55 slow_ssl_load_time_(
56 base::TimeDelta::FromSeconds(kDefaultSlowSSLTimeSeconds)),
57 open_login_tab_callback_(open_login_tab_callback),
58 weak_factory_(this) {
61 CaptivePortalTabReloader::~CaptivePortalTabReloader() {
64 void CaptivePortalTabReloader::OnLoadStart(bool is_ssl) {
65 provisional_main_frame_load_ = true;
66 ssl_url_in_redirect_chain_ = is_ssl;
68 SetState(STATE_NONE);
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
73 // protocol.
74 if (is_ssl)
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)
83 return;
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.
91 SetState(STATE_NONE);
92 return;
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) {
98 OnSlowSSLConnect();
99 return;
102 // If the tab needs to reload, do so asynchronously, to avoid reentrancy
103 // issues.
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);
120 if (!is_ssl)
121 return;
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();
138 return;
141 switch (state_) {
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);
152 return;
154 SetState(STATE_NONE);
155 return;
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
160 // timeout commits.
161 SetState(STATE_NEEDS_RELOAD);
162 return;
164 case STATE_NEEDS_RELOAD:
165 case STATE_NONE:
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.
169 return;
171 default:
172 NOTREACHED();
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();
193 } else {
194 DCHECK(!slow_ssl_load_timer_.IsRunning());
197 // Check for unexpected state transitions.
198 switch (state_) {
199 case STATE_NONE:
200 DCHECK(new_state == STATE_NONE ||
201 new_state == STATE_TIMER_RUNNING);
202 break;
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);
207 break;
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);
212 break;
213 case STATE_BROKEN_BY_PORTAL:
214 DCHECK(new_state == STATE_NONE ||
215 new_state == STATE_NEEDS_RELOAD);
216 break;
217 case STATE_NEEDS_RELOAD:
218 DCHECK_EQ(STATE_NONE, new_state);
219 break;
220 default:
221 NOTREACHED();
222 break;
225 state_ = new_state;
227 switch (state_) {
228 case STATE_TIMER_RUNNING:
229 slow_ssl_load_timer_.Start(
230 FROM_HERE,
231 slow_ssl_load_time_,
232 this,
233 &CaptivePortalTabReloader::OnSlowSSLConnect);
234 break;
236 case STATE_MAYBE_BROKEN_BY_PORTAL:
237 CheckForCaptivePortal();
238 break;
240 case STATE_NEEDS_RELOAD:
241 // Try to reload the tab now.
242 ReloadTabIfNeeded();
243 break;
245 default:
246 break;
250 void CaptivePortalTabReloader::ReloadTabIfNeeded() {
251 // If the page no longer needs to be reloaded, do nothing.
252 if (state_ != STATE_NEEDS_RELOAD)
253 return;
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_)) {
260 return;
263 SetState(STATE_NONE);
264 ReloadTab();
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_);
280 if (service)
281 service->DetectCaptivePortal();