| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 // This is a small utility that watches for and logs network changes. | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/at_exit.h" | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/command_line.h" | |
| 12 #include "base/compiler_specific.h" | |
| 13 #include "base/json/json_writer.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/message_loop/message_loop.h" | |
| 17 #include "base/profiler/scoped_tracker.h" | |
| 18 #include "base/values.h" | |
| 19 #include "build/build_config.h" | |
| 20 #include "net/base/network_change_notifier.h" | |
| 21 #include "net/proxy/proxy_config.h" | |
| 22 #include "net/proxy/proxy_config_service.h" | |
| 23 #include "net/proxy/proxy_service.h" | |
| 24 | |
| 25 #if defined(USE_GLIB) && !defined(OS_CHROMEOS) | |
| 26 #include <glib-object.h> | |
| 27 #endif | |
| 28 | |
| 29 #if defined(OS_MACOSX) | |
| 30 #include "base/mac/scoped_nsautorelease_pool.h" | |
| 31 #endif | |
| 32 | |
| 33 namespace { | |
| 34 | |
| 35 // Conversions from various network-related types to string. | |
| 36 | |
| 37 const char* ConnectionTypeToString( | |
| 38 net::NetworkChangeNotifier::ConnectionType type) { | |
| 39 switch (type) { | |
| 40 case net::NetworkChangeNotifier::CONNECTION_UNKNOWN: | |
| 41 return "CONNECTION_UNKNOWN"; | |
| 42 case net::NetworkChangeNotifier::CONNECTION_ETHERNET: | |
| 43 return "CONNECTION_ETHERNET"; | |
| 44 case net::NetworkChangeNotifier::CONNECTION_WIFI: | |
| 45 return "CONNECTION_WIFI"; | |
| 46 case net::NetworkChangeNotifier::CONNECTION_2G: | |
| 47 return "CONNECTION_2G"; | |
| 48 case net::NetworkChangeNotifier::CONNECTION_3G: | |
| 49 return "CONNECTION_3G"; | |
| 50 case net::NetworkChangeNotifier::CONNECTION_4G: | |
| 51 return "CONNECTION_4G"; | |
| 52 case net::NetworkChangeNotifier::CONNECTION_NONE: | |
| 53 return "CONNECTION_NONE"; | |
| 54 case net::NetworkChangeNotifier::CONNECTION_BLUETOOTH: | |
| 55 return "CONNECTION_BLUETOOTH"; | |
| 56 default: | |
| 57 return "CONNECTION_UNEXPECTED"; | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 std::string ProxyConfigToString(const net::ProxyConfig& config) { | |
| 62 scoped_ptr<base::Value> config_value(config.ToValue()); | |
| 63 std::string str; | |
| 64 base::JSONWriter::Write(config_value.get(), &str); | |
| 65 return str; | |
| 66 } | |
| 67 | |
| 68 const char* ConfigAvailabilityToString( | |
| 69 net::ProxyConfigService::ConfigAvailability availability) { | |
| 70 switch (availability) { | |
| 71 case net::ProxyConfigService::CONFIG_PENDING: | |
| 72 return "CONFIG_PENDING"; | |
| 73 case net::ProxyConfigService::CONFIG_VALID: | |
| 74 return "CONFIG_VALID"; | |
| 75 case net::ProxyConfigService::CONFIG_UNSET: | |
| 76 return "CONFIG_UNSET"; | |
| 77 default: | |
| 78 return "CONFIG_UNEXPECTED"; | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 // The main observer class that logs network events. | |
| 83 class NetWatcher : | |
| 84 public net::NetworkChangeNotifier::IPAddressObserver, | |
| 85 public net::NetworkChangeNotifier::ConnectionTypeObserver, | |
| 86 public net::NetworkChangeNotifier::DNSObserver, | |
| 87 public net::NetworkChangeNotifier::NetworkChangeObserver, | |
| 88 public net::ProxyConfigService::Observer { | |
| 89 public: | |
| 90 NetWatcher() {} | |
| 91 | |
| 92 ~NetWatcher() override {} | |
| 93 | |
| 94 // net::NetworkChangeNotifier::IPAddressObserver implementation. | |
| 95 void OnIPAddressChanged() override { LOG(INFO) << "OnIPAddressChanged()"; } | |
| 96 | |
| 97 // net::NetworkChangeNotifier::ConnectionTypeObserver implementation. | |
| 98 void OnConnectionTypeChanged( | |
| 99 net::NetworkChangeNotifier::ConnectionType type) override { | |
| 100 LOG(INFO) << "OnConnectionTypeChanged(" | |
| 101 << ConnectionTypeToString(type) << ")"; | |
| 102 } | |
| 103 | |
| 104 // net::NetworkChangeNotifier::DNSObserver implementation. | |
| 105 void OnDNSChanged() override { LOG(INFO) << "OnDNSChanged()"; } | |
| 106 | |
| 107 // net::NetworkChangeNotifier::NetworkChangeObserver implementation. | |
| 108 void OnNetworkChanged( | |
| 109 net::NetworkChangeNotifier::ConnectionType type) override { | |
| 110 LOG(INFO) << "OnNetworkChanged(" | |
| 111 << ConnectionTypeToString(type) << ")"; | |
| 112 } | |
| 113 | |
| 114 // net::ProxyConfigService::Observer implementation. | |
| 115 void OnProxyConfigChanged( | |
| 116 const net::ProxyConfig& config, | |
| 117 net::ProxyConfigService::ConfigAvailability availability) override { | |
| 118 // TODO(pkasting): Remove ScopedTracker below once crbug.com/455942 is | |
| 119 // fixed. | |
| 120 tracked_objects::ScopedTracker tracking_profile( | |
| 121 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
| 122 "455942 NetWatcher::OnProxyConfigChanged")); | |
| 123 LOG(INFO) << "OnProxyConfigChanged(" | |
| 124 << ProxyConfigToString(config) << ", " | |
| 125 << ConfigAvailabilityToString(availability) << ")"; | |
| 126 } | |
| 127 | |
| 128 private: | |
| 129 DISALLOW_COPY_AND_ASSIGN(NetWatcher); | |
| 130 }; | |
| 131 | |
| 132 } // namespace | |
| 133 | |
| 134 int main(int argc, char* argv[]) { | |
| 135 #if defined(OS_MACOSX) | |
| 136 base::mac::ScopedNSAutoreleasePool pool; | |
| 137 #endif | |
| 138 #if defined(USE_GLIB) && !defined(OS_CHROMEOS) | |
| 139 // g_type_init will be deprecated in 2.36. 2.35 is the development | |
| 140 // version for 2.36, hence do not call g_type_init starting 2.35. | |
| 141 // http://developer.gnome.org/gobject/unstable/gobject-Type-Information.html#g
-type-init | |
| 142 #if !GLIB_CHECK_VERSION(2, 35, 0) | |
| 143 // Needed so ProxyConfigServiceLinux can use gconf. | |
| 144 // Normally handled by BrowserMainLoop::InitializeToolkit(). | |
| 145 g_type_init(); | |
| 146 #endif | |
| 147 #endif // defined(USE_GLIB) && !defined(OS_CHROMEOS) | |
| 148 base::AtExitManager exit_manager; | |
| 149 base::CommandLine::Init(argc, argv); | |
| 150 logging::LoggingSettings settings; | |
| 151 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; | |
| 152 logging::InitLogging(settings); | |
| 153 | |
| 154 // Just make the main message loop the network loop. | |
| 155 base::MessageLoopForIO network_loop; | |
| 156 | |
| 157 NetWatcher net_watcher; | |
| 158 | |
| 159 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier( | |
| 160 net::NetworkChangeNotifier::Create()); | |
| 161 | |
| 162 // Use the network loop as the file loop also. | |
| 163 scoped_ptr<net::ProxyConfigService> proxy_config_service( | |
| 164 net::ProxyService::CreateSystemProxyConfigService( | |
| 165 network_loop.message_loop_proxy(), | |
| 166 network_loop.message_loop_proxy())); | |
| 167 | |
| 168 // Uses |network_change_notifier|. | |
| 169 net::NetworkChangeNotifier::AddIPAddressObserver(&net_watcher); | |
| 170 net::NetworkChangeNotifier::AddConnectionTypeObserver(&net_watcher); | |
| 171 net::NetworkChangeNotifier::AddDNSObserver(&net_watcher); | |
| 172 net::NetworkChangeNotifier::AddNetworkChangeObserver(&net_watcher); | |
| 173 | |
| 174 proxy_config_service->AddObserver(&net_watcher); | |
| 175 | |
| 176 LOG(INFO) << "Initial connection type: " | |
| 177 << ConnectionTypeToString( | |
| 178 network_change_notifier->GetCurrentConnectionType()); | |
| 179 | |
| 180 { | |
| 181 net::ProxyConfig config; | |
| 182 const net::ProxyConfigService::ConfigAvailability availability = | |
| 183 proxy_config_service->GetLatestProxyConfig(&config); | |
| 184 LOG(INFO) << "Initial proxy config: " | |
| 185 << ProxyConfigToString(config) << ", " | |
| 186 << ConfigAvailabilityToString(availability); | |
| 187 } | |
| 188 | |
| 189 LOG(INFO) << "Watching for network events..."; | |
| 190 | |
| 191 // Start watching for events. | |
| 192 network_loop.Run(); | |
| 193 | |
| 194 proxy_config_service->RemoveObserver(&net_watcher); | |
| 195 | |
| 196 // Uses |network_change_notifier|. | |
| 197 net::NetworkChangeNotifier::RemoveDNSObserver(&net_watcher); | |
| 198 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(&net_watcher); | |
| 199 net::NetworkChangeNotifier::RemoveIPAddressObserver(&net_watcher); | |
| 200 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(&net_watcher); | |
| 201 | |
| 202 return 0; | |
| 203 } | |
| OLD | NEW |