Chromium Code Reviews

Side by Side Diff: net/base/network_change_notifier_linux.cc

Issue 8249008: Offline state detection for linux, using new D-Bus library. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added unittest Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #include "net/base/network_change_notifier_linux.h" 5 #include "net/base/network_change_notifier_linux.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <sys/socket.h> 8 #include <sys/socket.h>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
12 #include "base/callback.h" 12 #include "base/callback.h"
13 #include "base/compiler_specific.h" 13 #include "base/compiler_specific.h"
14 #include "base/eintr_wrapper.h" 14 #include "base/eintr_wrapper.h"
15 #include "base/file_util.h" 15 #include "base/file_util.h"
16 #include "base/files/file_path_watcher.h" 16 #include "base/files/file_path_watcher.h"
17 #include "base/memory/weak_ptr.h" 17 #include "base/memory/weak_ptr.h"
18 #include "base/synchronization/lock.h"
19 #include "base/synchronization/waitable_event.h"
20 #include "base/threading/platform_thread.h"
18 #include "base/threading/thread.h" 21 #include "base/threading/thread.h"
22 #include "dbus/bus.h"
23 #include "dbus/message.h"
24 #include "dbus/object_proxy.h"
19 #include "net/base/net_errors.h" 25 #include "net/base/net_errors.h"
20 #include "net/base/network_change_notifier_netlink_linux.h" 26 #include "net/base/network_change_notifier_netlink_linux.h"
21 27
22 using ::base::files::FilePathWatcher; 28 using ::base::files::FilePathWatcher;
23 29
24 namespace net { 30 namespace net {
25 31
26 namespace { 32 namespace {
27 33
28 const int kInvalidSocket = -1; 34 const int kInvalidSocket = -1;
29 35
36 const char kNetworkManagerServiceName[] = "org.freedesktop.NetworkManager";
37 const char kNetworkManagerPath[] = "/org/freedesktop/NetworkManager";
38 const char kNetworkManagerInterface[] = "org.freedesktop.NetworkManager";
39
40 // http://projects.gnome.org/NetworkManager/developers/spec-08.html#type-NM_STAT E
41 enum {
42 NM_LEGACY_STATE_UNKNOWN = 0,
43 NM_LEGACY_STATE_ASLEEP = 1,
44 NM_LEGACY_STATE_CONNECTING = 2,
45 NM_LEGACY_STATE_CONNECTED = 3,
46 NM_LEGACY_STATE_DISCONNECTED = 4
47 };
48
49 // http://projects.gnome.org/NetworkManager/developers/migrating-to-09/spec.html #type-NM_STATE
50 enum {
51 NM_STATE_UNKNOWN = 0,
52 NM_STATE_ASLEEP = 10,
53 NM_STATE_DISCONNECTED = 20,
54 NM_STATE_DISCONNECTING = 30,
55 NM_STATE_CONNECTING = 40,
56 NM_STATE_CONNECTED_LOCAL = 50,
57 NM_STATE_CONNECTED_SITE = 60,
58 NM_STATE_CONNECTED_GLOBAL = 70
59 };
60
61 // A wrapper around NetworkManager's D-Bus API.
62 class NetworkManagerApi {
63 public:
64 explicit NetworkManagerApi(const base::Closure& notification_callback)
65 : is_offline_(false),
66 offline_state_initialized_(true /*manual_reset*/, false),
67 notification_callback_(notification_callback),
68 helper_thread_id_(base::kInvalidThreadId),
69 ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)) { }
70
71 // For testing.
72 NetworkManagerApi(const base::Closure& notification_callback, dbus::Bus* bus)
73 : is_offline_(false),
74 offline_state_initialized_(true /*manual_reset*/, false),
75 notification_callback_(notification_callback),
76 helper_thread_id_(base::kInvalidThreadId),
77 ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)),
78 system_bus_(bus) { }
79
80 ~NetworkManagerApi() { }
81
82 // Should be called on a helper thread which must be of type IO.
83 void Init();
84
85 // Must be called by the helper thread's CleanUp() method.
86 void CleanUp();
87
88 // Implementation of NetworkChangeNotifierLinux::IsCurrentlyOffline().
89 // Safe to call from any thread, but will block until Init() has completed.
90 bool IsCurrentlyOffline();
91
92 private:
93 // Callbacks for D-Bus API.
94 void OnStateChanged(dbus::Message* message);
satorux1 2011/10/26 17:37:09 We usually have a blank line between functions.
adamk 2011/10/26 18:35:52 Done.
95 void OnResponse(dbus::Response* response) {
96 OnStateChanged(response);
97 offline_state_initialized_.Signal();
98 }
99 void OnSignaled(dbus::Signal* signal) {
100 OnStateChanged(signal);
101 }
102 void OnConnected(const std::string&, const std::string&, bool success) {
103 DLOG_IF(WARNING, !success) << "Failed to set up offline state detection";
104 }
105
106 // Converts a NetworkManager state uint to a bool.
107 static bool StateIsOffline(uint32 state);
108
109 bool is_offline_;
110 base::Lock is_offline_lock_;
111 base::WaitableEvent offline_state_initialized_;
112
113 base::Closure notification_callback_;
114
115 base::PlatformThreadId helper_thread_id_;
116
117 base::WeakPtrFactory<NetworkManagerApi> ptr_factory_;
118
119 scoped_refptr<dbus::Bus> system_bus_;
120
121 DISALLOW_COPY_AND_ASSIGN(NetworkManagerApi);
122 };
123
124 void NetworkManagerApi::Init() {
125 // D-Bus requires an IO MessageLoop.
126 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_IO);
127 helper_thread_id_ = base::PlatformThread::CurrentId();
128
129 if (!system_bus_) {
130 dbus::Bus::Options options;
131 options.bus_type = dbus::Bus::SYSTEM;
132 options.connection_type = dbus::Bus::PRIVATE;
133 system_bus_ = new dbus::Bus(options);
134 }
135
136 dbus::ObjectProxy* proxy =
137 system_bus_->GetObjectProxy(kNetworkManagerServiceName,
138 kNetworkManagerPath);
139
140 // Get the initial state asynchronously.
141 dbus::MethodCall method_call(DBUS_INTERFACE_PROPERTIES, "Get");
142 dbus::MessageWriter builder(&method_call);
143 builder.AppendString(kNetworkManagerInterface);
144 builder.AppendString("State");
145 proxy->CallMethod(
146 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
147 base::Bind(&NetworkManagerApi::OnResponse, ptr_factory_.GetWeakPtr()));
148
149 // And sign up for notifications.
150 proxy->ConnectToSignal(
151 kNetworkManagerInterface,
152 "StateChanged",
153 base::Bind(&NetworkManagerApi::OnSignaled, ptr_factory_.GetWeakPtr()),
154 base::Bind(&NetworkManagerApi::OnConnected, ptr_factory_.GetWeakPtr()));
155 }
156
157 void NetworkManagerApi::CleanUp() {
158 DCHECK_EQ(helper_thread_id_, base::PlatformThread::CurrentId());
159 ptr_factory_.InvalidateWeakPtrs();
160 }
161
162 void NetworkManagerApi::OnStateChanged(dbus::Message* message) {
163 DCHECK_EQ(helper_thread_id_, base::PlatformThread::CurrentId());
164 if (!message) {
165 DLOG(WARNING) << "No response received for initial state request";
166 return;
167 }
168 dbus::MessageReader reader(message);
169 uint32 state = 0;
170 if (!reader.HasMoreData() || !reader.PopUint32(&state)) {
171 DLOG(WARNING) << "Unexpected response for NetworkManager State request: "
172 << message->ToString();
173 return;
174 }
175 bool new_is_offline = StateIsOffline(state);
176 {
177 base::AutoLock lock(is_offline_lock_);
178 if (is_offline_ != new_is_offline)
179 is_offline_ = new_is_offline;
180 else
181 return;
182 }
183 if (offline_state_initialized_.IsSignaled())
184 notification_callback_.Run();
185 }
186
187 bool NetworkManagerApi::StateIsOffline(uint32 state) {
188 switch (state) {
189 case NM_LEGACY_STATE_CONNECTED:
190 case NM_STATE_CONNECTED_SITE:
191 case NM_STATE_CONNECTED_GLOBAL:
192 // Definitely connected
193 return false;
194 case NM_LEGACY_STATE_DISCONNECTED:
195 case NM_STATE_DISCONNECTED:
196 // Definitely disconnected
197 return true;
198 case NM_STATE_CONNECTED_LOCAL:
199 // Local networking only; I'm treating this as offline (keybuk)
200 return true;
201 case NM_LEGACY_STATE_CONNECTING:
202 case NM_STATE_DISCONNECTING:
203 case NM_STATE_CONNECTING:
204 // In-flight change to connection status currently underway
205 return true;
206 case NM_LEGACY_STATE_ASLEEP:
207 case NM_STATE_ASLEEP:
208 // Networking disabled or no devices on system
209 return true;
210 default:
211 // Unknown status
212 return false;
213 }
214 }
215
216 bool NetworkManagerApi::IsCurrentlyOffline() {
217 offline_state_initialized_.Wait();
218 base::AutoLock lock(is_offline_lock_);
219 return is_offline_;
220 }
221
30 class DNSWatchDelegate : public FilePathWatcher::Delegate { 222 class DNSWatchDelegate : public FilePathWatcher::Delegate {
31 public: 223 public:
32 explicit DNSWatchDelegate(const base::Closure& callback) 224 explicit DNSWatchDelegate(const base::Closure& callback)
33 : callback_(callback) {} 225 : callback_(callback) {}
34 virtual ~DNSWatchDelegate() {} 226 virtual ~DNSWatchDelegate() {}
35 // FilePathWatcher::Delegate interface 227 // FilePathWatcher::Delegate interface
36 virtual void OnFilePathChanged(const FilePath& path) OVERRIDE; 228 virtual void OnFilePathChanged(const FilePath& path) OVERRIDE;
37 virtual void OnFilePathError(const FilePath& path) OVERRIDE; 229 virtual void OnFilePathError(const FilePath& path) OVERRIDE;
38 private: 230 private:
39 base::Closure callback_; 231 base::Closure callback_;
40 DISALLOW_COPY_AND_ASSIGN(DNSWatchDelegate); 232 DISALLOW_COPY_AND_ASSIGN(DNSWatchDelegate);
41 }; 233 };
42 234
43 void DNSWatchDelegate::OnFilePathChanged(const FilePath& path) { 235 void DNSWatchDelegate::OnFilePathChanged(const FilePath& path) {
44 // Calls NetworkChangeNotifier::NotifyObserversOfDNSChange(). 236 // Calls NetworkChangeNotifier::NotifyObserversOfDNSChange().
45 callback_.Run(); 237 callback_.Run();
46 } 238 }
47 239
48 void DNSWatchDelegate::OnFilePathError(const FilePath& path) { 240 void DNSWatchDelegate::OnFilePathError(const FilePath& path) {
49 LOG(ERROR) << "DNSWatchDelegate::OnFilePathError for " << path.value(); 241 LOG(ERROR) << "DNSWatchDelegate::OnFilePathError for " << path.value();
50 } 242 }
51 243
52 } // namespace 244 } // namespace
53 245
54 class NetworkChangeNotifierLinux::Thread 246 class NetworkChangeNotifierLinux::Thread
55 : public base::Thread, public MessageLoopForIO::Watcher { 247 : public base::Thread, public MessageLoopForIO::Watcher {
56 public: 248 public:
57 Thread(); 249 Thread();
250 explicit Thread(dbus::Bus* bus);
satorux1 2011/10/26 17:37:09 Function overloading is prohibited per our C++ sty
adamk 2011/10/26 18:35:52 Done, and got rid of the extra constructor on Netw
58 virtual ~Thread(); 251 virtual ~Thread();
59 252
60 // MessageLoopForIO::Watcher: 253 // MessageLoopForIO::Watcher:
61 virtual void OnFileCanReadWithoutBlocking(int fd); 254 virtual void OnFileCanReadWithoutBlocking(int fd);
62 virtual void OnFileCanWriteWithoutBlocking(int /* fd */); 255 virtual void OnFileCanWriteWithoutBlocking(int /* fd */);
63 256
257 // Plumbing for NetworkChangeNotifier::IsCurrentlyOffline.
258 // Safe to call from any thread.
259 bool IsCurrentlyOffline() {
260 return network_manager_api_.IsCurrentlyOffline();
261 }
262
64 protected: 263 protected:
65 // base::Thread 264 // base::Thread
66 virtual void Init(); 265 virtual void Init();
67 virtual void CleanUp(); 266 virtual void CleanUp();
68 267
69 private: 268 private:
70 void NotifyObserversOfIPAddressChange() { 269 void NotifyObserversOfIPAddressChange() {
71 NetworkChangeNotifier::NotifyObserversOfIPAddressChange(); 270 NetworkChangeNotifier::NotifyObserversOfIPAddressChange();
72 } 271 }
73 272
74 void NotifyObserversOfDNSChange() { 273 void NotifyObserversOfDNSChange() {
75 NetworkChangeNotifier::NotifyObserversOfDNSChange(); 274 NetworkChangeNotifier::NotifyObserversOfDNSChange();
76 } 275 }
77 276
277 static void NotifyObserversOfOnlineStateChange() {
278 NetworkChangeNotifier::NotifyObserversOfOnlineStateChange();
279 }
280
78 // Starts listening for netlink messages. Also handles the messages if there 281 // Starts listening for netlink messages. Also handles the messages if there
79 // are any available on the netlink socket. 282 // are any available on the netlink socket.
80 void ListenForNotifications(); 283 void ListenForNotifications();
81 284
82 // Attempts to read from the netlink socket into |buf| of length |len|. 285 // Attempts to read from the netlink socket into |buf| of length |len|.
83 // Returns the bytes read on synchronous success and ERR_IO_PENDING if the 286 // Returns the bytes read on synchronous success and ERR_IO_PENDING if the
84 // recv() would block. Otherwise, it returns a net error code. 287 // recv() would block. Otherwise, it returns a net error code.
85 int ReadNotificationMessage(char* buf, size_t len); 288 int ReadNotificationMessage(char* buf, size_t len);
86 289
87 // The netlink socket descriptor. 290 // The netlink socket descriptor.
88 int netlink_fd_; 291 int netlink_fd_;
89 MessageLoopForIO::FileDescriptorWatcher netlink_watcher_; 292 MessageLoopForIO::FileDescriptorWatcher netlink_watcher_;
90 293
91 // Technically only needed for ChromeOS, but it's ugly to #ifdef out. 294 // Technically only needed for ChromeOS, but it's ugly to #ifdef out.
92 base::WeakPtrFactory<Thread> ptr_factory_; 295 base::WeakPtrFactory<Thread> ptr_factory_;
93 296
94 // Used to watch for changes to /etc/resolv.conf and /etc/hosts. 297 // Used to watch for changes to /etc/resolv.conf and /etc/hosts.
95 scoped_ptr<base::files::FilePathWatcher> resolv_file_watcher_; 298 scoped_ptr<base::files::FilePathWatcher> resolv_file_watcher_;
96 scoped_ptr<base::files::FilePathWatcher> hosts_file_watcher_; 299 scoped_ptr<base::files::FilePathWatcher> hosts_file_watcher_;
97 scoped_refptr<DNSWatchDelegate> file_watcher_delegate_; 300 scoped_refptr<DNSWatchDelegate> file_watcher_delegate_;
98 301
302 // Used to detect online/offline state changes.
303 NetworkManagerApi network_manager_api_;
304
99 DISALLOW_COPY_AND_ASSIGN(Thread); 305 DISALLOW_COPY_AND_ASSIGN(Thread);
100 }; 306 };
101 307
102 NetworkChangeNotifierLinux::Thread::Thread() 308 NetworkChangeNotifierLinux::Thread::Thread()
103 : base::Thread("NetworkChangeNotifier"), 309 : base::Thread("NetworkChangeNotifier"),
104 netlink_fd_(kInvalidSocket), 310 netlink_fd_(kInvalidSocket),
105 ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)) { 311 ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)),
312 network_manager_api_(
313 base::Bind(&NetworkChangeNotifierLinux::Thread
314 ::NotifyObserversOfOnlineStateChange)) {
315 }
316
317 NetworkChangeNotifierLinux::Thread::Thread(dbus::Bus* bus)
318 : base::Thread("NetworkChangeNotifier"),
319 netlink_fd_(kInvalidSocket),
320 ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)),
321 network_manager_api_(
322 base::Bind(&NetworkChangeNotifierLinux::Thread
323 ::NotifyObserversOfOnlineStateChange),
324 bus) {
106 } 325 }
107 326
108 NetworkChangeNotifierLinux::Thread::~Thread() {} 327 NetworkChangeNotifierLinux::Thread::~Thread() {}
109 328
110 void NetworkChangeNotifierLinux::Thread::Init() { 329 void NetworkChangeNotifierLinux::Thread::Init() {
111 resolv_file_watcher_.reset(new FilePathWatcher); 330 resolv_file_watcher_.reset(new FilePathWatcher);
112 hosts_file_watcher_.reset(new FilePathWatcher); 331 hosts_file_watcher_.reset(new FilePathWatcher);
113 file_watcher_delegate_ = new DNSWatchDelegate(base::Bind( 332 file_watcher_delegate_ = new DNSWatchDelegate(base::Bind(
114 &NetworkChangeNotifierLinux::Thread::NotifyObserversOfDNSChange, 333 &NetworkChangeNotifierLinux::Thread::NotifyObserversOfDNSChange,
115 base::Unretained(this))); 334 base::Unretained(this)));
satorux1 2011/10/26 17:37:09 For base::Unretained(this) to be safe, |this| need
adamk 2011/10/26 18:35:52 This code didn't change in this patch. But I've m
116 if (!resolv_file_watcher_->Watch( 335 if (!resolv_file_watcher_->Watch(
117 FilePath(FILE_PATH_LITERAL("/etc/resolv.conf")), 336 FilePath(FILE_PATH_LITERAL("/etc/resolv.conf")),
118 file_watcher_delegate_.get())) { 337 file_watcher_delegate_.get())) {
119 LOG(ERROR) << "Failed to setup watch for /etc/resolv.conf"; 338 LOG(ERROR) << "Failed to setup watch for /etc/resolv.conf";
120 } 339 }
121 if (!hosts_file_watcher_->Watch(FilePath(FILE_PATH_LITERAL("/etc/hosts")), 340 if (!hosts_file_watcher_->Watch(FilePath(FILE_PATH_LITERAL("/etc/hosts")),
122 file_watcher_delegate_.get())) { 341 file_watcher_delegate_.get())) {
123 LOG(ERROR) << "Failed to setup watch for /etc/hosts"; 342 LOG(ERROR) << "Failed to setup watch for /etc/hosts";
124 } 343 }
125 netlink_fd_ = InitializeNetlinkSocket(); 344 netlink_fd_ = InitializeNetlinkSocket();
126 if (netlink_fd_ < 0) { 345 if (netlink_fd_ < 0) {
127 netlink_fd_ = kInvalidSocket; 346 netlink_fd_ = kInvalidSocket;
128 return; 347 return;
129 } 348 }
130 ListenForNotifications(); 349 ListenForNotifications();
350
351 network_manager_api_.Init();
131 } 352 }
132 353
133 void NetworkChangeNotifierLinux::Thread::CleanUp() { 354 void NetworkChangeNotifierLinux::Thread::CleanUp() {
134 if (netlink_fd_ != kInvalidSocket) { 355 if (netlink_fd_ != kInvalidSocket) {
135 if (HANDLE_EINTR(close(netlink_fd_)) != 0) 356 if (HANDLE_EINTR(close(netlink_fd_)) != 0)
136 PLOG(ERROR) << "Failed to close socket"; 357 PLOG(ERROR) << "Failed to close socket";
137 netlink_fd_ = kInvalidSocket; 358 netlink_fd_ = kInvalidSocket;
138 netlink_watcher_.StopWatchingFileDescriptor(); 359 netlink_watcher_.StopWatchingFileDescriptor();
139 } 360 }
140 // Kill watchers early to make sure they won't try to call 361 // Kill watchers early to make sure they won't try to call
141 // into us via the delegate during destruction. 362 // into us via the delegate during destruction.
142 resolv_file_watcher_.reset(); 363 resolv_file_watcher_.reset();
143 hosts_file_watcher_.reset(); 364 hosts_file_watcher_.reset();
365
366 network_manager_api_.CleanUp();
144 } 367 }
145 368
146 void NetworkChangeNotifierLinux::Thread::OnFileCanReadWithoutBlocking(int fd) { 369 void NetworkChangeNotifierLinux::Thread::OnFileCanReadWithoutBlocking(int fd) {
147 DCHECK_EQ(fd, netlink_fd_); 370 DCHECK_EQ(fd, netlink_fd_);
148 ListenForNotifications(); 371 ListenForNotifications();
149 } 372 }
150 373
151 void NetworkChangeNotifierLinux::Thread::OnFileCanWriteWithoutBlocking( 374 void NetworkChangeNotifierLinux::Thread::OnFileCanWriteWithoutBlocking(
152 int /* fd */) { 375 int /* fd */) {
153 NOTREACHED(); 376 NOTREACHED();
(...skipping 52 matching lines...)
206 429
207 NetworkChangeNotifierLinux::NetworkChangeNotifierLinux() 430 NetworkChangeNotifierLinux::NetworkChangeNotifierLinux()
208 : notifier_thread_(new Thread) { 431 : notifier_thread_(new Thread) {
209 // We create this notifier thread because the notification implementation 432 // We create this notifier thread because the notification implementation
210 // needs a MessageLoopForIO, and there's no guarantee that 433 // needs a MessageLoopForIO, and there's no guarantee that
211 // MessageLoop::current() meets that criterion. 434 // MessageLoop::current() meets that criterion.
212 base::Thread::Options thread_options(MessageLoop::TYPE_IO, 0); 435 base::Thread::Options thread_options(MessageLoop::TYPE_IO, 0);
213 notifier_thread_->StartWithOptions(thread_options); 436 notifier_thread_->StartWithOptions(thread_options);
214 } 437 }
215 438
439 // Used only for testing, injects a dbus::Bus into the helper thread.
440 NetworkChangeNotifierLinux::NetworkChangeNotifierLinux(dbus::Bus* bus)
441 : notifier_thread_(new Thread(bus)) {
442 base::Thread::Options thread_options(MessageLoop::TYPE_IO, 0);
443 notifier_thread_->StartWithOptions(thread_options);
444 }
445
216 NetworkChangeNotifierLinux::~NetworkChangeNotifierLinux() { 446 NetworkChangeNotifierLinux::~NetworkChangeNotifierLinux() {
217 // We don't need to explicitly Stop(), but doing so allows us to sanity- 447 // We don't need to explicitly Stop(), but doing so allows us to sanity-
218 // check that the notifier thread shut down properly. 448 // check that the notifier thread shut down properly.
219 notifier_thread_->Stop(); 449 notifier_thread_->Stop();
220 } 450 }
221 451
222 bool NetworkChangeNotifierLinux::IsCurrentlyOffline() const { 452 bool NetworkChangeNotifierLinux::IsCurrentlyOffline() const {
223 // TODO(eroman): http://crbug.com/53473 453 return notifier_thread_->IsCurrentlyOffline();
224 return false;
225 } 454 }
226 455
227 } // namespace net 456 } // namespace net
OLDNEW

Powered by Google App Engine