Index: chrome/browser/captive_portal/captive_portal_tab_helper.h |
=================================================================== |
--- chrome/browser/captive_portal/captive_portal_tab_helper.h (revision 0) |
+++ chrome/browser/captive_portal/captive_portal_tab_helper.h (revision 0) |
@@ -0,0 +1,162 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef CHROME_BROWSER_CAPTIVE_PORTAL_CAPTIVE_PORTAL_TAB_HELPER_H_ |
+#define CHROME_BROWSER_CAPTIVE_PORTAL_CAPTIVE_PORTAL_TAB_HELPER_H_ |
+#pragma once |
+ |
+#include "base/basictypes.h" |
+#include "base/compiler_specific.h" |
+#include "base/memory/weak_ptr.h" |
+#include "base/threading/non_thread_safe.h" |
+#include "base/time.h" |
+#include "base/timer.h" |
+#include "chrome/browser/captive_portal/captive_portal_tab_observer.h" |
+#include "content/public/browser/notification_observer.h" |
+#include "content/public/browser/notification_registrar.h" |
+#include "content/public/browser/web_contents_observer.h" |
+ |
+class Profile; |
+class TabContentsWrapper; |
+ |
+namespace captive_portal { |
+ |
+// Class that keeps track of whether a tab is a login page or has encountered |
+// a navigation error caused by a captive portal, triggering captive portal |
+// checks and opening tabs at the portal's login page as necessary. |
+// CaptivePortalTabHelpers exist only on the UI thread. |
+class CaptivePortalTabHelper : public content::NotificationObserver, |
+ public base::NonThreadSafe { |
+ public: |
+ enum State { |
+ STATE_NONE, |
+ // The slow load timer is running. We only start it running on SSL |
+ // provisional loads. |
+ STATE_TIMER_RUNNING, |
+ // The tab may have been broken by a captive portal. A tab switches to |
+ // this state either on an SSL timeout or when an SSL request takes too |
+ // long. It will remain in this state until the current load succeeds, a |
+ // new provisional load starts, or it gets a captive portal result. |
+ STATE_MAYBE_BROKEN_BY_PORTAL, |
+ // We switch to this state when we get a captive portal result indicating |
+ // we're behind a captive portal. The tab will remain in this state until |
+ // a provisional load occurs, the original load successfully commits, or |
+ // we reload the page because we're no longer behind a captive portal. |
+ STATE_BROKEN_BY_PORTAL, |
+ // If the page fails with a timeout, we'll need to reload the page. If |
+ // anything else happens (Another error, successful navigation, or a new |
+ // navigation), we just transition to STATE_NONE without reloading. |
+ STATE_NEEDS_RELOAD, |
+ // This tab is, or was, at the captive portal login page. This state is |
+ // only cleared once we no longer detect a captive portal. |
+ STATE_CAPTIVE_PORTAL_LOGIN_PAGE, |
+ }; |
+ |
+ // Creates a new CaptivePortalTabHelper for the |tab_contents|. |
+ static CaptivePortalTabHelper* CaptivePortalTabHelper::Create( |
cbentzel
2012/04/18 15:12:48
Curious - why do you need a factory function inste
mmenke
2012/04/18 19:15:46
I used to take in a CaptivePortalService as well,
|
+ TabContentsWrapper* tab_contents); |
+ |
+ virtual ~CaptivePortalTabHelper(); |
+ |
+ protected: |
+ // |profile| and |web_contents| will only be dereferenced in ReloadTab, |
+ // MaybeOpenCaptivePortalLoginTab, and CheckForCaptivePortal, so we can |
+ // override those functions and let the values be NULL in unit tests, though |
+ // |profile| will still be used as a notification source. |
+ CaptivePortalTabHelper(Profile* profile, content::WebContents* web_contents); |
+ |
+ // The following 4 functions are all invoked by |observer_|. |
+ |
+ // Called when a non-error main frame load starts. Resets current state, |
+ // unless this is a login tab. Each load will eventually result in a call to |
+ // OnLoadCommitted or OnAbort. The former will be called both on successful |
+ // loads and for error pages. |
+ virtual void OnLoadStart(bool is_ssl); |
+ |
+ // Called when an page is committed. |net_error| will be net::OK in the case |
+ // of a successful load. For an errror page, the entire 3-step process of |
+ // getting the error, starting a new provisional load for the error page, and |
+ // committing the error page is treated as a single commit. |
+ virtual void OnLoadCommitted(int net_error); |
+ |
+ // This is called when the current provisional load is canceled. |
+ // Clears current state, unless this is a login tab. |
+ virtual void OnAbort(); |
+ |
+ // Called when the TabContents stops loading. Starts a captive portal check |
+ // if this is the login tab. |
+ virtual void OnStopLoading(); |
+ |
+ // Sets |state_| and takes any action associated with the new state. |
+ void SetState(State new_state); |
+ |
+ State state() const { return state_; } |
+ |
+ void set_slow_ssl_load_time(base::TimeDelta slow_ssl_load_time) { |
+ slow_ssl_load_time_ = slow_ssl_load_time; |
+ } |
+ |
+ // Started whenever an SSL tab starts loading. If we haven't received a |
+ // response by the time this triggers, we switch to |
+ // STATE_MAYBE_BROKEN_BY_PORTAL. Should only be running when we're in |
+ // STATE_TIMER_RUNNING. |
+ // TODO(mmenke): On redirects, update this timer. |
+ base::OneShotTimer<CaptivePortalTabHelper> slow_ssl_load_timer_; |
+ |
+ // Our WebContentsObserver. Separate class to simplify logic and testing. |
+ CaptivePortalTabObserver observer_; |
+ |
+ private: |
+ friend class CaptivePortalBrowserTest; |
+ friend class CaptivePortalTabObserver; |
+ |
+ // content::NotificationObserver: |
+ virtual void Observe( |
+ int type, |
+ const content::NotificationSource& source, |
+ const content::NotificationDetails& details) OVERRIDE; |
+ |
+ // Called by a timer when an SSL main frame provisional load is taking a |
+ // while to commit. |
+ void OnSlowSSLConnect(); |
+ |
+ // Reloads the tab if there's no provisional load going on and we're still in |
+ // STATE_BROKEN_BY_PORTAL. |
+ void ReloadTabIfNeeded(); |
+ |
+ // Reloads the tab. |
+ virtual void ReloadTab(); |
+ |
+ // Opens a login tab in the topmost browser window for the observer's profile, |
+ // if there isn't one already and there is a tabbed browser window for the |
+ // profile. |
+ virtual void MaybeOpenCaptivePortalLoginTab(); |
+ |
+ // Tells |captive_portal_service_| to start a captive portal check. |
+ virtual void CheckForCaptivePortal(); |
+ |
+ Profile* profile_; |
+ |
+ State state_; |
+ |
+ // Tracks if there's a load going on that we don't want to interrupt. This |
+ // should be true between the provisional load failed and provisional load |
+ // start in the case of an error page, so does not perfectly align with the |
+ // notion of a provisional load used by the WebContents. |
+ bool provisional_main_frame_load_; |
+ |
+ // Load time for a provisional HTTPS load before we trigger a captive portal |
+ // check. |
+ base::TimeDelta slow_ssl_load_time_; |
+ |
+ base::WeakPtrFactory<CaptivePortalTabHelper> weak_factory_; |
+ |
+ content::NotificationRegistrar registrar_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(CaptivePortalTabHelper); |
+}; |
+ |
+} // namespace captive_portal |
+ |
+#endif // CHROME_BROWSER_CAPTIVE_PORTAL_CAPTIVE_PORTAL_TAB_HELPER_H_ |