Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / net / tools / net_watcher / net_watcher.cc
blobb6cf393ee841f51f6eefa4092b7ad98fd61cc601
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/values.h"
18 #include "build/build_config.h"
19 #include "net/base/network_change_notifier.h"
20 #include "net/proxy/proxy_config.h"
21 #include "net/proxy/proxy_config_service.h"
22 #include "net/proxy/proxy_service.h"
24 #if defined(USE_GLIB) && !defined(OS_CHROMEOS)
25 #include <glib-object.h>
26 #endif
28 #if defined(OS_MACOSX)
29 #include "base/mac/scoped_nsautorelease_pool.h"
30 #endif
32 namespace {
34 // Conversions from various network-related types to string.
36 const char* ConnectionTypeToString(
37 net::NetworkChangeNotifier::ConnectionType type) {
38 switch (type) {
39 case net::NetworkChangeNotifier::CONNECTION_UNKNOWN:
40 return "CONNECTION_UNKNOWN";
41 case net::NetworkChangeNotifier::CONNECTION_ETHERNET:
42 return "CONNECTION_ETHERNET";
43 case net::NetworkChangeNotifier::CONNECTION_WIFI:
44 return "CONNECTION_WIFI";
45 case net::NetworkChangeNotifier::CONNECTION_2G:
46 return "CONNECTION_2G";
47 case net::NetworkChangeNotifier::CONNECTION_3G:
48 return "CONNECTION_3G";
49 case net::NetworkChangeNotifier::CONNECTION_4G:
50 return "CONNECTION_4G";
51 case net::NetworkChangeNotifier::CONNECTION_NONE:
52 return "CONNECTION_NONE";
53 case net::NetworkChangeNotifier::CONNECTION_BLUETOOTH:
54 return "CONNECTION_BLUETOOTH";
55 default:
56 return "CONNECTION_UNEXPECTED";
60 std::string ProxyConfigToString(const net::ProxyConfig& config) {
61 scoped_ptr<base::Value> config_value(config.ToValue());
62 std::string str;
63 base::JSONWriter::Write(config_value.get(), &str);
64 return str;
67 const char* ConfigAvailabilityToString(
68 net::ProxyConfigService::ConfigAvailability availability) {
69 switch (availability) {
70 case net::ProxyConfigService::CONFIG_PENDING:
71 return "CONFIG_PENDING";
72 case net::ProxyConfigService::CONFIG_VALID:
73 return "CONFIG_VALID";
74 case net::ProxyConfigService::CONFIG_UNSET:
75 return "CONFIG_UNSET";
76 default:
77 return "CONFIG_UNEXPECTED";
81 // The main observer class that logs network events.
82 class NetWatcher :
83 public net::NetworkChangeNotifier::IPAddressObserver,
84 public net::NetworkChangeNotifier::ConnectionTypeObserver,
85 public net::NetworkChangeNotifier::DNSObserver,
86 public net::NetworkChangeNotifier::NetworkChangeObserver,
87 public net::ProxyConfigService::Observer {
88 public:
89 NetWatcher() {}
91 ~NetWatcher() override {}
93 // net::NetworkChangeNotifier::IPAddressObserver implementation.
94 void OnIPAddressChanged() override { LOG(INFO) << "OnIPAddressChanged()"; }
96 // net::NetworkChangeNotifier::ConnectionTypeObserver implementation.
97 void OnConnectionTypeChanged(
98 net::NetworkChangeNotifier::ConnectionType type) override {
99 LOG(INFO) << "OnConnectionTypeChanged("
100 << ConnectionTypeToString(type) << ")";
103 // net::NetworkChangeNotifier::DNSObserver implementation.
104 void OnDNSChanged() override { LOG(INFO) << "OnDNSChanged()"; }
106 // net::NetworkChangeNotifier::NetworkChangeObserver implementation.
107 void OnNetworkChanged(
108 net::NetworkChangeNotifier::ConnectionType type) override {
109 LOG(INFO) << "OnNetworkChanged("
110 << ConnectionTypeToString(type) << ")";
113 // net::ProxyConfigService::Observer implementation.
114 void OnProxyConfigChanged(
115 const net::ProxyConfig& config,
116 net::ProxyConfigService::ConfigAvailability availability) override {
117 LOG(INFO) << "OnProxyConfigChanged("
118 << ProxyConfigToString(config) << ", "
119 << ConfigAvailabilityToString(availability) << ")";
122 private:
123 DISALLOW_COPY_AND_ASSIGN(NetWatcher);
126 } // namespace
128 int main(int argc, char* argv[]) {
129 #if defined(OS_MACOSX)
130 base::mac::ScopedNSAutoreleasePool pool;
131 #endif
132 #if defined(USE_GLIB) && !defined(OS_CHROMEOS)
133 // g_type_init will be deprecated in 2.36. 2.35 is the development
134 // version for 2.36, hence do not call g_type_init starting 2.35.
135 // http://developer.gnome.org/gobject/unstable/gobject-Type-Information.html#g-type-init
136 #if !GLIB_CHECK_VERSION(2, 35, 0)
137 // Needed so ProxyConfigServiceLinux can use gconf.
138 // Normally handled by BrowserMainLoop::InitializeToolkit().
139 g_type_init();
140 #endif
141 #endif // defined(USE_GLIB) && !defined(OS_CHROMEOS)
142 base::AtExitManager exit_manager;
143 base::CommandLine::Init(argc, argv);
144 logging::LoggingSettings settings;
145 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
146 logging::InitLogging(settings);
148 // Just make the main message loop the network loop.
149 base::MessageLoopForIO network_loop;
151 NetWatcher net_watcher;
153 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier(
154 net::NetworkChangeNotifier::Create());
156 // Use the network loop as the file loop also.
157 scoped_ptr<net::ProxyConfigService> proxy_config_service(
158 net::ProxyService::CreateSystemProxyConfigService(
159 network_loop.message_loop_proxy(),
160 network_loop.message_loop_proxy()));
162 // Uses |network_change_notifier|.
163 net::NetworkChangeNotifier::AddIPAddressObserver(&net_watcher);
164 net::NetworkChangeNotifier::AddConnectionTypeObserver(&net_watcher);
165 net::NetworkChangeNotifier::AddDNSObserver(&net_watcher);
166 net::NetworkChangeNotifier::AddNetworkChangeObserver(&net_watcher);
168 proxy_config_service->AddObserver(&net_watcher);
170 LOG(INFO) << "Initial connection type: "
171 << ConnectionTypeToString(
172 network_change_notifier->GetCurrentConnectionType());
175 net::ProxyConfig config;
176 const net::ProxyConfigService::ConfigAvailability availability =
177 proxy_config_service->GetLatestProxyConfig(&config);
178 LOG(INFO) << "Initial proxy config: "
179 << ProxyConfigToString(config) << ", "
180 << ConfigAvailabilityToString(availability);
183 LOG(INFO) << "Watching for network events...";
185 // Start watching for events.
186 network_loop.Run();
188 proxy_config_service->RemoveObserver(&net_watcher);
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);
196 return 0;