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