Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chromeos / network / shill_property_util.cc
index 35e4bb1..405de1e 100644 (file)
@@ -50,12 +50,23 @@ bool CopyStringFromDictionary(const base::DictionaryValue& source,
                               base::DictionaryValue* dest) {
   std::string string_value;
   if (!source.GetStringWithoutPathExpansion(key, &string_value) ||
-      string_value.empty())
+      string_value.empty()) {
     return false;
+  }
   dest->SetStringWithoutPathExpansion(key, string_value);
   return true;
 }
 
+// This is the same normalization that Shill applies to security types for the
+// sake of comparing/identifying WiFi networks. See Shill's
+// WiFiService::GetSecurityClass.
+std::string GetSecurityClass(const std::string& security) {
+  if (security == shill::kSecurityRsn || security == shill::kSecurityWpa)
+    return shill::kSecurityPsk;
+  else
+    return security;
+}
+
 }  // namespace
 
 void SetSSID(const std::string ssid, base::DictionaryValue* properties) {
@@ -65,13 +76,22 @@ void SetSSID(const std::string ssid, base::DictionaryValue* properties) {
 
 std::string GetSSIDFromProperties(const base::DictionaryValue& properties,
                                   bool* unknown_encoding) {
-  if (unknown_encoding)
+  bool verbose_logging = false;
+  if (unknown_encoding) {
     *unknown_encoding = false;
+    verbose_logging = true;
+  }
+
+  // Get name for debugging.
+  std::string name;
+  properties.GetStringWithoutPathExpansion(shill::kNameProperty, &name);
+
   std::string hex_ssid;
   properties.GetStringWithoutPathExpansion(shill::kWifiHexSsid, &hex_ssid);
 
   if (hex_ssid.empty()) {
-    NET_LOG_ERROR("GetSSIDFromProperties", "No HexSSID set.");
+    if (verbose_logging)
+      NET_LOG_DEBUG("GetSSIDFromProperties: No HexSSID set.", name);
     return std::string();
   }
 
@@ -79,16 +99,18 @@ std::string GetSSIDFromProperties(const base::DictionaryValue& properties,
   std::vector<uint8> raw_ssid_bytes;
   if (base::HexStringToBytes(hex_ssid, &raw_ssid_bytes)) {
     ssid = std::string(raw_ssid_bytes.begin(), raw_ssid_bytes.end());
-    NET_LOG_DEBUG(
-        "GetSSIDFromProperties",
-        base::StringPrintf("%s, SSID: %s", hex_ssid.c_str(), ssid.c_str()));
+    if (verbose_logging) {
+      NET_LOG_DEBUG(base::StringPrintf("GetSSIDFromProperties: %s, SSID: %s",
+                                       hex_ssid.c_str(), ssid.c_str()), name);
+    }
   } else {
-    NET_LOG_ERROR("GetSSIDFromProperties",
-                  base::StringPrintf("Error processing: %s", hex_ssid.c_str()));
+    NET_LOG_ERROR(
+        base::StringPrintf("GetSSIDFromProperties: Error processing: %s",
+                           hex_ssid.c_str()), name);
     return std::string();
   }
 
-  if (IsStringUTF8(ssid))
+  if (base::IsStringUTF8(ssid))
     return ssid;
 
   // Detect encoding and convert to UTF-8.
@@ -104,19 +126,22 @@ std::string GetSSIDFromProperties(const base::DictionaryValue& properties,
   if (!encoding.empty() &&
       base::ConvertToUtf8AndNormalize(ssid, encoding, &utf8_ssid)) {
     if (utf8_ssid != ssid) {
-      NET_LOG_DEBUG(
-          "GetSSIDFromProperties",
-          base::StringPrintf(
-              "Encoding=%s: %s", encoding.c_str(), utf8_ssid.c_str()));
+      if (verbose_logging) {
+        NET_LOG_DEBUG(
+            base::StringPrintf("GetSSIDFromProperties: Encoding=%s: %s",
+                               encoding.c_str(), utf8_ssid.c_str()), name);
+      }
     }
     return utf8_ssid;
   }
 
   if (unknown_encoding)
     *unknown_encoding = true;
-  NET_LOG_DEBUG(
-      "GetSSIDFromProperties",
-      base::StringPrintf("Unrecognized Encoding=%s", encoding.c_str()));
+  if (verbose_logging) {
+    NET_LOG_DEBUG(
+        base::StringPrintf("GetSSIDFromProperties: Unrecognized Encoding=%s",
+                           encoding.c_str()), name);
+  }
   return ssid;
 }
 
@@ -218,6 +243,7 @@ void SetUIData(const NetworkUIData& ui_data,
 }
 
 bool CopyIdentifyingProperties(const base::DictionaryValue& service_properties,
+                               const bool properties_read_from_shill,
                                base::DictionaryValue* dest) {
   bool success = true;
 
@@ -229,8 +255,15 @@ bool CopyIdentifyingProperties(const base::DictionaryValue& service_properties,
   success &= !type.empty();
   dest->SetStringWithoutPathExpansion(shill::kTypeProperty, type);
   if (type == shill::kTypeWifi) {
-    success &= CopyStringFromDictionary(
-        service_properties, shill::kSecurityProperty, dest);
+    std::string security;
+    service_properties.GetStringWithoutPathExpansion(shill::kSecurityProperty,
+                                                     &security);
+    if (security.empty()) {
+      success = false;
+    } else {
+      dest->SetStringWithoutPathExpansion(shill::kSecurityProperty,
+                                          GetSecurityClass(security));
+    }
     success &=
         CopyStringFromDictionary(service_properties, shill::kWifiHexSsid, dest);
     success &= CopyStringFromDictionary(
@@ -238,25 +271,33 @@ bool CopyIdentifyingProperties(const base::DictionaryValue& service_properties,
   } else if (type == shill::kTypeVPN) {
     success &= CopyStringFromDictionary(
         service_properties, shill::kNameProperty, dest);
+
     // VPN Provider values are read from the "Provider" dictionary, but written
     // with the keys "Provider.Type" and "Provider.Host".
-    const base::DictionaryValue* provider_properties = NULL;
-    if (!service_properties.GetDictionaryWithoutPathExpansion(
-            shill::kProviderProperty, &provider_properties)) {
-      NET_LOG_ERROR("Missing VPN provider dict",
-                    GetNetworkIdFromProperties(service_properties));
-      return false;
-    }
+    // TODO(pneubeck): Simplify this once http://crbug.com/381135 is fixed.
     std::string vpn_provider_type;
-    provider_properties->GetStringWithoutPathExpansion(shill::kTypeProperty,
-                                                       &vpn_provider_type);
+    std::string vpn_provider_host;
+    if (properties_read_from_shill) {
+      const base::DictionaryValue* provider_properties = NULL;
+      if (!service_properties.GetDictionaryWithoutPathExpansion(
+              shill::kProviderProperty, &provider_properties)) {
+        NET_LOG_ERROR("Missing VPN provider dict",
+                      GetNetworkIdFromProperties(service_properties));
+      }
+      provider_properties->GetStringWithoutPathExpansion(shill::kTypeProperty,
+                                                         &vpn_provider_type);
+      provider_properties->GetStringWithoutPathExpansion(shill::kHostProperty,
+                                                         &vpn_provider_host);
+    } else {
+      service_properties.GetStringWithoutPathExpansion(
+          shill::kProviderTypeProperty, &vpn_provider_type);
+      service_properties.GetStringWithoutPathExpansion(
+          shill::kProviderHostProperty, &vpn_provider_host);
+    }
     success &= !vpn_provider_type.empty();
     dest->SetStringWithoutPathExpansion(shill::kProviderTypeProperty,
                                         vpn_provider_type);
 
-    std::string vpn_provider_host;
-    provider_properties->GetStringWithoutPathExpansion(shill::kHostProperty,
-                                                       &vpn_provider_host);
     success &= !vpn_provider_host.empty();
     dest->SetStringWithoutPathExpansion(shill::kProviderHostProperty,
                                         vpn_provider_host);
@@ -274,16 +315,23 @@ bool CopyIdentifyingProperties(const base::DictionaryValue& service_properties,
   return success;
 }
 
-bool DoIdentifyingPropertiesMatch(const base::DictionaryValue& properties_a,
-                                  const base::DictionaryValue& properties_b) {
-  base::DictionaryValue identifying_a;
-  if (!CopyIdentifyingProperties(properties_a, &identifying_a))
+bool DoIdentifyingPropertiesMatch(const base::DictionaryValue& new_properties,
+                                  const base::DictionaryValue& old_properties) {
+  base::DictionaryValue new_identifying;
+  if (!CopyIdentifyingProperties(
+          new_properties,
+          false /* properties were not read from Shill */,
+          &new_identifying)) {
     return false;
-  base::DictionaryValue identifying_b;
-  if (!CopyIdentifyingProperties(properties_b, &identifying_b))
+  }
+  base::DictionaryValue old_identifying;
+  if (!CopyIdentifyingProperties(old_properties,
+                                 true /* properties were read from Shill */,
+                                 &old_identifying)) {
     return false;
+  }
 
-  return identifying_a.Equals(&identifying_b);
+  return new_identifying.Equals(&old_identifying);
 }
 
 bool IsPassphraseKey(const std::string& key) {
@@ -300,143 +348,33 @@ bool IsPassphraseKey(const std::string& key) {
       key == shill::kApnPasswordProperty;
 }
 
-}  // namespace shill_property_util
-
-namespace {
-
-const char kPatternDefault[] = "PatternDefault";
-const char kPatternEthernet[] = "PatternEthernet";
-const char kPatternWireless[] = "PatternWireless";
-const char kPatternMobile[] = "PatternMobile";
-const char kPatternNonVirtual[] = "PatternNonVirtual";
-
-enum NetworkTypeBitFlag {
-  kNetworkTypeNone = 0,
-  kNetworkTypeEthernet = 1 << 0,
-  kNetworkTypeWifi = 1 << 1,
-  kNetworkTypeWimax = 1 << 2,
-  kNetworkTypeCellular = 1 << 3,
-  kNetworkTypeVPN = 1 << 4,
-  kNetworkTypeEthernetEap = 1 << 5,
-  kNetworkTypeBluetooth = 1 << 6
-};
-
-struct ShillToBitFlagEntry {
-  const char* shill_network_type;
-  NetworkTypeBitFlag bit_flag;
-} shill_type_to_flag[] = {
-  { shill::kTypeEthernet, kNetworkTypeEthernet },
-  { shill::kTypeEthernetEap, kNetworkTypeEthernetEap },
-  { shill::kTypeWifi, kNetworkTypeWifi },
-  { shill::kTypeWimax, kNetworkTypeWimax },
-  { shill::kTypeCellular, kNetworkTypeCellular },
-  { shill::kTypeVPN, kNetworkTypeVPN },
-  { shill::kTypeBluetooth, kNetworkTypeBluetooth }
-};
-
-NetworkTypeBitFlag ShillNetworkTypeToFlag(const std::string& shill_type) {
-  for (size_t i = 0; i < arraysize(shill_type_to_flag); ++i) {
-    if (shill_type_to_flag[i].shill_network_type == shill_type)
-      return shill_type_to_flag[i].bit_flag;
-  }
-  NET_LOG_ERROR("ShillNetworkTypeToFlag", "Unknown type: " + shill_type);
-  return kNetworkTypeNone;
-}
-
-}  // namespace
-
-// static
-NetworkTypePattern NetworkTypePattern::Default() {
-  return NetworkTypePattern(~0);
-}
-
-// static
-NetworkTypePattern NetworkTypePattern::Wireless() {
-  return NetworkTypePattern(kNetworkTypeWifi | kNetworkTypeWimax |
-                            kNetworkTypeCellular);
-}
-
-// static
-NetworkTypePattern NetworkTypePattern::Mobile() {
-  return NetworkTypePattern(kNetworkTypeCellular | kNetworkTypeWimax);
-}
-
-// static
-NetworkTypePattern NetworkTypePattern::NonVirtual() {
-  return NetworkTypePattern(~kNetworkTypeVPN);
-}
-
-// static
-NetworkTypePattern NetworkTypePattern::Ethernet() {
-  return NetworkTypePattern(kNetworkTypeEthernet);
-}
-
-// static
-NetworkTypePattern NetworkTypePattern::WiFi() {
-  return NetworkTypePattern(kNetworkTypeWifi);
-}
-
-// static
-NetworkTypePattern NetworkTypePattern::Cellular() {
-  return NetworkTypePattern(kNetworkTypeCellular);
-}
-
-// static
-NetworkTypePattern NetworkTypePattern::VPN() {
-  return NetworkTypePattern(kNetworkTypeVPN);
-}
-
-// static
-NetworkTypePattern NetworkTypePattern::Wimax() {
-  return NetworkTypePattern(kNetworkTypeWimax);
-}
-
-// static
-NetworkTypePattern NetworkTypePattern::Primitive(
-    const std::string& shill_network_type) {
-  return NetworkTypePattern(ShillNetworkTypeToFlag(shill_network_type));
-}
-
-bool NetworkTypePattern::Equals(const NetworkTypePattern& other) const {
-  return pattern_ == other.pattern_;
-}
-
-bool NetworkTypePattern::MatchesType(
-    const std::string& shill_network_type) const {
-  return MatchesPattern(Primitive(shill_network_type));
-}
-
-bool NetworkTypePattern::MatchesPattern(
-    const NetworkTypePattern& other_pattern) const {
-  if (Equals(other_pattern))
-    return true;
-
-  return pattern_ & other_pattern.pattern_;
-}
-
-std::string NetworkTypePattern::ToDebugString() const {
-  if (Equals(Default()))
-    return kPatternDefault;
-  if (Equals(Ethernet()))
-    return kPatternEthernet;
-  if (Equals(Wireless()))
-    return kPatternWireless;
-  if (Equals(Mobile()))
-    return kPatternMobile;
-  if (Equals(NonVirtual()))
-    return kPatternNonVirtual;
-
-  std::string str;
-  for (size_t i = 0; i < arraysize(shill_type_to_flag); ++i) {
-    if (!(pattern_ & shill_type_to_flag[i].bit_flag))
-      continue;
-    if (!str.empty())
-      str += "|";
-    str += shill_type_to_flag[i].shill_network_type;
+bool GetHomeProviderFromProperty(const base::Value& value,
+                                 std::string* home_provider_id) {
+  const base::DictionaryValue* dict = NULL;
+  if (!value.GetAsDictionary(&dict))
+    return false;
+  std::string home_provider_country;
+  std::string home_provider_name;
+  dict->GetStringWithoutPathExpansion(shill::kOperatorCountryKey,
+                                      &home_provider_country);
+  dict->GetStringWithoutPathExpansion(shill::kOperatorNameKey,
+                                      &home_provider_name);
+  // Set home_provider_id
+  if (!home_provider_name.empty() && !home_provider_country.empty()) {
+    *home_provider_id = base::StringPrintf(
+        "%s (%s)", home_provider_name.c_str(), home_provider_country.c_str());
+  } else {
+    if (!dict->GetStringWithoutPathExpansion(shill::kOperatorCodeKey,
+                                             home_provider_id)) {
+      return false;
+    }
+    LOG(WARNING)
+        << "Provider name and country not defined, using code instead: "
+        << *home_provider_id;
   }
-  return str;
+  return true;
 }
 
-NetworkTypePattern::NetworkTypePattern(int pattern) : pattern_(pattern) {}
+}  // namespace shill_property_util
 
 }  // namespace chromeos