summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/network_library.h
blob: 021fd7dddcc91b3e2e370ed2a5f4ee3f232cd553 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright (c) 2009 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_LIBRARY_H_
#define CHROME_BROWSER_CHROMEOS_NETWORK_LIBRARY_H_

#include <string>
#include <vector>

#include "base/observer_list.h"
#include "base/platform_thread.h"
#include "base/singleton.h"
#include "base/string16.h"
#include "base/timer.h"
#include "net/url_request/url_request_job_tracker.h"
#include "third_party/cros/chromeos_network.h"

namespace chromeos {

struct EthernetNetwork {
  EthernetNetwork()
      : connecting(false),
        connected(false) {}
  EthernetNetwork(bool connecting, bool connected)
      : connecting(connecting),
        connected(connected) {}

  bool connecting;
  bool connected;
};

struct WifiNetwork {
  WifiNetwork()
      : encrypted(false),
        encryption(chromeos::NONE),
        strength(0),
        connecting(false),
        connected(false) {}
  WifiNetwork(const std::string& ssid, bool encrypted,
              chromeos::EncryptionType encryption, int strength,
              bool connecting, bool connected)
      : ssid(ssid),
        encrypted(encrypted),
        encryption(encryption),
        strength(strength),
        connecting(connecting),
        connected(connected) {}

  // WifiNetworks are sorted by ssids.
  bool operator< (const WifiNetwork& other) const {
    return ssid < other.ssid;
  }

  std::string ssid;
  bool encrypted;
  chromeos::EncryptionType encryption;
  int strength;
  bool connecting;
  bool connected;
};
typedef std::vector<WifiNetwork> WifiNetworkVector;

// This class handles the interaction with the ChromeOS network library APIs.
// Classes can add themselves as observers. Users can get an instance of this
// library class like this: NetworkLibrary::Get()
class NetworkLibrary : public URLRequestJobTracker::JobObserver {
 public:
  class Observer {
   public:
    // A bitfield mask for traffic types.
    enum TrafficTypes {
      TRAFFIC_DOWNLOAD = 0x1,
      TRAFFIC_UPLOAD = 0x2,
    } TrafficTypeMasks;

    // Called when the network has changed. (wifi networks, and ethernet)
    virtual void NetworkChanged(NetworkLibrary* obj) = 0;

    // Called when network traffic has been detected.
    // Takes a bitfield of TrafficTypeMasks.
    virtual void NetworkTraffic(NetworkLibrary* obj, int traffic_type) = 0;
  };

  // This gets the singleton NetworkLibrary
  static NetworkLibrary* Get();

  // Makes sure the library is loaded, loading it if necessary. Returns true if
  // the library has been successfully loaded.
  static bool EnsureLoaded();

  // URLRequestJobTracker::JobObserver methods (called on the IO thread):
  virtual void OnJobAdded(URLRequestJob* job);
  virtual void OnJobRemoved(URLRequestJob* job);
  virtual void OnJobDone(URLRequestJob* job, const URLRequestStatus& status);
  virtual void OnJobRedirect(URLRequestJob* job, const GURL& location,
                             int status_code);
  virtual void OnBytesRead(URLRequestJob* job, int byte_count);

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  bool ethernet_connecting() const { return ethernet_.connecting; }
  bool ethernet_connected() const { return ethernet_.connected; }
  const std::string& wifi_ssid() const { return wifi_.ssid; }
  bool wifi_connecting() const { return wifi_.connecting; }
  bool wifi_connected() const { return wifi_.connected; }
  int wifi_strength() const { return wifi_.strength; }

  // Returns the current list of wifi networks.
  const WifiNetworkVector& wifi_networks() const { return wifi_networks_; }

  // Connect to the specified wireless network with password.
  void ConnectToWifiNetwork(WifiNetwork network, const string16& password);

  bool ethernet_enabled() const { return network_devices_ & TYPE_ETHERNET; }
  bool wifi_enabled() const { return network_devices_ & TYPE_WIFI; }
  bool offline_mode() const { return offline_mode_; }

  // Enables/disables the ethernet network device.
  void EnableEthernetNetworkDevice(bool enable);

  // Enables/disables the wifi network device.
  void EnableWifiNetworkDevice(bool enable);

  // Enables/disables offline mode.
  void EnableOfflineMode(bool enable);

 private:
  friend struct DefaultSingletonTraits<NetworkLibrary>;

  NetworkLibrary();
  ~NetworkLibrary();

  // This method is called when there's a change in network status.
  // This method is called on a background thread.
  static void NetworkStatusChangedHandler(void* object,
      const chromeos::ServiceStatus& service_status);

  // This parses ServiceStatus and creates a WifiNetworkVector of wifi networks.
  // It also sets the ethernet connecting/connected status.
  static void ParseNetworks(const chromeos::ServiceStatus& service_status,
                            WifiNetworkVector* networks,
                            EthernetNetwork* ethernet);

  // This methods loads the initial list of networks on startup and starts the
  // monitoring of network changes.
  void Init();

  // Enables/disables the specified network device.
  void EnableNetworkDevice(chromeos::ConnectionType device, bool enable);

  // Update the network with the a list of wifi networks and ethernet status.
  // This will notify all the Observers.
  void UpdateNetworkStatus(const WifiNetworkVector& networks,
                           const EthernetNetwork& ethernet);

  // Checks network traffic to see if there is any uploading.
  // If there is download traffic, then true is passed in for download.
  // If there is network traffic then start timer that invokes
  // NetworkTrafficTimerFired.
  void CheckNetworkTraffic(bool download);

  // Called when the timer fires and we need to send out NetworkTraffic
  // notifications.
  void NetworkTrafficTimerFired();

  // This is a helper method to notify the observers on the UI thread.
  void NotifyNetworkTraffic(int traffic_type);

  // This will notify all obeservers on the UI thread.
  void NotifyObservers();

  ObserverList<Observer> observers_;

  // The amount of time to wait between each NetworkTraffic notifications.
  static const int kNetworkTrafficeTimerSecs;

  // Timer for sending NetworkTraffic notification every
  // kNetworkTrafficeTimerSecs seconds.
  base::OneShotTimer<NetworkLibrary> timer_;

  // The current traffic type that will be sent out for the next NetworkTraffic
  // notification. This is a bitfield of TrafficTypeMasks.
  int traffic_type_;

  // The network status connection for monitoring network status changes.
  chromeos::NetworkStatusConnection network_status_connection_;

  // The ethernet network.
  EthernetNetwork ethernet_;

  // The list of available wifi networks.
  WifiNetworkVector wifi_networks_;

  // The current connected (or connecting) wifi network.
  WifiNetwork wifi_;

  // The current enabled network devices. This is a bitwise flag of
  // ConnectionTypes.
  int network_devices_;

  bool offline_mode_;

  DISALLOW_COPY_AND_ASSIGN(NetworkLibrary);
};

}  // namespace chromeos

#endif  // CHROME_BROWSER_CHROMEOS_NETWORK_LIBRARY_H_