| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This is a small utility that watches for and logs network changes. | 5 // This is a small utility that watches for and logs network changes. |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 return "CONFIG_PENDING"; | 69 return "CONFIG_PENDING"; |
| 70 case net::ProxyConfigService::CONFIG_VALID: | 70 case net::ProxyConfigService::CONFIG_VALID: |
| 71 return "CONFIG_VALID"; | 71 return "CONFIG_VALID"; |
| 72 case net::ProxyConfigService::CONFIG_UNSET: | 72 case net::ProxyConfigService::CONFIG_UNSET: |
| 73 return "CONFIG_UNSET"; | 73 return "CONFIG_UNSET"; |
| 74 default: | 74 default: |
| 75 return "CONFIG_UNEXPECTED"; | 75 return "CONFIG_UNEXPECTED"; |
| 76 } | 76 } |
| 77 } | 77 } |
| 78 | 78 |
| 79 } // namespace |
| 80 |
| 79 // The main observer class that logs network events. | 81 // The main observer class that logs network events. |
| 80 class NetWatcher : | 82 class NetWatcher : |
| 81 public net::NetworkChangeNotifier::IPAddressObserver, | 83 public net::NetworkChangeNotifier::IPAddressObserver, |
| 82 public net::NetworkChangeNotifier::ConnectionTypeObserver, | 84 public net::NetworkChangeNotifier::ConnectionTypeObserver, |
| 83 public net::NetworkChangeNotifier::DNSObserver, | 85 public net::NetworkChangeNotifier::DNSObserver, |
| 84 public net::NetworkChangeNotifier::NetworkChangeObserver, | 86 public net::NetworkChangeNotifier::NetworkChangeObserver, |
| 85 public net::ProxyConfigService::Observer { | 87 public net::ProxyConfigService::Observer { |
| 86 public: | 88 public: |
| 87 NetWatcher() {} | 89 NetWatcher() {} |
| 90 virtual ~NetWatcher() { |
| 91 net::NetworkChangeNotifier::RemoveDNSObserver(this); |
| 92 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
| 93 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); |
| 94 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); |
| 95 } |
| 88 | 96 |
| 89 virtual ~NetWatcher() {} | 97 void Init() { |
| 98 net::NetworkChangeNotifier::Deprecated::AddIPAddressObserver(this); |
| 99 net::NetworkChangeNotifier::Deprecated::AddConnectionTypeObserver(this); |
| 100 net::NetworkChangeNotifier::AddDNSObserver(this); |
| 101 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); |
| 102 } |
| 90 | 103 |
| 91 // net::NetworkChangeNotifier::IPAddressObserver implementation. | 104 // net::NetworkChangeNotifier::IPAddressObserver implementation. |
| 92 virtual void OnIPAddressChanged() OVERRIDE { | 105 virtual void OnIPAddressChanged() OVERRIDE { |
| 93 LOG(INFO) << "OnIPAddressChanged()"; | 106 LOG(INFO) << "OnIPAddressChanged()"; |
| 94 } | 107 } |
| 95 | 108 |
| 96 // net::NetworkChangeNotifier::ConnectionTypeObserver implementation. | 109 // net::NetworkChangeNotifier::ConnectionTypeObserver implementation. |
| 97 virtual void OnConnectionTypeChanged( | 110 virtual void OnConnectionTypeChanged( |
| 98 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE { | 111 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE { |
| 99 LOG(INFO) << "OnConnectionTypeChanged(" | 112 LOG(INFO) << "OnConnectionTypeChanged(" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 118 net::ProxyConfigService::ConfigAvailability availability) OVERRIDE { | 131 net::ProxyConfigService::ConfigAvailability availability) OVERRIDE { |
| 119 LOG(INFO) << "OnProxyConfigChanged(" | 132 LOG(INFO) << "OnProxyConfigChanged(" |
| 120 << ProxyConfigToString(config) << ", " | 133 << ProxyConfigToString(config) << ", " |
| 121 << ConfigAvailabilityToString(availability) << ")"; | 134 << ConfigAvailabilityToString(availability) << ")"; |
| 122 } | 135 } |
| 123 | 136 |
| 124 private: | 137 private: |
| 125 DISALLOW_COPY_AND_ASSIGN(NetWatcher); | 138 DISALLOW_COPY_AND_ASSIGN(NetWatcher); |
| 126 }; | 139 }; |
| 127 | 140 |
| 128 } // namespace | |
| 129 | |
| 130 int main(int argc, char* argv[]) { | 141 int main(int argc, char* argv[]) { |
| 131 #if defined(OS_MACOSX) | 142 #if defined(OS_MACOSX) |
| 132 base::mac::ScopedNSAutoreleasePool pool; | 143 base::mac::ScopedNSAutoreleasePool pool; |
| 133 #endif | 144 #endif |
| 134 #if (defined(OS_LINUX) || defined(OS_OPENBSD)) && !defined(OS_CHROMEOS) | 145 #if (defined(OS_LINUX) || defined(OS_OPENBSD)) && !defined(OS_CHROMEOS) |
| 135 // Needed so ProxyConfigServiceLinux can use gconf. | 146 // Needed so ProxyConfigServiceLinux can use gconf. |
| 136 // Normally handled by BrowserMainLoop::InitializeToolkit(). | 147 // Normally handled by BrowserMainLoop::InitializeToolkit(). |
| 137 g_type_init(); | 148 g_type_init(); |
| 138 #endif | 149 #endif |
| 139 base::AtExitManager exit_manager; | 150 base::AtExitManager exit_manager; |
| 140 CommandLine::Init(argc, argv); | 151 CommandLine::Init(argc, argv); |
| 141 logging::InitLogging( | 152 logging::InitLogging( |
| 142 NULL, | 153 NULL, |
| 143 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG, | 154 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG, |
| 144 logging::LOCK_LOG_FILE, | 155 logging::LOCK_LOG_FILE, |
| 145 logging::DELETE_OLD_LOG_FILE, | 156 logging::DELETE_OLD_LOG_FILE, |
| 146 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS); | 157 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS); |
| 147 | 158 |
| 148 // Just make the main message loop the network loop. | 159 // Just make the main message loop the network loop. |
| 149 MessageLoopForIO network_loop; | 160 MessageLoopForIO network_loop; |
| 150 | 161 |
| 151 NetWatcher net_watcher; | |
| 152 | |
| 153 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier( | 162 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier( |
| 154 net::NetworkChangeNotifier::Create()); | 163 net::NetworkChangeNotifier::Create()); |
| 155 | 164 |
| 156 // Use the network loop as the file loop also. | 165 // Use the network loop as the file loop also. |
| 157 scoped_ptr<net::ProxyConfigService> proxy_config_service( | 166 scoped_ptr<net::ProxyConfigService> proxy_config_service( |
| 158 net::ProxyService::CreateSystemProxyConfigService( | 167 net::ProxyService::CreateSystemProxyConfigService( |
| 159 network_loop.message_loop_proxy(), | 168 network_loop.message_loop_proxy(), |
| 160 &network_loop)); | 169 &network_loop)); |
| 161 | 170 |
| 171 // Destruction uses |network_change_notifier| so define after. |
| 172 NetWatcher net_watcher; |
| 162 // Uses |network_change_notifier|. | 173 // Uses |network_change_notifier|. |
| 163 net::NetworkChangeNotifier::AddIPAddressObserver(&net_watcher); | 174 net_watcher.Init(); |
| 164 net::NetworkChangeNotifier::AddConnectionTypeObserver(&net_watcher); | |
| 165 net::NetworkChangeNotifier::AddDNSObserver(&net_watcher); | |
| 166 net::NetworkChangeNotifier::AddNetworkChangeObserver(&net_watcher); | |
| 167 | 175 |
| 168 proxy_config_service->AddObserver(&net_watcher); | 176 proxy_config_service->AddObserver(&net_watcher); |
| 169 | 177 |
| 170 LOG(INFO) << "Initial connection type: " | 178 LOG(INFO) << "Initial connection type: " |
| 171 << ConnectionTypeToString( | 179 << ConnectionTypeToString( |
| 172 network_change_notifier->GetCurrentConnectionType()); | 180 network_change_notifier->GetCurrentConnectionType()); |
| 173 | 181 |
| 174 { | 182 { |
| 175 net::ProxyConfig config; | 183 net::ProxyConfig config; |
| 176 const net::ProxyConfigService::ConfigAvailability availability = | 184 const net::ProxyConfigService::ConfigAvailability availability = |
| 177 proxy_config_service->GetLatestProxyConfig(&config); | 185 proxy_config_service->GetLatestProxyConfig(&config); |
| 178 LOG(INFO) << "Initial proxy config: " | 186 LOG(INFO) << "Initial proxy config: " |
| 179 << ProxyConfigToString(config) << ", " | 187 << ProxyConfigToString(config) << ", " |
| 180 << ConfigAvailabilityToString(availability); | 188 << ConfigAvailabilityToString(availability); |
| 181 } | 189 } |
| 182 | 190 |
| 183 LOG(INFO) << "Watching for network events..."; | 191 LOG(INFO) << "Watching for network events..."; |
| 184 | 192 |
| 185 // Start watching for events. | 193 // Start watching for events. |
| 186 network_loop.Run(); | 194 network_loop.Run(); |
| 187 | 195 |
| 188 proxy_config_service->RemoveObserver(&net_watcher); | 196 proxy_config_service->RemoveObserver(&net_watcher); |
| 189 | 197 |
| 190 // Uses |network_change_notifier|. | |
| 191 net::NetworkChangeNotifier::RemoveDNSObserver(&net_watcher); | |
| 192 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(&net_watcher); | |
| 193 net::NetworkChangeNotifier::RemoveIPAddressObserver(&net_watcher); | |
| 194 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(&net_watcher); | |
| 195 | |
| 196 return 0; | 198 return 0; |
| 197 } | 199 } |
| OLD | NEW |