Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / login / network_state_informer.h
blob23b75edb8588c9b9b888bf0091a7327b0586bef8
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_
8 #include <map>
9 #include <string>
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"
24 namespace chromeos {
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> {
35 public:
36 enum State {
37 OFFLINE = 0,
38 ONLINE,
39 CAPTIVE_PORTAL,
40 CONNECTING,
41 PROXY_AUTH_REQUIRED,
42 UNKNOWN
45 class NetworkStateInformerObserver {
46 public:
47 NetworkStateInformerObserver() {}
48 virtual ~NetworkStateInformerObserver() {}
50 virtual void UpdateState(NetworkError::ErrorReason reason) = 0;
51 virtual void OnNetworkReady() {}
54 NetworkStateInformer();
56 void Init();
58 // Adds observer to be notified when network state has been changed.
59 void AddObserver(NetworkStateInformerObserver* observer);
61 // Removes 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);
86 private:
87 friend class base::RefCounted<NetworkStateInformer>;
89 ~NetworkStateInformer() override;
91 bool UpdateState();
93 void UpdateStateAndNotify();
95 void SendStateToObservers(NetworkError::ErrorReason reason);
97 State state_;
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_