OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_CHROMEOS_CROS_NETWORK_LIBRARY_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_NETWORK_LIBRARY_H_ |
6 #define CHROME_BROWSER_CHROMEOS_CROS_NETWORK_LIBRARY_H_ | 6 #define CHROME_BROWSER_CHROMEOS_NETWORK_LIBRARY_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
12 #include "base/platform_thread.h" | 12 #include "base/platform_thread.h" |
13 #include "base/singleton.h" | 13 #include "base/singleton.h" |
14 #include "base/string16.h" | 14 #include "base/string16.h" |
15 #include "base/timer.h" | 15 #include "base/timer.h" |
16 #include "net/url_request/url_request_job_tracker.h" | 16 #include "net/url_request/url_request_job_tracker.h" |
17 #include "third_party/cros/chromeos_network.h" | 17 #include "third_party/cros/chromeos_network.h" |
18 | 18 |
| 19 namespace chromeos { |
| 20 |
19 struct WifiNetwork { | 21 struct WifiNetwork { |
20 WifiNetwork() | 22 WifiNetwork() |
21 : encrypted(false), | 23 : encrypted(false), |
22 encryption(chromeos::NONE), | 24 encryption(chromeos::NONE), |
23 strength(0), | 25 strength(0), |
24 connecting(false), | 26 connecting(false), |
25 connected(false), | 27 connected(false), |
26 destroyed(false) {} | 28 destroyed(false) {} |
27 WifiNetwork(const std::string& ssid, bool encrypted, | 29 WifiNetwork(const std::string& ssid, bool encrypted, |
28 chromeos::EncryptionType encryption, int strength, | 30 chromeos::EncryptionType encryption, int strength, |
(...skipping 16 matching lines...) Expand all Loading... |
45 chromeos::EncryptionType encryption; | 47 chromeos::EncryptionType encryption; |
46 int strength; | 48 int strength; |
47 bool connecting; | 49 bool connecting; |
48 bool connected; | 50 bool connected; |
49 bool destroyed; | 51 bool destroyed; |
50 }; | 52 }; |
51 typedef std::vector<WifiNetwork> WifiNetworkVector; | 53 typedef std::vector<WifiNetwork> WifiNetworkVector; |
52 | 54 |
53 // This class handles the interaction with the ChromeOS network library APIs. | 55 // 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 | 56 // Classes can add themselves as observers. Users can get an instance of this |
55 // library class like this: CrosNetworkLibrary::Get() | 57 // library class like this: NetworkLibrary::Get() |
56 class CrosNetworkLibrary : public URLRequestJobTracker::JobObserver { | 58 class NetworkLibrary : public URLRequestJobTracker::JobObserver { |
57 public: | 59 public: |
58 class Observer { | 60 class Observer { |
59 public: | 61 public: |
60 // A bitfield mask for traffic types. | 62 // A bitfield mask for traffic types. |
61 enum TrafficTypes { | 63 enum TrafficTypes { |
62 TRAFFIC_DOWNLOAD = 0x1, | 64 TRAFFIC_DOWNLOAD = 0x1, |
63 TRAFFIC_UPLOAD = 0x2, | 65 TRAFFIC_UPLOAD = 0x2, |
64 } TrafficTypeMasks; | 66 } TrafficTypeMasks; |
65 | 67 |
66 // Called when the network has changed. (wifi networks, and ethernet) | 68 // Called when the network has changed. (wifi networks, and ethernet) |
67 virtual void NetworkChanged(CrosNetworkLibrary* obj) = 0; | 69 virtual void NetworkChanged(NetworkLibrary* obj) = 0; |
68 | 70 |
69 // Called when network traffic has been detected. | 71 // Called when network traffic has been detected. |
70 // Takes a bitfield of TrafficTypeMasks. | 72 // Takes a bitfield of TrafficTypeMasks. |
71 virtual void NetworkTraffic(CrosNetworkLibrary* obj, int traffic_type) = 0; | 73 virtual void NetworkTraffic(NetworkLibrary* obj, int traffic_type) = 0; |
72 }; | 74 }; |
73 | 75 |
74 // This gets the singleton CrosNetworkLibrary | 76 // This gets the singleton NetworkLibrary |
75 static CrosNetworkLibrary* Get(); | 77 static NetworkLibrary* Get(); |
76 | 78 |
77 // Returns true if the ChromeOS library was loaded. | 79 // Returns true if the ChromeOS library was loaded. |
78 static bool loaded(); | 80 static bool loaded(); |
79 | 81 |
80 // URLRequestJobTracker::JobObserver methods (called on the IO thread): | 82 // URLRequestJobTracker::JobObserver methods (called on the IO thread): |
81 virtual void OnJobAdded(URLRequestJob* job); | 83 virtual void OnJobAdded(URLRequestJob* job); |
82 virtual void OnJobRemoved(URLRequestJob* job); | 84 virtual void OnJobRemoved(URLRequestJob* job); |
83 virtual void OnJobDone(URLRequestJob* job, const URLRequestStatus& status); | 85 virtual void OnJobDone(URLRequestJob* job, const URLRequestStatus& status); |
84 virtual void OnJobRedirect(URLRequestJob* job, const GURL& location, | 86 virtual void OnJobRedirect(URLRequestJob* job, const GURL& location, |
85 int status_code); | 87 int status_code); |
86 virtual void OnBytesRead(URLRequestJob* job, int byte_count); | 88 virtual void OnBytesRead(URLRequestJob* job, int byte_count); |
87 | 89 |
88 void AddObserver(Observer* observer); | 90 void AddObserver(Observer* observer); |
89 void RemoveObserver(Observer* observer); | 91 void RemoveObserver(Observer* observer); |
90 | 92 |
91 bool ethernet_connected() const { return ethernet_connected_; } | 93 bool ethernet_connected() const { return ethernet_connected_; } |
92 const std::string& wifi_ssid() const { return wifi_.ssid; } | 94 const std::string& wifi_ssid() const { return wifi_.ssid; } |
93 bool wifi_connecting() const { return wifi_.connecting; } | 95 bool wifi_connecting() const { return wifi_.connecting; } |
94 int wifi_strength() const { return wifi_.strength; } | 96 int wifi_strength() const { return wifi_.strength; } |
95 | 97 |
96 // Returns the current list of wifi networks. | 98 // Returns the current list of wifi networks. |
97 const WifiNetworkVector& wifi_networks() const { return wifi_networks_; } | 99 const WifiNetworkVector& wifi_networks() const { return wifi_networks_; } |
98 | 100 |
99 // Connect to the specified wireless network with password. | 101 // Connect to the specified wireless network with password. |
100 void ConnectToWifiNetwork(WifiNetwork network, const string16& password); | 102 void ConnectToWifiNetwork(WifiNetwork network, const string16& password); |
101 | 103 |
102 private: | 104 private: |
103 friend struct DefaultSingletonTraits<CrosNetworkLibrary>; | 105 friend struct DefaultSingletonTraits<NetworkLibrary>; |
104 | 106 |
105 CrosNetworkLibrary(); | 107 NetworkLibrary(); |
106 ~CrosNetworkLibrary(); | 108 ~NetworkLibrary(); |
107 | 109 |
108 // This method is called when there's a change in network status. | 110 // This method is called when there's a change in network status. |
109 // This method is called on a background thread. | 111 // This method is called on a background thread. |
110 static void NetworkStatusChangedHandler(void* object, | 112 static void NetworkStatusChangedHandler(void* object, |
111 const chromeos::ServiceStatus& service_status); | 113 const chromeos::ServiceStatus& service_status); |
112 | 114 |
113 // This parses ServiceStatus and creates a WifiNetworkVector of wifi networks. | 115 // This parses ServiceStatus and creates a WifiNetworkVector of wifi networks. |
114 // It also sets ethernet_connected depending on if we have ethernet or not. | 116 // It also sets ethernet_connected depending on if we have ethernet or not. |
115 static void ParseNetworks(const chromeos::ServiceStatus& service_status, | 117 static void ParseNetworks(const chromeos::ServiceStatus& service_status, |
116 WifiNetworkVector* networks, | 118 WifiNetworkVector* networks, |
(...skipping 24 matching lines...) Expand all Loading... |
141 // This will notify all obeservers on the UI thread. | 143 // This will notify all obeservers on the UI thread. |
142 void NotifyObservers(); | 144 void NotifyObservers(); |
143 | 145 |
144 ObserverList<Observer> observers_; | 146 ObserverList<Observer> observers_; |
145 | 147 |
146 // The amount of time to wait between each NetworkTraffic notifications. | 148 // The amount of time to wait between each NetworkTraffic notifications. |
147 static const int kNetworkTrafficeTimerSecs; | 149 static const int kNetworkTrafficeTimerSecs; |
148 | 150 |
149 // Timer for sending NetworkTraffic notification every | 151 // Timer for sending NetworkTraffic notification every |
150 // kNetworkTrafficeTimerSecs seconds. | 152 // kNetworkTrafficeTimerSecs seconds. |
151 base::OneShotTimer<CrosNetworkLibrary> timer_; | 153 base::OneShotTimer<NetworkLibrary> timer_; |
152 | 154 |
153 // The current traffic type that will be sent out for the next NetworkTraffic | 155 // The current traffic type that will be sent out for the next NetworkTraffic |
154 // notification. This is a bitfield of TrafficTypeMasks. | 156 // notification. This is a bitfield of TrafficTypeMasks. |
155 int traffic_type_; | 157 int traffic_type_; |
156 | 158 |
157 // The network status connection for monitoring network status changes. | 159 // The network status connection for monitoring network status changes. |
158 chromeos::NetworkStatusConnection network_status_connection_; | 160 chromeos::NetworkStatusConnection network_status_connection_; |
159 | 161 |
160 // Whether or not we are connected to the ethernet line. | 162 // Whether or not we are connected to the ethernet line. |
161 bool ethernet_connected_; | 163 bool ethernet_connected_; |
162 | 164 |
163 // The list of available wifi networks. | 165 // The list of available wifi networks. |
164 WifiNetworkVector wifi_networks_; | 166 WifiNetworkVector wifi_networks_; |
165 | 167 |
166 // The current connected (or connecting) wifi network. | 168 // The current connected (or connecting) wifi network. |
167 WifiNetwork wifi_; | 169 WifiNetwork wifi_; |
168 | 170 |
169 DISALLOW_COPY_AND_ASSIGN(CrosNetworkLibrary); | 171 DISALLOW_COPY_AND_ASSIGN(NetworkLibrary); |
170 }; | 172 }; |
171 | 173 |
172 #endif // CHROME_BROWSER_CHROMEOS_CROS_NETWORK_LIBRARY_H_ | 174 } // namespace chromeos |
| 175 |
| 176 #endif // CHROME_BROWSER_CHROMEOS_NETWORK_LIBRARY_H_ |
OLD | NEW |