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 |
| 26 // a navigation error caused by a captive portal, triggering captive portal |
| 27 // checks and opening tabs at the portal's login page as necessary. |
| 28 // CaptivePortalTabHelpers exist only on the UI thread. |
| 29 class CaptivePortalTabHelper : public content::NotificationObserver, |
| 30 public base::NonThreadSafe { |
| 31 public: |
| 32 enum State { |
| 33 STATE_NONE, |
| 34 // The slow load timer is running. We only start it running on SSL |
| 35 // provisional loads. |
| 36 STATE_TIMER_RUNNING, |
| 37 // The tab may have been broken by a captive portal. A tab switches to |
| 38 // this state either on an SSL timeout or when an SSL request takes too |
| 39 // long. It will remain in this state until the current load succeeds, a |
| 40 // new provisional load starts, or it gets a captive portal result. |
| 41 STATE_MAYBE_BROKEN_BY_PORTAL, |
| 42 // We switch to this state when we get a captive portal result indicating |
| 43 // we're behind a captive portal. The tab will remain in this state until |
| 44 // a provisional load occurs, the original load successfully commits, or |
| 45 // we reload the page because we're no longer behind a captive portal. |
| 46 STATE_BROKEN_BY_PORTAL, |
| 47 // If the page fails with a timeout, we'll need to reload the page. If |
| 48 // anything else happens (Another error, successful navigation, or a new |
| 49 // navigation), we just transition to STATE_NONE without reloading. |
| 50 STATE_NEEDS_RELOAD, |
| 51 // This tab is, or was, at the captive portal login page. This state is |
| 52 // only cleared once we no longer detect a captive portal. |
| 53 STATE_CAPTIVE_PORTAL_LOGIN_PAGE, |
| 54 }; |
| 55 |
| 56 // Creates a new CaptivePortalTabHelper for the |tab_contents|. |
| 57 static CaptivePortalTabHelper* CaptivePortalTabHelper::Create( |
| 58 TabContentsWrapper* tab_contents); |
| 59 |
| 60 virtual ~CaptivePortalTabHelper(); |
| 61 |
| 62 protected: |
| 63 // |profile| and |web_contents| will only be dereferenced in ReloadTab, |
| 64 // MaybeOpenCaptivePortalLoginTab, and CheckForCaptivePortal, so we can |
| 65 // override those functions and let the values be NULL in unit tests, though |
| 66 // |profile| will still be used as a notification source. |
| 67 CaptivePortalTabHelper(Profile* profile, content::WebContents* web_contents); |
| 68 |
| 69 // The following 4 functions are all invoked by |observer_|. |
| 70 |
| 71 // Called when a non-error main frame load starts. Resets current state, |
| 72 // unless this is a login tab. Each load will eventually result in a call to |
| 73 // OnLoadCommitted or OnAbort. The former will be called both on successful |
| 74 // loads and for error pages. |
| 75 virtual void OnLoadStart(bool is_ssl); |
| 76 |
| 77 // Called when an page is committed. |net_error| will be net::OK in the case |
| 78 // of a successful load. For an errror page, the entire 3-step process of |
| 79 // getting the error, starting a new provisional load for the error page, and |
| 80 // committing the error page is treated as a single commit. |
| 81 virtual void OnLoadCommitted(int net_error); |
| 82 |
| 83 // This is called when the current provisional load is canceled. |
| 84 // Clears current state, unless this is a login tab. |
| 85 virtual void OnAbort(); |
| 86 |
| 87 // Called when the TabContents stops loading. Starts a captive portal check |
| 88 // if this is the login tab. |
| 89 virtual void OnStopLoading(); |
| 90 |
| 91 // Sets |state_| and takes any action associated with the new state. |
| 92 void SetState(State new_state); |
| 93 |
| 94 State state() const { return state_; } |
| 95 |
| 96 void set_slow_ssl_load_time(base::TimeDelta slow_ssl_load_time) { |
| 97 slow_ssl_load_time_ = slow_ssl_load_time; |
| 98 } |
| 99 |
| 100 // Started whenever an SSL tab starts loading. If we haven't received a |
| 101 // response by the time this triggers, we switch to |
| 102 // STATE_MAYBE_BROKEN_BY_PORTAL. Should only be running when we're in |
| 103 // STATE_TIMER_RUNNING. |
| 104 // TODO(mmenke): On redirects, update this timer. |
| 105 base::OneShotTimer<CaptivePortalTabHelper> slow_ssl_load_timer_; |
| 106 |
| 107 // Our WebContentsObserver. Separate class to simplify logic and testing. |
| 108 CaptivePortalTabObserver observer_; |
| 109 |
| 110 private: |
| 111 friend class CaptivePortalBrowserTest; |
| 112 friend class CaptivePortalTabObserver; |
| 113 |
| 114 // content::NotificationObserver: |
| 115 virtual void Observe( |
| 116 int type, |
| 117 const content::NotificationSource& source, |
| 118 const content::NotificationDetails& details) OVERRIDE; |
| 119 |
| 120 // Called by a timer when an SSL main frame provisional load is taking a |
| 121 // while to commit. |
| 122 void OnSlowSSLConnect(); |
| 123 |
| 124 // Reloads the tab if there's no provisional load going on and we're still in |
| 125 // STATE_BROKEN_BY_PORTAL. |
| 126 void ReloadTabIfNeeded(); |
| 127 |
| 128 // Reloads the tab. |
| 129 virtual void ReloadTab(); |
| 130 |
| 131 // Opens a login tab in the topmost browser window for the observer's profile, |
| 132 // if there isn't one already and there is a tabbed browser window for the |
| 133 // profile. |
| 134 virtual void MaybeOpenCaptivePortalLoginTab(); |
| 135 |
| 136 // Tries to get |profile_|'s CaptivePortalService and have it start a captive |
| 137 // portal check. |
| 138 virtual void CheckForCaptivePortal(); |
| 139 |
| 140 Profile* profile_; |
| 141 |
| 142 State state_; |
| 143 |
| 144 // Tracks if there's a load going on that we don't want to interrupt. This |
| 145 // should be true between the provisional load failed and provisional load |
| 146 // start in the case of an error page, so does not perfectly align with the |
| 147 // notion of a provisional load used by the WebContents. |
| 148 bool provisional_main_frame_load_; |
| 149 |
| 150 // Load time for a provisional HTTPS load before we trigger a captive portal |
| 151 // check. |
| 152 base::TimeDelta slow_ssl_load_time_; |
| 153 |
| 154 base::WeakPtrFactory<CaptivePortalTabHelper> weak_factory_; |
| 155 |
| 156 content::NotificationRegistrar registrar_; |
| 157 |
| 158 DISALLOW_COPY_AND_ASSIGN(CaptivePortalTabHelper); |
| 159 }; |
| 160 |
| 161 } // namespace captive_portal |
| 162 |
| 163 #endif // CHROME_BROWSER_CAPTIVE_PORTAL_CAPTIVE_PORTAL_TAB_HELPER_H_ |
OLD | NEW |