Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 // This implementation of NetworkChangeNotifier's offline state detection | 5 // This implementation of NetworkChangeNotifier's offline state detection |
| 6 // depends on D-Bus and NetworkManager, and is known to work on at least | 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 | 7 // GNOME version 2.30. If D-Bus or NetworkManager are unavailable, this |
| 8 // implementation will always behave as if it is online. | 8 // implementation will always behave as if it is online. |
| 9 | 9 |
| 10 #include "net/base/network_change_notifier_linux.h" | 10 #include "net/base/network_change_notifier_linux.h" |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 81 | 81 |
| 82 // Must be called by the helper thread's CleanUp() method. | 82 // Must be called by the helper thread's CleanUp() method. |
| 83 void CleanUp(); | 83 void CleanUp(); |
| 84 | 84 |
| 85 // Implementation of NetworkChangeNotifierLinux::IsCurrentlyOffline(). | 85 // Implementation of NetworkChangeNotifierLinux::IsCurrentlyOffline(). |
| 86 // Safe to call from any thread, but will block until Init() has completed. | 86 // Safe to call from any thread, but will block until Init() has completed. |
| 87 bool IsCurrentlyOffline(); | 87 bool IsCurrentlyOffline(); |
| 88 | 88 |
| 89 private: | 89 private: |
| 90 // Callbacks for D-Bus API. | 90 // Callbacks for D-Bus API. |
| 91 void OnStateChanged(dbus::Message* message); | 91 void OnResponse(dbus::Response* response); |
| 92 | 92 void OnSignaled(dbus::Signal* signal); |
| 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 | 93 |
| 102 void OnConnected(const std::string&, const std::string&, bool success) { | 94 void OnConnected(const std::string&, const std::string&, bool success) { |
| 103 if (!success) { | 95 if (!success) { |
| 104 DLOG(WARNING) << "Failed to set up offline state detection"; | 96 DLOG(WARNING) << "Failed to set up offline state detection"; |
| 105 offline_state_initialized_.Signal(); | 97 offline_state_initialized_.Signal(); |
| 106 } | 98 } |
| 107 } | 99 } |
| 108 | 100 |
| 109 // Converts a NetworkManager state uint to a bool. | 101 // Converts a NetworkManager state uint to a bool. |
| 110 static bool StateIsOffline(uint32 state); | 102 static bool StateIsOffline(uint32 state); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 "StateChanged", | 147 "StateChanged", |
| 156 base::Bind(&NetworkManagerApi::OnSignaled, ptr_factory_.GetWeakPtr()), | 148 base::Bind(&NetworkManagerApi::OnSignaled, ptr_factory_.GetWeakPtr()), |
| 157 base::Bind(&NetworkManagerApi::OnConnected, ptr_factory_.GetWeakPtr())); | 149 base::Bind(&NetworkManagerApi::OnConnected, ptr_factory_.GetWeakPtr())); |
| 158 } | 150 } |
| 159 | 151 |
| 160 void NetworkManagerApi::CleanUp() { | 152 void NetworkManagerApi::CleanUp() { |
| 161 DCHECK_EQ(helper_thread_id_, base::PlatformThread::CurrentId()); | 153 DCHECK_EQ(helper_thread_id_, base::PlatformThread::CurrentId()); |
| 162 ptr_factory_.InvalidateWeakPtrs(); | 154 ptr_factory_.InvalidateWeakPtrs(); |
| 163 } | 155 } |
| 164 | 156 |
| 165 void NetworkManagerApi::OnStateChanged(dbus::Message* message) { | 157 void NetworkManagerApi::OnResponse(dbus::Response* response) { |
| 166 DCHECK_EQ(helper_thread_id_, base::PlatformThread::CurrentId()); | 158 DCHECK_EQ(helper_thread_id_, base::PlatformThread::CurrentId()); |
| 167 if (!message) { | 159 if (!response) { |
| 168 DLOG(WARNING) << "No response received for initial state request"; | 160 DLOG(WARNING) << "No response received for initial state request"; |
| 161 offline_state_initialized_.Signal(); | |
| 169 return; | 162 return; |
| 170 } | 163 } |
| 171 dbus::MessageReader reader(message); | 164 dbus::MessageReader reader(response); |
| 165 uint32 state = 0; | |
| 166 if (!reader.HasMoreData() || !reader.PopVariantOfUint32(&state)) { | |
|
satorux1
2011/11/17 17:40:15
You don't need !reader.HasMoreData(). PopVariantOf
adamk
2011/11/17 18:58:24
Good to know, done.
| |
| 167 DLOG(WARNING) << "Unexpected response for NetworkManager State request: " | |
| 168 << response->ToString(); | |
| 169 offline_state_initialized_.Signal(); | |
| 170 return; | |
| 171 } | |
| 172 { | |
| 173 base::AutoLock lock(is_offline_lock_); | |
| 174 is_offline_ = StateIsOffline(state); | |
| 175 } | |
| 176 offline_state_initialized_.Signal(); | |
|
satorux1
2011/11/17 17:40:15
Calling offline_state_initialized_.Signal() from t
adamk
2011/11/17 18:58:24
Yeah, I agree; this is covered by the unittests, b
| |
| 177 } | |
| 178 | |
| 179 void NetworkManagerApi::OnSignaled(dbus::Signal* signal) { | |
| 180 DCHECK_EQ(helper_thread_id_, base::PlatformThread::CurrentId()); | |
| 181 dbus::MessageReader reader(signal); | |
| 172 uint32 state = 0; | 182 uint32 state = 0; |
| 173 if (!reader.HasMoreData() || !reader.PopUint32(&state)) { | 183 if (!reader.HasMoreData() || !reader.PopUint32(&state)) { |
|
satorux1
2011/11/17 17:40:15
You can remove !reader.HasMoreData() from here too
adamk
2011/11/17 18:58:24
Done.
| |
| 174 DLOG(WARNING) << "Unexpected response for NetworkManager State request: " | 184 DLOG(WARNING) << "Unexpected signal for NetworkManager StateChanged: " |
| 175 << message->ToString(); | 185 << signal->ToString(); |
| 176 return; | 186 return; |
| 177 } | 187 } |
| 178 bool new_is_offline = StateIsOffline(state); | 188 bool new_is_offline = StateIsOffline(state); |
| 179 { | 189 { |
| 180 base::AutoLock lock(is_offline_lock_); | 190 base::AutoLock lock(is_offline_lock_); |
| 181 if (is_offline_ != new_is_offline) | 191 if (is_offline_ != new_is_offline) |
| 182 is_offline_ = new_is_offline; | 192 is_offline_ = new_is_offline; |
| 183 else | 193 else |
| 184 return; | 194 return; |
| 185 } | 195 } |
| 186 if (offline_state_initialized_.IsSignaled()) | 196 notification_callback_.Run(); |
| 187 notification_callback_.Run(); | |
| 188 } | 197 } |
| 189 | 198 |
| 190 bool NetworkManagerApi::StateIsOffline(uint32 state) { | 199 bool NetworkManagerApi::StateIsOffline(uint32 state) { |
| 191 switch (state) { | 200 switch (state) { |
| 192 case NM_LEGACY_STATE_CONNECTED: | 201 case NM_LEGACY_STATE_CONNECTED: |
| 193 case NM_STATE_CONNECTED_SITE: | 202 case NM_STATE_CONNECTED_SITE: |
| 194 case NM_STATE_CONNECTED_GLOBAL: | 203 case NM_STATE_CONNECTED_GLOBAL: |
| 195 // Definitely connected | 204 // Definitely connected |
| 196 return false; | 205 return false; |
| 197 case NM_LEGACY_STATE_DISCONNECTED: | 206 case NM_LEGACY_STATE_DISCONNECTED: |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 443 // Stopping from here allows us to sanity- check that the notifier | 452 // Stopping from here allows us to sanity- check that the notifier |
| 444 // thread shut down properly. | 453 // thread shut down properly. |
| 445 notifier_thread_->Stop(); | 454 notifier_thread_->Stop(); |
| 446 } | 455 } |
| 447 | 456 |
| 448 bool NetworkChangeNotifierLinux::IsCurrentlyOffline() const { | 457 bool NetworkChangeNotifierLinux::IsCurrentlyOffline() const { |
| 449 return notifier_thread_->IsCurrentlyOffline(); | 458 return notifier_thread_->IsCurrentlyOffline(); |
| 450 } | 459 } |
| 451 | 460 |
| 452 } // namespace net | 461 } // namespace net |
| OLD | NEW |