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.
4
5// This is a small utility that watches for and logs network changes.
6
7#include <string>
8
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"
23
24#if defined(USE_GLIB) && !defined(OS_CHROMEOS)
25#include <glib-object.h>
26#endif
27
28#if defined(OS_MACOSX)
29#include "base/mac/scoped_nsautorelease_pool.h"
30#endif
31
32namespace {
33
34// Conversions from various network-related types to string.
35
36const 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";
57  }
58}
59
60std::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;
65}
66
67const 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";
78  }
79}
80
81// The main observer class that logs network events.
82class 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() {}
90
91  virtual ~NetWatcher() {}
92
93  // net::NetworkChangeNotifier::IPAddressObserver implementation.
94  virtual void OnIPAddressChanged() OVERRIDE {
95    LOG(INFO) << "OnIPAddressChanged()";
96  }
97
98  // net::NetworkChangeNotifier::ConnectionTypeObserver implementation.
99  virtual void OnConnectionTypeChanged(
100      net::NetworkChangeNotifier::ConnectionType type) OVERRIDE {
101    LOG(INFO) << "OnConnectionTypeChanged("
102              << ConnectionTypeToString(type) << ")";
103  }
104
105  // net::NetworkChangeNotifier::DNSObserver implementation.
106  virtual void OnDNSChanged() OVERRIDE {
107    LOG(INFO) << "OnDNSChanged()";
108  }
109
110  // net::NetworkChangeNotifier::NetworkChangeObserver implementation.
111  virtual void OnNetworkChanged(
112      net::NetworkChangeNotifier::ConnectionType type) OVERRIDE {
113    LOG(INFO) << "OnNetworkChanged("
114              << ConnectionTypeToString(type) << ")";
115  }
116
117  // net::ProxyConfigService::Observer implementation.
118  virtual void OnProxyConfigChanged(
119      const net::ProxyConfig& config,
120      net::ProxyConfigService::ConfigAvailability availability) OVERRIDE {
121    LOG(INFO) << "OnProxyConfigChanged("
122              << ProxyConfigToString(config) << ", "
123              << ConfigAvailabilityToString(availability) << ")";
124  }
125
126 private:
127  DISALLOW_COPY_AND_ASSIGN(NetWatcher);
128};
129
130}  // namespace
131
132int main(int argc, char* argv[]) {
133#if defined(OS_MACOSX)
134  base::mac::ScopedNSAutoreleasePool pool;
135#endif
136#if defined(USE_GLIB) && !defined(OS_CHROMEOS)
137  // g_type_init will be deprecated in 2.36. 2.35 is the development
138  // version for 2.36, hence do not call g_type_init starting 2.35.
139  // http://developer.gnome.org/gobject/unstable/gobject-Type-Information.html#g-type-init
140#if !GLIB_CHECK_VERSION(2, 35, 0)
141  // Needed so ProxyConfigServiceLinux can use gconf.
142  // Normally handled by BrowserMainLoop::InitializeToolkit().
143  g_type_init();
144#endif
145#endif  // defined(USE_GLIB) && !defined(OS_CHROMEOS)
146  base::AtExitManager exit_manager;
147  base::CommandLine::Init(argc, argv);
148  logging::LoggingSettings settings;
149  settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
150  logging::InitLogging(settings);
151
152  // Just make the main message loop the network loop.
153  base::MessageLoopForIO network_loop;
154
155  NetWatcher net_watcher;
156
157  scoped_ptr<net::NetworkChangeNotifier> network_change_notifier(
158      net::NetworkChangeNotifier::Create());
159
160  // Use the network loop as the file loop also.
161  scoped_ptr<net::ProxyConfigService> proxy_config_service(
162      net::ProxyService::CreateSystemProxyConfigService(
163          network_loop.message_loop_proxy(),
164          network_loop.message_loop_proxy()));
165
166  // Uses |network_change_notifier|.
167  net::NetworkChangeNotifier::AddIPAddressObserver(&net_watcher);
168  net::NetworkChangeNotifier::AddConnectionTypeObserver(&net_watcher);
169  net::NetworkChangeNotifier::AddDNSObserver(&net_watcher);
170  net::NetworkChangeNotifier::AddNetworkChangeObserver(&net_watcher);
171
172  proxy_config_service->AddObserver(&net_watcher);
173
174  LOG(INFO) << "Initial connection type: "
175            << ConnectionTypeToString(
176                network_change_notifier->GetCurrentConnectionType());
177
178  {
179    net::ProxyConfig config;
180    const net::ProxyConfigService::ConfigAvailability availability =
181        proxy_config_service->GetLatestProxyConfig(&config);
182    LOG(INFO) << "Initial proxy config: "
183              << ProxyConfigToString(config) << ", "
184              << ConfigAvailabilityToString(availability);
185  }
186
187  LOG(INFO) << "Watching for network events...";
188
189  // Start watching for events.
190  network_loop.Run();
191
192  proxy_config_service->RemoveObserver(&net_watcher);
193
194  // Uses |network_change_notifier|.
195  net::NetworkChangeNotifier::RemoveDNSObserver(&net_watcher);
196  net::NetworkChangeNotifier::RemoveConnectionTypeObserver(&net_watcher);
197  net::NetworkChangeNotifier::RemoveIPAddressObserver(&net_watcher);
198  net::NetworkChangeNotifier::RemoveNetworkChangeObserver(&net_watcher);
199
200  return 0;
201}
202