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/screens/network_error.h"
17 #include "chrome/browser/chromeos/login/ui/captive_portal_window_proxy.h"
18 #include "chromeos/network/network_state_handler_observer.h"
19 #include "chromeos/network/portal_detector/network_portal_detector.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(NetworkError::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 void DefaultNetworkChanged(const NetworkState
* network
) override
;
67 // NetworkPortalDetector::Observer implementation:
68 void OnPortalDetectionCompleted(
69 const NetworkState
* network
,
70 const NetworkPortalDetector::CaptivePortalState
& state
) override
;
72 // content::NotificationObserver implementation.
73 void Observe(int type
,
74 const content::NotificationSource
& source
,
75 const content::NotificationDetails
& details
) override
;
77 // CaptivePortalWindowProxyDelegate implementation:
78 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_
; }
84 static const char* StatusString(State state
);
87 friend class base::RefCounted
<NetworkStateInformer
>;
89 ~NetworkStateInformer() override
;
93 void UpdateStateAndNotify();
95 void SendStateToObservers(NetworkError::ErrorReason reason
);
98 std::string network_path_
;
99 std::string network_type_
;
101 base::ObserverList
<NetworkStateInformerObserver
> observers_
;
102 content::NotificationRegistrar registrar_
;
104 base::WeakPtrFactory
<NetworkStateInformer
> weak_ptr_factory_
;
107 } // namespace chromeos
109 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_NETWORK_STATE_INFORMER_H_