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_UI_WEBUI_CHROMEOS_LOGIN_NETWORK_STATE_INFORMER_H_ 6 #define CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_NETWORK_STATE_INFORMER_H_ 7 8 #include <map> 9 #include <string> 10 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" 23 24 namespace chromeos { 25 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 43 }; 44 45 class NetworkStateInformerObserver { 46 public: 47 NetworkStateInformerObserver() {} 48 virtual ~NetworkStateInformerObserver() {} 49 50 virtual void UpdateState(ErrorScreenActor::ErrorReason reason) = 0; 51 virtual void OnNetworkReady() {} 52 }; 53 54 NetworkStateInformer(); 55 56 void Init(); 57 58 // Adds observer to be notified when network state has been changed. 59 void AddObserver(NetworkStateInformerObserver* observer); 60 61 // Removes observer. 62 void RemoveObserver(NetworkStateInformerObserver* observer); 63 64 // NetworkStateHandlerObserver implementation: 65 virtual void DefaultNetworkChanged(const NetworkState* network) OVERRIDE; 66 67 // NetworkPortalDetector::Observer implementation: 68 virtual void OnPortalDetectionCompleted( 69 const NetworkState* network, 70 const NetworkPortalDetector::CaptivePortalState& state) OVERRIDE; 71 72 // content::NotificationObserver implementation. 73 virtual void Observe(int type, 74 const content::NotificationSource& source, 75 const content::NotificationDetails& details) OVERRIDE; 76 77 // CaptivePortalWindowProxyDelegate implementation: 78 virtual void OnPortalDetected() OVERRIDE; 79 80 State state() const { return state_; } 81 std::string network_path() const { return network_path_; } 82 std::string network_type() const { return network_type_; } 83 84 static const char* StatusString(State state); 85 86 private: 87 friend class base::RefCounted<NetworkStateInformer>; 88 89 virtual ~NetworkStateInformer(); 90 91 bool UpdateState(); 92 93 void UpdateStateAndNotify(); 94 95 void SendStateToObservers(ErrorScreenActor::ErrorReason reason); 96 97 State state_; 98 std::string network_path_; 99 std::string network_type_; 100 101 ObserverList<NetworkStateInformerObserver> observers_; 102 content::NotificationRegistrar registrar_; 103 104 base::WeakPtrFactory<NetworkStateInformer> weak_ptr_factory_; 105 }; 106 107 } // namespace chromeos 108 109 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_NETWORK_STATE_INFORMER_H_ 110