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 #ifndef CHROME_BROWSER_CAPTIVE_PORTAL_CAPTIVE_PORTAL_TAB_HELPER_H_
6 #define CHROME_BROWSER_CAPTIVE_PORTAL_CAPTIVE_PORTAL_TAB_HELPER_H_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/threading/non_thread_safe.h"
12 #include "chrome/browser/captive_portal/captive_portal_service.h"
13 #include "content/public/browser/notification_observer.h"
14 #include "content/public/browser/notification_registrar.h"
15 #include "content/public/browser/web_contents_observer.h"
16 #include "content/public/browser/web_contents_user_data.h"
17 #include "webkit/common/resource_type.h"
30 namespace captive_portal
{
32 class CaptivePortalLoginDetector
;
33 class CaptivePortalTabReloader
;
35 // Along with the classes it owns, responsible for detecting page loads broken
36 // by a captive portal, triggering captive portal checks on navigation events
37 // that may indicate a captive portal is present, or has been removed / logged
38 // in to, and taking any correcting actions.
40 // It acts as a WebContentsObserver for its CaptivePortalLoginDetector and
41 // CaptivePortalTabReloader. It filters out non-main-frame resource loads, and
42 // treats the commit of an error page as a single event, rather than as 3
43 // (ProvisionalLoadFail, DidStartProvisionalLoad, DidCommit), which simplifies
44 // the CaptivePortalTabReloader. It is also needed by CaptivePortalTabReloaders
45 // to inform the tab's CaptivePortalLoginDetector when the tab is at a captive
46 // portal's login page.
48 // The TabHelper assumes that a WebContents can only have one RenderViewHost
49 // with a provisional load at a time, and tracks only that navigation. This
50 // assumption can be violated in rare cases, for example, a same-site
51 // navigation interrupted by a cross-process navigation started from the
52 // omnibox, may commit before it can be cancelled. In these cases, this class
53 // may pass incorrect messages to the TabReloader, which will, at worst, result
54 // in not opening up a login tab until a second load fails or not automatically
55 // reloading a tab after logging in.
57 // For the design doc, see:
58 // https://docs.google.com/document/d/1k-gP2sswzYNvryu9NcgN7q5XrsMlUdlUdoW9WRaEmfM/edit
59 class CaptivePortalTabHelper
60 : public content::WebContentsObserver
,
61 public content::NotificationObserver
,
62 public base::NonThreadSafe
,
63 public content::WebContentsUserData
<CaptivePortalTabHelper
> {
65 virtual ~CaptivePortalTabHelper();
67 // content::WebContentsObserver:
68 virtual void RenderViewDeleted(
69 content::RenderViewHost
* render_view_host
) OVERRIDE
;
71 virtual void DidStartProvisionalLoadForFrame(
73 int64 parent_frame_id
,
75 const GURL
& validated_url
,
77 bool is_iframe_srcdoc
,
78 content::RenderViewHost
* render_view_host
) OVERRIDE
;
80 virtual void DidCommitProvisionalLoadForFrame(
82 const base::string16
& frame_unique_name
,
85 content::PageTransition transition_type
,
86 content::RenderViewHost
* render_view_host
) OVERRIDE
;
88 virtual void DidFailProvisionalLoad(
90 const base::string16
& frame_unique_name
,
92 const GURL
& validated_url
,
94 const base::string16
& error_description
,
95 content::RenderViewHost
* render_view_host
) OVERRIDE
;
97 virtual void DidStopLoading(
98 content::RenderViewHost
* render_view_host
) OVERRIDE
;
100 // content::NotificationObserver:
101 virtual void Observe(
103 const content::NotificationSource
& source
,
104 const content::NotificationDetails
& details
) OVERRIDE
;
106 // Called when a certificate interstitial error page is about to be shown.
107 void OnSSLCertError(const net::SSLInfo
& ssl_info
);
109 // A "Login Tab" is a tab that was originally at a captive portal login
110 // page. This is set to false when a captive portal is no longer detected.
111 bool IsLoginTab() const;
114 friend class CaptivePortalBrowserTest
;
115 friend class CaptivePortalTabHelperTest
;
117 friend class content::WebContentsUserData
<CaptivePortalTabHelper
>;
118 explicit CaptivePortalTabHelper(content::WebContents
* web_contents
);
120 // Called by Observe in response to the corresponding event.
121 void OnRedirect(int child_id
,
122 ResourceType::Type resource_type
,
123 const GURL
& new_url
);
125 // Called by Observe in response to the corresponding event.
126 void OnCaptivePortalResults(Result previous_result
, Result result
);
128 void OnLoadAborted();
130 // Called to indicate a tab is at, or is navigating to, the captive portal
132 void SetIsLoginTab();
134 // |this| takes ownership of |tab_reloader|.
135 void SetTabReloaderForTest(CaptivePortalTabReloader
* tab_reloader
);
137 const content::RenderViewHost
* provisional_render_view_host() const {
138 return provisional_render_view_host_
;
141 CaptivePortalTabReloader
* GetTabReloaderForTest();
143 // Opens a login tab if the profile's active window doesn't have one already.
148 // Neither of these will ever be NULL.
149 scoped_ptr
<CaptivePortalTabReloader
> tab_reloader_
;
150 scoped_ptr
<CaptivePortalLoginDetector
> login_detector_
;
152 content::WebContents
* web_contents_
;
154 // If a provisional load has failed, and the tab is loading an error page, the
155 // error code associated with the error page we're loading.
156 // net::OK, otherwise.
157 int pending_error_code_
;
159 // The RenderViewHost with a provisional load, if any. Can either be
160 // the currently displayed RenderViewHost or a pending RenderViewHost for
161 // cross-process navitations. NULL when there's currently no provisional
163 content::RenderViewHost
* provisional_render_view_host_
;
165 content::NotificationRegistrar registrar_
;
167 DISALLOW_COPY_AND_ASSIGN(CaptivePortalTabHelper
);
170 } // namespace captive_portal
172 #endif // CHROME_BROWSER_CAPTIVE_PORTAL_CAPTIVE_PORTAL_TAB_HELPER_H_