xref: /aosp_15_r20/external/cronet/net/base/logging_network_change_observer.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2016 The Chromium Authors
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 "net/base/logging_network_change_observer.h"
6 
7 #include <string>
8 
9 #include "base/functional/bind.h"
10 #include "base/logging.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/values.h"
13 #include "build/build_config.h"
14 #include "net/log/net_log.h"
15 #include "net/log/net_log_event_type.h"
16 
17 #if BUILDFLAG(IS_ANDROID)
18 #include "base/android/build_info.h"
19 #endif
20 
21 namespace net {
22 
23 namespace {
24 
25 // Returns a human readable integer from a handles::NetworkHandle.
HumanReadableNetworkHandle(handles::NetworkHandle network)26 int HumanReadableNetworkHandle(handles::NetworkHandle network) {
27 #if BUILDFLAG(IS_ANDROID)
28   // On Marshmallow, demunge the NetID to undo munging done in java
29   // Network.getNetworkHandle() by shifting away 0xfacade from
30   // http://androidxref.com/6.0.1_r10/xref/frameworks/base/core/java/android/net/Network.java#385
31   if (base::android::BuildInfo::GetInstance()->sdk_int() >=
32       base::android::SDK_VERSION_MARSHMALLOW) {
33     return network >> 32;
34   }
35 #endif
36   return network;
37 }
38 
39 // Return a dictionary of values that provide information about a
40 // network-specific change. This also includes relevant current state
41 // like the default network, and the types of active networks.
NetworkSpecificNetLogParams(handles::NetworkHandle network)42 base::Value::Dict NetworkSpecificNetLogParams(handles::NetworkHandle network) {
43   base::Value::Dict dict;
44   dict.Set("changed_network_handle", HumanReadableNetworkHandle(network));
45   dict.Set("changed_network_type",
46            NetworkChangeNotifier::ConnectionTypeToString(
47                NetworkChangeNotifier::GetNetworkConnectionType(network)));
48   dict.Set(
49       "default_active_network_handle",
50       HumanReadableNetworkHandle(NetworkChangeNotifier::GetDefaultNetwork()));
51   NetworkChangeNotifier::NetworkList networks;
52   NetworkChangeNotifier::GetConnectedNetworks(&networks);
53   for (handles::NetworkHandle active_network : networks) {
54     dict.Set(
55         "current_active_networks." +
56             base::NumberToString(HumanReadableNetworkHandle(active_network)),
57         NetworkChangeNotifier::ConnectionTypeToString(
58             NetworkChangeNotifier::GetNetworkConnectionType(active_network)));
59   }
60   return dict;
61 }
62 
NetLogNetworkSpecific(NetLogWithSource & net_log,NetLogEventType type,handles::NetworkHandle network)63 void NetLogNetworkSpecific(NetLogWithSource& net_log,
64                            NetLogEventType type,
65                            handles::NetworkHandle network) {
66   net_log.AddEvent(type,
67                           [&] { return NetworkSpecificNetLogParams(network); });
68 }
69 
70 }  // namespace
71 
LoggingNetworkChangeObserver(NetLog * net_log)72 LoggingNetworkChangeObserver::LoggingNetworkChangeObserver(NetLog* net_log)
73     : net_log_(NetLogWithSource::Make(net_log, NetLogSourceType::NETWORK_CHANGE_NOTIFIER)) {
74   NetworkChangeNotifier::AddIPAddressObserver(this);
75   NetworkChangeNotifier::AddConnectionTypeObserver(this);
76   NetworkChangeNotifier::AddNetworkChangeObserver(this);
77   if (NetworkChangeNotifier::AreNetworkHandlesSupported())
78     NetworkChangeNotifier::AddNetworkObserver(this);
79 }
80 
~LoggingNetworkChangeObserver()81 LoggingNetworkChangeObserver::~LoggingNetworkChangeObserver() {
82   NetworkChangeNotifier::RemoveIPAddressObserver(this);
83   NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
84   NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
85   if (NetworkChangeNotifier::AreNetworkHandlesSupported())
86     NetworkChangeNotifier::RemoveNetworkObserver(this);
87 }
88 
OnIPAddressChanged()89 void LoggingNetworkChangeObserver::OnIPAddressChanged() {
90   VLOG(1) << "Observed a change to the network IP addresses";
91 
92   net_log_.AddEvent(NetLogEventType::NETWORK_IP_ADDRESSES_CHANGED);
93 }
94 
OnConnectionTypeChanged(NetworkChangeNotifier::ConnectionType type)95 void LoggingNetworkChangeObserver::OnConnectionTypeChanged(
96     NetworkChangeNotifier::ConnectionType type) {
97   std::string type_as_string =
98       NetworkChangeNotifier::ConnectionTypeToString(type);
99 
100   VLOG(1) << "Observed a change to network connectivity state "
101           << type_as_string;
102 
103   net_log_.AddEventWithStringParams(
104       NetLogEventType::NETWORK_CONNECTIVITY_CHANGED, "new_connection_type",
105       type_as_string);
106 }
107 
OnNetworkChanged(NetworkChangeNotifier::ConnectionType type)108 void LoggingNetworkChangeObserver::OnNetworkChanged(
109     NetworkChangeNotifier::ConnectionType type) {
110   std::string type_as_string =
111       NetworkChangeNotifier::ConnectionTypeToString(type);
112 
113   VLOG(1) << "Observed a network change to state " << type_as_string;
114 
115   net_log_.AddEventWithStringParams(
116       NetLogEventType::NETWORK_CHANGED, "new_connection_type", type_as_string);
117 }
118 
OnNetworkConnected(handles::NetworkHandle network)119 void LoggingNetworkChangeObserver::OnNetworkConnected(
120     handles::NetworkHandle network) {
121   VLOG(1) << "Observed network " << network << " connect";
122 
123   NetLogNetworkSpecific(net_log_, NetLogEventType::SPECIFIC_NETWORK_CONNECTED,
124                         network);
125 }
126 
OnNetworkDisconnected(handles::NetworkHandle network)127 void LoggingNetworkChangeObserver::OnNetworkDisconnected(
128     handles::NetworkHandle network) {
129   VLOG(1) << "Observed network " << network << " disconnect";
130 
131   NetLogNetworkSpecific(
132       net_log_, NetLogEventType::SPECIFIC_NETWORK_DISCONNECTED, network);
133 }
134 
OnNetworkSoonToDisconnect(handles::NetworkHandle network)135 void LoggingNetworkChangeObserver::OnNetworkSoonToDisconnect(
136     handles::NetworkHandle network) {
137   VLOG(1) << "Observed network " << network << " soon to disconnect";
138 
139   NetLogNetworkSpecific(
140       net_log_, NetLogEventType::SPECIFIC_NETWORK_SOON_TO_DISCONNECT, network);
141 }
142 
OnNetworkMadeDefault(handles::NetworkHandle network)143 void LoggingNetworkChangeObserver::OnNetworkMadeDefault(
144     handles::NetworkHandle network) {
145   VLOG(1) << "Observed network " << network << " made the default network";
146 
147   NetLogNetworkSpecific(
148       net_log_, NetLogEventType::SPECIFIC_NETWORK_MADE_DEFAULT, network);
149 }
150 
151 }  // namespace net
152