| 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 return "CONNECTION_3G"; | 44 return "CONNECTION_3G"; |
| 45 case net::NetworkChangeNotifier::CONNECTION_4G: | 45 case net::NetworkChangeNotifier::CONNECTION_4G: |
| 46 return "CONNECTION_4G"; | 46 return "CONNECTION_4G"; |
| 47 case net::NetworkChangeNotifier::CONNECTION_NONE: | 47 case net::NetworkChangeNotifier::CONNECTION_NONE: |
| 48 return "CONNECTION_NONE"; | 48 return "CONNECTION_NONE"; |
| 49 default: | 49 default: |
| 50 return "CONNECTION_UNEXPECTED"; | 50 return "CONNECTION_UNEXPECTED"; |
| 51 } | 51 } |
| 52 } | 52 } |
| 53 | 53 |
| 54 std::string DNSDetailToString(unsigned detail) { | |
| 55 const char kSeparator[] = " | "; | |
| 56 std::string detail_str; | |
| 57 if (detail & net::NetworkChangeNotifier::CHANGE_DNS_SETTINGS) | |
| 58 detail_str += "CHANGE_DNS_SETTINGS"; | |
| 59 if (detail & net::NetworkChangeNotifier::CHANGE_DNS_HOSTS) { | |
| 60 if (!detail_str.empty()) | |
| 61 detail_str += kSeparator; | |
| 62 detail_str += "CHANGE_DNS_HOSTS"; | |
| 63 } | |
| 64 if (detail & net::NetworkChangeNotifier::CHANGE_DNS_WATCH_STARTED) { | |
| 65 if (!detail_str.empty()) | |
| 66 detail_str += kSeparator; | |
| 67 detail_str += "CHANGE_DNS_WATCH_STARTED"; | |
| 68 } | |
| 69 if (detail & net::NetworkChangeNotifier::CHANGE_DNS_WATCH_FAILED) { | |
| 70 if (!detail_str.empty()) | |
| 71 detail_str += kSeparator; | |
| 72 detail_str += "CHANGE_DNS_WATCH_FAILED"; | |
| 73 } | |
| 74 return detail_str; | |
| 75 } | |
| 76 | |
| 77 std::string ProxyConfigToString(const net::ProxyConfig& config) { | 54 std::string ProxyConfigToString(const net::ProxyConfig& config) { |
| 78 scoped_ptr<base::Value> config_value(config.ToValue()); | 55 scoped_ptr<base::Value> config_value(config.ToValue()); |
| 79 std::string str; | 56 std::string str; |
| 80 base::JSONWriter::Write(config_value.get(), &str); | 57 base::JSONWriter::Write(config_value.get(), &str); |
| 81 return str; | 58 return str; |
| 82 } | 59 } |
| 83 | 60 |
| 84 const char* ConfigAvailabilityToString( | 61 const char* ConfigAvailabilityToString( |
| 85 net::ProxyConfigService::ConfigAvailability availability) { | 62 net::ProxyConfigService::ConfigAvailability availability) { |
| 86 switch (availability) { | 63 switch (availability) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 112 } | 89 } |
| 113 | 90 |
| 114 // net::NetworkChangeNotifier::ConnectionTypeObserver implementation. | 91 // net::NetworkChangeNotifier::ConnectionTypeObserver implementation. |
| 115 virtual void OnConnectionTypeChanged( | 92 virtual void OnConnectionTypeChanged( |
| 116 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE { | 93 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE { |
| 117 LOG(INFO) << "OnConnectionTypeChanged(" | 94 LOG(INFO) << "OnConnectionTypeChanged(" |
| 118 << ConnectionTypeToString(type) << ")"; | 95 << ConnectionTypeToString(type) << ")"; |
| 119 } | 96 } |
| 120 | 97 |
| 121 // net::NetworkChangeNotifier::DNSObserver implementation. | 98 // net::NetworkChangeNotifier::DNSObserver implementation. |
| 122 virtual void OnDNSChanged(unsigned detail) OVERRIDE { | 99 virtual void OnDNSChanged() OVERRIDE { |
| 123 LOG(INFO) << "OnDNSChanged(" << DNSDetailToString(detail) << ")"; | 100 LOG(INFO) << "OnDNSChanged()"; |
| 124 } | 101 } |
| 125 | 102 |
| 126 // net::ProxyConfigService::Observer implementation. | 103 // net::ProxyConfigService::Observer implementation. |
| 127 virtual void OnProxyConfigChanged( | 104 virtual void OnProxyConfigChanged( |
| 128 const net::ProxyConfig& config, | 105 const net::ProxyConfig& config, |
| 129 net::ProxyConfigService::ConfigAvailability availability) OVERRIDE { | 106 net::ProxyConfigService::ConfigAvailability availability) OVERRIDE { |
| 130 LOG(INFO) << "OnProxyConfigChanged(" | 107 LOG(INFO) << "OnProxyConfigChanged(" |
| 131 << ProxyConfigToString(config) << ", " | 108 << ProxyConfigToString(config) << ", " |
| 132 << ConfigAvailabilityToString(availability) << ")"; | 109 << ConfigAvailabilityToString(availability) << ")"; |
| 133 } | 110 } |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 net::ProxyConfig config; | 157 net::ProxyConfig config; |
| 181 const net::ProxyConfigService::ConfigAvailability availability = | 158 const net::ProxyConfigService::ConfigAvailability availability = |
| 182 proxy_config_service->GetLatestProxyConfig(&config); | 159 proxy_config_service->GetLatestProxyConfig(&config); |
| 183 LOG(INFO) << "Initial proxy config: " | 160 LOG(INFO) << "Initial proxy config: " |
| 184 << ProxyConfigToString(config) << ", " | 161 << ProxyConfigToString(config) << ", " |
| 185 << ConfigAvailabilityToString(availability); | 162 << ConfigAvailabilityToString(availability); |
| 186 } | 163 } |
| 187 | 164 |
| 188 LOG(INFO) << "Watching for network events..."; | 165 LOG(INFO) << "Watching for network events..."; |
| 189 | 166 |
| 190 if (net::NetworkChangeNotifier::IsWatchingDNS()) { | |
| 191 LOG(INFO) << "Watching for DNS changes..."; | |
| 192 } else { | |
| 193 LOG(INFO) << "Not watching for DNS changes"; | |
| 194 } | |
| 195 | |
| 196 // Start watching for events. | 167 // Start watching for events. |
| 197 network_loop.Run(); | 168 network_loop.Run(); |
| 198 | 169 |
| 199 proxy_config_service->RemoveObserver(&net_watcher); | 170 proxy_config_service->RemoveObserver(&net_watcher); |
| 200 | 171 |
| 201 // Uses |network_change_notifier|. | 172 // Uses |network_change_notifier|. |
| 202 net::NetworkChangeNotifier::RemoveDNSObserver(&net_watcher); | 173 net::NetworkChangeNotifier::RemoveDNSObserver(&net_watcher); |
| 203 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(&net_watcher); | 174 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(&net_watcher); |
| 204 net::NetworkChangeNotifier::RemoveIPAddressObserver(&net_watcher); | 175 net::NetworkChangeNotifier::RemoveIPAddressObserver(&net_watcher); |
| 205 | 176 |
| 206 return 0; | 177 return 0; |
| 207 } | 178 } |
| OLD | NEW |