logging_network_change_observer.cc 5.3 KB

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