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_helper.h"
8 #include "chrome/browser/captive_portal/captive_portal_login_detector.h"
9 #include "chrome/browser/captive_portal/captive_portal_service_factory.h"
10 #include "chrome/browser/captive_portal/captive_portal_tab_reloader.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_finder.h"
14 #include "chrome/browser/ui/browser_tabstrip.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/common/chrome_notification_types.h"
17 #include "content/public/browser/notification_details.h"
18 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/notification_source.h"
20 #include "content/public/browser/notification_types.h"
21 #include "content/public/browser/render_process_host.h"
22 #include "content/public/browser/render_view_host.h"
23 #include "content/public/browser/resource_request_details.h"
24 #include "content/public/browser/web_contents.h"
25 #include "net/base/net_errors.h"
26 #include "net/ssl/ssl_info.h"
28 DEFINE_WEB_CONTENTS_USER_DATA_KEY(captive_portal::CaptivePortalTabHelper
);
30 namespace captive_portal
{
32 CaptivePortalTabHelper::CaptivePortalTabHelper(
33 content::WebContents
* web_contents
)
34 : content::WebContentsObserver(web_contents
),
35 // web_contents is NULL in unit tests.
36 profile_(web_contents
? Profile::FromBrowserContext(
37 web_contents
->GetBrowserContext())
40 new CaptivePortalTabReloader(
43 base::Bind(&CaptivePortalTabHelper::OpenLoginTab
,
44 base::Unretained(this)))),
45 login_detector_(new CaptivePortalLoginDetector(profile_
)),
46 web_contents_(web_contents
),
47 pending_error_code_(net::OK
),
48 provisional_render_view_host_(NULL
) {
50 chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT
,
51 content::Source
<Profile
>(profile_
));
53 content::NOTIFICATION_RESOURCE_RECEIVED_REDIRECT
,
54 content::Source
<content::WebContents
>(web_contents
));
57 CaptivePortalTabHelper::~CaptivePortalTabHelper() {
60 void CaptivePortalTabHelper::RenderViewDeleted(
61 content::RenderViewHost
* render_view_host
) {
62 // This can happen when a cross-process navigation is aborted, either by
63 // pressing stop or by starting a new cross-process navigation that can't
64 // re-use |provisional_render_view_host_|. May also happen on a crash.
65 if (render_view_host
== provisional_render_view_host_
)
69 void CaptivePortalTabHelper::DidStartProvisionalLoadForFrame(
71 int64 parent_frame_id
,
73 const GURL
& validated_url
,
75 bool is_iframe_srcdoc
,
76 content::RenderViewHost
* render_view_host
) {
77 DCHECK(CalledOnValidThread());
83 if (provisional_render_view_host_
) {
84 // If loading an error page for a previous failure, treat this as part of
85 // the previous load. Link Doctor pages act like two error page loads in a
86 // row. The second time, provisional_render_view_host_ will be NULL.
87 if (is_error_page
&& provisional_render_view_host_
== render_view_host
)
89 // Otherwise, abort the old load.
93 provisional_render_view_host_
= render_view_host
;
94 pending_error_code_
= net::OK
;
96 tab_reloader_
->OnLoadStart(validated_url
.SchemeIsSecure());
99 void CaptivePortalTabHelper::DidCommitProvisionalLoadForFrame(
103 content::PageTransition transition_type
,
104 content::RenderViewHost
* render_view_host
) {
105 DCHECK(CalledOnValidThread());
111 if (provisional_render_view_host_
== render_view_host
) {
112 tab_reloader_
->OnLoadCommitted(pending_error_code_
);
114 // This may happen if the active RenderView commits a page before a cross
115 // process navigation cancels the old load. In this case, the commit of the
116 // old navigation will cancel the newer one.
119 // Send information about the new load.
120 tab_reloader_
->OnLoadStart(url
.SchemeIsSecure());
121 tab_reloader_
->OnLoadCommitted(net::OK
);
124 provisional_render_view_host_
= NULL
;
125 pending_error_code_
= net::OK
;
128 void CaptivePortalTabHelper::DidFailProvisionalLoad(
131 const GURL
& validated_url
,
133 const string16
& error_description
,
134 content::RenderViewHost
* render_view_host
) {
135 DCHECK(CalledOnValidThread());
137 // Ignore subframes and unexpected RenderViewHosts.
138 if (!is_main_frame
|| render_view_host
!= provisional_render_view_host_
)
141 // Aborts generally aren't followed by loading an error page, so go ahead and
142 // reset the state now, to prevent any captive portal checks from triggering.
143 if (error_code
== net::ERR_ABORTED
) {
148 pending_error_code_
= error_code
;
151 void CaptivePortalTabHelper::DidStopLoading(
152 content::RenderViewHost
* render_view_host
) {
153 DCHECK(CalledOnValidThread());
155 login_detector_
->OnStoppedLoading();
158 void CaptivePortalTabHelper::Observe(
160 const content::NotificationSource
& source
,
161 const content::NotificationDetails
& details
) {
162 DCHECK(CalledOnValidThread());
164 case content::NOTIFICATION_RESOURCE_RECEIVED_REDIRECT
: {
165 DCHECK_EQ(web_contents(),
166 content::Source
<content::WebContents
>(source
).ptr());
168 const content::ResourceRedirectDetails
* redirect_details
=
169 content::Details
<content::ResourceRedirectDetails
>(details
).ptr();
171 OnRedirect(redirect_details
->origin_child_id
,
172 redirect_details
->resource_type
,
173 redirect_details
->new_url
);
176 case chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT
: {
177 DCHECK_EQ(profile_
, content::Source
<Profile
>(source
).ptr());
179 const CaptivePortalService::Results
* results
=
180 content::Details
<CaptivePortalService::Results
>(details
).ptr();
182 OnCaptivePortalResults(results
->previous_result
, results
->result
);
190 void CaptivePortalTabHelper::OnSSLCertError(const net::SSLInfo
& ssl_info
) {
191 tab_reloader_
->OnSSLCertError(ssl_info
);
194 bool CaptivePortalTabHelper::IsLoginTab() const {
195 return login_detector_
->is_login_tab();
198 void CaptivePortalTabHelper::OnRedirect(int child_id
,
199 ResourceType::Type resource_type
,
200 const GURL
& new_url
) {
201 // Only main frame redirects for the provisional RenderViewHost matter.
202 if (resource_type
!= ResourceType::MAIN_FRAME
||
203 !provisional_render_view_host_
||
204 provisional_render_view_host_
->GetProcess()->GetID() != child_id
) {
208 tab_reloader_
->OnRedirect(new_url
.SchemeIsSecure());
211 void CaptivePortalTabHelper::OnCaptivePortalResults(Result previous_result
,
213 tab_reloader_
->OnCaptivePortalResults(previous_result
, result
);
214 login_detector_
->OnCaptivePortalResults(previous_result
, result
);
217 void CaptivePortalTabHelper::OnLoadAborted() {
218 // No further messages for the cancelled navigation will occur.
219 provisional_render_view_host_
= NULL
;
220 // May have been aborting the load of an error page.
221 pending_error_code_
= net::OK
;
223 tab_reloader_
->OnAbort();
226 void CaptivePortalTabHelper::SetIsLoginTab() {
227 login_detector_
->SetIsLoginTab();
230 void CaptivePortalTabHelper::SetTabReloaderForTest(
231 CaptivePortalTabReloader
* tab_reloader
) {
232 tab_reloader_
.reset(tab_reloader
);
235 CaptivePortalTabReloader
* CaptivePortalTabHelper::GetTabReloaderForTest() {
236 return tab_reloader_
.get();
239 void CaptivePortalTabHelper::OpenLoginTab() {
240 Browser
* browser
= chrome::FindBrowserWithWebContents(web_contents_
);
242 // If the Profile doesn't have a tabbed browser window open, do nothing.
246 // Check if the Profile's topmost browser window already has a login tab.
247 // If so, do nothing.
248 // TODO(mmenke): Consider focusing that tab, at least if this is the tab
249 // helper for the currently active tab for the profile.
250 for (int i
= 0; i
< browser
->tab_strip_model()->count(); ++i
) {
251 content::WebContents
* web_contents
=
252 browser
->tab_strip_model()->GetWebContentsAt(i
);
253 captive_portal::CaptivePortalTabHelper
* captive_portal_tab_helper
=
254 captive_portal::CaptivePortalTabHelper::FromWebContents(web_contents
);
255 if (captive_portal_tab_helper
->IsLoginTab())
259 // Otherwise, open a login tab. Only end up here when a captive portal result
260 // was received, so it's safe to assume |profile_| has a CaptivePortalService.
261 content::WebContents
* web_contents
= chrome::AddSelectedTabWithURL(
263 CaptivePortalServiceFactory::GetForProfile(profile_
)->test_url(),
264 content::PAGE_TRANSITION_TYPED
);
265 captive_portal::CaptivePortalTabHelper
* captive_portal_tab_helper
=
266 captive_portal::CaptivePortalTabHelper::FromWebContents(web_contents
);
267 captive_portal_tab_helper
->SetIsLoginTab();
270 } // namespace captive_portal