| OLD | NEW | 
|    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_util.h" |    9 #include "base/string_util.h" | 
|   10 #include "base/utf_string_conversions.h" |   10 #include "base/utf_string_conversions.h" | 
|   11 #include "chrome/browser/chrome_thread.h" |   11 #include "chrome/browser/chrome_thread.h" | 
|   12 #include "chrome/browser/chromeos/cros/cros_library.h" |   12 #include "chrome/browser/chromeos/cros/cros_library.h" | 
|   13 #include "net/url_request/url_request_job.h" |   13 #include "net/url_request/url_request_job.h" | 
|   14  |   14  | 
|   15 // Allows InvokeLater without adding refcounting. This class is a Singleton and |   15 // Allows InvokeLater without adding refcounting. This class is a Singleton and | 
|   16 // won't be deleted until it's last InvokeLater is run. |   16 // won't be deleted until it's last InvokeLater is run. | 
|   17 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::NetworkLibraryImpl); |   17 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::NetworkLibraryImpl); | 
|   18  |   18  | 
|   19 namespace chromeos { |   19 namespace chromeos { | 
|   20  |   20  | 
 |   21 static const std::string kGoogleWifi = "Google"; | 
|   21 static const std::string kGoogleAWifi = "Google-A"; |   22 static const std::string kGoogleAWifi = "Google-A"; | 
|   22  |   23  | 
|   23 // Helper function to wrap Html with <th> tag. |   24 // Helper function to wrap Html with <th> tag. | 
|   24 static std::string WrapWithTH(std::string text) { |   25 static std::string WrapWithTH(std::string text) { | 
|   25   return "<th>" + text + "</th>"; |   26   return "<th>" + text + "</th>"; | 
|   26 } |   27 } | 
|   27  |   28  | 
|   28 // Helper function to wrap Html with <td> tag. |   29 // Helper function to wrap Html with <td> tag. | 
|   29 static std::string WrapWithTD(std::string text) { |   30 static std::string WrapWithTD(std::string text) { | 
|   30   return "<td>" + text + "</td>"; |   31   return "<td>" + text + "</td>"; | 
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  250 void NetworkLibraryImpl::RequestWifiScan() { |  251 void NetworkLibraryImpl::RequestWifiScan() { | 
|  251   if (CrosLibrary::Get()->EnsureLoaded()) { |  252   if (CrosLibrary::Get()->EnsureLoaded()) { | 
|  252     RequestScan(TYPE_WIFI); |  253     RequestScan(TYPE_WIFI); | 
|  253   } |  254   } | 
|  254 } |  255 } | 
|  255  |  256  | 
|  256 bool NetworkLibraryImpl::ConnectToPreferredNetworkIfAvailable() { |  257 bool NetworkLibraryImpl::ConnectToPreferredNetworkIfAvailable() { | 
|  257   // TODO(chocobo): Add the concept of preferred network to libcros. |  258   // TODO(chocobo): Add the concept of preferred network to libcros. | 
|  258   // So that we don't have to hard-code Google-A here. |  259   // So that we don't have to hard-code Google-A here. | 
|  259   if (CrosLibrary::Get()->EnsureLoaded()) { |  260   if (CrosLibrary::Get()->EnsureLoaded()) { | 
|  260     LOG(INFO) << "Attempting to auto-connect to Google-A wifi."; |  261     LOG(INFO) << "Attempting to auto-connect to Google wifi."; | 
|  261     // First force a refresh of the system info. |  262     // First force a refresh of the system info. | 
|  262     UpdateSystemInfo(); |  263     UpdateSystemInfo(); | 
|  263  |  264  | 
|  264     // If ethernet is connected, then don't bother. |  265     // If ethernet is connected, then don't bother. | 
|  265     if (ethernet_connected()) { |  266     if (ethernet_connected()) { | 
|  266       LOG(INFO) << "Ethernet connected, so don't need Google-A."; |  267       LOG(INFO) << "Ethernet connected, so don't need Google wifi."; | 
|  267       return false; |  268       return false; | 
|  268     } |  269     } | 
|  269  |  270  | 
|  270     // Find Google-A network. |  271     // First look for Google-A then look for Google. | 
|  271     WifiNetwork wifi; |  272     // Only care if set to auto-connect. | 
|  272     bool found = GetWifiNetworkByName(kGoogleAWifi, &wifi); |  273     WifiNetwork* wifi = GetWifiNetworkByName(kGoogleAWifi); | 
|  273     if (!found) { |  274     // If wifi found and set to not auto-connect, then ignore it. | 
|  274       LOG(INFO) << "Google-A wifi not found."; |  275     if (wifi && !wifi->auto_connect()) | 
 |  276       wifi = NULL; | 
 |  277  | 
 |  278     if (!wifi) { | 
 |  279       wifi = GetWifiNetworkByName(kGoogleWifi); | 
 |  280       // If wifi found and set to not auto-connect, then ignore it. | 
 |  281       if (wifi && !wifi->auto_connect()) | 
 |  282         wifi = NULL; | 
 |  283     } | 
 |  284  | 
 |  285     if (!wifi) { | 
 |  286       LOG(INFO) << "Google-A/Google wifi not found or set to not auto-connect."; | 
|  275       return false; |  287       return false; | 
|  276     } |  288     } | 
|  277  |  289  | 
|  278     // Make sure we want to auto-connect. |  290     // Save the wifi path, so we know which one we want to auto-connect to. | 
|  279     // Since auto-connect is true, we assume the identity and certificate |  291     const std::string wifi_path = wifi->service_path(); | 
|  280     // is setup propertly. If it's not, the connection will fail. |  | 
|  281     if (!wifi.auto_connect()) { |  | 
|  282       LOG(INFO) << "Google-A wifi is set to not auto-connect."; |  | 
|  283       return false; |  | 
|  284     } |  | 
|  285  |  292  | 
|  286     // It takes some time for the enterprise daemon to start up and populate the |  293     // It takes some time for the enterprise daemon to start up and populate the | 
|  287     // certificate and identity. So we wait at most 3 seconds here. And every |  294     // certificate and identity. So we wait at most 3 seconds here. And every | 
|  288     // 100ms, we refetch the system info and check the cert and identify on the |  295     // 100ms, we refetch the system info and check the cert and identify on the | 
|  289     // wifi. The enterprise daemon takes between 0.4 to 0.9 seconds to setup. |  296     // wifi. The enterprise daemon takes between 0.4 to 0.9 seconds to setup. | 
|  290     bool setup = false; |  297     bool setup = false; | 
|  291     for (int i = 0; i < 30; i++) { |  298     for (int i = 0; i < 30; i++) { | 
|  292       // Update the system and refetch the network. |  299       // Update the system and refetch the network. | 
|  293       UpdateSystemInfo(); |  300       UpdateSystemInfo(); | 
|  294       GetWifiNetworkByName(kGoogleAWifi, &wifi); |  301       wifi = GetWifiNetworkByPath(wifi_path); | 
|  295       // See if identity and certpath are available. |  302       // See if identity and certpath are available. | 
|  296       if (!wifi.identity().empty() && !wifi.cert_path().empty()) { |  303       if (wifi && !wifi->identity().empty() && !wifi->cert_path().empty()) { | 
|  297         LOG(INFO) << "Google-A wifi set up after " << (i*0.1) << " seconds."; |  304         LOG(INFO) << "Google wifi set up after " << (i*0.1) << " seconds."; | 
|  298         setup = true; |  305         setup = true; | 
|  299         break; |  306         break; | 
|  300       } |  307       } | 
|  301       PlatformThread::Sleep(100); |  308       PlatformThread::Sleep(100); | 
|  302     } |  309     } | 
|  303  |  310  | 
|  304     if (!setup) { |  311     if (!setup) { | 
|  305       LOG(INFO) << "Google-A wifi not set up after 3 seconds."; |  312       LOG(INFO) << "Google wifi not set up after 3 seconds."; | 
|  306       return false; |  313       return false; | 
|  307     } |  314     } | 
|  308  |  315  | 
|  309     // Now that we have a setup Google-A, we can connect to it. |  316     // Now that we have a setup Google wifi, we can connect to it. | 
|  310     ConnectToNetwork(wifi.service_path().c_str(), NULL); |  317     ConnectToNetwork(wifi_path.c_str(), NULL); | 
|  311     return true; |  318     return true; | 
|  312   } |  319   } | 
|  313   return false; |  320   return false; | 
|  314 } |  321 } | 
|  315  |  322  | 
|  316 bool NetworkLibraryImpl::PreferredNetworkConnected() { |  323 bool NetworkLibraryImpl::PreferredNetworkConnected() { | 
|  317   WifiNetwork wifi; |  324   WifiNetwork* wifi = GetWifiNetworkByName(kGoogleAWifi); | 
|  318   bool found = GetWifiNetworkByName(kGoogleAWifi, &wifi); |  325   return wifi && wifi->connected(); | 
|  319   return found && wifi.connected(); |  | 
|  320 } |  326 } | 
|  321  |  327  | 
|  322 bool NetworkLibraryImpl::PreferredNetworkFailed() { |  328 bool NetworkLibraryImpl::PreferredNetworkFailed() { | 
|  323   WifiNetwork wifi; |  329   WifiNetwork* wifi = GetWifiNetworkByName(kGoogleAWifi); | 
|  324   bool found = GetWifiNetworkByName(kGoogleAWifi, &wifi); |  330   return !wifi || wifi->failed(); | 
|  325   return !found || wifi.failed(); |  | 
|  326 } |  331 } | 
|  327  |  332  | 
|  328 void NetworkLibraryImpl::ConnectToWifiNetwork(WifiNetwork network, |  333 void NetworkLibraryImpl::ConnectToWifiNetwork(WifiNetwork network, | 
|  329                                               const string16& password, |  334                                               const string16& password, | 
|  330                                               const string16& identity, |  335                                               const string16& identity, | 
|  331                                               const string16& certpath) { |  336                                               const string16& certpath) { | 
|  332   if (CrosLibrary::Get()->EnsureLoaded()) { |  337   if (CrosLibrary::Get()->EnsureLoaded()) { | 
|  333     ConnectToNetworkWithCertInfo(network.service_path().c_str(), |  338     ConnectToNetworkWithCertInfo(network.service_path().c_str(), | 
|  334                      password.empty() ? NULL : UTF16ToUTF8(password).c_str(), |  339                      password.empty() ? NULL : UTF16ToUTF8(password).c_str(), | 
|  335                      identity.empty() ? NULL : UTF16ToUTF8(identity).c_str(), |  340                      identity.empty() ? NULL : UTF16ToUTF8(identity).c_str(), | 
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  371 } |  376 } | 
|  372  |  377  | 
|  373 void NetworkLibraryImpl::DisconnectFromWirelessNetwork( |  378 void NetworkLibraryImpl::DisconnectFromWirelessNetwork( | 
|  374     const WirelessNetwork& network) { |  379     const WirelessNetwork& network) { | 
|  375   if (CrosLibrary::Get()->EnsureLoaded()) { |  380   if (CrosLibrary::Get()->EnsureLoaded()) { | 
|  376     DisconnectFromNetwork(network.service_path().c_str()); |  381     DisconnectFromNetwork(network.service_path().c_str()); | 
|  377   } |  382   } | 
|  378 } |  383 } | 
|  379  |  384  | 
|  380 void NetworkLibraryImpl::SaveWifiNetwork(const WifiNetwork& network) { |  385 void NetworkLibraryImpl::SaveWifiNetwork(const WifiNetwork& network) { | 
 |  386   // Update the wifi network in the local cache. | 
 |  387   WifiNetwork* wifi = GetWifiNetworkByPath(network.service_path()); | 
 |  388   if (wifi) | 
 |  389     *wifi = network; | 
 |  390  | 
 |  391   // Update the wifi network with libcros. | 
|  381   if (CrosLibrary::Get()->EnsureLoaded()) { |  392   if (CrosLibrary::Get()->EnsureLoaded()) { | 
|  382     SetPassphrase(network.service_path().c_str(), network.passphrase().c_str()); |  393     SetPassphrase(network.service_path().c_str(), network.passphrase().c_str()); | 
|  383     SetIdentity(network.service_path().c_str(), network.identity().c_str()); |  394     SetIdentity(network.service_path().c_str(), network.identity().c_str()); | 
|  384     SetCertPath(network.service_path().c_str(), network.cert_path().c_str()); |  395     SetCertPath(network.service_path().c_str(), network.cert_path().c_str()); | 
|  385     SetAutoConnect(network.service_path().c_str(), network.auto_connect()); |  396     SetAutoConnect(network.service_path().c_str(), network.auto_connect()); | 
|  386   } |  397   } | 
|  387 } |  398 } | 
|  388  |  399  | 
|  389 void NetworkLibraryImpl::ForgetWirelessNetwork(const WirelessNetwork& network) { |  400 void NetworkLibraryImpl::ForgetWirelessNetwork(const WirelessNetwork& network) { | 
|  390   if (CrosLibrary::Get()->EnsureLoaded()) { |  401   if (CrosLibrary::Get()->EnsureLoaded()) { | 
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  573 } |  584 } | 
|  574  |  585  | 
|  575 void NetworkLibraryImpl::UpdateSystemInfo() { |  586 void NetworkLibraryImpl::UpdateSystemInfo() { | 
|  576   SystemInfo* system = GetSystemInfo(); |  587   SystemInfo* system = GetSystemInfo(); | 
|  577   if (system) { |  588   if (system) { | 
|  578     UpdateNetworkStatus(system); |  589     UpdateNetworkStatus(system); | 
|  579     FreeSystemInfo(system); |  590     FreeSystemInfo(system); | 
|  580   } |  591   } | 
|  581 } |  592 } | 
|  582  |  593  | 
|  583 bool NetworkLibraryImpl::GetWifiNetworkByName(const std::string& name, |  594 WifiNetwork* NetworkLibraryImpl::GetWifiNetworkByName(const std::string& name) { | 
|  584                                               WifiNetwork* wifi) { |  | 
|  585   for (size_t i = 0; i < wifi_networks_.size(); ++i) { |  595   for (size_t i = 0; i < wifi_networks_.size(); ++i) { | 
|  586     if (wifi_networks_[i].name().compare(name) == 0) { |  596     if (wifi_networks_[i].name().compare(name) == 0) { | 
|  587       *wifi = wifi_networks_[i]; |  597       return &wifi_networks_[i]; | 
|  588       return true; |  | 
|  589     } |  598     } | 
|  590   } |  599   } | 
|  591   return false; |  600   return NULL; | 
 |  601 } | 
 |  602  | 
 |  603 WifiNetwork* NetworkLibraryImpl::GetWifiNetworkByPath(const std::string& path) { | 
 |  604   for (size_t i = 0; i < wifi_networks_.size(); ++i) { | 
 |  605     if (wifi_networks_[i].service_path().compare(path) == 0) { | 
 |  606       return &wifi_networks_[i]; | 
 |  607     } | 
 |  608   } | 
 |  609   return NULL; | 
|  592 } |  610 } | 
|  593  |  611  | 
|  594 void NetworkLibraryImpl::EnableNetworkDeviceType(ConnectionType device, |  612 void NetworkLibraryImpl::EnableNetworkDeviceType(ConnectionType device, | 
|  595                                                  bool enable) { |  613                                                  bool enable) { | 
|  596   if (!CrosLibrary::Get()->EnsureLoaded()) |  614   if (!CrosLibrary::Get()->EnsureLoaded()) | 
|  597     return; |  615     return; | 
|  598  |  616  | 
|  599   // If network device is already enabled/disabled, then don't do anything. |  617   // If network device is already enabled/disabled, then don't do anything. | 
|  600   if (enable && (enabled_devices_ & (1 << device))) { |  618   if (enable && (enabled_devices_ & (1 << device))) { | 
|  601     LOG(WARNING) << "Trying to enable a device that's already enabled: " |  619     LOG(WARNING) << "Trying to enable a device that's already enabled: " | 
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  706   if (ethernet_connected()) |  724   if (ethernet_connected()) | 
|  707     return ethernet_.ip_address(); |  725     return ethernet_.ip_address(); | 
|  708   if (wifi_connected()) |  726   if (wifi_connected()) | 
|  709     return wifi_.ip_address(); |  727     return wifi_.ip_address(); | 
|  710   if (cellular_connected()) |  728   if (cellular_connected()) | 
|  711     return cellular_.ip_address(); |  729     return cellular_.ip_address(); | 
|  712   return ethernet_.ip_address(); |  730   return ethernet_.ip_address(); | 
|  713 } |  731 } | 
|  714  |  732  | 
|  715 }  // namespace chromeos |  733 }  // namespace chromeos | 
| OLD | NEW |