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

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: Created 9 years, 2 months 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
« no previous file with comments | « no previous file | net/net.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/callback.h"
11 #include "base/callback_old.h" 12 #include "base/callback_old.h"
12 #include "base/compiler_specific.h" 13 #include "base/compiler_specific.h"
13 #include "base/eintr_wrapper.h" 14 #include "base/eintr_wrapper.h"
14 #include "base/file_util.h" 15 #include "base/file_util.h"
15 #include "base/files/file_path_watcher.h" 16 #include "base/files/file_path_watcher.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/synchronization/condition_variable.h"
19 #include "base/synchronization/lock.h"
16 #include "base/task.h" 20 #include "base/task.h"
17 #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"
18 #include "net/base/net_errors.h" 25 #include "net/base/net_errors.h"
19 #include "net/base/network_change_notifier_netlink_linux.h" 26 #include "net/base/network_change_notifier_netlink_linux.h"
20 27
21 using ::base::files::FilePathWatcher; 28 using ::base::files::FilePathWatcher;
22 29
23 namespace net { 30 namespace net {
24 31
25 namespace { 32 namespace {
26 33
27 const int kInvalidSocket = -1; 34 const int kInvalidSocket = -1;
28 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 class NetworkManagerApi : public base::SupportsWeakPtr<NetworkManagerApi> {
satorux1 2011/10/12 22:28:43 Please write a class comment. BTW, I think WeakPt
adamk 2011/10/13 01:46:17 Added a comment and used WeakPtrFactory.
62 public:
63 typedef base::Callback<void(void)> NotificationCallback;
64 explicit NetworkManagerApi(NotificationCallback notification_callback);
65 ~NetworkManagerApi();
66
67 void Init();
68 bool IsCurrentlyOffline();
satorux1 2011/10/12 22:28:43 Function comments are missing. One line comments w
adamk 2011/10/13 01:46:17 I've added comments for each of these (and a few o
satorux1 2011/10/13 06:58:05 Thank you for adding comments. Sorry for being nit
adamk 2011/10/13 18:14:06 Understood, I guess my point is that there are som
69
70 private:
71 enum OnlineState {
72 UNINITIALIZED = -1,
73 OFFLINE = 0,
74 ONLINE = 1
75 };
76
77 void NotifyObserversOfOnlineStateChange() {
78 notification_callback_.Run();
79 }
80 void OnStateChanged(dbus::Signal* signal);
81 void OnConnected(const std::string&, const std::string&, bool success) {
82 if (!success)
83 LOG(WARNING) << "Failed to set up offline state detection";
satorux1 2011/10/12 22:28:43 LOG_IF(WARNING, !success) is a bit more succinct.
adamk 2011/10/13 01:46:17 Done.
84 }
85
86 static uint32 GetCurrentState(dbus::ObjectProxy* proxy);
87 static OnlineState TranslateState(uint32 state);
88
89 OnlineState online_state_;
90 base::Lock online_state_lock_;
91 base::ConditionVariable initial_state_cv_;
satorux1 2011/10/12 22:28:43 I guess you can use WaitedEvent instead?
satorux1 2011/10/12 22:29:50 I meant WaitableEvent in base/synchronization
adamk 2011/10/13 01:46:17 Hmm, it's not obvious to me that it's a win to use
satorux1 2011/10/13 06:58:05 That's a good point. Generally WaitableEvent is si
adamk 2011/10/13 18:14:06 Yeah, I guess there's no good reason _not_ to use
92
93 NotificationCallback notification_callback_;
94
95 scoped_refptr<dbus::Bus> system_bus_;
96
97 DISALLOW_COPY_AND_ASSIGN(NetworkManagerApi);
98 };
99
100 NetworkManagerApi::NetworkManagerApi(NotificationCallback notification_callback)
101 : online_state_(UNINITIALIZED),
102 initial_state_cv_(&online_state_lock_),
103 notification_callback_(notification_callback) {
104 }
105
106 NetworkManagerApi::~NetworkManagerApi() {
107 }
108
109 void NetworkManagerApi::Init() {
satorux1 2011/10/12 22:28:43 What thread is the function running on? I assume i
adamk 2011/10/13 01:46:17 It's run on the NetworkChangeNotifier's helper thr
satorux1 2011/10/13 06:58:05 Thanks. I think it'd be nice to add a comment abou
adamk 2011/10/13 18:14:06 I added a comment in the declaration saying how to
110 dbus::Bus::Options options;
111 options.bus_type = dbus::Bus::SYSTEM;
112 options.connection_type = dbus::Bus::PRIVATE;
113 system_bus_ = new dbus::Bus(options);
satorux1 2011/10/12 22:28:43 I suppose the bus works on the current thread beca
adamk 2011/10/13 01:46:17 Yup, added a DCHECK for this (as noted above).
114
115 dbus::ObjectProxy* proxy =
116 system_bus_->GetObjectProxy(kNetworkManagerServiceName,
117 kNetworkManagerPath);
118 OnlineState online_state = TranslateState(GetCurrentState(proxy));
119 {
120 base::AutoLock lock(online_state_lock_);
121 online_state_ = online_state;
122 initial_state_cv_.Signal();
123 }
124
125 proxy->ConnectToSignal(
126 kNetworkManagerInterface,
127 "StateChanged",
128 base::Bind(&NetworkManagerApi::OnStateChanged, AsWeakPtr()),
129 base::Bind(&NetworkManagerApi::OnConnected, AsWeakPtr()));
130 }
131
132 void NetworkManagerApi::OnStateChanged(dbus::Signal* signal) {
133 DCHECK(signal);
134 dbus::MessageReader reader(signal);
135 uint32 state;
136 if (!reader.PopUint32(&state)) {
137 LOG(WARNING) << "Unexpected response for NetworkManager State request: "
138 << signal->ToString();
139 return;
140 }
141 OnlineState new_online_state = TranslateState(state);
142 {
143 base::AutoLock lock(online_state_lock_);
144 if (new_online_state != online_state_)
145 online_state_ = new_online_state;
146 else
147 return;
148 }
149 // Can't notify while we hold the lock
150 NotifyObserversOfOnlineStateChange();
151 }
152
153 uint32 NetworkManagerApi::GetCurrentState(dbus::ObjectProxy* proxy) {
154 dbus::MethodCall method_call(DBUS_INTERFACE_PROPERTIES, "Get");
155 dbus::MessageWriter builder(&method_call);
156 builder.AppendString(kNetworkManagerInterface);
157 builder.AppendString("State");
158 scoped_ptr<dbus::Response> response(
159 proxy->CallMethodAndBlock(&method_call,
160 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT));
satorux1 2011/10/12 22:28:43 Not sure if it's ok to do a blocking method call h
adamk 2011/10/13 01:46:17 Again, the helper thread. I wouldn't mind making
satorux1 2011/10/13 06:58:05 If it's in a dedicated thread, a blocking call wou
adamk 2011/10/13 18:14:06 Added a comment.
161 if (!response.get())
162 return NM_STATE_UNKNOWN;
163
164 dbus::MessageReader reader(response.get());
165 uint32 state;
166 if (!reader.PopUint32(&state)) {
167 LOG(WARNING) << "Unexpected response for NetworkManager State request: "
168 << response->ToString();
169 return NM_STATE_UNKNOWN;
170 }
171
172 return state;
173 }
174
175 NetworkManagerApi::OnlineState NetworkManagerApi::TranslateState(uint32 state) {
176 switch (state) {
177 case NM_LEGACY_STATE_CONNECTED:
178 case NM_STATE_CONNECTED_SITE:
179 case NM_STATE_CONNECTED_GLOBAL:
180 // Definitely connected
181 return ONLINE;
182 case NM_LEGACY_STATE_DISCONNECTED:
183 case NM_STATE_DISCONNECTED:
184 // Definitely disconnected
185 return OFFLINE;
186 case NM_STATE_CONNECTED_LOCAL:
187 // Local networking only; I'm treating this as offline (keybuk)
188 return OFFLINE;
189 case NM_LEGACY_STATE_CONNECTING:
190 case NM_STATE_DISCONNECTING:
191 case NM_STATE_CONNECTING:
192 // In-flight change to connection status currently underway
193 return OFFLINE;
194 case NM_LEGACY_STATE_ASLEEP:
195 case NM_STATE_ASLEEP:
196 // Networking disabled or no devices on system
197 return OFFLINE;
198 default:
199 // Unknown status
200 return ONLINE;
201 }
202 }
203
204 bool NetworkManagerApi::IsCurrentlyOffline() {
205 base::AutoLock lock(online_state_lock_);
206 while (online_state_ == UNINITIALIZED) {
207 initial_state_cv_.Wait();
208 }
209 return online_state_ == OFFLINE;
210 }
211
29 class DNSWatchDelegate : public FilePathWatcher::Delegate { 212 class DNSWatchDelegate : public FilePathWatcher::Delegate {
30 public: 213 public:
31 explicit DNSWatchDelegate(Callback0::Type* callback) 214 explicit DNSWatchDelegate(Callback0::Type* callback)
32 : callback_(callback) {} 215 : callback_(callback) {}
33 virtual ~DNSWatchDelegate() {} 216 virtual ~DNSWatchDelegate() {}
34 // FilePathWatcher::Delegate interface 217 // FilePathWatcher::Delegate interface
35 virtual void OnFilePathChanged(const FilePath& path) OVERRIDE; 218 virtual void OnFilePathChanged(const FilePath& path) OVERRIDE;
36 virtual void OnFilePathError(const FilePath& path) OVERRIDE; 219 virtual void OnFilePathError(const FilePath& path) OVERRIDE;
37 private: 220 private:
38 scoped_ptr<Callback0::Type> callback_; 221 scoped_ptr<Callback0::Type> callback_;
(...skipping 15 matching lines...) Expand all
54 class NetworkChangeNotifierLinux::Thread 237 class NetworkChangeNotifierLinux::Thread
55 : public base::Thread, public MessageLoopForIO::Watcher { 238 : public base::Thread, public MessageLoopForIO::Watcher {
56 public: 239 public:
57 Thread(); 240 Thread();
58 virtual ~Thread(); 241 virtual ~Thread();
59 242
60 // MessageLoopForIO::Watcher: 243 // MessageLoopForIO::Watcher:
61 virtual void OnFileCanReadWithoutBlocking(int fd); 244 virtual void OnFileCanReadWithoutBlocking(int fd);
62 virtual void OnFileCanWriteWithoutBlocking(int /* fd */); 245 virtual void OnFileCanWriteWithoutBlocking(int /* fd */);
63 246
247 bool IsCurrentlyOffline() {
248 return network_manager_api_.IsCurrentlyOffline();
249 }
250
64 protected: 251 protected:
65 // base::Thread 252 // base::Thread
66 virtual void Init(); 253 virtual void Init();
67 virtual void CleanUp(); 254 virtual void CleanUp();
68 255
69 private: 256 private:
70 void NotifyObserversOfIPAddressChange() { 257 void NotifyObserversOfIPAddressChange() {
71 NetworkChangeNotifier::NotifyObserversOfIPAddressChange(); 258 NetworkChangeNotifier::NotifyObserversOfIPAddressChange();
72 } 259 }
73 260
74 void NotifyObserversOfDNSChange() { 261 void NotifyObserversOfDNSChange() {
75 NetworkChangeNotifier::NotifyObserversOfDNSChange(); 262 NetworkChangeNotifier::NotifyObserversOfDNSChange();
76 } 263 }
77 264
265 static void NotifyObserversOfOnlineStateChange() {
266 NetworkChangeNotifier::NotifyObserversOfOnlineStateChange();
267 }
268
78 // Starts listening for netlink messages. Also handles the messages if there 269 // Starts listening for netlink messages. Also handles the messages if there
79 // are any available on the netlink socket. 270 // are any available on the netlink socket.
80 void ListenForNotifications(); 271 void ListenForNotifications();
81 272
82 // Attempts to read from the netlink socket into |buf| of length |len|. 273 // 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 274 // Returns the bytes read on synchronous success and ERR_IO_PENDING if the
84 // recv() would block. Otherwise, it returns a net error code. 275 // recv() would block. Otherwise, it returns a net error code.
85 int ReadNotificationMessage(char* buf, size_t len); 276 int ReadNotificationMessage(char* buf, size_t len);
86 277
87 // The netlink socket descriptor. 278 // The netlink socket descriptor.
88 int netlink_fd_; 279 int netlink_fd_;
89 MessageLoopForIO::FileDescriptorWatcher netlink_watcher_; 280 MessageLoopForIO::FileDescriptorWatcher netlink_watcher_;
90 281
91 // Technically only needed for ChromeOS, but it's ugly to #ifdef out. 282 // Technically only needed for ChromeOS, but it's ugly to #ifdef out.
92 ScopedRunnableMethodFactory<Thread> method_factory_; 283 ScopedRunnableMethodFactory<Thread> method_factory_;
93 284
94 // Used to watch for changes to /etc/resolv.conf and /etc/hosts. 285 // Used to watch for changes to /etc/resolv.conf and /etc/hosts.
95 scoped_ptr<base::files::FilePathWatcher> resolv_file_watcher_; 286 scoped_ptr<base::files::FilePathWatcher> resolv_file_watcher_;
96 scoped_ptr<base::files::FilePathWatcher> hosts_file_watcher_; 287 scoped_ptr<base::files::FilePathWatcher> hosts_file_watcher_;
97 scoped_refptr<DNSWatchDelegate> file_watcher_delegate_; 288 scoped_refptr<DNSWatchDelegate> file_watcher_delegate_;
98 289
290 // Used to detect online/offline state changes.
291 NetworkManagerApi network_manager_api_;
292
99 DISALLOW_COPY_AND_ASSIGN(Thread); 293 DISALLOW_COPY_AND_ASSIGN(Thread);
100 }; 294 };
101 295
102 NetworkChangeNotifierLinux::Thread::Thread() 296 NetworkChangeNotifierLinux::Thread::Thread()
103 : base::Thread("NetworkChangeNotifier"), 297 : base::Thread("NetworkChangeNotifier"),
104 netlink_fd_(kInvalidSocket), 298 netlink_fd_(kInvalidSocket),
105 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { 299 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)),
300 network_manager_api_(
301 base::Bind(&NetworkChangeNotifierLinux::Thread
302 ::NotifyObserversOfOnlineStateChange)) {
106 } 303 }
107 304
108 NetworkChangeNotifierLinux::Thread::~Thread() {} 305 NetworkChangeNotifierLinux::Thread::~Thread() {}
109 306
110 void NetworkChangeNotifierLinux::Thread::Init() { 307 void NetworkChangeNotifierLinux::Thread::Init() {
111 resolv_file_watcher_.reset(new FilePathWatcher); 308 resolv_file_watcher_.reset(new FilePathWatcher);
112 hosts_file_watcher_.reset(new FilePathWatcher); 309 hosts_file_watcher_.reset(new FilePathWatcher);
113 file_watcher_delegate_ = new DNSWatchDelegate(NewCallback(this, 310 file_watcher_delegate_ = new DNSWatchDelegate(NewCallback(this,
114 &NetworkChangeNotifierLinux::Thread::NotifyObserversOfDNSChange)); 311 &NetworkChangeNotifierLinux::Thread::NotifyObserversOfDNSChange));
115 if (!resolv_file_watcher_->Watch( 312 if (!resolv_file_watcher_->Watch(
116 FilePath(FILE_PATH_LITERAL("/etc/resolv.conf")), 313 FilePath(FILE_PATH_LITERAL("/etc/resolv.conf")),
117 file_watcher_delegate_.get())) { 314 file_watcher_delegate_.get())) {
118 LOG(ERROR) << "Failed to setup watch for /etc/resolv.conf"; 315 LOG(ERROR) << "Failed to setup watch for /etc/resolv.conf";
119 } 316 }
120 if (!hosts_file_watcher_->Watch(FilePath(FILE_PATH_LITERAL("/etc/hosts")), 317 if (!hosts_file_watcher_->Watch(FilePath(FILE_PATH_LITERAL("/etc/hosts")),
121 file_watcher_delegate_.get())) { 318 file_watcher_delegate_.get())) {
122 LOG(ERROR) << "Failed to setup watch for /etc/hosts"; 319 LOG(ERROR) << "Failed to setup watch for /etc/hosts";
123 } 320 }
124 netlink_fd_ = InitializeNetlinkSocket(); 321 netlink_fd_ = InitializeNetlinkSocket();
125 if (netlink_fd_ < 0) { 322 if (netlink_fd_ < 0) {
126 netlink_fd_ = kInvalidSocket; 323 netlink_fd_ = kInvalidSocket;
127 return; 324 return;
128 } 325 }
129 ListenForNotifications(); 326 ListenForNotifications();
327
328 network_manager_api_.Init();
130 } 329 }
131 330
132 void NetworkChangeNotifierLinux::Thread::CleanUp() { 331 void NetworkChangeNotifierLinux::Thread::CleanUp() {
133 if (netlink_fd_ != kInvalidSocket) { 332 if (netlink_fd_ != kInvalidSocket) {
134 if (HANDLE_EINTR(close(netlink_fd_)) != 0) 333 if (HANDLE_EINTR(close(netlink_fd_)) != 0)
135 PLOG(ERROR) << "Failed to close socket"; 334 PLOG(ERROR) << "Failed to close socket";
136 netlink_fd_ = kInvalidSocket; 335 netlink_fd_ = kInvalidSocket;
137 netlink_watcher_.StopWatchingFileDescriptor(); 336 netlink_watcher_.StopWatchingFileDescriptor();
138 } 337 }
139 // Kill watchers early to make sure they won't try to call 338 // Kill watchers early to make sure they won't try to call
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 notifier_thread_->StartWithOptions(thread_options); 410 notifier_thread_->StartWithOptions(thread_options);
212 } 411 }
213 412
214 NetworkChangeNotifierLinux::~NetworkChangeNotifierLinux() { 413 NetworkChangeNotifierLinux::~NetworkChangeNotifierLinux() {
215 // We don't need to explicitly Stop(), but doing so allows us to sanity- 414 // We don't need to explicitly Stop(), but doing so allows us to sanity-
216 // check that the notifier thread shut down properly. 415 // check that the notifier thread shut down properly.
217 notifier_thread_->Stop(); 416 notifier_thread_->Stop();
218 } 417 }
219 418
220 bool NetworkChangeNotifierLinux::IsCurrentlyOffline() const { 419 bool NetworkChangeNotifierLinux::IsCurrentlyOffline() const {
221 // TODO(eroman): http://crbug.com/53473 420 return notifier_thread_->IsCurrentlyOffline();
222 return false;
223 } 421 }
224 422
225 } // namespace net 423 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | net/net.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698