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