Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / captive_portal / captive_portal_tab_reloader.h
blobdb6b1228d6e54b2383a8094ea816b69d89e3ff23
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_RELOADER_H_
6 #define CHROME_BROWSER_CAPTIVE_PORTAL_CAPTIVE_PORTAL_TAB_RELOADER_H_
8 #include "base/basictypes.h"
9 #include "base/callback_forward.h"
10 #include "base/compiler_specific.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/time/time.h"
13 #include "base/timer/timer.h"
14 #include "chrome/browser/captive_portal/captive_portal_service.h"
16 class Profile;
18 namespace content {
19 class WebContents;
22 namespace net {
23 class SSLInfo;
26 // Keeps track of whether a tab has encountered a navigation error caused by a
27 // captive portal. Also triggers captive portal checks when a page load may
28 // have been broken or be taking longer due to a captive portal. All methods
29 // may only be called on the UI thread.
31 // Only supports SSL main frames which end at error pages as a result of
32 // captive portals, since these make for a particularly bad user experience.
33 // Non-SSL requests are intercepted by captive portals, which take users to the
34 // login page. SSL requests, however, may be silently blackholed, or result
35 // in a variety of error pages, and will continue to do so if a user tries to
36 // reload them.
37 class CaptivePortalTabReloader {
38 public:
39 enum State {
40 STATE_NONE,
41 // The slow load timer is running. Only started on SSL provisional loads.
42 // If the timer triggers before the page has been committed, a captive
43 // portal test will be requested.
44 STATE_TIMER_RUNNING,
45 // The tab may have been broken by a captive portal. A tab switches to
46 // this state either on a main frame SSL error that may be caused by a
47 // captive portal, or when an SSL request takes too long to commit. The
48 // tab will remain in this state until the current load succeeds, a new
49 // provisional load starts, it gets a captive portal result, or the load
50 // fails with error that indicates the page was not broken by a captive
51 // portal.
52 STATE_MAYBE_BROKEN_BY_PORTAL,
53 // The TabHelper switches to this state from STATE_MAYBE_BROKEN_BY_PORTAL in
54 // response to a RESULT_BEHIND_CAPTIVE_PORTAL. The tab will remain in this
55 // state until a new provisional load starts, the original load successfully
56 // commits, the current load is aborted, or the tab reloads the page in
57 // response to receiving a captive portal result other than
58 // RESULT_BEHIND_CAPTIVE_PORTAL.
59 STATE_BROKEN_BY_PORTAL,
60 // The page may need to be reloaded. The tab will be reloaded if the page
61 // fails the next load with a timeout, or immediately upon switching to this
62 // state, if the page already timed out. If anything else happens
63 // when in this state (Another error, successful navigation, or the original
64 // navigation was aborted), the TabHelper transitions to STATE_NONE without
65 // reloading.
66 STATE_NEEDS_RELOAD,
69 // Function to open a login tab, if there isn't one already.
70 typedef base::Callback<void()> OpenLoginTabCallback;
72 // |profile| and |web_contents| will only be dereferenced in ReloadTab,
73 // MaybeOpenCaptivePortalLoginTab, and CheckForCaptivePortal, so they can
74 // both be NULL in the unit tests as long as those functions are not called.
75 CaptivePortalTabReloader(Profile* profile,
76 content::WebContents* web_contents,
77 const OpenLoginTabCallback& open_login_tab_callback);
79 virtual ~CaptivePortalTabReloader();
81 // The following functions are all invoked by the CaptivePortalTabHelper:
83 // Called when a non-error main frame load starts. Resets current state,
84 // unless this is a login tab. Each load will eventually result in a call to
85 // OnLoadCommitted or OnAbort. The former will be called both on successful
86 // loads and for error pages.
87 virtual void OnLoadStart(bool is_ssl);
89 // Called when the main frame is committed. |net_error| will be net::OK in
90 // the case of a successful load. For an errror page, the entire 3-step
91 // process of getting the error, starting a new provisional load for the error
92 // page, and committing the error page is treated as a single commit.
94 // The Link Doctor page will typically be one OnLoadCommitted with an error
95 // code, followed by another OnLoadCommitted with net::OK for the Link Doctor
96 // page.
97 virtual void OnLoadCommitted(int net_error);
99 // This is called when the current provisional main frame load is canceled.
100 // Sets state to STATE_NONE, unless this is a login tab.
101 virtual void OnAbort();
103 // Called whenever a provisional load to the main frame is redirected.
104 virtual void OnRedirect(bool is_ssl);
106 // Called whenever a captive portal test completes.
107 virtual void OnCaptivePortalResults(
108 captive_portal::CaptivePortalResult previous_result,
109 captive_portal::CaptivePortalResult result);
111 // Called on certificate errors, which often indicate a captive portal.
112 void OnSSLCertError(const net::SSLInfo& ssl_info);
114 protected:
115 // The following functions are used only when testing:
117 State state() const { return state_; }
119 content::WebContents* web_contents() { return web_contents_; }
121 void set_slow_ssl_load_time(base::TimeDelta slow_ssl_load_time) {
122 slow_ssl_load_time_ = slow_ssl_load_time;
125 // Started whenever an SSL tab starts loading, when the state is switched to
126 // STATE_TIMER_RUNNING. Stopped on any state change, including when a page
127 // commits or there's an error. If the timer triggers, the state switches to
128 // STATE_MAYBE_BROKEN_BY_PORTAL and |this| kicks off a captive portal check.
129 base::OneShotTimer<CaptivePortalTabReloader> slow_ssl_load_timer_;
131 private:
132 friend class CaptivePortalBrowserTest;
134 // Sets |state_| and takes any action associated with the new state. Also
135 // stops the timer, if needed.
136 void SetState(State new_state);
138 // Called by a timer when an SSL main frame provisional load is taking a
139 // while to commit.
140 void OnSlowSSLConnect();
142 // Reloads the tab if there's no provisional load going on and the current
143 // state is STATE_NEEDS_RELOAD. Not safe to call synchronously when called
144 // by from a WebContentsObserver function, since the WebContents is currently
145 // performing some action.
146 void ReloadTabIfNeeded();
148 // Reloads the tab.
149 virtual void ReloadTab();
151 // Opens a login tab in the topmost browser window for the |profile_|, if the
152 // profile has a tabbed browser window and the window doesn't already have a
153 // login tab. Otherwise, does nothing.
154 virtual void MaybeOpenCaptivePortalLoginTab();
156 // Tries to get |profile_|'s CaptivePortalService and have it start a captive
157 // portal check.
158 virtual void CheckForCaptivePortal();
160 Profile* profile_;
161 content::WebContents* web_contents_;
163 State state_;
165 // Tracks if there's a load going on that can't safely be interrupted. This
166 // is true between the time when a provisional load fails and when an error
167 // page's provisional load starts, so does not perfectly align with the
168 // notion of a provisional load used by the WebContents.
169 bool provisional_main_frame_load_;
171 // True if there was an SSL URL the in the redirect chain for the current
172 // provisional main frame load.
173 bool ssl_url_in_redirect_chain_;
175 // Time to wait after a provisional HTTPS load before triggering a captive
176 // portal check.
177 base::TimeDelta slow_ssl_load_time_;
179 const OpenLoginTabCallback open_login_tab_callback_;
181 base::WeakPtrFactory<CaptivePortalTabReloader> weak_factory_;
183 DISALLOW_COPY_AND_ASSIGN(CaptivePortalTabReloader);
186 #endif // CHROME_BROWSER_CAPTIVE_PORTAL_CAPTIVE_PORTAL_TAB_RELOADER_H_