OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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_CROS_NETWORK_LIBRARY_H_ | |
6 #define CHROME_BROWSER_CHROMEOS_CROS_NETWORK_LIBRARY_H_ | |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/observer_list.h" | |
12 #include "base/platform_thread.h" | |
13 #include "base/singleton.h" | |
14 #include "base/string16.h" | |
15 #include "base/timer.h" | |
16 #include "net/url_request/url_request_job_tracker.h" | |
17 #include "third_party/cros/chromeos_network.h" | |
18 | |
19 struct WifiNetwork { | |
20 WifiNetwork() | |
21 : encrypted(false), | |
22 encryption(chromeos::NONE), | |
23 strength(0), | |
24 connecting(false), | |
25 connected(false), | |
26 destroyed(false) {} | |
27 WifiNetwork(const std::string& ssid, bool encrypted, | |
28 chromeos::EncryptionType encryption, int strength, | |
29 bool connecting, bool connected) | |
30 : ssid(ssid), | |
31 encrypted(encrypted), | |
32 encryption(encryption), | |
33 strength(strength), | |
34 connecting(connecting), | |
35 connected(connected), | |
36 destroyed(false) {} | |
37 | |
38 // WifiNetworks are sorted by ssids. | |
39 bool operator< (const WifiNetwork& other) const { | |
40 return ssid < other.ssid; | |
41 } | |
42 | |
43 std::string ssid; | |
44 bool encrypted; | |
45 chromeos::EncryptionType encryption; | |
46 int strength; | |
47 bool connecting; | |
48 bool connected; | |
49 bool destroyed; | |
50 }; | |
51 typedef std::vector<WifiNetwork> WifiNetworkVector; | |
52 | |
53 // This class handles the interaction with the ChromeOS network library APIs. | |
54 // Classes can add themselves as observers. Users can get an instance of this | |
55 // library class like this: CrosNetworkLibrary::Get() | |
56 class CrosNetworkLibrary : public URLRequestJobTracker::JobObserver { | |
57 public: | |
58 class Observer { | |
59 public: | |
60 // A bitfield mask for traffic types. | |
61 enum TrafficTypes { | |
62 TRAFFIC_DOWNLOAD = 0x1, | |
63 TRAFFIC_UPLOAD = 0x2, | |
64 } TrafficTypeMasks; | |
65 | |
66 // Called when the network has changed. (wifi networks, and ethernet) | |
67 virtual void NetworkChanged(CrosNetworkLibrary* obj) = 0; | |
68 | |
69 // Called when network traffic has been detected. | |
70 // Takes a bitfield of TrafficTypeMasks. | |
71 virtual void NetworkTraffic(CrosNetworkLibrary* obj, int traffic_type) = 0; | |
72 }; | |
73 | |
74 // This gets the singleton CrosNetworkLibrary | |
75 static CrosNetworkLibrary* Get(); | |
76 | |
77 // Returns true if the ChromeOS library was loaded. | |
78 static bool loaded(); | |
79 | |
80 // URLRequestJobTracker::JobObserver methods (called on the IO thread): | |
81 virtual void OnJobAdded(URLRequestJob* job); | |
82 virtual void OnJobRemoved(URLRequestJob* job); | |
83 virtual void OnJobDone(URLRequestJob* job, const URLRequestStatus& status); | |
84 virtual void OnJobRedirect(URLRequestJob* job, const GURL& location, | |
85 int status_code); | |
86 virtual void OnBytesRead(URLRequestJob* job, int byte_count); | |
87 | |
88 void AddObserver(Observer* observer); | |
89 void RemoveObserver(Observer* observer); | |
90 | |
91 bool ethernet_connected() const { return ethernet_connected_; } | |
92 const std::string& wifi_ssid() const { return wifi_.ssid; } | |
93 bool wifi_connecting() const { return wifi_.connecting; } | |
94 int wifi_strength() const { return wifi_.strength; } | |
95 | |
96 // Returns the current list of wifi networks. | |
97 const WifiNetworkVector& wifi_networks() const { return wifi_networks_; } | |
98 | |
99 // Connect to the specified wireless network with password. | |
100 void ConnectToWifiNetwork(WifiNetwork network, const string16& password); | |
101 | |
102 private: | |
103 friend struct DefaultSingletonTraits<CrosNetworkLibrary>; | |
104 | |
105 CrosNetworkLibrary(); | |
106 ~CrosNetworkLibrary(); | |
107 | |
108 // This method is called when there's a change in network status. | |
109 // This method is called on a background thread. | |
110 static void NetworkStatusChangedHandler(void* object, | |
111 const chromeos::ServiceStatus& service_status); | |
112 | |
113 // This parses ServiceStatus and creates a WifiNetworkVector of wifi networks. | |
114 // It also sets ethernet_connected depending on if we have ethernet or not. | |
115 static void ParseNetworks(const chromeos::ServiceStatus& service_status, | |
116 WifiNetworkVector* networks, | |
117 bool* ethernet_connected); | |
118 | |
119 // This methods loads the initial list of networks on startup and starts the | |
120 // monitoring of network changes. | |
121 void Init(); | |
122 | |
123 // Update the network with the a list of wifi networks and ethernet status. | |
124 // This will notify all the Observers. | |
125 void UpdateNetworkStatus(const WifiNetworkVector& networks, | |
126 bool ethernet_connected); | |
127 | |
128 // Checks network traffic to see if there is any uploading. | |
129 // If there is download traffic, then true is passed in for download. | |
130 // If there is network traffic then start timer that invokes | |
131 // NetworkTrafficTimerFired. | |
132 void CheckNetworkTraffic(bool download); | |
133 | |
134 // Called when the timer fires and we need to send out NetworkTraffic | |
135 // notifications. | |
136 void NetworkTrafficTimerFired(); | |
137 | |
138 // This is a helper method to notify the observers on the UI thread. | |
139 void NotifyNetworkTraffic(int traffic_type); | |
140 | |
141 // This will notify all obeservers on the UI thread. | |
142 void NotifyObservers(); | |
143 | |
144 ObserverList<Observer> observers_; | |
145 | |
146 // The amount of time to wait between each NetworkTraffic notifications. | |
147 static const int kNetworkTrafficeTimerSecs; | |
148 | |
149 // Timer for sending NetworkTraffic notification every | |
150 // kNetworkTrafficeTimerSecs seconds. | |
151 base::OneShotTimer<CrosNetworkLibrary> timer_; | |
152 | |
153 // The current traffic type that will be sent out for the next NetworkTraffic | |
154 // notification. This is a bitfield of TrafficTypeMasks. | |
155 int traffic_type_; | |
156 | |
157 // The network status connection for monitoring network status changes. | |
158 chromeos::NetworkStatusConnection network_status_connection_; | |
159 | |
160 // Whether or not we are connected to the ethernet line. | |
161 bool ethernet_connected_; | |
162 | |
163 // The list of available wifi networks. | |
164 WifiNetworkVector wifi_networks_; | |
165 | |
166 // The current connected (or connecting) wifi network. | |
167 WifiNetwork wifi_; | |
168 | |
169 DISALLOW_COPY_AND_ASSIGN(CrosNetworkLibrary); | |
170 }; | |
171 | |
172 #endif // CHROME_BROWSER_CHROMEOS_CROS_NETWORK_LIBRARY_H_ | |
OLD | NEW |