| 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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 default: | 74 default: |
| 75 return "CONFIG_UNEXPECTED"; | 75 return "CONFIG_UNEXPECTED"; |
| 76 } | 76 } |
| 77 } | 77 } |
| 78 | 78 |
| 79 // The main observer class that logs network events. | 79 // The main observer class that logs network events. |
| 80 class NetWatcher : | 80 class NetWatcher : |
| 81 public net::NetworkChangeNotifier::IPAddressObserver, | 81 public net::NetworkChangeNotifier::IPAddressObserver, |
| 82 public net::NetworkChangeNotifier::ConnectionTypeObserver, | 82 public net::NetworkChangeNotifier::ConnectionTypeObserver, |
| 83 public net::NetworkChangeNotifier::DNSObserver, | 83 public net::NetworkChangeNotifier::DNSObserver, |
| 84 public net::NetworkChangeNotifier::NetworkChangeObserver, |
| 84 public net::ProxyConfigService::Observer { | 85 public net::ProxyConfigService::Observer { |
| 85 public: | 86 public: |
| 86 NetWatcher() {} | 87 NetWatcher() {} |
| 87 | 88 |
| 88 virtual ~NetWatcher() {} | 89 virtual ~NetWatcher() {} |
| 89 | 90 |
| 90 // net::NetworkChangeNotifier::IPAddressObserver implementation. | 91 // net::NetworkChangeNotifier::IPAddressObserver implementation. |
| 91 virtual void OnIPAddressChanged() OVERRIDE { | 92 virtual void OnIPAddressChanged() OVERRIDE { |
| 92 LOG(INFO) << "OnIPAddressChanged()"; | 93 LOG(INFO) << "OnIPAddressChanged()"; |
| 93 } | 94 } |
| 94 | 95 |
| 95 // net::NetworkChangeNotifier::ConnectionTypeObserver implementation. | 96 // net::NetworkChangeNotifier::ConnectionTypeObserver implementation. |
| 96 virtual void OnConnectionTypeChanged( | 97 virtual void OnConnectionTypeChanged( |
| 97 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE { | 98 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE { |
| 98 LOG(INFO) << "OnConnectionTypeChanged(" | 99 LOG(INFO) << "OnConnectionTypeChanged(" |
| 99 << ConnectionTypeToString(type) << ")"; | 100 << ConnectionTypeToString(type) << ")"; |
| 100 } | 101 } |
| 101 | 102 |
| 102 // net::NetworkChangeNotifier::DNSObserver implementation. | 103 // net::NetworkChangeNotifier::DNSObserver implementation. |
| 103 virtual void OnDNSChanged() OVERRIDE { | 104 virtual void OnDNSChanged() OVERRIDE { |
| 104 LOG(INFO) << "OnDNSChanged()"; | 105 LOG(INFO) << "OnDNSChanged()"; |
| 105 } | 106 } |
| 106 | 107 |
| 108 // net::NetworkChangeNotifier::NetworkChangeObserver implementation. |
| 109 virtual void OnNetworkChanged( |
| 110 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE { |
| 111 LOG(INFO) << "OnNetworkChanged(" |
| 112 << ConnectionTypeToString(type) << ")"; |
| 113 } |
| 114 |
| 107 // net::ProxyConfigService::Observer implementation. | 115 // net::ProxyConfigService::Observer implementation. |
| 108 virtual void OnProxyConfigChanged( | 116 virtual void OnProxyConfigChanged( |
| 109 const net::ProxyConfig& config, | 117 const net::ProxyConfig& config, |
| 110 net::ProxyConfigService::ConfigAvailability availability) OVERRIDE { | 118 net::ProxyConfigService::ConfigAvailability availability) OVERRIDE { |
| 111 LOG(INFO) << "OnProxyConfigChanged(" | 119 LOG(INFO) << "OnProxyConfigChanged(" |
| 112 << ProxyConfigToString(config) << ", " | 120 << ProxyConfigToString(config) << ", " |
| 113 << ConfigAvailabilityToString(availability) << ")"; | 121 << ConfigAvailabilityToString(availability) << ")"; |
| 114 } | 122 } |
| 115 | 123 |
| 116 private: | 124 private: |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 // Use the network loop as the file loop also. | 156 // Use the network loop as the file loop also. |
| 149 scoped_ptr<net::ProxyConfigService> proxy_config_service( | 157 scoped_ptr<net::ProxyConfigService> proxy_config_service( |
| 150 net::ProxyService::CreateSystemProxyConfigService( | 158 net::ProxyService::CreateSystemProxyConfigService( |
| 151 network_loop.message_loop_proxy(), | 159 network_loop.message_loop_proxy(), |
| 152 &network_loop)); | 160 &network_loop)); |
| 153 | 161 |
| 154 // Uses |network_change_notifier|. | 162 // Uses |network_change_notifier|. |
| 155 net::NetworkChangeNotifier::AddIPAddressObserver(&net_watcher); | 163 net::NetworkChangeNotifier::AddIPAddressObserver(&net_watcher); |
| 156 net::NetworkChangeNotifier::AddConnectionTypeObserver(&net_watcher); | 164 net::NetworkChangeNotifier::AddConnectionTypeObserver(&net_watcher); |
| 157 net::NetworkChangeNotifier::AddDNSObserver(&net_watcher); | 165 net::NetworkChangeNotifier::AddDNSObserver(&net_watcher); |
| 166 net::NetworkChangeNotifier::AddNetworkChangeObserver(&net_watcher); |
| 158 | 167 |
| 159 proxy_config_service->AddObserver(&net_watcher); | 168 proxy_config_service->AddObserver(&net_watcher); |
| 160 | 169 |
| 161 LOG(INFO) << "Initial connection type: " | 170 LOG(INFO) << "Initial connection type: " |
| 162 << ConnectionTypeToString( | 171 << ConnectionTypeToString( |
| 163 network_change_notifier->GetCurrentConnectionType()); | 172 network_change_notifier->GetCurrentConnectionType()); |
| 164 | 173 |
| 165 { | 174 { |
| 166 net::ProxyConfig config; | 175 net::ProxyConfig config; |
| 167 const net::ProxyConfigService::ConfigAvailability availability = | 176 const net::ProxyConfigService::ConfigAvailability availability = |
| 168 proxy_config_service->GetLatestProxyConfig(&config); | 177 proxy_config_service->GetLatestProxyConfig(&config); |
| 169 LOG(INFO) << "Initial proxy config: " | 178 LOG(INFO) << "Initial proxy config: " |
| 170 << ProxyConfigToString(config) << ", " | 179 << ProxyConfigToString(config) << ", " |
| 171 << ConfigAvailabilityToString(availability); | 180 << ConfigAvailabilityToString(availability); |
| 172 } | 181 } |
| 173 | 182 |
| 174 LOG(INFO) << "Watching for network events..."; | 183 LOG(INFO) << "Watching for network events..."; |
| 175 | 184 |
| 176 // Start watching for events. | 185 // Start watching for events. |
| 177 network_loop.Run(); | 186 network_loop.Run(); |
| 178 | 187 |
| 179 proxy_config_service->RemoveObserver(&net_watcher); | 188 proxy_config_service->RemoveObserver(&net_watcher); |
| 180 | 189 |
| 181 // Uses |network_change_notifier|. | 190 // Uses |network_change_notifier|. |
| 182 net::NetworkChangeNotifier::RemoveDNSObserver(&net_watcher); | 191 net::NetworkChangeNotifier::RemoveDNSObserver(&net_watcher); |
| 183 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(&net_watcher); | 192 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(&net_watcher); |
| 184 net::NetworkChangeNotifier::RemoveIPAddressObserver(&net_watcher); | 193 net::NetworkChangeNotifier::RemoveIPAddressObserver(&net_watcher); |
| 194 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(&net_watcher); |
| 185 | 195 |
| 186 return 0; | 196 return 0; |
| 187 } | 197 } |
| OLD | NEW |