|
OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #ifndef CHROME_BROWSER_CAPTIVE_PORTAL_CAPTIVE_PORTAL_TAB_HELPER_H_ | |
6 #define CHROME_BROWSER_CAPTIVE_PORTAL_CAPTIVE_PORTAL_TAB_HELPER_H_ | |
7 #pragma once | |
8 | |
9 #include "base/basictypes.h" | |
10 #include "base/compiler_specific.h" | |
11 #include "base/memory/weak_ptr.h" | |
12 #include "base/threading/non_thread_safe.h" | |
13 #include "base/time.h" | |
14 #include "base/timer.h" | |
15 #include "chrome/browser/captive_portal/captive_portal_tab_observer.h" | |
16 #include "content/public/browser/notification_observer.h" | |
17 #include "content/public/browser/notification_registrar.h" | |
18 #include "content/public/browser/web_contents_observer.h" | |
19 | |
20 class Profile; | |
21 class TabContentsWrapper; | |
22 | |
23 namespace captive_portal { | |
24 | |
25 // Keeps track of whether a tab is a login page or has encountered a navigation | |
26 // error caused by a captive portal. It triggers captive portal checks and | |
27 // opens tabs at the portal's login page as necessary. All methods may only be | |
28 // called on the UI thread. | |
29 // | |
30 // Only supports SSL main frames which time out in response to captive portals, | |
cbentzel
2012/05/18 03:23:16
Ah - I think what I was getting at here is that no
mmenke
2012/05/18 06:12:53
Comment updated.
| |
31 // since these make for a particularly bad user experience. Pages can take 2 | |
32 // minutes to timeout, and will timeout again when refreshed. | |
33 // | |
34 // For the design doc, see: | |
35 // https://docs.google.com/document/d/1k-gP2sswzYNvryu9NcgN7q5XrsMlUdlUdoW9WRaEm fM/edit | |
36 class CaptivePortalTabHelper : public content::NotificationObserver, | |
37 public base::NonThreadSafe { | |
38 public: | |
39 enum State { | |
cbentzel
2012/05/18 03:23:16
Why does this need to be public?
mmenke
2012/05/18 06:12:53
So individual unit tests can use them, without fri
| |
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 an ERR_CONNECTION_TIMEOUT of an SSL page or when | |
47 // an SSL request takes too long to commit. The tab will remain in this | |
48 // state until the current load succeeds, a new provisional load starts, | |
49 // or it gets a captive portal result. | |
50 STATE_MAYBE_BROKEN_BY_PORTAL, | |
51 // The TabHelper switches to this state from STATE_MAYBE_BROKEN_BY_PORTAL in | |
52 // response to a RESULT_BEHIND_CAPTIVE_PORTAL. The tab will remain in this | |
53 // state until a new provisional load starts, the original load successfully | |
54 // commits, the current load is aborted, or the tab reloads the page in | |
55 // response to receiving a captive portal result other than | |
56 // RESULT_BEHIND_CAPTIVE_PORTAL. | |
57 STATE_BROKEN_BY_PORTAL, | |
58 // The page may need to be reloaded. The tab will be reloaded if the page | |
59 // fails the next load with a timeout, or immediately upon switching to this | |
60 // state, if the page already timed out. If anything else happens | |
61 // when in this state (Another error, successful navigation, or the original | |
62 // navigation was aborted), the TabHelper transitions to STATE_NONE without | |
63 // reloading. | |
64 STATE_NEEDS_RELOAD, | |
65 // This tab is, or was, at the captive portal login page. This state is | |
66 // only cleared once the CaptivePortalService returns something other than | |
67 // RESULT_BEHIND_CAPTIVE_PORTAL. | |
68 STATE_CAPTIVE_PORTAL_LOGIN_PAGE, | |
69 }; | |
70 | |
71 // |profile| and |web_contents| will only be dereferenced in ReloadTab, | |
72 // MaybeOpenCaptivePortalLoginTab, and CheckForCaptivePortal, so they can | |
73 // both be NULL in the unit tests, though |profile| will still be used as a | |
74 // notification source. | |
75 CaptivePortalTabHelper(Profile* profile, content::WebContents* web_contents); | |
76 | |
77 virtual ~CaptivePortalTabHelper(); | |
78 | |
79 protected: | |
80 // The following 4 functions are all invoked by |observer_|. | |
81 | |
82 // Called when a non-error main frame load starts. Resets current state, | |
83 // unless this is a login tab. Each load will eventually result in a call to | |
84 // OnLoadCommitted or OnAbort. The former will be called both on successful | |
85 // loads and for error pages. | |
86 virtual void OnLoadStart(bool is_ssl); | |
87 | |
88 // Called when a page is committed. |net_error| will be net::OK in the case | |
89 // of a successful load. For an errror page, the entire 3-step process of | |
90 // getting the error, starting a new provisional load for the error page, and | |
91 // committing the error page is treated as a single commit. | |
92 // | |
93 // The Link Doctor page will typically be one OnLoadCommitted with an error | |
94 // code, followed by another OnLoadCommitted with net::OK for the Link Doctor | |
95 // page. | |
96 virtual void OnLoadCommitted(int net_error); | |
97 | |
98 // This is called when the current provisional load is canceled. | |
99 // Sets state to STATE_NONE, unless this is a login tab. | |
100 virtual void OnAbort(); | |
101 | |
102 // Called when the WebContents stops loading. Starts a captive portal check | |
103 // if this is the login tab. | |
104 virtual void OnStopLoading(); | |
105 | |
106 // Sets |state_| and takes any action associated with the new state. | |
107 void SetState(State new_state); | |
108 | |
109 State state() const { return state_; } | |
110 | |
111 // Used by unit tests. | |
112 void set_slow_ssl_load_time(base::TimeDelta slow_ssl_load_time) { | |
113 slow_ssl_load_time_ = slow_ssl_load_time; | |
114 } | |
115 | |
116 // Started whenever an SSL tab starts loading, when the state is switched to | |
117 // STATE_TIMER_RUNNING. Stopped on any state change, including when a page | |
118 // commits or there's an error. If the timer triggers, the state switches to | |
119 // STATE_MAYBE_BROKEN_BY_PORTAL and |this| kicks off a captive portal check. | |
120 // TODO(mmenke): On redirects, update this timer. | |
121 base::OneShotTimer<CaptivePortalTabHelper> slow_ssl_load_timer_; | |
122 | |
123 // Handles messages from the WebContents. Separate class to simplify logic | |
124 // and testing. | |
125 CaptivePortalTabObserver observer_; | |
126 | |
127 private: | |
128 friend class CaptivePortalBrowserTest; | |
129 friend class CaptivePortalTabObserver; | |
130 | |
131 // content::NotificationObserver: | |
132 virtual void Observe( | |
133 int type, | |
134 const content::NotificationSource& source, | |
135 const content::NotificationDetails& details) OVERRIDE; | |
136 | |
137 // Called by a timer when an SSL main frame provisional load is taking a | |
138 // while to commit. | |
139 void OnSlowSSLConnect(); | |
140 | |
141 // Reloads the tab if there's no provisional load going on and the current | |
142 // state is STATE_NEEDS_RELOAD. Not safe to call synchronously when called | |
143 // by |observer_|, since the WebContents is already taking some action. | |
144 void ReloadTabIfNeeded(); | |
145 | |
146 // Reloads the tab. | |
147 virtual void ReloadTab(); | |
148 | |
149 // Opens a login tab in the topmost browser window for the |profile_|, if the | |
150 // profile has a tabbed browser window and the window doesn't already have a | |
151 // login tab. Otherwise, does nothing. | |
152 virtual void MaybeOpenCaptivePortalLoginTab(); | |
153 | |
154 // Tries to get |profile_|'s CaptivePortalService and have it start a captive | |
155 // portal check. | |
156 virtual void CheckForCaptivePortal(); | |
157 | |
158 Profile* profile_; | |
159 | |
160 State state_; | |
161 | |
162 // Tracks if there's a load going on that can't safely be interrupted. This | |
163 // is true between the time when a provisional load fails and when an error | |
164 // page's provisional load starts, so does not perfectly align with the | |
165 // notion of a provisional load used by the WebContents. | |
166 bool provisional_main_frame_load_; | |
167 | |
168 // Time to wait after a provisional HTTPS load before triggering a captive | |
169 // portal check. | |
170 base::TimeDelta slow_ssl_load_time_; | |
171 | |
172 base::WeakPtrFactory<CaptivePortalTabHelper> weak_factory_; | |
173 | |
174 content::NotificationRegistrar registrar_; | |
175 | |
176 DISALLOW_COPY_AND_ASSIGN(CaptivePortalTabHelper); | |
177 }; | |
178 | |
179 } // namespace captive_portal | |
180 | |
181 #endif // CHROME_BROWSER_CAPTIVE_PORTAL_CAPTIVE_PORTAL_TAB_HELPER_H_ | |
OLD | NEW |