314ddc546a414860e282aee158a91012e111fd18
[platform/framework/web/crosswalk.git] / src / components / metrics / net / wifi_access_point_info_provider_chromeos.cc
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/metrics/net/wifi_access_point_info_provider_chromeos.h"
6
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "chromeos/network/network_configuration_handler.h"
11 #include "chromeos/network/network_handler.h"
12 #include "chromeos/network/network_state.h"
13 #include "chromeos/network/network_state_handler.h"
14 #include "chromeos/network/shill_property_util.h"
15 #include "third_party/cros_system_api/dbus/service_constants.h"
16
17 using chromeos::NetworkHandler;
18
19 namespace metrics {
20
21 WifiAccessPointInfoProviderChromeos::WifiAccessPointInfoProviderChromeos() {
22   NetworkHandler::Get()->network_state_handler()->AddObserver(this, FROM_HERE);
23
24   // Update initial connection state.
25   DefaultNetworkChanged(
26       NetworkHandler::Get()->network_state_handler()->DefaultNetwork());
27 }
28
29 WifiAccessPointInfoProviderChromeos::~WifiAccessPointInfoProviderChromeos() {
30   NetworkHandler::Get()->network_state_handler()->RemoveObserver(this,
31                                                                  FROM_HERE);
32 }
33
34 bool WifiAccessPointInfoProviderChromeos::GetInfo(WifiAccessPointInfo* info) {
35   // Wifi access point information is not provided if the BSSID is empty.
36   // This assumes the BSSID is never empty when access point information exists.
37   if (wifi_access_point_info_.bssid.empty())
38     return false;
39
40   *info = wifi_access_point_info_;
41   return true;
42 }
43
44 void WifiAccessPointInfoProviderChromeos::DefaultNetworkChanged(
45     const chromeos::NetworkState* default_network) {
46   // Reset access point info to prevent reporting of out-dated data.
47   wifi_access_point_info_ = WifiAccessPointInfo();
48
49   // Skip non-wifi connections
50   if (!default_network || default_network->type() != shill::kTypeWifi)
51     return;
52
53   // Retrieve access point info for wifi connection.
54   NetworkHandler::Get()->network_configuration_handler()->GetProperties(
55       default_network->path(),
56       base::Bind(&WifiAccessPointInfoProviderChromeos::ParseInfo,
57                  AsWeakPtr()),
58       chromeos::network_handler::ErrorCallback());
59 }
60
61 void WifiAccessPointInfoProviderChromeos::ParseInfo(
62     const std::string &service_path,
63     const base::DictionaryValue& properties) {
64   // Skip services that contain "_nomap" in the SSID.
65   std::string ssid =
66       chromeos::shill_property_util::GetSSIDFromProperties(properties, NULL);
67   if (ssid.find("_nomap", 0) != std::string::npos)
68     return;
69
70   std::string bssid;
71   if (!properties.GetStringWithoutPathExpansion(shill::kWifiBSsid, &bssid) ||
72       bssid.empty())
73     return;
74
75   // Filter out BSSID with local bit set in the first byte.
76   uint32 first_octet;
77   if (!base::HexStringToUInt(bssid.substr(0, 2), &first_octet))
78     NOTREACHED();
79   if (first_octet & 0x2)
80     return;
81   wifi_access_point_info_.bssid = bssid;
82
83   // Parse security info.
84   std::string security;
85   properties.GetStringWithoutPathExpansion(
86       shill::kSecurityProperty, &security);
87   wifi_access_point_info_.security = WIFI_SECURITY_UNKNOWN;
88   if (security == shill::kSecurityWpa)
89     wifi_access_point_info_.security = WIFI_SECURITY_WPA;
90   else if (security == shill::kSecurityWep)
91     wifi_access_point_info_.security = WIFI_SECURITY_WEP;
92   else if (security == shill::kSecurityRsn)
93     wifi_access_point_info_.security = WIFI_SECURITY_RSN;
94   else if (security == shill::kSecurity8021x)
95     wifi_access_point_info_.security = WIFI_SECURITY_802_1X;
96   else if (security == shill::kSecurityPsk)
97     wifi_access_point_info_.security = WIFI_SECURITY_PSK;
98   else if (security == shill::kSecurityNone)
99     wifi_access_point_info_.security = WIFI_SECURITY_NONE;
100
101   properties.GetStringWithoutPathExpansion(
102       shill::kWifiBSsid, &wifi_access_point_info_.bssid);
103   const base::DictionaryValue* vendor_dict = NULL;
104   if (!properties.GetDictionaryWithoutPathExpansion(
105           shill::kWifiVendorInformationProperty,
106           &vendor_dict))
107     return;
108
109   vendor_dict->GetStringWithoutPathExpansion(
110       shill::kVendorWPSModelNumberProperty,
111       &wifi_access_point_info_.model_number);
112   vendor_dict->GetStringWithoutPathExpansion(
113       shill::kVendorWPSModelNameProperty,
114       &wifi_access_point_info_.model_name);
115   vendor_dict->GetStringWithoutPathExpansion(
116       shill::kVendorWPSDeviceNameProperty,
117       &wifi_access_point_info_.device_name);
118   vendor_dict->GetStringWithoutPathExpansion(shill::kVendorOUIListProperty,
119                                              &wifi_access_point_info_.oui_list);
120 }
121
122 }  // namespace metrics