summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/network_state_notifier.h
blob: 94e57f41432a9385a5c9d8ed361977b99325d66a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_CHROMEOS_NETWORK_STATE_NOTIFIER_H_
#define CHROME_BROWSER_CHROMEOS_NETWORK_STATE_NOTIFIER_H_
#pragma once

#include "chrome/browser/chromeos/cros/network_library.h"

#include "base/singleton.h"
#include "base/task.h"
#include "base/time.h"

namespace chromeos {

// NetworkStateDetails contains the information about
// network status.
class NetworkStateDetails {
 public:
  enum State {
    UNKNOWN = 0,
    DISCONNECTED,
    CONNECTING,
    CONNECTED,
  };

  State state() const {
    return state_;
  }

 private:
  friend class NetworkStateNotifier;

  explicit NetworkStateDetails(State state)
      : state_(state) {
  }

  State state_;

  DISALLOW_COPY_AND_ASSIGN(NetworkStateDetails);
};

// NetworkStateNotifier sends notification when network state has
// chagned. Notification is sent in UI thread.
// TODO(oshima): port this to other platform. merge with
// NetworkChangeNotifier if possible.
class NetworkStateNotifier : public NetworkLibrary::NetworkManagerObserver {
 public:
  // Returns the singleton instance of the network state notifier;
  static NetworkStateNotifier* Get();

  // The duration of being in offline. The value is undefined when
  // when network is connected.
  static base::TimeDelta GetOfflineDuration();

  // Returns true if the network is connected.
  static bool is_connected() {
    return Get()->state_ == NetworkStateDetails::CONNECTED;
  }

  // NetworkLibrary::NetworkManagerObserver implementation.
  virtual void OnNetworkManagerChanged(NetworkLibrary* cros);

 private:
  friend struct DefaultSingletonTraits<NetworkStateNotifier>;

  // Retrieve the current state from libcros.
  static NetworkStateDetails::State RetrieveState();

  NetworkStateNotifier();
  virtual ~NetworkStateNotifier();

  // Update the current state and sends notification to observers.
  // This should be invoked in UI thread.
  void UpdateNetworkState(NetworkStateDetails::State new_state);

  // A factory to post a task in UI thread.
  ScopedRunnableMethodFactory<NetworkStateNotifier> task_factory_;

  // The current network state.
  NetworkStateDetails::State state_;

  // The start time of offline.
  base::Time offline_start_time_;

  DISALLOW_COPY_AND_ASSIGN(NetworkStateNotifier);
};

}  // namespace chromeos

#endif  // CHROME_BROWSER_CHROMEOS_NETWORK_STATE_NOTIFIER_H_