1// Copyright (c) 2011 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_CHROMEOS_NETWORK_STATE_NOTIFIER_H_ 6#define CHROME_BROWSER_CHROMEOS_NETWORK_STATE_NOTIFIER_H_ 7#pragma once 8 9#include "chrome/browser/chromeos/cros/network_library.h" 10 11#include "base/memory/singleton.h" 12#include "base/task.h" 13#include "base/time.h" 14 15namespace chromeos { 16 17// NetworkStateDetails contains the information about 18// network status. 19class NetworkStateDetails { 20 public: 21 enum State { 22 UNKNOWN = 0, 23 DISCONNECTED, 24 CONNECTING, 25 CONNECTED, 26 }; 27 28 State state() const { 29 return state_; 30 } 31 32 private: 33 friend class NetworkStateNotifier; 34 35 explicit NetworkStateDetails(State state) 36 : state_(state) { 37 } 38 39 State state_; 40 41 DISALLOW_COPY_AND_ASSIGN(NetworkStateDetails); 42}; 43 44// NetworkStateNotifier sends notification when network state has 45// chagned. Notification is sent in UI thread. 46// TODO(oshima): port this to other platform. merge with 47// NetworkChangeNotifier if possible. 48class NetworkStateNotifier : public NetworkLibrary::NetworkManagerObserver { 49 public: 50 // Returns the singleton instance of the network state notifier; 51 static NetworkStateNotifier* GetInstance(); 52 53 // The duration of being in offline. The value is undefined when 54 // when network is connected. 55 static base::TimeDelta GetOfflineDuration(); 56 57 // Returns true if the network is connected. 58 static bool is_connected() { 59 return GetInstance()->state_ == NetworkStateDetails::CONNECTED; 60 } 61 62 // NetworkLibrary::NetworkManagerObserver implementation. 63 virtual void OnNetworkManagerChanged(NetworkLibrary* cros); 64 65 private: 66 friend struct DefaultSingletonTraits<NetworkStateNotifier>; 67 68 // Retrieve the current state from libcros. 69 static NetworkStateDetails::State RetrieveState(); 70 71 NetworkStateNotifier(); 72 virtual ~NetworkStateNotifier(); 73 74 // Update the current state and sends notification to observers. 75 // This should be invoked in UI thread. 76 void UpdateNetworkState(NetworkStateDetails::State new_state); 77 78 // A factory to post a task in UI thread. 79 ScopedRunnableMethodFactory<NetworkStateNotifier> task_factory_; 80 81 // The current network state. 82 NetworkStateDetails::State state_; 83 84 // The start time of offline. 85 base::Time offline_start_time_; 86 87 DISALLOW_COPY_AND_ASSIGN(NetworkStateNotifier); 88}; 89 90} // namespace chromeos 91 92#endif // CHROME_BROWSER_CHROMEOS_NETWORK_STATE_NOTIFIER_H_ 93