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/compiler_specific.h" |
| 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/time.h" |
| 13 #include "base/timer.h" |
| 14 #include "chrome/browser/captive_portal/captive_portal_service.h" |
| 15 |
| 16 class Profile; |
| 17 class TabContentsWrapper; |
| 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 // |profile| and |web_contents| will only be dereferenced in ReloadTab, |
| 66 // MaybeOpenCaptivePortalLoginTab, and CheckForCaptivePortal, so they can |
| 67 // both be NULL in the unit tests as long as those functions are not called. |
| 68 CaptivePortalTabReloader(Profile* profile, |
| 69 content::WebContents* web_contents); |
| 70 |
| 71 virtual ~CaptivePortalTabReloader(); |
| 72 |
| 73 // The following 4 functions are all invoked by the CaptivePortalTabHelper: |
| 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 by CaptivePortalTabHelper whenever a captive portal test completes. |
| 96 virtual void OnCaptivePortalResults(Result previous_result, Result result); |
| 97 |
| 98 protected: |
| 99 // The following functions are used only when testing: |
| 100 |
| 101 State state() const { return state_; } |
| 102 |
| 103 void set_slow_ssl_load_time(base::TimeDelta slow_ssl_load_time) { |
| 104 slow_ssl_load_time_ = slow_ssl_load_time; |
| 105 } |
| 106 |
| 107 // Started whenever an SSL tab starts loading, when the state is switched to |
| 108 // STATE_TIMER_RUNNING. Stopped on any state change, including when a page |
| 109 // commits or there's an error. If the timer triggers, the state switches to |
| 110 // STATE_MAYBE_BROKEN_BY_PORTAL and |this| kicks off a captive portal check. |
| 111 // TODO(mmenke): On redirects, update this timer. |
| 112 base::OneShotTimer<CaptivePortalTabReloader> slow_ssl_load_timer_; |
| 113 |
| 114 private: |
| 115 friend class CaptivePortalBrowserTest; |
| 116 |
| 117 // Sets |state_| and takes any action associated with the new state. Also |
| 118 // stops the timer, if needed. |
| 119 void SetState(State new_state); |
| 120 |
| 121 // Called by a timer when an SSL main frame provisional load is taking a |
| 122 // while to commit. |
| 123 void OnSlowSSLConnect(); |
| 124 |
| 125 // Reloads the tab if there's no provisional load going on and the current |
| 126 // state is STATE_NEEDS_RELOAD. Not safe to call synchronously when called |
| 127 // by from a WebContentsObserver function, since the WebContents is currently |
| 128 // performing some action. |
| 129 void ReloadTabIfNeeded(); |
| 130 |
| 131 // Reloads the tab. |
| 132 virtual void ReloadTab(); |
| 133 |
| 134 // Opens a login tab in the topmost browser window for the |profile_|, if the |
| 135 // profile has a tabbed browser window and the window doesn't already have a |
| 136 // login tab. Otherwise, does nothing. |
| 137 virtual void MaybeOpenCaptivePortalLoginTab(); |
| 138 |
| 139 // Tries to get |profile_|'s CaptivePortalService and have it start a captive |
| 140 // portal check. |
| 141 virtual void CheckForCaptivePortal(); |
| 142 |
| 143 Profile* profile_; |
| 144 content::WebContents* web_contents_; |
| 145 |
| 146 State state_; |
| 147 |
| 148 // Tracks if there's a load going on that can't safely be interrupted. This |
| 149 // is true between the time when a provisional load fails and when an error |
| 150 // page's provisional load starts, so does not perfectly align with the |
| 151 // notion of a provisional load used by the WebContents. |
| 152 bool provisional_main_frame_load_; |
| 153 |
| 154 // Time to wait after a provisional HTTPS load before triggering a captive |
| 155 // portal check. |
| 156 base::TimeDelta slow_ssl_load_time_; |
| 157 |
| 158 base::WeakPtrFactory<CaptivePortalTabReloader> weak_factory_; |
| 159 |
| 160 DISALLOW_COPY_AND_ASSIGN(CaptivePortalTabReloader); |
| 161 }; |
| 162 |
| 163 } // namespace captive_portal |
| 164 |
| 165 #endif // CHROME_BROWSER_CAPTIVE_PORTAL_CAPTIVE_PORTAL_TAB_RELOADER_H_ |
OLD | NEW |