Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(193)

Side by Side Diff: chrome/browser/chromeos/cros/network_library.cc

Issue 2010001: Refactor WifiNetwork, CellularNetwork, and EthernetNetwork into classes to ma... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 #include "chrome/browser/chromeos/cros/network_library.h" 5 #include "chrome/browser/chromeos/cros/network_library.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/utf_string_conversions.h" 9 #include "base/utf_string_conversions.h"
10 #include "chrome/browser/chrome_thread.h" 10 #include "chrome/browser/chrome_thread.h"
11 #include "chrome/browser/chromeos/cros/cros_library.h" 11 #include "chrome/browser/chromeos/cros/cros_library.h"
12 #include "net/url_request/url_request_job.h" 12 #include "net/url_request/url_request_job.h"
13 13
14 // Allows InvokeLater without adding refcounting. This class is a Singleton and 14 // Allows InvokeLater without adding refcounting. This class is a Singleton and
15 // won't be deleted until it's last InvokeLater is run. 15 // won't be deleted until it's last InvokeLater is run.
16 template <> 16 template <>
17 struct RunnableMethodTraits<chromeos::NetworkLibraryImpl> { 17 struct RunnableMethodTraits<chromeos::NetworkLibraryImpl> {
18 void RetainCallee(chromeos::NetworkLibraryImpl* obj) {} 18 void RetainCallee(chromeos::NetworkLibraryImpl* obj) {}
19 void ReleaseCallee(chromeos::NetworkLibraryImpl* obj) {} 19 void ReleaseCallee(chromeos::NetworkLibraryImpl* obj) {}
20 }; 20 };
21 21
22 namespace chromeos { 22 namespace chromeos {
23 23
24 //////////////////////////////////////////////////////////////////////////////// 24 ////////////////////////////////////////////////////////////////////////////////
25 // Network
26
27 void Network::Clear() {
28 connecting_ = false;
29 connected_ = false;
30 service_path_.clear();
31 device_path_.clear();
32 ip_address_.clear();
33 }
34
35 void Network::ConfigureFromService(const ServiceInfo& service) {
36 connecting_ = service.state == STATE_ASSOCIATION ||
37 service.state == STATE_CONFIGURATION ||
38 service.state == STATE_CARRIER;
39 connected_ = service.state == STATE_READY;
40 service_path_ = service.service_path;
41 device_path_ = service.device_path ? service.device_path : std::string();
42 ip_address_.clear();
43 // If connected, get ip config.
44 if (connected_ && service.device_path) {
45 IPConfigStatus* ipconfig_status = ListIPConfigs(service.device_path);
46 if (ipconfig_status) {
47 for (int i = 0; i < ipconfig_status->size; i++) {
48 IPConfig ipconfig = ipconfig_status->ips[i];
49 if (strlen(ipconfig.address) > 0)
50 ip_address_ = ipconfig.address;
51 }
52 FreeIPConfigStatus(ipconfig_status);
53 }
54 }
55 }
56
57 ////////////////////////////////////////////////////////////////////////////////
58 // WirelessNetwork
59
60 void WirelessNetwork::Clear() {
61 Network::Clear();
62 name_.clear();
63 strength_ = 0;
64 auto_connect_ = false;
65 }
66
67 void WirelessNetwork::ConfigureFromService(const ServiceInfo& service) {
68 Network::ConfigureFromService(service);
69 name_ = service.name;
70 strength_ = service.strength;
71 auto_connect_ = service.auto_connect;
72 }
73
74 ////////////////////////////////////////////////////////////////////////////////
75 // WifiNetwork
76
77 void WifiNetwork::Clear() {
78 WirelessNetwork::Clear();
79 encryption_ = SECURITY_NONE;
80 passphrase_.clear();
81 }
82
83 void WifiNetwork::ConfigureFromService(const ServiceInfo& service) {
84 WirelessNetwork::ConfigureFromService(service);
85 encryption_ = service.security;
86 passphrase_ = service.passphrase;
87 }
88
89 ////////////////////////////////////////////////////////////////////////////////
25 // NetworkLibrary 90 // NetworkLibrary
26 91
27 // static 92 // static
28 const int NetworkLibraryImpl::kNetworkTrafficeTimerSecs = 1; 93 const int NetworkLibraryImpl::kNetworkTrafficeTimerSecs = 1;
29 94
30 NetworkLibraryImpl::NetworkLibraryImpl() 95 NetworkLibraryImpl::NetworkLibraryImpl()
31 : traffic_type_(0), 96 : traffic_type_(0),
32 network_status_connection_(NULL), 97 network_status_connection_(NULL),
33 available_devices_(0), 98 available_devices_(0),
34 enabled_devices_(0), 99 enabled_devices_(0),
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 if (CrosLibrary::Get()->EnsureLoaded()) { 149 if (CrosLibrary::Get()->EnsureLoaded()) {
85 RequestScan(TYPE_WIFI); 150 RequestScan(TYPE_WIFI);
86 } 151 }
87 } 152 }
88 153
89 void NetworkLibraryImpl::ConnectToWifiNetwork(WifiNetwork network, 154 void NetworkLibraryImpl::ConnectToWifiNetwork(WifiNetwork network,
90 const string16& password, 155 const string16& password,
91 const string16& identity, 156 const string16& identity,
92 const string16& certpath) { 157 const string16& certpath) {
93 if (CrosLibrary::Get()->EnsureLoaded()) { 158 if (CrosLibrary::Get()->EnsureLoaded()) {
94 ConnectToNetworkWithCertInfo(network.service_path.c_str(), 159 ConnectToNetworkWithCertInfo(network.service_path().c_str(),
95 password.empty() ? NULL : UTF16ToUTF8(password).c_str(), 160 password.empty() ? NULL : UTF16ToUTF8(password).c_str(),
96 identity.empty() ? NULL : UTF16ToUTF8(identity).c_str(), 161 identity.empty() ? NULL : UTF16ToUTF8(identity).c_str(),
97 certpath.empty() ? NULL : UTF16ToUTF8(certpath).c_str()); 162 certpath.empty() ? NULL : UTF16ToUTF8(certpath).c_str());
98 } 163 }
99 } 164 }
100 165
101 void NetworkLibraryImpl::ConnectToWifiNetwork(const string16& ssid, 166 void NetworkLibraryImpl::ConnectToWifiNetwork(const string16& ssid,
102 const string16& password, 167 const string16& password,
103 const string16& identity, 168 const string16& identity,
104 const string16& certpath, 169 const string16& certpath,
(...skipping 15 matching lines...) Expand all
120 FreeServiceInfo(service); 185 FreeServiceInfo(service);
121 } else { 186 } else {
122 LOG(WARNING) << "Cannot find hidden network: " << ssid; 187 LOG(WARNING) << "Cannot find hidden network: " << ssid;
123 // TODO(chocobo): Show error message. 188 // TODO(chocobo): Show error message.
124 } 189 }
125 } 190 }
126 } 191 }
127 192
128 void NetworkLibraryImpl::ConnectToCellularNetwork(CellularNetwork network) { 193 void NetworkLibraryImpl::ConnectToCellularNetwork(CellularNetwork network) {
129 if (CrosLibrary::Get()->EnsureLoaded()) { 194 if (CrosLibrary::Get()->EnsureLoaded()) {
130 ConnectToNetwork(network.service_path.c_str(), NULL); 195 ConnectToNetwork(network.service_path().c_str(), NULL);
131 } 196 }
132 } 197 }
133 198
134 void NetworkLibraryImpl::DisconnectFromWifiNetwork(const WifiNetwork& network) { 199 void NetworkLibraryImpl::DisconnectFromWirelessNetwork(
200 const WirelessNetwork& network) {
135 if (CrosLibrary::Get()->EnsureLoaded()) { 201 if (CrosLibrary::Get()->EnsureLoaded()) {
136 DisconnectFromNetwork(network.service_path.c_str()); 202 DisconnectFromNetwork(network.service_path().c_str());
137 }
138 }
139
140 void NetworkLibraryImpl::DisconnectFromCellularNetwork(
141 const CellularNetwork& network) {
142 if (CrosLibrary::Get()->EnsureLoaded()) {
143 DisconnectFromNetwork(network.service_path.c_str());
144 } 203 }
145 } 204 }
146 205
147 void NetworkLibraryImpl::SaveWifiNetwork(const WifiNetwork& network) { 206 void NetworkLibraryImpl::SaveWifiNetwork(const WifiNetwork& network) {
148 if (CrosLibrary::Get()->EnsureLoaded()) { 207 if (CrosLibrary::Get()->EnsureLoaded()) {
149 SetPassphrase(network.service_path.c_str(), network.passphrase.c_str()); 208 SetPassphrase(network.service_path().c_str(), network.passphrase().c_str());
150 SetAutoConnect(network.service_path.c_str(), network.auto_connect); 209 SetAutoConnect(network.service_path().c_str(), network.auto_connect());
151 } 210 }
152 } 211 }
153 212
154 void NetworkLibraryImpl::ForgetWifiNetwork(const WifiNetwork& network) { 213 void NetworkLibraryImpl::ForgetWirelessNetwork(const WirelessNetwork& network) {
155 if (CrosLibrary::Get()->EnsureLoaded()) { 214 if (CrosLibrary::Get()->EnsureLoaded()) {
156 DeleteRememberedService(network.service_path.c_str()); 215 DeleteRememberedService(network.service_path().c_str());
157 }
158 }
159
160 void NetworkLibraryImpl::ForgetCellularNetwork(const CellularNetwork& network) {
161 if (CrosLibrary::Get()->EnsureLoaded()) {
162 DeleteRememberedService(network.service_path.c_str());
163 } 216 }
164 } 217 }
165 218
166 void NetworkLibraryImpl::EnableEthernetNetworkDevice(bool enable) { 219 void NetworkLibraryImpl::EnableEthernetNetworkDevice(bool enable) {
167 EnableNetworkDeviceType(TYPE_ETHERNET, enable); 220 EnableNetworkDeviceType(TYPE_ETHERNET, enable);
168 } 221 }
169 222
170 void NetworkLibraryImpl::EnableWifiNetworkDevice(bool enable) { 223 void NetworkLibraryImpl::EnableWifiNetworkDevice(bool enable) {
171 EnableNetworkDeviceType(TYPE_WIFI, enable); 224 EnableNetworkDeviceType(TYPE_WIFI, enable);
172 } 225 }
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 } 279 }
227 280
228 // static 281 // static
229 void NetworkLibraryImpl::ParseSystem(SystemInfo* system, 282 void NetworkLibraryImpl::ParseSystem(SystemInfo* system,
230 EthernetNetwork* ethernet, 283 EthernetNetwork* ethernet,
231 WifiNetworkVector* wifi_networks, 284 WifiNetworkVector* wifi_networks,
232 CellularNetworkVector* cellular_networks, 285 CellularNetworkVector* cellular_networks,
233 WifiNetworkVector* remembered_wifi_networks, 286 WifiNetworkVector* remembered_wifi_networks,
234 CellularNetworkVector* remembered_cellular_networks) { 287 CellularNetworkVector* remembered_cellular_networks) {
235 DLOG(INFO) << "ParseSystem:"; 288 DLOG(INFO) << "ParseSystem:";
236 bool ethernet_found = false; 289 ethernet->Clear();
237 for (int i = 0; i < system->service_size; i++) { 290 for (int i = 0; i < system->service_size; i++) {
238 const ServiceInfo& service = system->services[i]; 291 const ServiceInfo& service = system->services[i];
239 DLOG(INFO) << " (" << service.type << 292 DLOG(INFO) << " (" << service.type <<
240 ") " << service.name << 293 ") " << service.name <<
241 " mode=" << service.mode << 294 " mode=" << service.mode <<
242 " state=" << service.state << 295 " state=" << service.state <<
243 " sec=" << service.security << 296 " sec=" << service.security <<
244 " req=" << service.passphrase_required << 297 " req=" << service.passphrase_required <<
245 " pass=" << service.passphrase << 298 " pass=" << service.passphrase <<
246 " str=" << service.strength << 299 " str=" << service.strength <<
247 " fav=" << service.favorite << 300 " fav=" << service.favorite <<
248 " auto=" << service.auto_connect << 301 " auto=" << service.auto_connect <<
249 " error=" << service.error; 302 " error=" << service.error;
250 bool connecting = service.state == STATE_ASSOCIATION || 303 if (service.type == TYPE_ETHERNET)
251 service.state == STATE_CONFIGURATION || 304 ethernet->ConfigureFromService(service);
252 service.state == STATE_CARRIER; 305 else if (service.type == TYPE_WIFI)
253 bool connected = service.state == STATE_READY; 306 wifi_networks->push_back(WifiNetwork(service));
254 // if connected, get ip config 307 else if (service.type == TYPE_CELLULAR)
255 std::string ip_address; 308 cellular_networks->push_back(CellularNetwork(service));
256 if (connected && service.device_path) {
257 IPConfigStatus* ipconfig_status = ListIPConfigs(service.device_path);
258 if (ipconfig_status) {
259 for (int i = 0; i < ipconfig_status->size; i++) {
260 IPConfig ipconfig = ipconfig_status->ips[i];
261 if (strlen(ipconfig.address) > 0)
262 ip_address = ipconfig.address;
263 DLOG(INFO) << " ipconfig: " <<
264 " type=" << ipconfig.type <<
265 " address=" << ipconfig.address <<
266 " mtu=" << ipconfig.mtu <<
267 " netmask=" << ipconfig.netmask <<
268 " broadcast=" << ipconfig.broadcast <<
269 " peer_address=" << ipconfig.peer_address <<
270 " gateway=" << ipconfig.gateway <<
271 " domainname=" << ipconfig.domainname <<
272 " name_servers=" << ipconfig.name_servers;
273 }
274 FreeIPConfigStatus(ipconfig_status);
275 }
276 }
277 if (service.type == TYPE_ETHERNET) {
278 ethernet_found = true;
279 ethernet->connecting = connecting;
280 ethernet->connected = connected;
281 ethernet->device_path = service.device_path ? service.device_path :
282 std::string();
283 ethernet->ip_address = ip_address;
284 } else if (service.type == TYPE_WIFI) {
285 wifi_networks->push_back(WifiNetwork(service,
286 connecting,
287 connected,
288 ip_address));
289 } else if (service.type == TYPE_CELLULAR) {
290 cellular_networks->push_back(CellularNetwork(service,
291 connecting,
292 connected,
293 ip_address));
294 }
295 }
296 if (!ethernet_found) {
297 ethernet->connecting = false;
298 ethernet->connected = false;
299 ethernet->device_path = std::string();
300 ethernet->ip_address = std::string();
301 } 309 }
302 DLOG(INFO) << "Remembered networks:"; 310 DLOG(INFO) << "Remembered networks:";
303 for (int i = 0; i < system->remembered_service_size; i++) { 311 for (int i = 0; i < system->remembered_service_size; i++) {
304 const ServiceInfo& service = system->remembered_services[i]; 312 const ServiceInfo& service = system->remembered_services[i];
305 // Only serices marked as auto_connect are considered remembered networks. 313 // Only serices marked as auto_connect are considered remembered networks.
306 // TODO(chocobo): Don't add to remembered service if currently available. 314 // TODO(chocobo): Don't add to remembered service if currently available.
307 if (service.auto_connect) { 315 if (service.auto_connect) {
308 DLOG(INFO) << " (" << service.type << 316 DLOG(INFO) << " (" << service.type <<
309 ") " << service.name << 317 ") " << service.name <<
310 " mode=" << service.mode << 318 " mode=" << service.mode <<
311 " sec=" << service.security << 319 " sec=" << service.security <<
312 " pass=" << service.passphrase << 320 " pass=" << service.passphrase <<
313 " auto=" << service.auto_connect; 321 " auto=" << service.auto_connect;
314 if (service.type == TYPE_WIFI) { 322 if (service.type == TYPE_WIFI)
315 remembered_wifi_networks->push_back(WifiNetwork(service, 323 remembered_wifi_networks->push_back(WifiNetwork(service));
316 false, 324 else if (service.type == TYPE_CELLULAR)
317 false, 325 remembered_cellular_networks->push_back(CellularNetwork(service));
318 std::string()));
319 } else if (service.type == TYPE_CELLULAR) {
320 remembered_cellular_networks->push_back(CellularNetwork(service,
321 false,
322 false,
323 std::string()));
324 }
325 } 326 }
326 } 327 }
327 } 328 }
328 329
329 void NetworkLibraryImpl::Init() { 330 void NetworkLibraryImpl::Init() {
330 // First, get the currently available networks. This data is cached 331 // First, get the currently available networks. This data is cached
331 // on the connman side, so the call should be quick. 332 // on the connman side, so the call should be quick.
332 SystemInfo* system = GetSystemInfo(); 333 SystemInfo* system = GetSystemInfo();
333 if (system) { 334 if (system) {
334 LOG(INFO) << "Getting initial CrOS network info."; 335 LOG(INFO) << "Getting initial CrOS network info.";
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 374
374 wifi_networks_.clear(); 375 wifi_networks_.clear();
375 cellular_networks_.clear(); 376 cellular_networks_.clear();
376 remembered_wifi_networks_.clear(); 377 remembered_wifi_networks_.clear();
377 remembered_cellular_networks_.clear(); 378 remembered_cellular_networks_.clear();
378 ParseSystem(system, &ethernet_, &wifi_networks_, &cellular_networks_, 379 ParseSystem(system, &ethernet_, &wifi_networks_, &cellular_networks_,
379 &remembered_wifi_networks_, &remembered_cellular_networks_); 380 &remembered_wifi_networks_, &remembered_cellular_networks_);
380 381
381 wifi_ = WifiNetwork(); 382 wifi_ = WifiNetwork();
382 for (size_t i = 0; i < wifi_networks_.size(); i++) { 383 for (size_t i = 0; i < wifi_networks_.size(); i++) {
383 if (wifi_networks_[i].connecting || wifi_networks_[i].connected) { 384 if (wifi_networks_[i].connecting_or_connected()) {
384 wifi_ = wifi_networks_[i]; 385 wifi_ = wifi_networks_[i];
385 break; // There is only one connected or connecting wifi network. 386 break; // There is only one connected or connecting wifi network.
386 } 387 }
387 } 388 }
388 cellular_ = CellularNetwork(); 389 cellular_ = CellularNetwork();
389 for (size_t i = 0; i < cellular_networks_.size(); i++) { 390 for (size_t i = 0; i < cellular_networks_.size(); i++) {
390 if (cellular_networks_[i].connecting || cellular_networks_[i].connected) { 391 if (cellular_networks_[i].connecting_or_connected()) {
391 cellular_ = cellular_networks_[i]; 392 cellular_ = cellular_networks_[i];
392 break; // There is only one connected or connecting cellular network. 393 break; // There is only one connected or connecting cellular network.
393 } 394 }
394 } 395 }
395 396
396 available_devices_ = system->available_technologies; 397 available_devices_ = system->available_technologies;
397 enabled_devices_ = system->enabled_technologies; 398 enabled_devices_ = system->enabled_technologies;
398 connected_devices_ = system->connected_technologies; 399 connected_devices_ = system->connected_technologies;
399 offline_mode_ = system->offline_mode; 400 offline_mode_ = system->offline_mode;
400 401
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
447 return ethernet_connected() || wifi_connected() || cellular_connected(); 448 return ethernet_connected() || wifi_connected() || cellular_connected();
448 } 449 }
449 450
450 bool NetworkLibraryImpl::Connecting() const { 451 bool NetworkLibraryImpl::Connecting() const {
451 return ethernet_connecting() || wifi_connecting() || cellular_connecting(); 452 return ethernet_connecting() || wifi_connecting() || cellular_connecting();
452 } 453 }
453 454
454 const std::string& NetworkLibraryImpl::IPAddress() const { 455 const std::string& NetworkLibraryImpl::IPAddress() const {
455 // Returns highest priority IP address. 456 // Returns highest priority IP address.
456 if (ethernet_connected()) 457 if (ethernet_connected())
457 return ethernet_.ip_address; 458 return ethernet_.ip_address();
458 if (wifi_connected()) 459 if (wifi_connected())
459 return wifi_.ip_address; 460 return wifi_.ip_address();
460 if (cellular_connected()) 461 if (cellular_connected())
461 return cellular_.ip_address; 462 return cellular_.ip_address();
462 return ethernet_.ip_address; 463 return ethernet_.ip_address();
463 } 464 }
464 465
465 } // namespace chromeos 466 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/cros/network_library.h ('k') | chrome/browser/chromeos/login/network_screen.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698