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.
5 #ifndef CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_NETWORK_STATE_INFORMER_H_
6 #define CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_NETWORK_STATE_INFORMER_H_
11 #include "base/cancelable_callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/observer_list.h"
16 #include "chrome/browser/chromeos/login/captive_portal_window_proxy.h"
17 #include "chrome/browser/chromeos/login/screens/error_screen_actor.h"
18 #include "chrome/browser/chromeos/net/network_portal_detector.h"
19 #include "chromeos/network/network_state_handler_observer.h"
20 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h"
22 #include "content/public/browser/notification_service.h"
26 // Class which observes network state changes and calls registered callbacks.
27 // State is considered changed if connection or the active network has been
28 // changed. Also, it answers to the requests about current network state.
29 class NetworkStateInformer
30 : public chromeos::NetworkStateHandlerObserver,
31 public chromeos::NetworkPortalDetector::Observer,
32 public content::NotificationObserver,
33 public CaptivePortalWindowProxyDelegate,
34 public base::RefCounted<NetworkStateInformer> {
45 class NetworkStateInformerObserver {
47 NetworkStateInformerObserver() {}
48 virtual ~NetworkStateInformerObserver() {}
50 virtual void UpdateState(ErrorScreenActor::ErrorReason reason) = 0;
51 virtual void OnNetworkReady() {}
54 NetworkStateInformer();
58 // Adds observer to be notified when network state has been changed.
59 void AddObserver(NetworkStateInformerObserver* observer);
62 void RemoveObserver(NetworkStateInformerObserver* observer);
64 // NetworkStateHandlerObserver implementation:
65 virtual void DefaultNetworkChanged(const NetworkState* network) OVERRIDE;
67 // NetworkPortalDetector::Observer implementation:
68 virtual void OnPortalDetectionCompleted(
69 const NetworkState* network,
70 const NetworkPortalDetector::CaptivePortalState& state) OVERRIDE;
72 // content::NotificationObserver implementation.
73 virtual void Observe(int type,
74 const content::NotificationSource& source,
75 const content::NotificationDetails& details) OVERRIDE;
77 // CaptivePortalWindowProxyDelegate implementation:
78 virtual void OnPortalDetected() OVERRIDE;
80 State state() const { return state_; }
81 std::string network_path() const { return network_path_; }
82 std::string network_type() const { return network_type_; }
85 friend class base::RefCounted<NetworkStateInformer>;
87 virtual ~NetworkStateInformer();
91 void UpdateStateAndNotify();
93 void SendStateToObservers(ErrorScreenActor::ErrorReason reason);
96 std::string network_path_;
97 std::string network_type_;
99 ObserverList<NetworkStateInformerObserver> observers_;
100 content::NotificationRegistrar registrar_;
102 base::WeakPtrFactory<NetworkStateInformer> weak_ptr_factory_;
105 } // namespace chromeos
107 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_NETWORK_STATE_INFORMER_H_