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

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

Issue 3076029: Allow chrome for cros to be started with a username / password (Closed)
Patch Set: Only declare StubLogin on cros builds Created 10 years, 4 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
OLDNEW
1 // Copyright (c) 2010 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/string_number_conversions.h" 9 #include "base/string_number_conversions.h"
10 #include "base/string_util.h" 10 #include "base/string_util.h"
11 #include "base/utf_string_conversions.h" 11 #include "base/utf_string_conversions.h"
12 #include "chrome/browser/chrome_thread.h" 12 #include "chrome/browser/chrome_thread.h"
13 #include "chrome/browser/chromeos/cros/cros_library.h" 13 #include "chrome/browser/chromeos/cros/cros_library.h"
14 #include "net/url_request/url_request_job.h" 14 #include "net/url_request/url_request_job.h"
15 15
16 // Allows InvokeLater without adding refcounting. This class is a Singleton and
17 // won't be deleted until it's last InvokeLater is run.
18 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::NetworkLibraryImpl);
19
20 namespace chromeos { 16 namespace chromeos {
21 17
22 static const std::string kGoogleWifi = "Google"; 18 static const std::string kGoogleWifi = "Google";
23 static const std::string kGoogleAWifi = "Google-A"; 19 static const std::string kGoogleAWifi = "Google-A";
24 20
25 // Helper function to wrap Html with <th> tag. 21 // Helper function to wrap Html with <th> tag.
26 static std::string WrapWithTH(std::string text) { 22 static std::string WrapWithTH(std::string text) {
27 return "<th>" + text + "</th>"; 23 return "<th>" + text + "</th>";
28 } 24 }
29 25
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 case SECURITY_RSN: 197 case SECURITY_RSN:
202 return "RSN"; 198 return "RSN";
203 case SECURITY_8021X: 199 case SECURITY_8021X:
204 return "8021X"; 200 return "8021X";
205 } 201 }
206 return "Unknown";} 202 return "Unknown";}
207 203
208 //////////////////////////////////////////////////////////////////////////////// 204 ////////////////////////////////////////////////////////////////////////////////
209 // NetworkLibrary 205 // NetworkLibrary
210 206
211 // static 207 class NetworkLibraryImpl : public NetworkLibrary,
212 const int NetworkLibraryImpl::kNetworkTrafficeTimerSecs = 1; 208 public URLRequestJobTracker::JobObserver {
213 209 public:
214 NetworkLibraryImpl::NetworkLibraryImpl() 210 NetworkLibraryImpl()
215 : traffic_type_(0), 211 : traffic_type_(0),
216 network_status_connection_(NULL), 212 network_status_connection_(NULL),
217 available_devices_(0), 213 available_devices_(0),
218 enabled_devices_(0), 214 enabled_devices_(0),
219 connected_devices_(0), 215 connected_devices_(0),
220 offline_mode_(false) { 216 offline_mode_(false) {
221 if (CrosLibrary::Get()->EnsureLoaded()) { 217 if (CrosLibrary::Get()->EnsureLoaded()) {
222 Init(); 218 Init();
223 } else { 219 } else {
224 InitTestData(); 220 InitTestData();
225 } 221 }
226 g_url_request_job_tracker.AddObserver(this); 222 g_url_request_job_tracker.AddObserver(this);
227 } 223 }
228 224
229 NetworkLibraryImpl::~NetworkLibraryImpl() { 225 ~NetworkLibraryImpl() {
230 if (network_status_connection_) { 226 if (network_status_connection_) {
231 DisconnectMonitorNetwork(network_status_connection_); 227 DisconnectMonitorNetwork(network_status_connection_);
232 } 228 }
233 g_url_request_job_tracker.RemoveObserver(this); 229 g_url_request_job_tracker.RemoveObserver(this);
234 } 230 }
235 231
236 //////////////////////////////////////////////////////////////////////////////// 232 /////////////////////////////////////////////////////////////////////////////
237 // NetworkLibraryImpl, URLRequestJobTracker::JobObserver implementation: 233 // NetworkLibraryImpl, URLRequestJobTracker::JobObserver implementation:
238 234
239 void NetworkLibraryImpl::OnJobAdded(URLRequestJob* job) { 235 void OnJobAdded(URLRequestJob* job) {
240 CheckNetworkTraffic(false); 236 CheckNetworkTraffic(false);
241 } 237 }
242 238
243 void NetworkLibraryImpl::OnJobRemoved(URLRequestJob* job) { 239 void OnJobRemoved(URLRequestJob* job) {
244 CheckNetworkTraffic(false); 240 CheckNetworkTraffic(false);
245 } 241 }
246 242
247 void NetworkLibraryImpl::OnJobDone(URLRequestJob* job, 243 void OnJobDone(URLRequestJob* job, const URLRequestStatus& status) {
248 const URLRequestStatus& status) { 244 CheckNetworkTraffic(false);
249 CheckNetworkTraffic(false); 245 }
250 } 246
251 247 void OnJobRedirect(
252 void NetworkLibraryImpl::OnJobRedirect(URLRequestJob* job, const GURL& location, 248 URLRequestJob* job, const GURL& location, int status_code) {
253 int status_code) { 249 CheckNetworkTraffic(false);
254 CheckNetworkTraffic(false); 250 }
255 } 251
256 252 void OnBytesRead(URLRequestJob* job, int byte_count) {
257 void NetworkLibraryImpl::OnBytesRead(URLRequestJob* job, int byte_count) { 253 CheckNetworkTraffic(true);
258 CheckNetworkTraffic(true); 254 }
259 } 255
260 256 void AddObserver(Observer* observer) {
261 void NetworkLibraryImpl::AddObserver(Observer* observer) { 257 observers_.AddObserver(observer);
262 observers_.AddObserver(observer); 258 }
263 } 259
264 260 void RemoveObserver(Observer* observer) {
265 void NetworkLibraryImpl::RemoveObserver(Observer* observer) { 261 observers_.RemoveObserver(observer);
266 observers_.RemoveObserver(observer); 262 }
267 } 263
268 264 virtual const EthernetNetwork& ethernet_network() const { return ethernet_; }
269 //////////////////////////////////////////////////////////////////////////////// 265 virtual bool ethernet_connecting() const { return ethernet_.connecting(); }
270 266 virtual bool ethernet_connected() const { return ethernet_.connected(); }
271 bool NetworkLibraryImpl::FindWifiNetworkByPath( 267
272 const std::string& path, WifiNetwork* result) const { 268 virtual const std::string& wifi_name() const { return wifi_.name(); }
273 const WifiNetwork* wifi = 269 virtual bool wifi_connecting() const { return wifi_.connecting(); }
274 GetWirelessNetworkByPath(wifi_networks_, path); 270 virtual bool wifi_connected() const { return wifi_.connected(); }
275 if (wifi) { 271 virtual int wifi_strength() const { return wifi_.strength(); }
276 if (result) 272
277 *result = *wifi; 273 virtual const std::string& cellular_name() const { return cellular_.name(); }
274 virtual bool cellular_connecting() const { return cellular_.connecting(); }
275 virtual bool cellular_connected() const { return cellular_.connected(); }
276 virtual int cellular_strength() const { return cellular_.strength(); }
277
278 bool Connected() const {
279 return ethernet_connected() || wifi_connected() || cellular_connected();
280 }
281
282 bool Connecting() const {
283 return ethernet_connecting() || wifi_connecting() || cellular_connecting();
284 }
285
286 const std::string& IPAddress() const {
287 // Returns highest priority IP address.
288 if (ethernet_connected())
289 return ethernet_.ip_address();
290 if (wifi_connected())
291 return wifi_.ip_address();
292 if (cellular_connected())
293 return cellular_.ip_address();
294 return ethernet_.ip_address();
295 }
296
297 virtual const WifiNetworkVector& wifi_networks() const {
298 return wifi_networks_;
299 }
300
301 virtual const WifiNetworkVector& remembered_wifi_networks() const {
302 return remembered_wifi_networks_;
303 }
304
305 virtual const CellularNetworkVector& cellular_networks() const {
306 return cellular_networks_;
307 }
308
309 virtual const CellularNetworkVector& remembered_cellular_networks() const {
310 return remembered_cellular_networks_;
311 }
312
313 /////////////////////////////////////////////////////////////////////////////
314
315 bool FindWifiNetworkByPath(
316 const std::string& path, WifiNetwork* result) const {
317 const WifiNetwork* wifi =
318 GetWirelessNetworkByPath(wifi_networks_, path);
319 if (wifi) {
320 if (result)
321 *result = *wifi;
322 return true;
323 }
324 return false;
325 }
326
327 bool FindCellularNetworkByPath(
328 const std::string& path, CellularNetwork* result) const {
329 const CellularNetwork* cellular =
330 GetWirelessNetworkByPath(cellular_networks_, path);
331 if (cellular) {
332 if (result)
333 *result = *cellular;
334 return true;
335 }
336 return false;
337 }
338
339 void RequestWifiScan() {
340 if (CrosLibrary::Get()->EnsureLoaded()) {
341 RequestScan(TYPE_WIFI);
342 }
343 }
344
345 bool GetWifiAccessPoints(WifiAccessPointVector* result) {
346 if (!CrosLibrary::Get()->EnsureLoaded())
347 return false;
348 DeviceNetworkList* network_list = GetDeviceNetworkList();
349 if (network_list == NULL)
350 return false;
351 result->clear();
352 result->reserve(network_list->network_size);
353 const base::Time now = base::Time::Now();
354 for (size_t i = 0; i < network_list->network_size; ++i) {
355 DCHECK(network_list->networks[i].address);
356 DCHECK(network_list->networks[i].name);
357 WifiAccessPoint ap;
358 ap.mac_address = network_list->networks[i].address;
359 ap.name = network_list->networks[i].name;
360 ap.timestamp = now -
361 base::TimeDelta::FromSeconds(network_list->networks[i].age_seconds);
362 ap.signal_strength = network_list->networks[i].strength;
363 ap.channel = network_list->networks[i].channel;
364 result->push_back(ap);
365 }
366 FreeDeviceNetworkList(network_list);
278 return true; 367 return true;
279 } 368 }
280 return false; 369
281 } 370 bool ConnectToPreferredNetworkIfAvailable() {
282 371 // TODO(chocobo): Add the concept of preferred network to libcros.
283 bool NetworkLibraryImpl::FindCellularNetworkByPath( 372 // So that we don't have to hard-code Google-A here.
284 const std::string& path, CellularNetwork* result) const { 373 if (CrosLibrary::Get()->EnsureLoaded()) {
285 const CellularNetwork* cellular = 374 LOG(INFO) << "Attempting to auto-connect to Google wifi.";
286 GetWirelessNetworkByPath(cellular_networks_, path); 375 // First force a refresh of the system info.
287 if (cellular) { 376 UpdateSystemInfo();
288 if (result) 377
289 *result = *cellular; 378 // If ethernet is connected, then don't bother.
290 return true; 379 if (ethernet_connected()) {
291 } 380 LOG(INFO) << "Ethernet connected, so don't need Google wifi.";
292 return false; 381 return false;
293 } 382 }
294 383
295 void NetworkLibraryImpl::RequestWifiScan() { 384 WifiNetwork* wifi = GetPreferredNetwork();
296 if (CrosLibrary::Get()->EnsureLoaded()) { 385 if (!wifi) {
297 RequestScan(TYPE_WIFI); 386 LOG(INFO) <<
298 } 387 "Google-A/Google wifi not found or set to not auto-connect.";
299 } 388 return false;
300 389 }
301 bool NetworkLibraryImpl::GetWifiAccessPoints(WifiAccessPointVector* result) { 390
302 if (!CrosLibrary::Get()->EnsureLoaded()) 391 // Save the wifi path, so we know which one we want to auto-connect to.
392 const std::string wifi_path = wifi->service_path();
393
394 // It takes some time for the enterprise daemon to start up and populate
395 // the certificate and identity. So we wait at most 3 seconds here. And
396 // every 100ms, we refetch the system info and check the cert and identify
397 // on the wifi. The enterprise daemon takes between 0.4 to 0.9 seconds to
398 // setup.
399 bool setup = false;
400 for (int i = 0; i < 30; i++) {
401 // Update the system and refetch the network.
402 UpdateSystemInfo();
403 wifi = GetWirelessNetworkByPath(wifi_networks_, wifi_path);
404 // See if identity and certpath are available.
405 if (wifi && !wifi->identity().empty() && !wifi->cert_path().empty()) {
406 LOG(INFO) << "Google wifi set up after " << (i*0.1) << " seconds.";
407 setup = true;
408 break;
409 }
410 PlatformThread::Sleep(100);
411 }
412
413 if (!setup) {
414 LOG(INFO) << "Google wifi not set up after 3 seconds.";
415 return false;
416 }
417
418 // Now that we have a setup Google wifi, we can connect to it.
419 ConnectToNetwork(wifi_path.c_str(), NULL);
420 return true;
421 }
303 return false; 422 return false;
304 DeviceNetworkList* network_list = GetDeviceNetworkList(); 423 }
305 if (network_list == NULL) 424
306 return false; 425 bool PreferredNetworkConnected() {
307 result->clear();
308 result->reserve(network_list->network_size);
309 const base::Time now = base::Time::Now();
310 for (size_t i = 0; i < network_list->network_size; ++i) {
311 DCHECK(network_list->networks[i].address);
312 DCHECK(network_list->networks[i].name);
313 WifiAccessPoint ap;
314 ap.mac_address = network_list->networks[i].address;
315 ap.name = network_list->networks[i].name;
316 ap.timestamp = now -
317 base::TimeDelta::FromSeconds(network_list->networks[i].age_seconds);
318 ap.signal_strength = network_list->networks[i].strength;
319 ap.channel = network_list->networks[i].channel;
320 result->push_back(ap);
321 }
322 FreeDeviceNetworkList(network_list);
323 return true;
324 }
325
326 bool NetworkLibraryImpl::ConnectToPreferredNetworkIfAvailable() {
327 // TODO(chocobo): Add the concept of preferred network to libcros.
328 // So that we don't have to hard-code Google-A here.
329 if (CrosLibrary::Get()->EnsureLoaded()) {
330 LOG(INFO) << "Attempting to auto-connect to Google wifi.";
331 // First force a refresh of the system info.
332 UpdateSystemInfo();
333
334 // If ethernet is connected, then don't bother.
335 if (ethernet_connected()) {
336 LOG(INFO) << "Ethernet connected, so don't need Google wifi.";
337 return false;
338 }
339
340 WifiNetwork* wifi = GetPreferredNetwork(); 426 WifiNetwork* wifi = GetPreferredNetwork();
341 if (!wifi) { 427 return wifi && wifi->connected();
342 LOG(INFO) << "Google-A/Google wifi not found or set to not auto-connect."; 428 }
343 return false; 429
344 } 430 bool PreferredNetworkFailed() {
345 431 WifiNetwork* wifi = GetPreferredNetwork();
346 // Save the wifi path, so we know which one we want to auto-connect to. 432 return !wifi || wifi->failed();
347 const std::string wifi_path = wifi->service_path(); 433 }
348 434
349 // It takes some time for the enterprise daemon to start up and populate the 435 void ConnectToWifiNetwork(WifiNetwork network,
350 // certificate and identity. So we wait at most 3 seconds here. And every 436 const std::string& password,
351 // 100ms, we refetch the system info and check the cert and identify on the 437 const std::string& identity,
352 // wifi. The enterprise daemon takes between 0.4 to 0.9 seconds to setup. 438 const std::string& certpath) {
353 bool setup = false; 439 if (CrosLibrary::Get()->EnsureLoaded()) {
354 for (int i = 0; i < 30; i++) { 440 ConnectToNetworkWithCertInfo(network.service_path().c_str(),
355 // Update the system and refetch the network.
356 UpdateSystemInfo();
357 wifi = GetWirelessNetworkByPath(wifi_networks_, wifi_path);
358 // See if identity and certpath are available.
359 if (wifi && !wifi->identity().empty() && !wifi->cert_path().empty()) {
360 LOG(INFO) << "Google wifi set up after " << (i*0.1) << " seconds.";
361 setup = true;
362 break;
363 }
364 PlatformThread::Sleep(100);
365 }
366
367 if (!setup) {
368 LOG(INFO) << "Google wifi not set up after 3 seconds.";
369 return false;
370 }
371
372 // Now that we have a setup Google wifi, we can connect to it.
373 ConnectToNetwork(wifi_path.c_str(), NULL);
374 return true;
375 }
376 return false;
377 }
378
379 bool NetworkLibraryImpl::PreferredNetworkConnected() {
380 WifiNetwork* wifi = GetPreferredNetwork();
381 return wifi && wifi->connected();
382 }
383
384 bool NetworkLibraryImpl::PreferredNetworkFailed() {
385 WifiNetwork* wifi = GetPreferredNetwork();
386 return !wifi || wifi->failed();
387 }
388
389 void NetworkLibraryImpl::ConnectToWifiNetwork(WifiNetwork network,
390 const std::string& password,
391 const std::string& identity,
392 const std::string& certpath) {
393 if (CrosLibrary::Get()->EnsureLoaded()) {
394 ConnectToNetworkWithCertInfo(network.service_path().c_str(),
395 password.empty() ? NULL : password.c_str(),
396 identity.empty() ? NULL : identity.c_str(),
397 certpath.empty() ? NULL : certpath.c_str());
398 }
399 }
400
401 void NetworkLibraryImpl::ConnectToWifiNetwork(const std::string& ssid,
402 const std::string& password,
403 const std::string& identity,
404 const std::string& certpath,
405 bool auto_connect) {
406 if (CrosLibrary::Get()->EnsureLoaded()) {
407 // First create a service from hidden network.
408 ServiceInfo* service = GetWifiService(ssid.c_str(),
409 SECURITY_UNKNOWN);
410 if (service) {
411 // Set auto-connect.
412 SetAutoConnect(service->service_path, auto_connect);
413 // Now connect to that service.
414 ConnectToNetworkWithCertInfo(service->service_path,
415 password.empty() ? NULL : password.c_str(), 441 password.empty() ? NULL : password.c_str(),
416 identity.empty() ? NULL : identity.c_str(), 442 identity.empty() ? NULL : identity.c_str(),
417 certpath.empty() ? NULL : certpath.c_str()); 443 certpath.empty() ? NULL : certpath.c_str());
418 444 }
419 // Clean up ServiceInfo object. 445 }
420 FreeServiceInfo(service); 446
447 void ConnectToWifiNetwork(const std::string& ssid,
448 const std::string& password,
449 const std::string& identity,
450 const std::string& certpath,
451 bool auto_connect) {
452 if (CrosLibrary::Get()->EnsureLoaded()) {
453 // First create a service from hidden network.
454 ServiceInfo* service = GetWifiService(ssid.c_str(),
455 SECURITY_UNKNOWN);
456 if (service) {
457 // Set auto-connect.
458 SetAutoConnect(service->service_path, auto_connect);
459 // Now connect to that service.
460 ConnectToNetworkWithCertInfo(service->service_path,
461 password.empty() ? NULL : password.c_str(),
462 identity.empty() ? NULL : identity.c_str(),
463 certpath.empty() ? NULL : certpath.c_str());
464
465 // Clean up ServiceInfo object.
466 FreeServiceInfo(service);
467 } else {
468 LOG(WARNING) << "Cannot find hidden network: " << ssid;
469 // TODO(chocobo): Show error message.
470 }
471 }
472 }
473
474 void ConnectToCellularNetwork(CellularNetwork network) {
475 if (CrosLibrary::Get()->EnsureLoaded()) {
476 ConnectToNetwork(network.service_path().c_str(), NULL);
477 }
478 }
479
480 void DisconnectFromWirelessNetwork(
481 const WirelessNetwork& network) {
482 if (CrosLibrary::Get()->EnsureLoaded()) {
483 DisconnectFromNetwork(network.service_path().c_str());
484 }
485 }
486
487 void SaveCellularNetwork(const CellularNetwork& network) {
488 // Update the wifi network in the local cache.
489 CellularNetwork* cellular = GetWirelessNetworkByPath(
490 cellular_networks_,
491 network.service_path());
492 if (cellular)
493 *cellular = network;
494
495 // Update the cellular network with libcros.
496 if (CrosLibrary::Get()->EnsureLoaded()) {
497 SetAutoConnect(network.service_path().c_str(), network.auto_connect());
498 }
499 }
500
501 void SaveWifiNetwork(const WifiNetwork& network) {
502 // Update the wifi network in the local cache.
503 WifiNetwork* wifi = GetWirelessNetworkByPath(wifi_networks_,
504 network.service_path());
505 if (wifi)
506 *wifi = network;
507
508 // Update the wifi network with libcros.
509 if (CrosLibrary::Get()->EnsureLoaded()) {
510 SetPassphrase(
511 network.service_path().c_str(), network.passphrase().c_str());
512 SetIdentity(network.service_path().c_str(), network.identity().c_str());
513 SetCertPath(network.service_path().c_str(), network.cert_path().c_str());
514 SetAutoConnect(network.service_path().c_str(), network.auto_connect());
515 }
516 }
517
518 void ForgetWirelessNetwork(
519 const std::string& service_path) {
520 if (CrosLibrary::Get()->EnsureLoaded()) {
521 DeleteRememberedService(service_path.c_str());
522 }
523 }
524
525 virtual bool ethernet_available() const {
526 return available_devices_ & (1 << TYPE_ETHERNET);
527 }
528 virtual bool wifi_available() const {
529 return available_devices_ & (1 << TYPE_WIFI);
530 }
531 virtual bool cellular_available() const {
532 return available_devices_ & (1 << TYPE_CELLULAR);
533 }
534
535 virtual bool ethernet_enabled() const {
536 return enabled_devices_ & (1 << TYPE_ETHERNET);
537 }
538 virtual bool wifi_enabled() const {
539 return enabled_devices_ & (1 << TYPE_WIFI);
540 }
541 virtual bool cellular_enabled() const {
542 return enabled_devices_ & (1 << TYPE_CELLULAR);
543 }
544
545 virtual bool offline_mode() const { return offline_mode_; }
546
547 void EnableEthernetNetworkDevice(bool enable) {
548 EnableNetworkDeviceType(TYPE_ETHERNET, enable);
549 }
550
551 void EnableWifiNetworkDevice(bool enable) {
552 EnableNetworkDeviceType(TYPE_WIFI, enable);
553 }
554
555 void EnableCellularNetworkDevice(bool enable) {
556 EnableNetworkDeviceType(TYPE_CELLULAR, enable);
557 }
558
559 void EnableOfflineMode(bool enable) {
560 if (!CrosLibrary::Get()->EnsureLoaded())
561 return;
562
563 // If network device is already enabled/disabled, then don't do anything.
564 if (enable && offline_mode_) {
565 LOG(INFO) << "Trying to enable offline mode when it's already enabled. ";
566 return;
567 }
568 if (!enable && !offline_mode_) {
569 LOG(INFO) <<
570 "Trying to disable offline mode when it's already disabled. ";
571 return;
572 }
573
574 if (SetOfflineMode(enable)) {
575 offline_mode_ = enable;
576 }
577 }
578
579 NetworkIPConfigVector GetIPConfigs(
580 const std::string& device_path) {
581 NetworkIPConfigVector ipconfig_vector;
582 if (!device_path.empty()) {
583 IPConfigStatus* ipconfig_status = ListIPConfigs(device_path.c_str());
584 if (ipconfig_status) {
585 for (int i = 0; i < ipconfig_status->size; i++) {
586 IPConfig ipconfig = ipconfig_status->ips[i];
587 ipconfig_vector.push_back(
588 NetworkIPConfig(device_path, ipconfig.type, ipconfig.address,
589 ipconfig.netmask, ipconfig.gateway,
590 ipconfig.name_servers));
591 }
592 FreeIPConfigStatus(ipconfig_status);
593 // Sort the list of ip configs by type.
594 std::sort(ipconfig_vector.begin(), ipconfig_vector.end());
595 }
596 }
597 return ipconfig_vector;
598 }
599
600 std::string GetHtmlInfo(int refresh) {
601 std::string output;
602 output.append("<html><head><title>About Network</title>");
603 if (refresh > 0)
604 output.append("<meta http-equiv=\"refresh\" content=\"" +
605 base::IntToString(refresh) + "\"/>");
606 output.append("</head><body>");
607 if (refresh > 0) {
608 output.append("(Auto-refreshing page every " +
609 base::IntToString(refresh) + "s)");
421 } else { 610 } else {
422 LOG(WARNING) << "Cannot find hidden network: " << ssid; 611 output.append("(To auto-refresh this page: about:network/&lt;secs&gt;)");
423 // TODO(chocobo): Show error message. 612 }
424 } 613
425 } 614 output.append("<h3>Ethernet:</h3><table border=1>");
426 } 615 output.append("<tr>" + ToHtmlTableHeader(&ethernet_) + "</tr>");
427 616 output.append("<tr>" + ToHtmlTableRow(&ethernet_) + "</tr>");
428 void NetworkLibraryImpl::ConnectToCellularNetwork(CellularNetwork network) { 617
429 if (CrosLibrary::Get()->EnsureLoaded()) { 618 output.append("</table><h3>Wifi:</h3><table border=1>");
430 ConnectToNetwork(network.service_path().c_str(), NULL); 619 for (size_t i = 0; i < wifi_networks_.size(); ++i) {
431 } 620 if (i == 0)
432 } 621 output.append("<tr>" + ToHtmlTableHeader(&wifi_networks_[i]) + "</tr>");
433 622 output.append("<tr>" + ToHtmlTableRow(&wifi_networks_[i]) + "</tr>");
434 void NetworkLibraryImpl::DisconnectFromWirelessNetwork( 623 }
435 const WirelessNetwork& network) { 624
436 if (CrosLibrary::Get()->EnsureLoaded()) { 625 output.append("</table><h3>Cellular:</h3><table border=1>");
437 DisconnectFromNetwork(network.service_path().c_str()); 626 for (size_t i = 0; i < cellular_networks_.size(); ++i) {
438 } 627 if (i == 0)
439 } 628 output.append("<tr>" + ToHtmlTableHeader(&cellular_networks_[i]) +
440 629 "</tr>");
441 void NetworkLibraryImpl::SaveCellularNetwork(const CellularNetwork& network) { 630 output.append("<tr>" + ToHtmlTableRow(&cellular_networks_[i]) + "</tr>");
442 // Update the wifi network in the local cache. 631 }
443 CellularNetwork* cellular = GetWirelessNetworkByPath(cellular_networks_, 632
444 network.service_path()); 633 output.append("</table><h3>Remembered Wifi:</h3><table border=1>");
445 if (cellular) 634 for (size_t i = 0; i < remembered_wifi_networks_.size(); ++i) {
446 *cellular = network; 635 if (i == 0)
447 636 output.append(
448 // Update the cellular network with libcros. 637 "<tr>" + ToHtmlTableHeader(&remembered_wifi_networks_[i]) +
449 if (CrosLibrary::Get()->EnsureLoaded()) { 638 "</tr>");
450 SetAutoConnect(network.service_path().c_str(), network.auto_connect()); 639 output.append("<tr>" + ToHtmlTableRow(&remembered_wifi_networks_[i]) +
451 }
452 }
453
454 void NetworkLibraryImpl::SaveWifiNetwork(const WifiNetwork& network) {
455 // Update the wifi network in the local cache.
456 WifiNetwork* wifi = GetWirelessNetworkByPath(wifi_networks_,
457 network.service_path());
458 if (wifi)
459 *wifi = network;
460
461 // Update the wifi network with libcros.
462 if (CrosLibrary::Get()->EnsureLoaded()) {
463 SetPassphrase(network.service_path().c_str(), network.passphrase().c_str());
464 SetIdentity(network.service_path().c_str(), network.identity().c_str());
465 SetCertPath(network.service_path().c_str(), network.cert_path().c_str());
466 SetAutoConnect(network.service_path().c_str(), network.auto_connect());
467 }
468 }
469
470 void NetworkLibraryImpl::ForgetWirelessNetwork(
471 const std::string& service_path) {
472 if (CrosLibrary::Get()->EnsureLoaded()) {
473 DeleteRememberedService(service_path.c_str());
474 }
475 }
476
477 void NetworkLibraryImpl::EnableEthernetNetworkDevice(bool enable) {
478 EnableNetworkDeviceType(TYPE_ETHERNET, enable);
479 }
480
481 void NetworkLibraryImpl::EnableWifiNetworkDevice(bool enable) {
482 EnableNetworkDeviceType(TYPE_WIFI, enable);
483 }
484
485 void NetworkLibraryImpl::EnableCellularNetworkDevice(bool enable) {
486 EnableNetworkDeviceType(TYPE_CELLULAR, enable);
487 }
488
489 void NetworkLibraryImpl::EnableOfflineMode(bool enable) {
490 if (!CrosLibrary::Get()->EnsureLoaded())
491 return;
492
493 // If network device is already enabled/disabled, then don't do anything.
494 if (enable && offline_mode_) {
495 LOG(INFO) << "Trying to enable offline mode when it's already enabled. ";
496 return;
497 }
498 if (!enable && !offline_mode_) {
499 LOG(INFO) << "Trying to disable offline mode when it's already disabled. ";
500 return;
501 }
502
503 if (SetOfflineMode(enable)) {
504 offline_mode_ = enable;
505 }
506 }
507
508 NetworkIPConfigVector NetworkLibraryImpl::GetIPConfigs(
509 const std::string& device_path) {
510 NetworkIPConfigVector ipconfig_vector;
511 if (!device_path.empty()) {
512 IPConfigStatus* ipconfig_status = ListIPConfigs(device_path.c_str());
513 if (ipconfig_status) {
514 for (int i = 0; i < ipconfig_status->size; i++) {
515 IPConfig ipconfig = ipconfig_status->ips[i];
516 ipconfig_vector.push_back(
517 NetworkIPConfig(device_path, ipconfig.type, ipconfig.address,
518 ipconfig.netmask, ipconfig.gateway,
519 ipconfig.name_servers));
520 }
521 FreeIPConfigStatus(ipconfig_status);
522 // Sort the list of ip configs by type.
523 std::sort(ipconfig_vector.begin(), ipconfig_vector.end());
524 }
525 }
526 return ipconfig_vector;
527 }
528
529 std::string NetworkLibraryImpl::GetHtmlInfo(int refresh) {
530 std::string output;
531 output.append("<html><head><title>About Network</title>");
532 if (refresh > 0)
533 output.append("<meta http-equiv=\"refresh\" content=\"" +
534 base::IntToString(refresh) + "\"/>");
535 output.append("</head><body>");
536 if (refresh > 0) {
537 output.append("(Auto-refreshing page every " +
538 base::IntToString(refresh) + "s)");
539 } else {
540 output.append("(To auto-refresh this page: about:network/&lt;secs&gt;)");
541 }
542
543 output.append("<h3>Ethernet:</h3><table border=1>");
544 output.append("<tr>" + ToHtmlTableHeader(&ethernet_) + "</tr>");
545 output.append("<tr>" + ToHtmlTableRow(&ethernet_) + "</tr>");
546
547 output.append("</table><h3>Wifi:</h3><table border=1>");
548 for (size_t i = 0; i < wifi_networks_.size(); ++i) {
549 if (i == 0)
550 output.append("<tr>" + ToHtmlTableHeader(&wifi_networks_[i]) + "</tr>");
551 output.append("<tr>" + ToHtmlTableRow(&wifi_networks_[i]) + "</tr>");
552 }
553
554 output.append("</table><h3>Cellular:</h3><table border=1>");
555 for (size_t i = 0; i < cellular_networks_.size(); ++i) {
556 if (i == 0)
557 output.append("<tr>" + ToHtmlTableHeader(&cellular_networks_[i]) +
558 "</tr>"); 640 "</tr>");
559 output.append("<tr>" + ToHtmlTableRow(&cellular_networks_[i]) + "</tr>"); 641 }
560 } 642
561 643 output.append("</table><h3>Remembered Cellular:</h3><table border=1>");
562 output.append("</table><h3>Remembered Wifi:</h3><table border=1>"); 644 for (size_t i = 0; i < remembered_cellular_networks_.size(); ++i) {
563 for (size_t i = 0; i < remembered_wifi_networks_.size(); ++i) { 645 if (i == 0)
564 if (i == 0) 646 output.append("<tr>" +
565 output.append("<tr>" + ToHtmlTableHeader(&remembered_wifi_networks_[i]) + 647 ToHtmlTableHeader(&remembered_cellular_networks_[i]) + "</tr>");
648 output.append("<tr>" + ToHtmlTableRow(&remembered_cellular_networks_[i]) +
566 "</tr>"); 649 "</tr>");
567 output.append("<tr>" + ToHtmlTableRow(&remembered_wifi_networks_[i]) + 650 }
568 "</tr>"); 651
569 } 652 output.append("</table></body></html>");
570 653 return output;
571 output.append("</table><h3>Remembered Cellular:</h3><table border=1>"); 654 }
572 for (size_t i = 0; i < remembered_cellular_networks_.size(); ++i) { 655
573 if (i == 0) 656 private:
574 output.append("<tr>" + 657 static void NetworkStatusChangedHandler(void* object) {
575 ToHtmlTableHeader(&remembered_cellular_networks_[i]) + "</tr>"); 658 NetworkLibraryImpl* network = static_cast<NetworkLibraryImpl*>(object);
576 output.append("<tr>" + ToHtmlTableRow(&remembered_cellular_networks_[i]) + 659 DCHECK(network);
577 "</tr>"); 660 network->UpdateNetworkStatus();
578 } 661 }
579 662
580 output.append("</table></body></html>"); 663 static void ParseSystem(SystemInfo* system,
581 return output; 664 EthernetNetwork* ethernet,
582 } 665 WifiNetworkVector* wifi_networks,
583 666 CellularNetworkVector* cellular_networks,
584 // static 667 WifiNetworkVector* remembered_wifi_networks,
585 void NetworkLibraryImpl::NetworkStatusChangedHandler(void* object) { 668 CellularNetworkVector* remembered_cellular_networks) {
586 NetworkLibraryImpl* network = static_cast<NetworkLibraryImpl*>(object); 669 DLOG(INFO) << "ParseSystem:";
587 DCHECK(network); 670 ethernet->Clear();
588 network->UpdateNetworkStatus(); 671 for (int i = 0; i < system->service_size; i++) {
589 } 672 const ServiceInfo& service = system->services[i];
590
591 // static
592 void NetworkLibraryImpl::ParseSystem(SystemInfo* system,
593 EthernetNetwork* ethernet,
594 WifiNetworkVector* wifi_networks,
595 CellularNetworkVector* cellular_networks,
596 WifiNetworkVector* remembered_wifi_networks,
597 CellularNetworkVector* remembered_cellular_networks) {
598 DLOG(INFO) << "ParseSystem:";
599 ethernet->Clear();
600 for (int i = 0; i < system->service_size; i++) {
601 const ServiceInfo& service = system->services[i];
602 DLOG(INFO) << " (" << service.type <<
603 ") " << service.name <<
604 " mode=" << service.mode <<
605 " state=" << service.state <<
606 " sec=" << service.security <<
607 " req=" << service.passphrase_required <<
608 " pass=" << service.passphrase <<
609 " id=" << service.identity <<
610 " certpath=" << service.cert_path <<
611 " str=" << service.strength <<
612 " fav=" << service.favorite <<
613 " auto=" << service.auto_connect <<
614 " error=" << service.error;
615 // Once a connected ethernet service is found, disregard other ethernet
616 // services that are also found
617 if (service.type == TYPE_ETHERNET && !(ethernet->connected()))
618 ethernet->ConfigureFromService(service);
619 else if (service.type == TYPE_WIFI)
620 wifi_networks->push_back(WifiNetwork(service));
621 else if (service.type == TYPE_CELLULAR)
622 cellular_networks->push_back(CellularNetwork(service));
623 }
624 DLOG(INFO) << "Remembered networks:";
625 for (int i = 0; i < system->remembered_service_size; i++) {
626 const ServiceInfo& service = system->remembered_services[i];
627 // Only serices marked as auto_connect are considered remembered networks.
628 // TODO(chocobo): Don't add to remembered service if currently available.
629 if (service.auto_connect) {
630 DLOG(INFO) << " (" << service.type << 673 DLOG(INFO) << " (" << service.type <<
631 ") " << service.name << 674 ") " << service.name <<
632 " mode=" << service.mode << 675 " mode=" << service.mode <<
676 " state=" << service.state <<
633 " sec=" << service.security << 677 " sec=" << service.security <<
678 " req=" << service.passphrase_required <<
634 " pass=" << service.passphrase << 679 " pass=" << service.passphrase <<
635 " id=" << service.identity << 680 " id=" << service.identity <<
636 " certpath=" << service.cert_path << 681 " certpath=" << service.cert_path <<
637 " auto=" << service.auto_connect; 682 " str=" << service.strength <<
638 if (service.type == TYPE_WIFI) 683 " fav=" << service.favorite <<
639 remembered_wifi_networks->push_back(WifiNetwork(service)); 684 " auto=" << service.auto_connect <<
685 " error=" << service.error;
686 // Once a connected ethernet service is found, disregard other ethernet
687 // services that are also found
688 if (service.type == TYPE_ETHERNET && !(ethernet->connected()))
689 ethernet->ConfigureFromService(service);
690 else if (service.type == TYPE_WIFI)
691 wifi_networks->push_back(WifiNetwork(service));
640 else if (service.type == TYPE_CELLULAR) 692 else if (service.type == TYPE_CELLULAR)
641 remembered_cellular_networks->push_back(CellularNetwork(service)); 693 cellular_networks->push_back(CellularNetwork(service));
642 } 694 }
643 } 695 DLOG(INFO) << "Remembered networks:";
644 } 696 for (int i = 0; i < system->remembered_service_size; i++) {
645 697 const ServiceInfo& service = system->remembered_services[i];
646 void NetworkLibraryImpl::Init() { 698 // Only serices marked as auto_connect are considered remembered networks.
647 // First, get the currently available networks. This data is cached 699 // TODO(chocobo): Don't add to remembered service if currently available.
648 // on the connman side, so the call should be quick. 700 if (service.auto_connect) {
649 LOG(INFO) << "Getting initial CrOS network info."; 701 DLOG(INFO) << " (" << service.type <<
650 UpdateSystemInfo(); 702 ") " << service.name <<
651 703 " mode=" << service.mode <<
652 LOG(INFO) << "Registering for network status updates."; 704 " sec=" << service.security <<
653 // Now, register to receive updates on network status. 705 " pass=" << service.passphrase <<
654 network_status_connection_ = MonitorNetwork(&NetworkStatusChangedHandler, 706 " id=" << service.identity <<
655 this); 707 " certpath=" << service.cert_path <<
656 } 708 " auto=" << service.auto_connect;
657 709 if (service.type == TYPE_WIFI)
658 void NetworkLibraryImpl::InitTestData() { 710 remembered_wifi_networks->push_back(WifiNetwork(service));
659 ethernet_.Clear(); 711 else if (service.type == TYPE_CELLULAR)
660 ethernet_.set_connected(true); 712 remembered_cellular_networks->push_back(CellularNetwork(service));
661 713 }
662 wifi_networks_.clear(); 714 }
663 WifiNetwork wifi1 = WifiNetwork(); 715 }
664 wifi1.set_service_path("fw1"); 716
665 wifi1.set_name("Fake Wifi 1"); 717 void Init() {
666 wifi1.set_strength(90); 718 // First, get the currently available networks. This data is cached
667 wifi1.set_connected(false); 719 // on the connman side, so the call should be quick.
668 wifi1.set_encryption(SECURITY_NONE); 720 LOG(INFO) << "Getting initial CrOS network info.";
669 wifi_networks_.push_back(wifi1); 721 UpdateSystemInfo();
670 722
671 WifiNetwork wifi2 = WifiNetwork(); 723 LOG(INFO) << "Registering for network status updates.";
672 wifi2.set_service_path("fw2"); 724 // Now, register to receive updates on network status.
673 wifi2.set_name("Fake Wifi 2"); 725 network_status_connection_ = MonitorNetwork(&NetworkStatusChangedHandler,
674 wifi2.set_strength(70); 726 this);
675 wifi2.set_connected(true); 727 }
676 wifi2.set_encryption(SECURITY_WEP); 728
677 wifi_networks_.push_back(wifi2); 729 void InitTestData() {
678 730 ethernet_.Clear();
679 WifiNetwork wifi3 = WifiNetwork(); 731 ethernet_.set_connected(true);
680 wifi3.set_service_path("fw3"); 732
681 wifi3.set_name("Fake Wifi 3"); 733 wifi_networks_.clear();
682 wifi3.set_strength(50); 734 WifiNetwork wifi1 = WifiNetwork();
683 wifi3.set_connected(false); 735 wifi1.set_service_path("fw1");
684 wifi3.set_encryption(SECURITY_WEP); 736 wifi1.set_name("Fake Wifi 1");
685 wifi_networks_.push_back(wifi3); 737 wifi1.set_strength(90);
686 738 wifi1.set_connected(false);
687 wifi_ = wifi2; 739 wifi1.set_encryption(SECURITY_NONE);
688 740 wifi_networks_.push_back(wifi1);
689 cellular_networks_.clear(); 741
690 742 WifiNetwork wifi2 = WifiNetwork();
691 cellular_networks_.clear(); 743 wifi2.set_service_path("fw2");
692 CellularNetwork cellular1 = CellularNetwork(); 744 wifi2.set_name("Fake Wifi 2");
693 cellular1.set_service_path("fc1"); 745 wifi2.set_strength(70);
694 cellular1.set_name("Fake Cellular 1"); 746 wifi2.set_connected(true);
695 cellular1.set_strength(90); 747 wifi2.set_encryption(SECURITY_WEP);
696 cellular1.set_connected(false); 748 wifi_networks_.push_back(wifi2);
697 cellular_networks_.push_back(cellular1); 749
698 750 WifiNetwork wifi3 = WifiNetwork();
699 CellularNetwork cellular2 = CellularNetwork(); 751 wifi3.set_service_path("fw3");
700 cellular2.set_service_path("fc2"); 752 wifi3.set_name("Fake Wifi 3");
701 cellular2.set_name("Fake Cellular 2"); 753 wifi3.set_strength(50);
702 cellular2.set_strength(70); 754 wifi3.set_connected(false);
703 cellular2.set_connected(true); 755 wifi3.set_encryption(SECURITY_WEP);
704 cellular_networks_.push_back(cellular2); 756 wifi_networks_.push_back(wifi3);
705 757
706 CellularNetwork cellular3 = CellularNetwork(); 758 wifi_ = wifi2;
707 cellular3.set_service_path("fc3"); 759
708 cellular3.set_name("Fake Cellular 3"); 760 cellular_networks_.clear();
709 cellular3.set_strength(50); 761
710 cellular3.set_connected(false); 762 cellular_networks_.clear();
711 cellular_networks_.push_back(cellular3); 763 CellularNetwork cellular1 = CellularNetwork();
712 764 cellular1.set_service_path("fc1");
713 cellular_ = cellular2; 765 cellular1.set_name("Fake Cellular 1");
714 766 cellular1.set_strength(90);
715 remembered_wifi_networks_.clear(); 767 cellular1.set_connected(false);
716 remembered_wifi_networks_.push_back(wifi2); 768 cellular_networks_.push_back(cellular1);
717 769
718 remembered_cellular_networks_.clear(); 770 CellularNetwork cellular2 = CellularNetwork();
719 remembered_cellular_networks_.push_back(cellular2); 771 cellular2.set_service_path("fc2");
720 772 cellular2.set_name("Fake Cellular 2");
721 int devices = (1 << TYPE_ETHERNET) | (1 << TYPE_WIFI) | 773 cellular2.set_strength(70);
722 (1 << TYPE_CELLULAR); 774 cellular2.set_connected(true);
723 available_devices_ = devices; 775 cellular_networks_.push_back(cellular2);
724 enabled_devices_ = devices; 776
725 connected_devices_ = devices; 777 CellularNetwork cellular3 = CellularNetwork();
726 offline_mode_ = false; 778 cellular3.set_service_path("fc3");
727 } 779 cellular3.set_name("Fake Cellular 3");
728 780 cellular3.set_strength(50);
729 void NetworkLibraryImpl::UpdateSystemInfo() { 781 cellular3.set_connected(false);
730 if (CrosLibrary::Get()->EnsureLoaded()) { 782 cellular_networks_.push_back(cellular3);
731 UpdateNetworkStatus(); 783
732 } 784 cellular_ = cellular2;
733 } 785
734 786 remembered_wifi_networks_.clear();
735 WifiNetwork* NetworkLibraryImpl::GetPreferredNetwork() { 787 remembered_wifi_networks_.push_back(wifi2);
736 // First look for Google-A then look for Google. 788
737 // Only care if set to auto-connect. 789 remembered_cellular_networks_.clear();
738 WifiNetwork* wifi = GetWifiNetworkByName(kGoogleAWifi); 790 remembered_cellular_networks_.push_back(cellular2);
739 // If wifi found and set to not auto-connect, then ignore it. 791
740 if (wifi && !wifi->auto_connect()) 792 int devices = (1 << TYPE_ETHERNET) | (1 << TYPE_WIFI) |
741 wifi = NULL; 793 (1 << TYPE_CELLULAR);
742 794 available_devices_ = devices;
743 if (!wifi) { 795 enabled_devices_ = devices;
744 wifi = GetWifiNetworkByName(kGoogleWifi); 796 connected_devices_ = devices;
797 offline_mode_ = false;
798 }
799
800 void UpdateSystemInfo() {
801 if (CrosLibrary::Get()->EnsureLoaded()) {
802 UpdateNetworkStatus();
803 }
804 }
805
806 WifiNetwork* GetPreferredNetwork() {
807 // First look for Google-A then look for Google.
808 // Only care if set to auto-connect.
809 WifiNetwork* wifi = GetWifiNetworkByName(kGoogleAWifi);
745 // If wifi found and set to not auto-connect, then ignore it. 810 // If wifi found and set to not auto-connect, then ignore it.
746 if (wifi && !wifi->auto_connect()) 811 if (wifi && !wifi->auto_connect())
747 wifi = NULL; 812 wifi = NULL;
748 } 813
749 return wifi; 814 if (!wifi) {
750 } 815 wifi = GetWifiNetworkByName(kGoogleWifi);
751 816 // If wifi found and set to not auto-connect, then ignore it.
752 WifiNetwork* NetworkLibraryImpl::GetWifiNetworkByName(const std::string& name) { 817 if (wifi && !wifi->auto_connect())
753 for (size_t i = 0; i < wifi_networks_.size(); ++i) { 818 wifi = NULL;
754 if (wifi_networks_[i].name().compare(name) == 0) { 819 }
755 return &wifi_networks_[i]; 820 return wifi;
756 } 821 }
757 } 822
758 return NULL; 823 WifiNetwork* GetWifiNetworkByName(const std::string& name) {
759 } 824 for (size_t i = 0; i < wifi_networks_.size(); ++i) {
760 825 if (wifi_networks_[i].name().compare(name) == 0) {
761 template<typename T> T* NetworkLibraryImpl::GetWirelessNetworkByPath( 826 return &wifi_networks_[i];
762 std::vector<T>& networks, const std::string& path) { 827 }
763 typedef typename std::vector<T>::iterator iter_t; 828 }
764 iter_t iter = std::find_if(networks.begin(), networks.end(), 829 return NULL;
765 WirelessNetwork::ServicePathEq(path)); 830 }
766 return (iter != networks.end()) ? &(*iter) : NULL; 831
767 } 832 template<typename T> T* GetWirelessNetworkByPath(
768 833 std::vector<T>& networks, const std::string& path) {
769 // const version 834 typedef typename std::vector<T>::iterator iter_t;
770 template<typename T> const T* NetworkLibraryImpl::GetWirelessNetworkByPath( 835 iter_t iter = std::find_if(networks.begin(), networks.end(),
771 const std::vector<T>& networks, const std::string& path) const { 836 WirelessNetwork::ServicePathEq(path));
772 typedef typename std::vector<T>::const_iterator iter_t; 837 return (iter != networks.end()) ? &(*iter) : NULL;
773 iter_t iter = std::find_if(networks.begin(), networks.end(), 838 }
774 WirelessNetwork::ServicePathEq(path)); 839
775 return (iter != networks.end()) ? &(*iter) : NULL; 840 // const version
776 } 841 template<typename T> const T* GetWirelessNetworkByPath(
777 842 const std::vector<T>& networks, const std::string& path) const {
778 void NetworkLibraryImpl::EnableNetworkDeviceType(ConnectionType device, 843 typedef typename std::vector<T>::const_iterator iter_t;
779 bool enable) { 844 iter_t iter = std::find_if(networks.begin(), networks.end(),
780 if (!CrosLibrary::Get()->EnsureLoaded()) 845 WirelessNetwork::ServicePathEq(path));
781 return; 846 return (iter != networks.end()) ? &(*iter) : NULL;
782 847 }
783 // If network device is already enabled/disabled, then don't do anything. 848
784 if (enable && (enabled_devices_ & (1 << device))) { 849 void EnableNetworkDeviceType(ConnectionType device,
785 LOG(WARNING) << "Trying to enable a device that's already enabled: " 850 bool enable) {
786 << device; 851 if (!CrosLibrary::Get()->EnsureLoaded())
787 return; 852 return;
788 } 853
789 if (!enable && !(enabled_devices_ & (1 << device))) { 854 // If network device is already enabled/disabled, then don't do anything.
790 LOG(WARNING) << "Trying to disable a device that's already disabled: " 855 if (enable && (enabled_devices_ & (1 << device))) {
791 << device; 856 LOG(WARNING) << "Trying to enable a device that's already enabled: "
792 return; 857 << device;
793 } 858 return;
794 859 }
795 EnableNetworkDevice(device, enable); 860 if (!enable && !(enabled_devices_ & (1 << device))) {
796 } 861 LOG(WARNING) << "Trying to disable a device that's already disabled: "
797 862 << device;
798 void NetworkLibraryImpl::UpdateNetworkStatus() { 863 return;
799 // Make sure we run on UI thread. 864 }
800 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { 865
866 EnableNetworkDevice(device, enable);
867 }
868
869 void UpdateNetworkStatus() {
870 // Make sure we run on UI thread.
871 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) {
872 ChromeThread::PostTask(
873 ChromeThread::UI, FROM_HERE,
874 NewRunnableMethod(this,
875 &NetworkLibraryImpl::UpdateNetworkStatus));
876 return;
877 }
878
879 SystemInfo* system = GetSystemInfo();
880 if (!system)
881 return;
882
883 wifi_networks_.clear();
884 cellular_networks_.clear();
885 remembered_wifi_networks_.clear();
886 remembered_cellular_networks_.clear();
887 ParseSystem(system, &ethernet_, &wifi_networks_, &cellular_networks_,
888 &remembered_wifi_networks_, &remembered_cellular_networks_);
889
890 wifi_ = WifiNetwork();
891 for (size_t i = 0; i < wifi_networks_.size(); i++) {
892 if (wifi_networks_[i].connecting_or_connected()) {
893 wifi_ = wifi_networks_[i];
894 break; // There is only one connected or connecting wifi network.
895 }
896 }
897 cellular_ = CellularNetwork();
898 for (size_t i = 0; i < cellular_networks_.size(); i++) {
899 if (cellular_networks_[i].connecting_or_connected()) {
900 cellular_ = cellular_networks_[i];
901 break; // There is only one connected or connecting cellular network.
902 }
903 }
904
905 available_devices_ = system->available_technologies;
906 enabled_devices_ = system->enabled_technologies;
907 connected_devices_ = system->connected_technologies;
908 offline_mode_ = system->offline_mode;
909
910 FOR_EACH_OBSERVER(Observer, observers_, NetworkChanged(this));
911 FreeSystemInfo(system);
912 }
913
914 void CheckNetworkTraffic(bool download) {
915 // If we already have a pending upload and download notification, then
916 // shortcut and return.
917 if (traffic_type_ ==
918 (Observer::TRAFFIC_DOWNLOAD | Observer::TRAFFIC_UPLOAD)) {
919 return;
920 }
921 // Figure out if we are uploading and/or downloading. We are downloading
922 // if download == true. We are uploading if we have upload progress.
923 if (download)
924 traffic_type_ |= Observer::TRAFFIC_DOWNLOAD;
925 if ((traffic_type_ & Observer::TRAFFIC_UPLOAD) == 0) {
926 URLRequestJobTracker::JobIterator it;
927 for (it = g_url_request_job_tracker.begin();
928 it != g_url_request_job_tracker.end();
929 ++it) {
930 URLRequestJob* job = *it;
931 if (job->GetUploadProgress() > 0) {
932 traffic_type_ |= Observer::TRAFFIC_UPLOAD;
933 break;
934 }
935 }
936 }
937 // If we have new traffic data to send out and the timer is not currently
938 // running, then start a new timer.
939 if (traffic_type_ && !timer_.IsRunning()) {
940 timer_.Start(base::TimeDelta::FromSeconds(kNetworkTrafficeTimerSecs),
941 this,
942 &NetworkLibraryImpl::NetworkTrafficTimerFired);
943 }
944 }
945
946 void NetworkTrafficTimerFired() {
801 ChromeThread::PostTask( 947 ChromeThread::PostTask(
802 ChromeThread::UI, FROM_HERE, 948 ChromeThread::UI, FROM_HERE,
803 NewRunnableMethod(this, 949 NewRunnableMethod(this, &NetworkLibraryImpl::NotifyNetworkTraffic,
804 &NetworkLibraryImpl::UpdateNetworkStatus)); 950 traffic_type_));
805 return; 951 // Reset traffic type so that we don't send the same data next time.
806 } 952 traffic_type_ = 0;
807 953 }
808 SystemInfo* system = GetSystemInfo(); 954
809 if (!system) 955 void NotifyNetworkTraffic(int traffic_type) {
810 return; 956 FOR_EACH_OBSERVER(Observer, observers_, NetworkTraffic(this, traffic_type));
811 957 }
812 wifi_networks_.clear(); 958
813 cellular_networks_.clear(); 959 ObserverList<Observer> observers_;
814 remembered_wifi_networks_.clear(); 960
815 remembered_cellular_networks_.clear(); 961 // The amount of time to wait between each NetworkTraffic notifications.
816 ParseSystem(system, &ethernet_, &wifi_networks_, &cellular_networks_, 962 static const int kNetworkTrafficeTimerSecs = 1;
817 &remembered_wifi_networks_, &remembered_cellular_networks_); 963
818 964 // Timer for sending NetworkTraffic notification every
819 wifi_ = WifiNetwork(); 965 // kNetworkTrafficeTimerSecs seconds.
820 for (size_t i = 0; i < wifi_networks_.size(); i++) { 966 base::OneShotTimer<NetworkLibraryImpl> timer_;
821 if (wifi_networks_[i].connecting_or_connected()) { 967
822 wifi_ = wifi_networks_[i]; 968 // The current traffic type that will be sent out for the next NetworkTraffic
823 break; // There is only one connected or connecting wifi network. 969 // notification. This is a bitfield of TrafficTypeMasks.
824 } 970 int traffic_type_;
825 } 971
826 cellular_ = CellularNetwork(); 972 // The network status connection for monitoring network status changes.
827 for (size_t i = 0; i < cellular_networks_.size(); i++) { 973 MonitorNetworkConnection network_status_connection_;
828 if (cellular_networks_[i].connecting_or_connected()) { 974
829 cellular_ = cellular_networks_[i]; 975 // The ethernet network.
830 break; // There is only one connected or connecting cellular network. 976 EthernetNetwork ethernet_;
831 } 977
832 } 978 // The list of available wifi networks.
833 979 WifiNetworkVector wifi_networks_;
834 available_devices_ = system->available_technologies; 980
835 enabled_devices_ = system->enabled_technologies; 981 // The current connected (or connecting) wifi network.
836 connected_devices_ = system->connected_technologies; 982 WifiNetwork wifi_;
837 offline_mode_ = system->offline_mode; 983
838 984 // The remembered wifi networks.
839 FOR_EACH_OBSERVER(Observer, observers_, NetworkChanged(this)); 985 WifiNetworkVector remembered_wifi_networks_;
840 FreeSystemInfo(system); 986
987 // The list of available cellular networks.
988 CellularNetworkVector cellular_networks_;
989
990 // The current connected (or connecting) cellular network.
991 CellularNetwork cellular_;
992
993 // The remembered cellular networks.
994 CellularNetworkVector remembered_cellular_networks_;
995
996 // The current available network devices. Bitwise flag of ConnectionTypes.
997 int available_devices_;
998
999 // The current enabled network devices. Bitwise flag of ConnectionTypes.
1000 int enabled_devices_;
1001
1002 // The current connected network devices. Bitwise flag of ConnectionTypes.
1003 int connected_devices_;
1004
1005 bool offline_mode_;
1006
1007 DISALLOW_COPY_AND_ASSIGN(NetworkLibraryImpl);
1008 };
1009
1010 class NetworkLibraryStubImpl : public NetworkLibrary {
1011 public:
1012 NetworkLibraryStubImpl() : ip_address_("1.1.1.1") {}
1013 ~NetworkLibraryStubImpl() {}
1014 void OnJobAdded(URLRequestJob* job) {}
1015 void OnJobRemoved(URLRequestJob* job) {}
1016 void OnJobDone(URLRequestJob* job, const URLRequestStatus& status) {}
1017 void OnJobRedirect(
1018 URLRequestJob* job, const GURL& location, int status_code) {}
1019 void OnBytesRead(URLRequestJob* job, int byte_count) {}
1020 void AddObserver(Observer* observer) {}
1021 void RemoveObserver(Observer* observer) {}
1022 virtual const EthernetNetwork& ethernet_network() const {
1023 return ethernet_;
1024 }
1025 virtual bool ethernet_connecting() const { return false; }
1026 virtual bool ethernet_connected() const { return true; }
1027 virtual const std::string& wifi_name() const { return EmptyString(); }
1028 virtual bool wifi_connecting() const { return false; }
1029 virtual bool wifi_connected() const { return false; }
1030 virtual int wifi_strength() const { return 0; }
1031
1032 virtual const std::string& cellular_name() const { return EmptyString(); }
1033 virtual bool cellular_connecting() const { return false; }
1034 virtual bool cellular_connected() const { return false; }
1035 virtual int cellular_strength() const { return false; }
1036
1037 bool Connected() const { return true; }
1038 bool Connecting() const { return false; }
1039 const std::string& IPAddress() const { return ip_address_; }
1040 virtual const WifiNetworkVector& wifi_networks() const {
1041 return wifi_networks_;
1042 }
1043 virtual const WifiNetworkVector& remembered_wifi_networks() const {
1044 return wifi_networks_;
1045 }
1046 virtual const CellularNetworkVector& cellular_networks() const {
1047 return cellular_networks_;
1048 }
1049 virtual const CellularNetworkVector& remembered_cellular_networks() const {
1050 return cellular_networks_;
1051 }
1052
1053 /////////////////////////////////////////////////////////////////////////////
1054
1055 bool FindWifiNetworkByPath(
1056 const std::string& path, WifiNetwork* result) const { return false; }
1057 bool FindCellularNetworkByPath(
1058 const std::string& path, CellularNetwork* result) const { return false; }
1059 void RequestWifiScan() {}
1060 bool GetWifiAccessPoints(WifiAccessPointVector* result) { return false; }
1061 bool ConnectToPreferredNetworkIfAvailable() { return false; }
1062 bool PreferredNetworkConnected() { return false; }
1063 bool PreferredNetworkFailed() { return false; }
1064 void ConnectToWifiNetwork(WifiNetwork network,
1065 const std::string& password,
1066 const std::string& identity,
1067 const std::string& certpath) {}
1068 void ConnectToWifiNetwork(const std::string& ssid,
1069 const std::string& password,
1070 const std::string& identity,
1071 const std::string& certpath,
1072 bool auto_connect) {}
1073 void ConnectToCellularNetwork(CellularNetwork network) {}
1074 void DisconnectFromWirelessNetwork(const WirelessNetwork& network) {}
1075 void SaveCellularNetwork(const CellularNetwork& network) {}
1076 void SaveWifiNetwork(const WifiNetwork& network) {}
1077 void ForgetWirelessNetwork(const std::string& service_path) {}
1078 virtual bool ethernet_available() const { return true; }
1079 virtual bool wifi_available() const { return false; }
1080 virtual bool cellular_available() const { return false; }
1081 virtual bool ethernet_enabled() const { return true; }
1082 virtual bool wifi_enabled() const { return false; }
1083 virtual bool cellular_enabled() const { return false; }
1084 virtual bool offline_mode() const { return false; }
1085 void EnableEthernetNetworkDevice(bool enable) {}
1086 void EnableWifiNetworkDevice(bool enable) {}
1087 void EnableCellularNetworkDevice(bool enable) {}
1088 void EnableOfflineMode(bool enable) {}
1089 NetworkIPConfigVector GetIPConfigs(const std::string& device_path) {
1090 return NetworkIPConfigVector();
1091 }
1092 std::string GetHtmlInfo(int refresh) { return std::string(); }
1093 void UpdateSystemInfo() {}
1094
1095 private:
1096 std::string ip_address_;
1097 EthernetNetwork ethernet_;
1098 WifiNetworkVector wifi_networks_;
1099 CellularNetworkVector cellular_networks_;
1100 };
1101
1102 // static
1103 NetworkLibrary* NetworkLibrary::GetImpl(bool stub) {
1104 if (stub)
1105 return new NetworkLibraryStubImpl();
1106 else
1107 return new NetworkLibraryImpl();
841 } 1108 }
842 1109
843 void NetworkLibraryImpl::CheckNetworkTraffic(bool download) {
844 // If we already have a pending upload and download notification, then
845 // shortcut and return.
846 if (traffic_type_ == (Observer::TRAFFIC_DOWNLOAD | Observer::TRAFFIC_UPLOAD))
847 return;
848 // Figure out if we are uploading and/or downloading. We are downloading
849 // if download == true. We are uploading if we have upload progress.
850 if (download)
851 traffic_type_ |= Observer::TRAFFIC_DOWNLOAD;
852 if ((traffic_type_ & Observer::TRAFFIC_UPLOAD) == 0) {
853 URLRequestJobTracker::JobIterator it;
854 for (it = g_url_request_job_tracker.begin();
855 it != g_url_request_job_tracker.end();
856 ++it) {
857 URLRequestJob* job = *it;
858 if (job->GetUploadProgress() > 0) {
859 traffic_type_ |= Observer::TRAFFIC_UPLOAD;
860 break;
861 }
862 }
863 }
864 // If we have new traffic data to send out and the timer is not currently
865 // running, then start a new timer.
866 if (traffic_type_ && !timer_.IsRunning()) {
867 timer_.Start(base::TimeDelta::FromSeconds(kNetworkTrafficeTimerSecs), this,
868 &NetworkLibraryImpl::NetworkTrafficTimerFired);
869 }
870 }
871
872 void NetworkLibraryImpl:: NetworkTrafficTimerFired() {
873 ChromeThread::PostTask(
874 ChromeThread::UI, FROM_HERE,
875 NewRunnableMethod(this, &NetworkLibraryImpl::NotifyNetworkTraffic,
876 traffic_type_));
877 // Reset traffic type so that we don't send the same data next time.
878 traffic_type_ = 0;
879 }
880
881 void NetworkLibraryImpl::NotifyNetworkTraffic(int traffic_type) {
882 FOR_EACH_OBSERVER(Observer, observers_, NetworkTraffic(this, traffic_type));
883 }
884
885 bool NetworkLibraryImpl::Connected() const {
886 return ethernet_connected() || wifi_connected() || cellular_connected();
887 }
888
889 bool NetworkLibraryImpl::Connecting() const {
890 return ethernet_connecting() || wifi_connecting() || cellular_connecting();
891 }
892
893 const std::string& NetworkLibraryImpl::IPAddress() const {
894 // Returns highest priority IP address.
895 if (ethernet_connected())
896 return ethernet_.ip_address();
897 if (wifi_connected())
898 return wifi_.ip_address();
899 if (cellular_connected())
900 return cellular_.ip_address();
901 return ethernet_.ip_address();
902 }
903
904 } // namespace chromeos 1110 } // namespace chromeos
1111
1112 // Allows InvokeLater without adding refcounting. This class is a Singleton and
1113 // won't be deleted until it's last InvokeLater is run.
1114 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::NetworkLibraryImpl);
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/cros/network_library.h ('k') | chrome/browser/chromeos/cros/power_library.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698