Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(555)

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

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

Powered by Google App Engine
This is Rietveld 408576698