Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / net / tools / net_watcher / net_watcher.cc
blobca25040748da62f82d90b94d20153aa08ec89644
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.
5 // This is a small utility that watches for and logs network changes.
7 #include <string>
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/strings/string_split.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"
25 #if defined(USE_GLIB) && !defined(OS_CHROMEOS)
26 #include <glib-object.h>
27 #endif
29 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
30 #include "net/base/network_change_notifier_linux.h"
31 #endif
33 #if defined(OS_MACOSX)
34 #include "base/mac/scoped_nsautorelease_pool.h"
35 #endif
37 namespace {
39 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
40 // Flag to specifies which network interfaces to ignore. Interfaces should
41 // follow as a comma seperated list.
42 const char kIgnoreNetifFlag[] = "ignore-netif";
43 #endif
45 // Conversions from various network-related types to string.
47 const char* ConnectionTypeToString(
48 net::NetworkChangeNotifier::ConnectionType type) {
49 switch (type) {
50 case net::NetworkChangeNotifier::CONNECTION_UNKNOWN:
51 return "CONNECTION_UNKNOWN";
52 case net::NetworkChangeNotifier::CONNECTION_ETHERNET:
53 return "CONNECTION_ETHERNET";
54 case net::NetworkChangeNotifier::CONNECTION_WIFI:
55 return "CONNECTION_WIFI";
56 case net::NetworkChangeNotifier::CONNECTION_2G:
57 return "CONNECTION_2G";
58 case net::NetworkChangeNotifier::CONNECTION_3G:
59 return "CONNECTION_3G";
60 case net::NetworkChangeNotifier::CONNECTION_4G:
61 return "CONNECTION_4G";
62 case net::NetworkChangeNotifier::CONNECTION_NONE:
63 return "CONNECTION_NONE";
64 case net::NetworkChangeNotifier::CONNECTION_BLUETOOTH:
65 return "CONNECTION_BLUETOOTH";
66 default:
67 return "CONNECTION_UNEXPECTED";
71 std::string ProxyConfigToString(const net::ProxyConfig& config) {
72 scoped_ptr<base::Value> config_value(config.ToValue());
73 std::string str;
74 base::JSONWriter::Write(*config_value, &str);
75 return str;
78 const char* ConfigAvailabilityToString(
79 net::ProxyConfigService::ConfigAvailability availability) {
80 switch (availability) {
81 case net::ProxyConfigService::CONFIG_PENDING:
82 return "CONFIG_PENDING";
83 case net::ProxyConfigService::CONFIG_VALID:
84 return "CONFIG_VALID";
85 case net::ProxyConfigService::CONFIG_UNSET:
86 return "CONFIG_UNSET";
87 default:
88 return "CONFIG_UNEXPECTED";
92 // The main observer class that logs network events.
93 class NetWatcher :
94 public net::NetworkChangeNotifier::IPAddressObserver,
95 public net::NetworkChangeNotifier::ConnectionTypeObserver,
96 public net::NetworkChangeNotifier::DNSObserver,
97 public net::NetworkChangeNotifier::NetworkChangeObserver,
98 public net::ProxyConfigService::Observer {
99 public:
100 NetWatcher() {}
102 ~NetWatcher() override {}
104 // net::NetworkChangeNotifier::IPAddressObserver implementation.
105 void OnIPAddressChanged() override { LOG(INFO) << "OnIPAddressChanged()"; }
107 // net::NetworkChangeNotifier::ConnectionTypeObserver implementation.
108 void OnConnectionTypeChanged(
109 net::NetworkChangeNotifier::ConnectionType type) override {
110 LOG(INFO) << "OnConnectionTypeChanged("
111 << ConnectionTypeToString(type) << ")";
114 // net::NetworkChangeNotifier::DNSObserver implementation.
115 void OnDNSChanged() override { LOG(INFO) << "OnDNSChanged()"; }
116 void OnInitialDNSConfigRead() override {
117 LOG(INFO) << "OnInitialDNSConfigRead()";
120 // net::NetworkChangeNotifier::NetworkChangeObserver implementation.
121 void OnNetworkChanged(
122 net::NetworkChangeNotifier::ConnectionType type) override {
123 LOG(INFO) << "OnNetworkChanged("
124 << ConnectionTypeToString(type) << ")";
127 // net::ProxyConfigService::Observer implementation.
128 void OnProxyConfigChanged(
129 const net::ProxyConfig& config,
130 net::ProxyConfigService::ConfigAvailability availability) override {
131 LOG(INFO) << "OnProxyConfigChanged("
132 << ProxyConfigToString(config) << ", "
133 << ConfigAvailabilityToString(availability) << ")";
136 private:
137 DISALLOW_COPY_AND_ASSIGN(NetWatcher);
140 } // namespace
142 int main(int argc, char* argv[]) {
143 #if defined(OS_MACOSX)
144 base::mac::ScopedNSAutoreleasePool pool;
145 #endif
146 #if defined(USE_GLIB) && !defined(OS_CHROMEOS)
147 // g_type_init will be deprecated in 2.36. 2.35 is the development
148 // version for 2.36, hence do not call g_type_init starting 2.35.
149 // http://developer.gnome.org/gobject/unstable/gobject-Type-Information.html#g-type-init
150 #if !GLIB_CHECK_VERSION(2, 35, 0)
151 // Needed so ProxyConfigServiceLinux can use gconf.
152 // Normally handled by BrowserMainLoop::InitializeToolkit().
153 g_type_init();
154 #endif
155 #endif // defined(USE_GLIB) && !defined(OS_CHROMEOS)
156 base::AtExitManager exit_manager;
157 base::CommandLine::Init(argc, argv);
158 logging::LoggingSettings settings;
159 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
160 logging::InitLogging(settings);
162 // Just make the main message loop the network loop.
163 base::MessageLoopForIO network_loop;
165 NetWatcher net_watcher;
167 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
168 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
169 std::string ignored_netifs_str =
170 command_line->GetSwitchValueASCII(kIgnoreNetifFlag);
171 base::hash_set<std::string> ignored_interfaces;
172 if (!ignored_netifs_str.empty()) {
173 for (const std::string& ignored_netif :
174 base::SplitString(ignored_netifs_str, ",", base::TRIM_WHITESPACE,
175 base::SPLIT_WANT_ALL)) {
176 LOG(INFO) << "Ignoring: " << ignored_netif;
177 ignored_interfaces.insert(ignored_netif);
180 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier(
181 new net::NetworkChangeNotifierLinux(ignored_interfaces));
182 #else
183 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier(
184 net::NetworkChangeNotifier::Create());
185 #endif
187 // Use the network loop as the file loop also.
188 scoped_ptr<net::ProxyConfigService> proxy_config_service(
189 net::ProxyService::CreateSystemProxyConfigService(
190 network_loop.task_runner(), network_loop.task_runner()));
192 // Uses |network_change_notifier|.
193 net::NetworkChangeNotifier::AddIPAddressObserver(&net_watcher);
194 net::NetworkChangeNotifier::AddConnectionTypeObserver(&net_watcher);
195 net::NetworkChangeNotifier::AddDNSObserver(&net_watcher);
196 net::NetworkChangeNotifier::AddNetworkChangeObserver(&net_watcher);
198 proxy_config_service->AddObserver(&net_watcher);
200 LOG(INFO) << "Initial connection type: "
201 << ConnectionTypeToString(
202 network_change_notifier->GetCurrentConnectionType());
205 net::ProxyConfig config;
206 const net::ProxyConfigService::ConfigAvailability availability =
207 proxy_config_service->GetLatestProxyConfig(&config);
208 LOG(INFO) << "Initial proxy config: "
209 << ProxyConfigToString(config) << ", "
210 << ConfigAvailabilityToString(availability);
213 LOG(INFO) << "Watching for network events...";
215 // Start watching for events.
216 network_loop.Run();
218 proxy_config_service->RemoveObserver(&net_watcher);
220 // Uses |network_change_notifier|.
221 net::NetworkChangeNotifier::RemoveDNSObserver(&net_watcher);
222 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(&net_watcher);
223 net::NetworkChangeNotifier::RemoveIPAddressObserver(&net_watcher);
224 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(&net_watcher);
226 return 0;