1 // Copyright 2020 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 "chromeos/network/network_event_log.h"
6
7 #include "base/strings/string_util.h"
8 #include "chromeos/network/network_handler.h"
9 #include "chromeos/network/network_state.h"
10 #include "chromeos/network/network_state_handler.h"
11 #include "chromeos/network/tether_constants.h"
12 #include "third_party/cros_system_api/dbus/service_constants.h"
13
14 namespace {
15 constexpr char kServicePrefix[] = "/service/";
16 constexpr char kUnknownId[] = "<none>";
17
GetNetworkStateHandler()18 chromeos::NetworkStateHandler* GetNetworkStateHandler() {
19 if (!chromeos::NetworkHandler::IsInitialized())
20 return nullptr;
21 return chromeos::NetworkHandler::Get()->network_state_handler();
22 }
23
24 } // namespace
25
26 namespace chromeos {
27
28 // Returns a descriptive unique id for |network|
29 // e.g.: ethernet_0, wifi_psk_1, cellular_lte_2, vpn_openvpn_3.
NetworkId(const NetworkState * network)30 std::string NetworkId(const NetworkState* network) {
31 if (!network)
32 return kUnknownId;
33
34 const std::string& type = network->type();
35 if (type == kTypeTether) {
36 // Tether uses a GUID for its service path. Use the first 8 digits.
37 return type + "_" + network->path().substr(0, 8);
38 }
39
40 // Test networks may not follow the Shill pattern, just use the path.
41 if (!base::StartsWith(network->path(), kServicePrefix,
42 base::CompareCase::SENSITIVE)) {
43 return network->path();
44 }
45
46 std::string id = network->path().substr(strlen(kServicePrefix));
47 if (type.empty())
48 return "service_" + id;
49
50 std::string result = type + "_";
51 if (type == shill::kTypeWifi) {
52 result += network->security_class() + "_";
53 } else if (type == shill::kTypeCellular) {
54 result += network->network_technology() + "_";
55 } else if (type == shill::kTypeVPN) {
56 result += network->GetVpnProviderType() + "_";
57 }
58 return result + id;
59 }
60
61 // Calls NetworkId() if a NetworkState for |service_path| exists, otherwise
62 // returns service_{id}. If |service_path| does not represent a valid service
63 // path (e.g. in tests), returns |service_path|.
NetworkPathId(const std::string & service_path)64 std::string NetworkPathId(const std::string& service_path) {
65 NetworkStateHandler* handler = GetNetworkStateHandler();
66 if (handler) {
67 const NetworkState* network = handler->GetNetworkState(service_path);
68 if (network)
69 return NetworkId(network);
70 }
71 if (!base::StartsWith(service_path, kServicePrefix,
72 base::CompareCase::SENSITIVE)) {
73 return service_path;
74 }
75 std::string id = service_path.substr(strlen(kServicePrefix));
76 return "service_" + id;
77 }
78
79 // Calls NetworkId() if a NetworkState exists for |guid|, otherwise
80 // returns |guid|.
NetworkGuidId(const std::string & guid)81 std::string NetworkGuidId(const std::string& guid) {
82 if (guid.empty())
83 return kUnknownId;
84 NetworkStateHandler* handler = GetNetworkStateHandler();
85 if (handler) {
86 const NetworkState* network = handler->GetNetworkStateFromGuid(guid);
87 if (network)
88 return NetworkId(network);
89 }
90 return guid;
91 }
92
93 } // namespace chromeos
94