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