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" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/json/json_writer.h" | 13 #include "base/json/json_writer.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
17 #include "base/values.h" | 17 #include "base/values.h" |
18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
19 #include "net/base/network_change_notifier.h" | 19 #include "net/base/network_change_notifier.h" |
20 #include "net/proxy/proxy_config.h" | 20 #include "net/proxy/proxy_config.h" |
21 #include "net/proxy/proxy_config_service.h" | 21 #include "net/proxy/proxy_config_service.h" |
22 #include "net/proxy/proxy_service.h" | 22 #include "net/proxy/proxy_service.h" |
23 | 23 |
24 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) && !defined
(OS_CHROMEOS) | 24 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) && \ |
| 25 !defined(OS_CHROMEOS) |
25 #include <glib-object.h> | 26 #include <glib-object.h> |
26 #endif | 27 #endif |
27 | 28 |
28 #if defined(OS_MACOSX) | 29 #if defined(OS_MACOSX) |
29 #include "base/mac/scoped_nsautorelease_pool.h" | 30 #include "base/mac/scoped_nsautorelease_pool.h" |
30 #endif | 31 #endif |
31 | 32 |
32 namespace { | 33 namespace { |
33 | 34 |
34 // Conversions from various network-related types to string. | 35 // Conversions from various network-related types to string. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 case net::ProxyConfigService::CONFIG_VALID: | 71 case net::ProxyConfigService::CONFIG_VALID: |
71 return "CONFIG_VALID"; | 72 return "CONFIG_VALID"; |
72 case net::ProxyConfigService::CONFIG_UNSET: | 73 case net::ProxyConfigService::CONFIG_UNSET: |
73 return "CONFIG_UNSET"; | 74 return "CONFIG_UNSET"; |
74 default: | 75 default: |
75 return "CONFIG_UNEXPECTED"; | 76 return "CONFIG_UNEXPECTED"; |
76 } | 77 } |
77 } | 78 } |
78 | 79 |
79 // The main observer class that logs network events. | 80 // The main observer class that logs network events. |
80 class NetWatcher : | 81 class NetWatcher : 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::NetworkChangeNotifier::NetworkChangeObserver, | 85 public net::ProxyConfigService::Observer { |
85 public net::ProxyConfigService::Observer { | |
86 public: | 86 public: |
87 NetWatcher() {} | 87 NetWatcher() {} |
88 | 88 |
89 virtual ~NetWatcher() {} | 89 virtual ~NetWatcher() {} |
90 | 90 |
91 // net::NetworkChangeNotifier::IPAddressObserver implementation. | 91 // net::NetworkChangeNotifier::IPAddressObserver implementation. |
92 virtual void OnIPAddressChanged() OVERRIDE { | 92 virtual void OnIPAddressChanged() OVERRIDE { |
93 LOG(INFO) << "OnIPAddressChanged()"; | 93 LOG(INFO) << "OnIPAddressChanged()"; |
94 } | 94 } |
95 | 95 |
96 // net::NetworkChangeNotifier::ConnectionTypeObserver implementation. | 96 // net::NetworkChangeNotifier::ConnectionTypeObserver implementation. |
97 virtual void OnConnectionTypeChanged( | 97 virtual void OnConnectionTypeChanged( |
98 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE { | 98 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE { |
99 LOG(INFO) << "OnConnectionTypeChanged(" | 99 LOG(INFO) << "OnConnectionTypeChanged(" << ConnectionTypeToString(type) |
100 << ConnectionTypeToString(type) << ")"; | 100 << ")"; |
101 } | 101 } |
102 | 102 |
103 // net::NetworkChangeNotifier::DNSObserver implementation. | 103 // net::NetworkChangeNotifier::DNSObserver implementation. |
104 virtual void OnDNSChanged() OVERRIDE { | 104 virtual void OnDNSChanged() OVERRIDE { LOG(INFO) << "OnDNSChanged()"; } |
105 LOG(INFO) << "OnDNSChanged()"; | |
106 } | |
107 | 105 |
108 // net::NetworkChangeNotifier::NetworkChangeObserver implementation. | 106 // net::NetworkChangeNotifier::NetworkChangeObserver implementation. |
109 virtual void OnNetworkChanged( | 107 virtual void OnNetworkChanged( |
110 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE { | 108 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE { |
111 LOG(INFO) << "OnNetworkChanged(" | 109 LOG(INFO) << "OnNetworkChanged(" << ConnectionTypeToString(type) << ")"; |
112 << ConnectionTypeToString(type) << ")"; | |
113 } | 110 } |
114 | 111 |
115 // net::ProxyConfigService::Observer implementation. | 112 // net::ProxyConfigService::Observer implementation. |
116 virtual void OnProxyConfigChanged( | 113 virtual void OnProxyConfigChanged( |
117 const net::ProxyConfig& config, | 114 const net::ProxyConfig& config, |
118 net::ProxyConfigService::ConfigAvailability availability) OVERRIDE { | 115 net::ProxyConfigService::ConfigAvailability availability) OVERRIDE { |
119 LOG(INFO) << "OnProxyConfigChanged(" | 116 LOG(INFO) << "OnProxyConfigChanged(" << ProxyConfigToString(config) << ", " |
120 << ProxyConfigToString(config) << ", " | |
121 << ConfigAvailabilityToString(availability) << ")"; | 117 << ConfigAvailabilityToString(availability) << ")"; |
122 } | 118 } |
123 | 119 |
124 private: | 120 private: |
125 DISALLOW_COPY_AND_ASSIGN(NetWatcher); | 121 DISALLOW_COPY_AND_ASSIGN(NetWatcher); |
126 }; | 122 }; |
127 | 123 |
128 } // namespace | 124 } // namespace |
129 | 125 |
130 int main(int argc, char* argv[]) { | 126 int main(int argc, char* argv[]) { |
131 #if defined(OS_MACOSX) | 127 #if defined(OS_MACOSX) |
132 base::mac::ScopedNSAutoreleasePool pool; | 128 base::mac::ScopedNSAutoreleasePool pool; |
133 #endif | 129 #endif |
134 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) && !defined
(OS_CHROMEOS) | 130 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) && \ |
135 // g_type_init will be deprecated in 2.36. 2.35 is the development | 131 !defined(OS_CHROMEOS) |
136 // version for 2.36, hence do not call g_type_init starting 2.35. | 132 // g_type_init will be deprecated in 2.36. 2.35 is the development |
137 // http://developer.gnome.org/gobject/unstable/gobject-Type-Information.html#g
-type-init | 133 // version for 2.36, hence do not call g_type_init starting 2.35. |
| 134 // http://developer.gnome.org/gobject/unstable/gobject-Type-Information.html#g-t
ype-init |
138 #if !GLIB_CHECK_VERSION(2, 35, 0) | 135 #if !GLIB_CHECK_VERSION(2, 35, 0) |
139 // Needed so ProxyConfigServiceLinux can use gconf. | 136 // Needed so ProxyConfigServiceLinux can use gconf. |
140 // Normally handled by BrowserMainLoop::InitializeToolkit(). | 137 // Normally handled by BrowserMainLoop::InitializeToolkit(). |
141 g_type_init(); | 138 g_type_init(); |
142 #endif | 139 #endif |
143 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) && !
defined(OS_CHROMEOS) | 140 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) && |
| 141 // !defined(OS_CHROMEOS) |
144 base::AtExitManager exit_manager; | 142 base::AtExitManager exit_manager; |
145 CommandLine::Init(argc, argv); | 143 CommandLine::Init(argc, argv); |
146 logging::LoggingSettings settings; | 144 logging::LoggingSettings settings; |
147 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; | 145 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; |
148 logging::InitLogging(settings); | 146 logging::InitLogging(settings); |
149 | 147 |
150 // Just make the main message loop the network loop. | 148 // Just make the main message loop the network loop. |
151 base::MessageLoopForIO network_loop; | 149 base::MessageLoopForIO network_loop; |
152 | 150 |
153 NetWatcher net_watcher; | 151 NetWatcher net_watcher; |
154 | 152 |
155 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier( | 153 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier( |
156 net::NetworkChangeNotifier::Create()); | 154 net::NetworkChangeNotifier::Create()); |
157 | 155 |
158 // Use the network loop as the file loop also. | 156 // Use the network loop as the file loop also. |
159 scoped_ptr<net::ProxyConfigService> proxy_config_service( | 157 scoped_ptr<net::ProxyConfigService> proxy_config_service( |
160 net::ProxyService::CreateSystemProxyConfigService( | 158 net::ProxyService::CreateSystemProxyConfigService( |
161 network_loop.message_loop_proxy().get(), &network_loop)); | 159 network_loop.message_loop_proxy().get(), &network_loop)); |
162 | 160 |
163 // Uses |network_change_notifier|. | 161 // Uses |network_change_notifier|. |
164 net::NetworkChangeNotifier::AddIPAddressObserver(&net_watcher); | 162 net::NetworkChangeNotifier::AddIPAddressObserver(&net_watcher); |
165 net::NetworkChangeNotifier::AddConnectionTypeObserver(&net_watcher); | 163 net::NetworkChangeNotifier::AddConnectionTypeObserver(&net_watcher); |
166 net::NetworkChangeNotifier::AddDNSObserver(&net_watcher); | 164 net::NetworkChangeNotifier::AddDNSObserver(&net_watcher); |
167 net::NetworkChangeNotifier::AddNetworkChangeObserver(&net_watcher); | 165 net::NetworkChangeNotifier::AddNetworkChangeObserver(&net_watcher); |
168 | 166 |
169 proxy_config_service->AddObserver(&net_watcher); | 167 proxy_config_service->AddObserver(&net_watcher); |
170 | 168 |
171 LOG(INFO) << "Initial connection type: " | 169 LOG(INFO) << "Initial connection type: " |
172 << ConnectionTypeToString( | 170 << ConnectionTypeToString( |
173 network_change_notifier->GetCurrentConnectionType()); | 171 network_change_notifier->GetCurrentConnectionType()); |
174 | 172 |
175 { | 173 { |
176 net::ProxyConfig config; | 174 net::ProxyConfig config; |
177 const net::ProxyConfigService::ConfigAvailability availability = | 175 const net::ProxyConfigService::ConfigAvailability availability = |
178 proxy_config_service->GetLatestProxyConfig(&config); | 176 proxy_config_service->GetLatestProxyConfig(&config); |
179 LOG(INFO) << "Initial proxy config: " | 177 LOG(INFO) << "Initial proxy config: " << ProxyConfigToString(config) << ", " |
180 << ProxyConfigToString(config) << ", " | |
181 << ConfigAvailabilityToString(availability); | 178 << ConfigAvailabilityToString(availability); |
182 } | 179 } |
183 | 180 |
184 LOG(INFO) << "Watching for network events..."; | 181 LOG(INFO) << "Watching for network events..."; |
185 | 182 |
186 // Start watching for events. | 183 // Start watching for events. |
187 network_loop.Run(); | 184 network_loop.Run(); |
188 | 185 |
189 proxy_config_service->RemoveObserver(&net_watcher); | 186 proxy_config_service->RemoveObserver(&net_watcher); |
190 | 187 |
191 // Uses |network_change_notifier|. | 188 // Uses |network_change_notifier|. |
192 net::NetworkChangeNotifier::RemoveDNSObserver(&net_watcher); | 189 net::NetworkChangeNotifier::RemoveDNSObserver(&net_watcher); |
193 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(&net_watcher); | 190 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(&net_watcher); |
194 net::NetworkChangeNotifier::RemoveIPAddressObserver(&net_watcher); | 191 net::NetworkChangeNotifier::RemoveIPAddressObserver(&net_watcher); |
195 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(&net_watcher); | 192 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(&net_watcher); |
196 | 193 |
197 return 0; | 194 return 0; |
198 } | 195 } |
OLD | NEW |