adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 4 | |
| 5 | #include "dbus/bus.h" |
| 6 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 9 | #include "base/bind.h" |
| 10 | #include "base/logging.h" |
avi@chromium.org | a29af56 | 2013-07-18 08:00:30 +0900 | [diff] [blame] | 11 | #include "base/message_loop/message_loop.h" |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 12 | #include "base/stl_util.h" |
avi@chromium.org | 66c0f91 | 2013-06-21 04:40:12 +0900 | [diff] [blame] | 13 | #include "base/strings/stringprintf.h" |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 14 | #include "base/threading/thread.h" |
| 15 | #include "base/threading/thread_restrictions.h" |
avi@chromium.org | 78a7e7b | 2013-06-29 00:20:02 +0900 | [diff] [blame] | 16 | #include "base/time/time.h" |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 17 | #include "dbus/exported_object.h" |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 18 | #include "dbus/message.h" |
keybuk@chromium.org | 0971501 | 2013-03-26 03:20:08 +0900 | [diff] [blame] | 19 | #include "dbus/object_manager.h" |
| 20 | #include "dbus/object_path.h" |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 21 | #include "dbus/object_proxy.h" |
| 22 | #include "dbus/scoped_dbus_error.h" |
| 23 | |
| 24 | namespace dbus { |
| 25 | |
| 26 | namespace { |
| 27 | |
nona@chromium.org | 5a44d2b | 2013-02-08 19:53:39 +0900 | [diff] [blame] | 28 | const char kDisconnectedSignal[] = "Disconnected"; |
| 29 | const char kDisconnectedMatchRule[] = |
| 30 | "type='signal', path='/org/freedesktop/DBus/Local'," |
| 31 | "interface='org.freedesktop.DBus.Local', member='Disconnected'"; |
| 32 | |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 33 | // The NameOwnerChanged member in org.freedesktop.DBus |
| 34 | const char kNameOwnerChangedSignal[] = "NameOwnerChanged"; |
| 35 | |
| 36 | // The match rule used to filter for changes to a given service name owner. |
| 37 | const char kServiceNameOwnerChangeMatchRule[] = |
| 38 | "type='signal',interface='org.freedesktop.DBus'," |
| 39 | "member='NameOwnerChanged',path='/org/freedesktop/DBus'," |
| 40 | "sender='org.freedesktop.DBus',arg0='%s'"; |
| 41 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 42 | // The class is used for watching the file descriptor used for D-Bus |
| 43 | // communication. |
| 44 | class Watch : public base::MessagePumpLibevent::Watcher { |
| 45 | public: |
thestig@chromium.org | 074b1db | 2013-02-20 10:36:53 +0900 | [diff] [blame] | 46 | explicit Watch(DBusWatch* watch) |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 47 | : raw_watch_(watch) { |
| 48 | dbus_watch_set_data(raw_watch_, this, NULL); |
| 49 | } |
| 50 | |
dcheng | 7f5750d | 2014-12-30 03:30:17 +0900 | [diff] [blame] | 51 | ~Watch() override { dbus_watch_set_data(raw_watch_, NULL, NULL); } |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 52 | |
| 53 | // Returns true if the underlying file descriptor is ready to be watched. |
| 54 | bool IsReadyToBeWatched() { |
| 55 | return dbus_watch_get_enabled(raw_watch_); |
| 56 | } |
| 57 | |
| 58 | // Starts watching the underlying file descriptor. |
| 59 | void StartWatching() { |
| 60 | const int file_descriptor = dbus_watch_get_unix_fd(raw_watch_); |
| 61 | const int flags = dbus_watch_get_flags(raw_watch_); |
| 62 | |
xhwang@chromium.org | dff6b13 | 2013-05-02 01:10:30 +0900 | [diff] [blame] | 63 | base::MessageLoopForIO::Mode mode = base::MessageLoopForIO::WATCH_READ; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 64 | if ((flags & DBUS_WATCH_READABLE) && (flags & DBUS_WATCH_WRITABLE)) |
xhwang@chromium.org | dff6b13 | 2013-05-02 01:10:30 +0900 | [diff] [blame] | 65 | mode = base::MessageLoopForIO::WATCH_READ_WRITE; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 66 | else if (flags & DBUS_WATCH_READABLE) |
xhwang@chromium.org | dff6b13 | 2013-05-02 01:10:30 +0900 | [diff] [blame] | 67 | mode = base::MessageLoopForIO::WATCH_READ; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 68 | else if (flags & DBUS_WATCH_WRITABLE) |
xhwang@chromium.org | dff6b13 | 2013-05-02 01:10:30 +0900 | [diff] [blame] | 69 | mode = base::MessageLoopForIO::WATCH_WRITE; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 70 | else |
| 71 | NOTREACHED(); |
| 72 | |
| 73 | const bool persistent = true; // Watch persistently. |
xhwang@chromium.org | dff6b13 | 2013-05-02 01:10:30 +0900 | [diff] [blame] | 74 | const bool success = base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 75 | file_descriptor, persistent, mode, &file_descriptor_watcher_, this); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 76 | CHECK(success) << "Unable to allocate memory"; |
| 77 | } |
| 78 | |
| 79 | // Stops watching the underlying file descriptor. |
| 80 | void StopWatching() { |
| 81 | file_descriptor_watcher_.StopWatchingFileDescriptor(); |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | // Implement MessagePumpLibevent::Watcher. |
dcheng | 7f5750d | 2014-12-30 03:30:17 +0900 | [diff] [blame] | 86 | void OnFileCanReadWithoutBlocking(int file_descriptor) override { |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 87 | const bool success = dbus_watch_handle(raw_watch_, DBUS_WATCH_READABLE); |
| 88 | CHECK(success) << "Unable to allocate memory"; |
| 89 | } |
| 90 | |
| 91 | // Implement MessagePumpLibevent::Watcher. |
dcheng | 7f5750d | 2014-12-30 03:30:17 +0900 | [diff] [blame] | 92 | void OnFileCanWriteWithoutBlocking(int file_descriptor) override { |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 93 | const bool success = dbus_watch_handle(raw_watch_, DBUS_WATCH_WRITABLE); |
| 94 | CHECK(success) << "Unable to allocate memory"; |
| 95 | } |
| 96 | |
| 97 | DBusWatch* raw_watch_; |
| 98 | base::MessagePumpLibevent::FileDescriptorWatcher file_descriptor_watcher_; |
| 99 | }; |
| 100 | |
| 101 | // The class is used for monitoring the timeout used for D-Bus method |
| 102 | // calls. |
| 103 | // |
| 104 | // Unlike Watch, Timeout is a ref counted object, to ensure that |this| of |
| 105 | // the object is is alive when HandleTimeout() is called. It's unlikely |
| 106 | // but it may be possible that HandleTimeout() is called after |
| 107 | // Bus::OnRemoveTimeout(). That's why we don't simply delete the object in |
| 108 | // Bus::OnRemoveTimeout(). |
| 109 | class Timeout : public base::RefCountedThreadSafe<Timeout> { |
| 110 | public: |
thestig@chromium.org | 074b1db | 2013-02-20 10:36:53 +0900 | [diff] [blame] | 111 | explicit Timeout(DBusTimeout* timeout) |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 112 | : raw_timeout_(timeout), |
| 113 | monitoring_is_active_(false), |
| 114 | is_completed(false) { |
| 115 | dbus_timeout_set_data(raw_timeout_, this, NULL); |
| 116 | AddRef(); // Balanced on Complete(). |
| 117 | } |
| 118 | |
| 119 | // Returns true if the timeout is ready to be monitored. |
| 120 | bool IsReadyToBeMonitored() { |
| 121 | return dbus_timeout_get_enabled(raw_timeout_); |
| 122 | } |
| 123 | |
| 124 | // Starts monitoring the timeout. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 125 | void StartMonitoring(Bus* bus) { |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 126 | bus->GetDBusTaskRunner()->PostDelayedTask( |
| 127 | FROM_HERE, |
| 128 | base::Bind(&Timeout::HandleTimeout, this), |
| 129 | GetInterval()); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 130 | monitoring_is_active_ = true; |
| 131 | } |
| 132 | |
| 133 | // Stops monitoring the timeout. |
| 134 | void StopMonitoring() { |
| 135 | // We cannot take back the delayed task we posted in |
| 136 | // StartMonitoring(), so we just mark the monitoring is inactive now. |
| 137 | monitoring_is_active_ = false; |
| 138 | } |
| 139 | |
tedvessenes@gmail.com | 8d7a876 | 2012-03-11 10:12:20 +0900 | [diff] [blame] | 140 | // Returns the interval. |
| 141 | base::TimeDelta GetInterval() { |
| 142 | return base::TimeDelta::FromMilliseconds( |
| 143 | dbus_timeout_get_interval(raw_timeout_)); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // Cleans up the raw_timeout and marks that timeout is completed. |
| 147 | // See the class comment above for why we are doing this. |
| 148 | void Complete() { |
| 149 | dbus_timeout_set_data(raw_timeout_, NULL, NULL); |
| 150 | is_completed = true; |
| 151 | Release(); |
| 152 | } |
| 153 | |
| 154 | private: |
| 155 | friend class base::RefCountedThreadSafe<Timeout>; |
| 156 | ~Timeout() { |
| 157 | } |
| 158 | |
| 159 | // Handles the timeout. |
| 160 | void HandleTimeout() { |
| 161 | // If the timeout is marked completed, we should do nothing. This can |
| 162 | // occur if this function is called after Bus::OnRemoveTimeout(). |
| 163 | if (is_completed) |
| 164 | return; |
satorux@chromium.org | c6ac757 | 2011-09-01 03:02:43 +0900 | [diff] [blame] | 165 | // Skip if monitoring is canceled. |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 166 | if (!monitoring_is_active_) |
| 167 | return; |
| 168 | |
| 169 | const bool success = dbus_timeout_handle(raw_timeout_); |
| 170 | CHECK(success) << "Unable to allocate memory"; |
| 171 | } |
| 172 | |
| 173 | DBusTimeout* raw_timeout_; |
| 174 | bool monitoring_is_active_; |
| 175 | bool is_completed; |
| 176 | }; |
| 177 | |
| 178 | } // namespace |
| 179 | |
| 180 | Bus::Options::Options() |
| 181 | : bus_type(SESSION), |
mdm@chromium.org | 45f2c6a | 2011-09-07 05:03:24 +0900 | [diff] [blame] | 182 | connection_type(PRIVATE) { |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | Bus::Options::~Options() { |
| 186 | } |
| 187 | |
| 188 | Bus::Bus(const Options& options) |
| 189 | : bus_type_(options.bus_type), |
| 190 | connection_type_(options.connection_type), |
thestig@chromium.org | 074b1db | 2013-02-20 10:36:53 +0900 | [diff] [blame] | 191 | dbus_task_runner_(options.dbus_task_runner), |
satorux@chromium.org | d336d45 | 2011-09-02 15:56:23 +0900 | [diff] [blame] | 192 | on_shutdown_(false /* manual_reset */, false /* initially_signaled */), |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 193 | connection_(NULL), |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 194 | origin_thread_id_(base::PlatformThread::CurrentId()), |
satorux@chromium.org | 326a6f8 | 2011-08-27 16:26:34 +0900 | [diff] [blame] | 195 | async_operations_set_up_(false), |
satorux@chromium.org | d336d45 | 2011-09-02 15:56:23 +0900 | [diff] [blame] | 196 | shutdown_completed_(false), |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 197 | num_pending_watches_(0), |
nona@chromium.org | 9f638e0 | 2012-04-19 12:20:03 +0900 | [diff] [blame] | 198 | num_pending_timeouts_(0), |
hashimoto | 76b0cff | 2014-12-09 13:50:23 +0900 | [diff] [blame] | 199 | address_(options.address) { |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 200 | // This is safe to call multiple times. |
| 201 | dbus_threads_init_default(); |
satorux@chromium.org | cff0949 | 2011-09-09 07:28:42 +0900 | [diff] [blame] | 202 | // The origin message loop is unnecessary if the client uses synchronous |
| 203 | // functions only. |
xhwang@chromium.org | dff6b13 | 2013-05-02 01:10:30 +0900 | [diff] [blame] | 204 | if (base::MessageLoop::current()) |
skyostil | e5a8dc4 | 2015-06-18 00:46:04 +0900 | [diff] [blame] | 205 | origin_task_runner_ = base::MessageLoop::current()->task_runner(); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | Bus::~Bus() { |
| 209 | DCHECK(!connection_); |
| 210 | DCHECK(owned_service_names_.empty()); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 211 | DCHECK(match_rules_added_.empty()); |
| 212 | DCHECK(filter_functions_added_.empty()); |
| 213 | DCHECK(registered_object_paths_.empty()); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 214 | DCHECK_EQ(0, num_pending_watches_); |
jamescook@chromium.org | 255cd35 | 2011-11-24 16:00:43 +0900 | [diff] [blame] | 215 | // TODO(satorux): This check fails occasionally in browser_tests for tests |
| 216 | // that run very quickly. Perhaps something does not have time to clean up. |
| 217 | // Despite the check failing, the tests seem to run fine. crosbug.com/23416 |
| 218 | // DCHECK_EQ(0, num_pending_timeouts_); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | ObjectProxy* Bus::GetObjectProxy(const std::string& service_name, |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 222 | const ObjectPath& object_path) { |
adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 223 | return GetObjectProxyWithOptions(service_name, object_path, |
| 224 | ObjectProxy::DEFAULT_OPTIONS); |
| 225 | } |
| 226 | |
| 227 | ObjectProxy* Bus::GetObjectProxyWithOptions(const std::string& service_name, |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 228 | const ObjectPath& object_path, |
adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 229 | int options) { |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 230 | AssertOnOriginThread(); |
| 231 | |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 232 | // Check if we already have the requested object proxy. |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 233 | const ObjectProxyTable::key_type key(service_name + object_path.value(), |
| 234 | options); |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 235 | ObjectProxyTable::iterator iter = object_proxy_table_.find(key); |
| 236 | if (iter != object_proxy_table_.end()) { |
rsleevi@chromium.org | c5cb859 | 2013-06-03 08:38:09 +0900 | [diff] [blame] | 237 | return iter->second.get(); |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 238 | } |
| 239 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 240 | scoped_refptr<ObjectProxy> object_proxy = |
adamk@chromium.org | 35c0eef | 2012-02-11 06:45:23 +0900 | [diff] [blame] | 241 | new ObjectProxy(this, service_name, object_path, options); |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 242 | object_proxy_table_[key] = object_proxy; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 243 | |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 244 | return object_proxy.get(); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 245 | } |
| 246 | |
deymo@chromium.org | 6d168a7 | 2013-01-30 05:29:12 +0900 | [diff] [blame] | 247 | bool Bus::RemoveObjectProxy(const std::string& service_name, |
| 248 | const ObjectPath& object_path, |
| 249 | const base::Closure& callback) { |
| 250 | return RemoveObjectProxyWithOptions(service_name, object_path, |
| 251 | ObjectProxy::DEFAULT_OPTIONS, |
| 252 | callback); |
| 253 | } |
| 254 | |
| 255 | bool Bus::RemoveObjectProxyWithOptions(const std::string& service_name, |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 256 | const ObjectPath& object_path, |
deymo@chromium.org | 6d168a7 | 2013-01-30 05:29:12 +0900 | [diff] [blame] | 257 | int options, |
| 258 | const base::Closure& callback) { |
| 259 | AssertOnOriginThread(); |
| 260 | |
| 261 | // Check if we have the requested object proxy. |
| 262 | const ObjectProxyTable::key_type key(service_name + object_path.value(), |
| 263 | options); |
| 264 | ObjectProxyTable::iterator iter = object_proxy_table_.find(key); |
| 265 | if (iter != object_proxy_table_.end()) { |
stevenjb@chromium.org | b53cfb3 | 2013-10-08 07:56:57 +0900 | [diff] [blame] | 266 | scoped_refptr<ObjectProxy> object_proxy = iter->second; |
| 267 | object_proxy_table_.erase(iter); |
armansito | f436464 | 2014-09-06 02:49:34 +0900 | [diff] [blame] | 268 | // Object is present. Remove it now and Detach on the DBus thread. |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 269 | GetDBusTaskRunner()->PostTask( |
| 270 | FROM_HERE, |
| 271 | base::Bind(&Bus::RemoveObjectProxyInternal, |
stevenjb@chromium.org | b53cfb3 | 2013-10-08 07:56:57 +0900 | [diff] [blame] | 272 | this, object_proxy, callback)); |
deymo@chromium.org | 6d168a7 | 2013-01-30 05:29:12 +0900 | [diff] [blame] | 273 | return true; |
| 274 | } |
| 275 | return false; |
| 276 | } |
| 277 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 278 | void Bus::RemoveObjectProxyInternal(scoped_refptr<ObjectProxy> object_proxy, |
| 279 | const base::Closure& callback) { |
deymo@chromium.org | 6d168a7 | 2013-01-30 05:29:12 +0900 | [diff] [blame] | 280 | AssertOnDBusThread(); |
| 281 | |
| 282 | object_proxy.get()->Detach(); |
| 283 | |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 284 | GetOriginTaskRunner()->PostTask(FROM_HERE, callback); |
deymo@chromium.org | 6d168a7 | 2013-01-30 05:29:12 +0900 | [diff] [blame] | 285 | } |
| 286 | |
keybuk@chromium.org | 9cb73f0 | 2012-03-10 10:12:52 +0900 | [diff] [blame] | 287 | ExportedObject* Bus::GetExportedObject(const ObjectPath& object_path) { |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 288 | AssertOnOriginThread(); |
| 289 | |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 290 | // Check if we already have the requested exported object. |
keybuk@chromium.org | 9cb73f0 | 2012-03-10 10:12:52 +0900 | [diff] [blame] | 291 | ExportedObjectTable::iterator iter = exported_object_table_.find(object_path); |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 292 | if (iter != exported_object_table_.end()) { |
rsleevi@chromium.org | c5cb859 | 2013-06-03 08:38:09 +0900 | [diff] [blame] | 293 | return iter->second.get(); |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 294 | } |
| 295 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 296 | scoped_refptr<ExportedObject> exported_object = |
keybuk@chromium.org | 9cb73f0 | 2012-03-10 10:12:52 +0900 | [diff] [blame] | 297 | new ExportedObject(this, object_path); |
| 298 | exported_object_table_[object_path] = exported_object; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 299 | |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 300 | return exported_object.get(); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 301 | } |
| 302 | |
keybuk@chromium.org | d2ca8f3 | 2012-03-14 10:18:35 +0900 | [diff] [blame] | 303 | void Bus::UnregisterExportedObject(const ObjectPath& object_path) { |
| 304 | AssertOnOriginThread(); |
| 305 | |
| 306 | // Remove the registered object from the table first, to allow a new |
| 307 | // GetExportedObject() call to return a new object, rather than this one. |
| 308 | ExportedObjectTable::iterator iter = exported_object_table_.find(object_path); |
| 309 | if (iter == exported_object_table_.end()) |
| 310 | return; |
| 311 | |
| 312 | scoped_refptr<ExportedObject> exported_object = iter->second; |
| 313 | exported_object_table_.erase(iter); |
| 314 | |
| 315 | // Post the task to perform the final unregistration to the D-Bus thread. |
| 316 | // Since the registration also happens on the D-Bus thread in |
thestig@chromium.org | 074b1db | 2013-02-20 10:36:53 +0900 | [diff] [blame] | 317 | // TryRegisterObjectPath(), and the task runner we post to is a |
| 318 | // SequencedTaskRunner, there is a guarantee that this will happen before any |
| 319 | // future registration call. |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 320 | GetDBusTaskRunner()->PostTask( |
| 321 | FROM_HERE, |
| 322 | base::Bind(&Bus::UnregisterExportedObjectInternal, |
| 323 | this, exported_object)); |
keybuk@chromium.org | d2ca8f3 | 2012-03-14 10:18:35 +0900 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | void Bus::UnregisterExportedObjectInternal( |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 327 | scoped_refptr<ExportedObject> exported_object) { |
keybuk@chromium.org | d2ca8f3 | 2012-03-14 10:18:35 +0900 | [diff] [blame] | 328 | AssertOnDBusThread(); |
| 329 | |
| 330 | exported_object->Unregister(); |
| 331 | } |
| 332 | |
keybuk@chromium.org | 0971501 | 2013-03-26 03:20:08 +0900 | [diff] [blame] | 333 | ObjectManager* Bus::GetObjectManager(const std::string& service_name, |
| 334 | const ObjectPath& object_path) { |
| 335 | AssertOnOriginThread(); |
| 336 | |
| 337 | // Check if we already have the requested object manager. |
| 338 | const ObjectManagerTable::key_type key(service_name + object_path.value()); |
| 339 | ObjectManagerTable::iterator iter = object_manager_table_.find(key); |
| 340 | if (iter != object_manager_table_.end()) { |
rsleevi@chromium.org | c5cb859 | 2013-06-03 08:38:09 +0900 | [diff] [blame] | 341 | return iter->second.get(); |
keybuk@chromium.org | 0971501 | 2013-03-26 03:20:08 +0900 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | scoped_refptr<ObjectManager> object_manager = |
| 345 | new ObjectManager(this, service_name, object_path); |
| 346 | object_manager_table_[key] = object_manager; |
| 347 | |
| 348 | return object_manager.get(); |
| 349 | } |
| 350 | |
armansito | f436464 | 2014-09-06 02:49:34 +0900 | [diff] [blame] | 351 | bool Bus::RemoveObjectManager(const std::string& service_name, |
| 352 | const ObjectPath& object_path, |
| 353 | const base::Closure& callback) { |
keybuk@chromium.org | 0971501 | 2013-03-26 03:20:08 +0900 | [diff] [blame] | 354 | AssertOnOriginThread(); |
armansito | f436464 | 2014-09-06 02:49:34 +0900 | [diff] [blame] | 355 | DCHECK(!callback.is_null()); |
keybuk@chromium.org | 0971501 | 2013-03-26 03:20:08 +0900 | [diff] [blame] | 356 | |
| 357 | const ObjectManagerTable::key_type key(service_name + object_path.value()); |
| 358 | ObjectManagerTable::iterator iter = object_manager_table_.find(key); |
| 359 | if (iter == object_manager_table_.end()) |
armansito | f436464 | 2014-09-06 02:49:34 +0900 | [diff] [blame] | 360 | return false; |
keybuk@chromium.org | 0971501 | 2013-03-26 03:20:08 +0900 | [diff] [blame] | 361 | |
armansito | f436464 | 2014-09-06 02:49:34 +0900 | [diff] [blame] | 362 | // ObjectManager is present. Remove it now and CleanUp on the DBus thread. |
keybuk@chromium.org | 0971501 | 2013-03-26 03:20:08 +0900 | [diff] [blame] | 363 | scoped_refptr<ObjectManager> object_manager = iter->second; |
| 364 | object_manager_table_.erase(iter); |
armansito | f436464 | 2014-09-06 02:49:34 +0900 | [diff] [blame] | 365 | |
| 366 | GetDBusTaskRunner()->PostTask( |
| 367 | FROM_HERE, |
| 368 | base::Bind(&Bus::RemoveObjectManagerInternal, |
| 369 | this, object_manager, callback)); |
| 370 | |
| 371 | return true; |
| 372 | } |
| 373 | |
| 374 | void Bus::RemoveObjectManagerInternal( |
| 375 | scoped_refptr<dbus::ObjectManager> object_manager, |
| 376 | const base::Closure& callback) { |
| 377 | AssertOnDBusThread(); |
| 378 | DCHECK(object_manager.get()); |
| 379 | |
| 380 | object_manager->CleanUp(); |
| 381 | |
| 382 | // The ObjectManager has to be deleted on the origin thread since it was |
| 383 | // created there. |
| 384 | GetOriginTaskRunner()->PostTask( |
| 385 | FROM_HERE, |
| 386 | base::Bind(&Bus::RemoveObjectManagerInternalHelper, |
| 387 | this, object_manager, callback)); |
| 388 | } |
| 389 | |
| 390 | void Bus::RemoveObjectManagerInternalHelper( |
| 391 | scoped_refptr<dbus::ObjectManager> object_manager, |
| 392 | const base::Closure& callback) { |
| 393 | AssertOnOriginThread(); |
| 394 | DCHECK(object_manager.get()); |
| 395 | |
| 396 | // Release the object manager and run the callback. |
| 397 | object_manager = NULL; |
| 398 | callback.Run(); |
keybuk@chromium.org | 0971501 | 2013-03-26 03:20:08 +0900 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | void Bus::GetManagedObjects() { |
| 402 | for (ObjectManagerTable::iterator iter = object_manager_table_.begin(); |
| 403 | iter != object_manager_table_.end(); ++iter) { |
| 404 | iter->second->GetManagedObjects(); |
| 405 | } |
| 406 | } |
| 407 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 408 | bool Bus::Connect() { |
| 409 | // dbus_bus_get_private() and dbus_bus_get() are blocking calls. |
| 410 | AssertOnDBusThread(); |
| 411 | |
| 412 | // Check if it's already initialized. |
| 413 | if (connection_) |
| 414 | return true; |
| 415 | |
| 416 | ScopedDBusError error; |
nona@chromium.org | 9f638e0 | 2012-04-19 12:20:03 +0900 | [diff] [blame] | 417 | if (bus_type_ == CUSTOM_ADDRESS) { |
| 418 | if (connection_type_ == PRIVATE) { |
| 419 | connection_ = dbus_connection_open_private(address_.c_str(), error.get()); |
| 420 | } else { |
| 421 | connection_ = dbus_connection_open(address_.c_str(), error.get()); |
| 422 | } |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 423 | } else { |
nona@chromium.org | 9f638e0 | 2012-04-19 12:20:03 +0900 | [diff] [blame] | 424 | const DBusBusType dbus_bus_type = static_cast<DBusBusType>(bus_type_); |
| 425 | if (connection_type_ == PRIVATE) { |
| 426 | connection_ = dbus_bus_get_private(dbus_bus_type, error.get()); |
| 427 | } else { |
| 428 | connection_ = dbus_bus_get(dbus_bus_type, error.get()); |
| 429 | } |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 430 | } |
| 431 | if (!connection_) { |
| 432 | LOG(ERROR) << "Failed to connect to the bus: " |
tfarina@chromium.org | 56cc3fc | 2012-10-30 01:43:26 +0900 | [diff] [blame] | 433 | << (error.is_set() ? error.message() : ""); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 434 | return false; |
| 435 | } |
nona@chromium.org | eeaf241 | 2012-11-02 17:04:14 +0900 | [diff] [blame] | 436 | |
| 437 | if (bus_type_ == CUSTOM_ADDRESS) { |
| 438 | // We should call dbus_bus_register here, otherwise unique name can not be |
| 439 | // acquired. According to dbus specification, it is responsible to call |
| 440 | // org.freedesktop.DBus.Hello method at the beging of bus connection to |
| 441 | // acquire unique name. In the case of dbus_bus_get, dbus_bus_register is |
| 442 | // called internally. |
| 443 | if (!dbus_bus_register(connection_, error.get())) { |
| 444 | LOG(ERROR) << "Failed to register the bus component: " |
| 445 | << (error.is_set() ? error.message() : ""); |
| 446 | return false; |
| 447 | } |
| 448 | } |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 449 | // We shouldn't exit on the disconnected signal. |
| 450 | dbus_connection_set_exit_on_disconnect(connection_, false); |
| 451 | |
nona@chromium.org | 5a44d2b | 2013-02-08 19:53:39 +0900 | [diff] [blame] | 452 | // Watch Disconnected signal. |
| 453 | AddFilterFunction(Bus::OnConnectionDisconnectedFilter, this); |
| 454 | AddMatch(kDisconnectedMatchRule, error.get()); |
| 455 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 456 | return true; |
| 457 | } |
| 458 | |
nona@chromium.org | 1de76fd | 2013-02-16 01:44:40 +0900 | [diff] [blame] | 459 | void Bus::ClosePrivateConnection() { |
| 460 | // dbus_connection_close is blocking call. |
| 461 | AssertOnDBusThread(); |
| 462 | DCHECK_EQ(PRIVATE, connection_type_) |
| 463 | << "non-private connection should not be closed"; |
| 464 | dbus_connection_close(connection_); |
| 465 | } |
| 466 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 467 | void Bus::ShutdownAndBlock() { |
| 468 | AssertOnDBusThread(); |
| 469 | |
nona@chromium.org | 5a44d2b | 2013-02-08 19:53:39 +0900 | [diff] [blame] | 470 | if (shutdown_completed_) |
| 471 | return; // Already shutdowned, just return. |
| 472 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 473 | // Unregister the exported objects. |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 474 | for (ExportedObjectTable::iterator iter = exported_object_table_.begin(); |
| 475 | iter != exported_object_table_.end(); ++iter) { |
| 476 | iter->second->Unregister(); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | // Release all service names. |
| 480 | for (std::set<std::string>::iterator iter = owned_service_names_.begin(); |
| 481 | iter != owned_service_names_.end();) { |
| 482 | // This is a bit tricky but we should increment the iter here as |
| 483 | // ReleaseOwnership() may remove |service_name| from the set. |
| 484 | const std::string& service_name = *iter++; |
| 485 | ReleaseOwnership(service_name); |
| 486 | } |
| 487 | if (!owned_service_names_.empty()) { |
| 488 | LOG(ERROR) << "Failed to release all service names. # of services left: " |
| 489 | << owned_service_names_.size(); |
| 490 | } |
| 491 | |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 492 | // Detach from the remote objects. |
satorux@chromium.org | dccbb7b | 2011-08-24 04:25:20 +0900 | [diff] [blame] | 493 | for (ObjectProxyTable::iterator iter = object_proxy_table_.begin(); |
| 494 | iter != object_proxy_table_.end(); ++iter) { |
| 495 | iter->second->Detach(); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 496 | } |
| 497 | |
armansito | f436464 | 2014-09-06 02:49:34 +0900 | [diff] [blame] | 498 | // Clean up the object managers. |
| 499 | for (ObjectManagerTable::iterator iter = object_manager_table_.begin(); |
| 500 | iter != object_manager_table_.end(); ++iter) { |
| 501 | iter->second->CleanUp(); |
| 502 | } |
| 503 | |
satorux@chromium.org | f06eb89 | 2011-10-13 09:45:26 +0900 | [diff] [blame] | 504 | // Release object proxies and exported objects here. We should do this |
| 505 | // here rather than in the destructor to avoid memory leaks due to |
| 506 | // cyclic references. |
| 507 | object_proxy_table_.clear(); |
| 508 | exported_object_table_.clear(); |
| 509 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 510 | // Private connection should be closed. |
satorux@chromium.org | d336d45 | 2011-09-02 15:56:23 +0900 | [diff] [blame] | 511 | if (connection_) { |
nona@chromium.org | 5a44d2b | 2013-02-08 19:53:39 +0900 | [diff] [blame] | 512 | // Remove Disconnected watcher. |
| 513 | ScopedDBusError error; |
| 514 | RemoveFilterFunction(Bus::OnConnectionDisconnectedFilter, this); |
| 515 | RemoveMatch(kDisconnectedMatchRule, error.get()); |
| 516 | |
satorux@chromium.org | d336d45 | 2011-09-02 15:56:23 +0900 | [diff] [blame] | 517 | if (connection_type_ == PRIVATE) |
nona@chromium.org | 1de76fd | 2013-02-16 01:44:40 +0900 | [diff] [blame] | 518 | ClosePrivateConnection(); |
satorux@chromium.org | d336d45 | 2011-09-02 15:56:23 +0900 | [diff] [blame] | 519 | // dbus_connection_close() won't unref. |
| 520 | dbus_connection_unref(connection_); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 521 | } |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 522 | |
| 523 | connection_ = NULL; |
satorux@chromium.org | d336d45 | 2011-09-02 15:56:23 +0900 | [diff] [blame] | 524 | shutdown_completed_ = true; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 525 | } |
| 526 | |
satorux@chromium.org | d336d45 | 2011-09-02 15:56:23 +0900 | [diff] [blame] | 527 | void Bus::ShutdownOnDBusThreadAndBlock() { |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 528 | AssertOnOriginThread(); |
thestig@chromium.org | 074b1db | 2013-02-20 10:36:53 +0900 | [diff] [blame] | 529 | DCHECK(dbus_task_runner_.get()); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 530 | |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 531 | GetDBusTaskRunner()->PostTask( |
| 532 | FROM_HERE, |
| 533 | base::Bind(&Bus::ShutdownOnDBusThreadAndBlockInternal, this)); |
satorux@chromium.org | d336d45 | 2011-09-02 15:56:23 +0900 | [diff] [blame] | 534 | |
jam@chromium.org | 1755434 | 2012-04-27 04:08:58 +0900 | [diff] [blame] | 535 | // http://crbug.com/125222 |
| 536 | base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| 537 | |
satorux@chromium.org | d336d45 | 2011-09-02 15:56:23 +0900 | [diff] [blame] | 538 | // Wait until the shutdown is complete on the D-Bus thread. |
| 539 | // The shutdown should not hang, but set timeout just in case. |
| 540 | const int kTimeoutSecs = 3; |
| 541 | const base::TimeDelta timeout(base::TimeDelta::FromSeconds(kTimeoutSecs)); |
| 542 | const bool signaled = on_shutdown_.TimedWait(timeout); |
| 543 | LOG_IF(ERROR, !signaled) << "Failed to shutdown the bus"; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 544 | } |
| 545 | |
keybuk@chromium.org | 9cb73f0 | 2012-03-10 10:12:52 +0900 | [diff] [blame] | 546 | void Bus::RequestOwnership(const std::string& service_name, |
cmasone@chromium.org | 989857e | 2013-07-31 15:34:59 +0900 | [diff] [blame] | 547 | ServiceOwnershipOptions options, |
keybuk@chromium.org | 9cb73f0 | 2012-03-10 10:12:52 +0900 | [diff] [blame] | 548 | OnOwnershipCallback on_ownership_callback) { |
| 549 | AssertOnOriginThread(); |
| 550 | |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 551 | GetDBusTaskRunner()->PostTask( |
| 552 | FROM_HERE, |
| 553 | base::Bind(&Bus::RequestOwnershipInternal, |
| 554 | this, service_name, options, on_ownership_callback)); |
keybuk@chromium.org | 9cb73f0 | 2012-03-10 10:12:52 +0900 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | void Bus::RequestOwnershipInternal(const std::string& service_name, |
cmasone@chromium.org | 989857e | 2013-07-31 15:34:59 +0900 | [diff] [blame] | 558 | ServiceOwnershipOptions options, |
keybuk@chromium.org | 9cb73f0 | 2012-03-10 10:12:52 +0900 | [diff] [blame] | 559 | OnOwnershipCallback on_ownership_callback) { |
| 560 | AssertOnDBusThread(); |
| 561 | |
| 562 | bool success = Connect(); |
| 563 | if (success) |
cmasone@chromium.org | 989857e | 2013-07-31 15:34:59 +0900 | [diff] [blame] | 564 | success = RequestOwnershipAndBlock(service_name, options); |
keybuk@chromium.org | 9cb73f0 | 2012-03-10 10:12:52 +0900 | [diff] [blame] | 565 | |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 566 | GetOriginTaskRunner()->PostTask(FROM_HERE, |
| 567 | base::Bind(on_ownership_callback, |
| 568 | service_name, |
| 569 | success)); |
keybuk@chromium.org | 9cb73f0 | 2012-03-10 10:12:52 +0900 | [diff] [blame] | 570 | } |
| 571 | |
cmasone@chromium.org | 989857e | 2013-07-31 15:34:59 +0900 | [diff] [blame] | 572 | bool Bus::RequestOwnershipAndBlock(const std::string& service_name, |
| 573 | ServiceOwnershipOptions options) { |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 574 | DCHECK(connection_); |
| 575 | // dbus_bus_request_name() is a blocking call. |
| 576 | AssertOnDBusThread(); |
| 577 | |
| 578 | // Check if we already own the service name. |
| 579 | if (owned_service_names_.find(service_name) != owned_service_names_.end()) { |
| 580 | return true; |
| 581 | } |
| 582 | |
| 583 | ScopedDBusError error; |
| 584 | const int result = dbus_bus_request_name(connection_, |
| 585 | service_name.c_str(), |
cmasone@chromium.org | 989857e | 2013-07-31 15:34:59 +0900 | [diff] [blame] | 586 | options, |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 587 | error.get()); |
| 588 | if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) { |
satorux@chromium.org | c6ac757 | 2011-09-01 03:02:43 +0900 | [diff] [blame] | 589 | LOG(ERROR) << "Failed to get the ownership of " << service_name << ": " |
tfarina@chromium.org | 56cc3fc | 2012-10-30 01:43:26 +0900 | [diff] [blame] | 590 | << (error.is_set() ? error.message() : ""); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 591 | return false; |
| 592 | } |
| 593 | owned_service_names_.insert(service_name); |
| 594 | return true; |
| 595 | } |
| 596 | |
| 597 | bool Bus::ReleaseOwnership(const std::string& service_name) { |
| 598 | DCHECK(connection_); |
| 599 | // dbus_bus_request_name() is a blocking call. |
| 600 | AssertOnDBusThread(); |
| 601 | |
| 602 | // Check if we already own the service name. |
| 603 | std::set<std::string>::iterator found = |
| 604 | owned_service_names_.find(service_name); |
| 605 | if (found == owned_service_names_.end()) { |
| 606 | LOG(ERROR) << service_name << " is not owned by the bus"; |
| 607 | return false; |
| 608 | } |
| 609 | |
| 610 | ScopedDBusError error; |
| 611 | const int result = dbus_bus_release_name(connection_, service_name.c_str(), |
| 612 | error.get()); |
| 613 | if (result == DBUS_RELEASE_NAME_REPLY_RELEASED) { |
| 614 | owned_service_names_.erase(found); |
| 615 | return true; |
| 616 | } else { |
satorux@chromium.org | c6ac757 | 2011-09-01 03:02:43 +0900 | [diff] [blame] | 617 | LOG(ERROR) << "Failed to release the ownership of " << service_name << ": " |
cmasone@chromium.org | 989857e | 2013-07-31 15:34:59 +0900 | [diff] [blame] | 618 | << (error.is_set() ? error.message() : "") |
| 619 | << ", result code: " << result; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 620 | return false; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | bool Bus::SetUpAsyncOperations() { |
| 625 | DCHECK(connection_); |
| 626 | AssertOnDBusThread(); |
| 627 | |
satorux@chromium.org | 326a6f8 | 2011-08-27 16:26:34 +0900 | [diff] [blame] | 628 | if (async_operations_set_up_) |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 629 | return true; |
| 630 | |
| 631 | // Process all the incoming data if any, so that OnDispatchStatus() will |
| 632 | // be called when the incoming data is ready. |
| 633 | ProcessAllIncomingDataIfAny(); |
| 634 | |
| 635 | bool success = dbus_connection_set_watch_functions(connection_, |
| 636 | &Bus::OnAddWatchThunk, |
| 637 | &Bus::OnRemoveWatchThunk, |
| 638 | &Bus::OnToggleWatchThunk, |
| 639 | this, |
| 640 | NULL); |
| 641 | CHECK(success) << "Unable to allocate memory"; |
| 642 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 643 | success = dbus_connection_set_timeout_functions(connection_, |
| 644 | &Bus::OnAddTimeoutThunk, |
| 645 | &Bus::OnRemoveTimeoutThunk, |
| 646 | &Bus::OnToggleTimeoutThunk, |
| 647 | this, |
| 648 | NULL); |
| 649 | CHECK(success) << "Unable to allocate memory"; |
| 650 | |
| 651 | dbus_connection_set_dispatch_status_function( |
| 652 | connection_, |
| 653 | &Bus::OnDispatchStatusChangedThunk, |
| 654 | this, |
| 655 | NULL); |
| 656 | |
satorux@chromium.org | 326a6f8 | 2011-08-27 16:26:34 +0900 | [diff] [blame] | 657 | async_operations_set_up_ = true; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 658 | |
| 659 | return true; |
| 660 | } |
| 661 | |
| 662 | DBusMessage* Bus::SendWithReplyAndBlock(DBusMessage* request, |
| 663 | int timeout_ms, |
| 664 | DBusError* error) { |
| 665 | DCHECK(connection_); |
| 666 | AssertOnDBusThread(); |
| 667 | |
| 668 | return dbus_connection_send_with_reply_and_block( |
| 669 | connection_, request, timeout_ms, error); |
| 670 | } |
| 671 | |
| 672 | void Bus::SendWithReply(DBusMessage* request, |
| 673 | DBusPendingCall** pending_call, |
| 674 | int timeout_ms) { |
| 675 | DCHECK(connection_); |
| 676 | AssertOnDBusThread(); |
| 677 | |
| 678 | const bool success = dbus_connection_send_with_reply( |
| 679 | connection_, request, pending_call, timeout_ms); |
| 680 | CHECK(success) << "Unable to allocate memory"; |
| 681 | } |
| 682 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 683 | void Bus::Send(DBusMessage* request, uint32_t* serial) { |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 684 | DCHECK(connection_); |
| 685 | AssertOnDBusThread(); |
| 686 | |
| 687 | const bool success = dbus_connection_send(connection_, request, serial); |
| 688 | CHECK(success) << "Unable to allocate memory"; |
| 689 | } |
| 690 | |
hashimoto | 46be6e9 | 2014-12-04 16:41:55 +0900 | [diff] [blame] | 691 | void Bus::AddFilterFunction(DBusHandleMessageFunction filter_function, |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 692 | void* user_data) { |
| 693 | DCHECK(connection_); |
| 694 | AssertOnDBusThread(); |
| 695 | |
satorux@chromium.org | 66bc4c2 | 2011-10-06 09:20:53 +0900 | [diff] [blame] | 696 | std::pair<DBusHandleMessageFunction, void*> filter_data_pair = |
| 697 | std::make_pair(filter_function, user_data); |
| 698 | if (filter_functions_added_.find(filter_data_pair) != |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 699 | filter_functions_added_.end()) { |
satorux@chromium.org | 66bc4c2 | 2011-10-06 09:20:53 +0900 | [diff] [blame] | 700 | VLOG(1) << "Filter function already exists: " << filter_function |
| 701 | << " with associated data: " << user_data; |
hashimoto | 46be6e9 | 2014-12-04 16:41:55 +0900 | [diff] [blame] | 702 | return; |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | const bool success = dbus_connection_add_filter( |
| 706 | connection_, filter_function, user_data, NULL); |
| 707 | CHECK(success) << "Unable to allocate memory"; |
satorux@chromium.org | 66bc4c2 | 2011-10-06 09:20:53 +0900 | [diff] [blame] | 708 | filter_functions_added_.insert(filter_data_pair); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 709 | } |
| 710 | |
hashimoto | 46be6e9 | 2014-12-04 16:41:55 +0900 | [diff] [blame] | 711 | void Bus::RemoveFilterFunction(DBusHandleMessageFunction filter_function, |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 712 | void* user_data) { |
| 713 | DCHECK(connection_); |
| 714 | AssertOnDBusThread(); |
| 715 | |
satorux@chromium.org | 66bc4c2 | 2011-10-06 09:20:53 +0900 | [diff] [blame] | 716 | std::pair<DBusHandleMessageFunction, void*> filter_data_pair = |
| 717 | std::make_pair(filter_function, user_data); |
| 718 | if (filter_functions_added_.find(filter_data_pair) == |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 719 | filter_functions_added_.end()) { |
satorux@chromium.org | 66bc4c2 | 2011-10-06 09:20:53 +0900 | [diff] [blame] | 720 | VLOG(1) << "Requested to remove an unknown filter function: " |
| 721 | << filter_function |
| 722 | << " with associated data: " << user_data; |
hashimoto | 46be6e9 | 2014-12-04 16:41:55 +0900 | [diff] [blame] | 723 | return; |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | dbus_connection_remove_filter(connection_, filter_function, user_data); |
satorux@chromium.org | 66bc4c2 | 2011-10-06 09:20:53 +0900 | [diff] [blame] | 727 | filter_functions_added_.erase(filter_data_pair); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | void Bus::AddMatch(const std::string& match_rule, DBusError* error) { |
| 731 | DCHECK(connection_); |
| 732 | AssertOnDBusThread(); |
| 733 | |
deymo@chromium.org | 7894ebf | 2013-01-31 15:08:02 +0900 | [diff] [blame] | 734 | std::map<std::string, int>::iterator iter = |
| 735 | match_rules_added_.find(match_rule); |
| 736 | if (iter != match_rules_added_.end()) { |
| 737 | // The already existing rule's counter is incremented. |
| 738 | iter->second++; |
| 739 | |
satorux@chromium.org | a3a9793 | 2011-10-13 08:47:13 +0900 | [diff] [blame] | 740 | VLOG(1) << "Match rule already exists: " << match_rule; |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 741 | return; |
| 742 | } |
| 743 | |
| 744 | dbus_bus_add_match(connection_, match_rule.c_str(), error); |
deymo@chromium.org | 7894ebf | 2013-01-31 15:08:02 +0900 | [diff] [blame] | 745 | match_rules_added_[match_rule] = 1; |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 746 | } |
| 747 | |
deymo@chromium.org | 7894ebf | 2013-01-31 15:08:02 +0900 | [diff] [blame] | 748 | bool Bus::RemoveMatch(const std::string& match_rule, DBusError* error) { |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 749 | DCHECK(connection_); |
| 750 | AssertOnDBusThread(); |
| 751 | |
deymo@chromium.org | 7894ebf | 2013-01-31 15:08:02 +0900 | [diff] [blame] | 752 | std::map<std::string, int>::iterator iter = |
| 753 | match_rules_added_.find(match_rule); |
| 754 | if (iter == match_rules_added_.end()) { |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 755 | LOG(ERROR) << "Requested to remove an unknown match rule: " << match_rule; |
deymo@chromium.org | 7894ebf | 2013-01-31 15:08:02 +0900 | [diff] [blame] | 756 | return false; |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 757 | } |
| 758 | |
deymo@chromium.org | 7894ebf | 2013-01-31 15:08:02 +0900 | [diff] [blame] | 759 | // The rule's counter is decremented and the rule is deleted when reachs 0. |
| 760 | iter->second--; |
| 761 | if (iter->second == 0) { |
| 762 | dbus_bus_remove_match(connection_, match_rule.c_str(), error); |
| 763 | match_rules_added_.erase(match_rule); |
| 764 | } |
| 765 | return true; |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 766 | } |
| 767 | |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 768 | bool Bus::TryRegisterObjectPath(const ObjectPath& object_path, |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 769 | const DBusObjectPathVTable* vtable, |
| 770 | void* user_data, |
| 771 | DBusError* error) { |
| 772 | DCHECK(connection_); |
| 773 | AssertOnDBusThread(); |
| 774 | |
satorux@chromium.org | 326a6f8 | 2011-08-27 16:26:34 +0900 | [diff] [blame] | 775 | if (registered_object_paths_.find(object_path) != |
| 776 | registered_object_paths_.end()) { |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 777 | LOG(ERROR) << "Object path already registered: " << object_path.value(); |
satorux@chromium.org | 326a6f8 | 2011-08-27 16:26:34 +0900 | [diff] [blame] | 778 | return false; |
| 779 | } |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 780 | |
| 781 | const bool success = dbus_connection_try_register_object_path( |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 782 | connection_, |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 783 | object_path.value().c_str(), |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 784 | vtable, |
| 785 | user_data, |
| 786 | error); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 787 | if (success) |
| 788 | registered_object_paths_.insert(object_path); |
| 789 | return success; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 790 | } |
| 791 | |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 792 | void Bus::UnregisterObjectPath(const ObjectPath& object_path) { |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 793 | DCHECK(connection_); |
| 794 | AssertOnDBusThread(); |
| 795 | |
satorux@chromium.org | 326a6f8 | 2011-08-27 16:26:34 +0900 | [diff] [blame] | 796 | if (registered_object_paths_.find(object_path) == |
| 797 | registered_object_paths_.end()) { |
| 798 | LOG(ERROR) << "Requested to unregister an unknown object path: " |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 799 | << object_path.value(); |
satorux@chromium.org | 326a6f8 | 2011-08-27 16:26:34 +0900 | [diff] [blame] | 800 | return; |
| 801 | } |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 802 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 803 | const bool success = dbus_connection_unregister_object_path( |
| 804 | connection_, |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 805 | object_path.value().c_str()); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 806 | CHECK(success) << "Unable to allocate memory"; |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 807 | registered_object_paths_.erase(object_path); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 808 | } |
| 809 | |
satorux@chromium.org | d336d45 | 2011-09-02 15:56:23 +0900 | [diff] [blame] | 810 | void Bus::ShutdownOnDBusThreadAndBlockInternal() { |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 811 | AssertOnDBusThread(); |
| 812 | |
| 813 | ShutdownAndBlock(); |
satorux@chromium.org | d336d45 | 2011-09-02 15:56:23 +0900 | [diff] [blame] | 814 | on_shutdown_.Signal(); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | void Bus::ProcessAllIncomingDataIfAny() { |
| 818 | AssertOnDBusThread(); |
| 819 | |
| 820 | // As mentioned at the class comment in .h file, connection_ can be NULL. |
nona@chromium.org | 5a44d2b | 2013-02-08 19:53:39 +0900 | [diff] [blame] | 821 | if (!connection_) |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 822 | return; |
| 823 | |
nona@chromium.org | 5a44d2b | 2013-02-08 19:53:39 +0900 | [diff] [blame] | 824 | // It is safe and necessary to call dbus_connection_get_dispatch_status even |
hashimoto | 76b0cff | 2014-12-09 13:50:23 +0900 | [diff] [blame] | 825 | // if the connection is lost. |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 826 | if (dbus_connection_get_dispatch_status(connection_) == |
| 827 | DBUS_DISPATCH_DATA_REMAINS) { |
| 828 | while (dbus_connection_dispatch(connection_) == |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 829 | DBUS_DISPATCH_DATA_REMAINS) { |
| 830 | } |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 834 | base::TaskRunner* Bus::GetDBusTaskRunner() { |
| 835 | if (dbus_task_runner_.get()) |
| 836 | return dbus_task_runner_.get(); |
| 837 | else |
| 838 | return GetOriginTaskRunner(); |
haruki@chromium.org | 4a1f956 | 2013-05-08 20:57:14 +0900 | [diff] [blame] | 839 | } |
| 840 | |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 841 | base::TaskRunner* Bus::GetOriginTaskRunner() { |
thestig@chromium.org | 074b1db | 2013-02-20 10:36:53 +0900 | [diff] [blame] | 842 | DCHECK(origin_task_runner_.get()); |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 843 | return origin_task_runner_.get(); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | bool Bus::HasDBusThread() { |
thestig@chromium.org | 074b1db | 2013-02-20 10:36:53 +0900 | [diff] [blame] | 847 | return dbus_task_runner_.get() != NULL; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | void Bus::AssertOnOriginThread() { |
| 851 | DCHECK_EQ(origin_thread_id_, base::PlatformThread::CurrentId()); |
| 852 | } |
| 853 | |
| 854 | void Bus::AssertOnDBusThread() { |
| 855 | base::ThreadRestrictions::AssertIOAllowed(); |
| 856 | |
thestig@chromium.org | 074b1db | 2013-02-20 10:36:53 +0900 | [diff] [blame] | 857 | if (dbus_task_runner_.get()) { |
| 858 | DCHECK(dbus_task_runner_->RunsTasksOnCurrentThread()); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 859 | } else { |
| 860 | AssertOnOriginThread(); |
| 861 | } |
| 862 | } |
| 863 | |
thestig@chromium.org | 56057f2 | 2013-05-05 00:48:37 +0900 | [diff] [blame] | 864 | std::string Bus::GetServiceOwnerAndBlock(const std::string& service_name, |
| 865 | GetServiceOwnerOption options) { |
| 866 | AssertOnDBusThread(); |
| 867 | |
| 868 | MethodCall get_name_owner_call("org.freedesktop.DBus", "GetNameOwner"); |
| 869 | MessageWriter writer(&get_name_owner_call); |
| 870 | writer.AppendString(service_name); |
| 871 | VLOG(1) << "Method call: " << get_name_owner_call.ToString(); |
| 872 | |
| 873 | const ObjectPath obj_path("/org/freedesktop/DBus"); |
| 874 | if (!get_name_owner_call.SetDestination("org.freedesktop.DBus") || |
| 875 | !get_name_owner_call.SetPath(obj_path)) { |
| 876 | if (options == REPORT_ERRORS) |
| 877 | LOG(ERROR) << "Failed to get name owner."; |
| 878 | return ""; |
| 879 | } |
| 880 | |
| 881 | ScopedDBusError error; |
| 882 | DBusMessage* response_message = |
| 883 | SendWithReplyAndBlock(get_name_owner_call.raw_message(), |
| 884 | ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 885 | error.get()); |
| 886 | if (!response_message) { |
| 887 | if (options == REPORT_ERRORS) { |
| 888 | LOG(ERROR) << "Failed to get name owner. Got " << error.name() << ": " |
| 889 | << error.message(); |
| 890 | } |
| 891 | return ""; |
| 892 | } |
| 893 | |
| 894 | scoped_ptr<Response> response(Response::FromRawMessage(response_message)); |
| 895 | MessageReader reader(response.get()); |
| 896 | |
| 897 | std::string service_owner; |
| 898 | if (!reader.PopString(&service_owner)) |
| 899 | service_owner.clear(); |
| 900 | return service_owner; |
| 901 | } |
| 902 | |
| 903 | void Bus::GetServiceOwner(const std::string& service_name, |
| 904 | const GetServiceOwnerCallback& callback) { |
| 905 | AssertOnOriginThread(); |
| 906 | |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 907 | GetDBusTaskRunner()->PostTask( |
thestig@chromium.org | 56057f2 | 2013-05-05 00:48:37 +0900 | [diff] [blame] | 908 | FROM_HERE, |
| 909 | base::Bind(&Bus::GetServiceOwnerInternal, this, service_name, callback)); |
| 910 | } |
| 911 | |
| 912 | void Bus::GetServiceOwnerInternal(const std::string& service_name, |
| 913 | const GetServiceOwnerCallback& callback) { |
| 914 | AssertOnDBusThread(); |
| 915 | |
| 916 | std::string service_owner; |
| 917 | if (Connect()) |
thestig@chromium.org | 493b0ea | 2013-05-09 05:47:18 +0900 | [diff] [blame] | 918 | service_owner = GetServiceOwnerAndBlock(service_name, SUPPRESS_ERRORS); |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 919 | GetOriginTaskRunner()->PostTask(FROM_HERE, |
| 920 | base::Bind(callback, service_owner)); |
thestig@chromium.org | 56057f2 | 2013-05-05 00:48:37 +0900 | [diff] [blame] | 921 | } |
| 922 | |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 923 | void Bus::ListenForServiceOwnerChange( |
| 924 | const std::string& service_name, |
| 925 | const GetServiceOwnerCallback& callback) { |
| 926 | AssertOnOriginThread(); |
| 927 | DCHECK(!service_name.empty()); |
| 928 | DCHECK(!callback.is_null()); |
| 929 | |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 930 | GetDBusTaskRunner()->PostTask( |
| 931 | FROM_HERE, |
| 932 | base::Bind(&Bus::ListenForServiceOwnerChangeInternal, |
| 933 | this, service_name, callback)); |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | void Bus::ListenForServiceOwnerChangeInternal( |
| 937 | const std::string& service_name, |
| 938 | const GetServiceOwnerCallback& callback) { |
| 939 | AssertOnDBusThread(); |
| 940 | DCHECK(!service_name.empty()); |
| 941 | DCHECK(!callback.is_null()); |
| 942 | |
| 943 | if (!Connect() || !SetUpAsyncOperations()) |
| 944 | return; |
| 945 | |
hashimoto | 46be6e9 | 2014-12-04 16:41:55 +0900 | [diff] [blame] | 946 | if (service_owner_changed_listener_map_.empty()) |
| 947 | AddFilterFunction(Bus::OnServiceOwnerChangedFilter, this); |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 948 | |
| 949 | ServiceOwnerChangedListenerMap::iterator it = |
| 950 | service_owner_changed_listener_map_.find(service_name); |
| 951 | if (it == service_owner_changed_listener_map_.end()) { |
| 952 | // Add a match rule for the new service name. |
| 953 | const std::string name_owner_changed_match_rule = |
| 954 | base::StringPrintf(kServiceNameOwnerChangeMatchRule, |
| 955 | service_name.c_str()); |
| 956 | ScopedDBusError error; |
| 957 | AddMatch(name_owner_changed_match_rule, error.get()); |
| 958 | if (error.is_set()) { |
| 959 | LOG(ERROR) << "Failed to add match rule for " << service_name |
| 960 | << ". Got " << error.name() << ": " << error.message(); |
| 961 | return; |
| 962 | } |
| 963 | |
| 964 | service_owner_changed_listener_map_[service_name].push_back(callback); |
| 965 | return; |
| 966 | } |
| 967 | |
| 968 | // Check if the callback has already been added. |
| 969 | std::vector<GetServiceOwnerCallback>& callbacks = it->second; |
| 970 | for (size_t i = 0; i < callbacks.size(); ++i) { |
| 971 | if (callbacks[i].Equals(callback)) |
| 972 | return; |
| 973 | } |
| 974 | callbacks.push_back(callback); |
| 975 | } |
| 976 | |
| 977 | void Bus::UnlistenForServiceOwnerChange( |
| 978 | const std::string& service_name, |
| 979 | const GetServiceOwnerCallback& callback) { |
| 980 | AssertOnOriginThread(); |
| 981 | DCHECK(!service_name.empty()); |
| 982 | DCHECK(!callback.is_null()); |
| 983 | |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 984 | GetDBusTaskRunner()->PostTask( |
| 985 | FROM_HERE, |
| 986 | base::Bind(&Bus::UnlistenForServiceOwnerChangeInternal, |
| 987 | this, service_name, callback)); |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | void Bus::UnlistenForServiceOwnerChangeInternal( |
| 991 | const std::string& service_name, |
| 992 | const GetServiceOwnerCallback& callback) { |
| 993 | AssertOnDBusThread(); |
| 994 | DCHECK(!service_name.empty()); |
| 995 | DCHECK(!callback.is_null()); |
| 996 | |
| 997 | ServiceOwnerChangedListenerMap::iterator it = |
| 998 | service_owner_changed_listener_map_.find(service_name); |
| 999 | if (it == service_owner_changed_listener_map_.end()) |
| 1000 | return; |
| 1001 | |
| 1002 | std::vector<GetServiceOwnerCallback>& callbacks = it->second; |
| 1003 | for (size_t i = 0; i < callbacks.size(); ++i) { |
| 1004 | if (callbacks[i].Equals(callback)) { |
| 1005 | callbacks.erase(callbacks.begin() + i); |
| 1006 | break; // There can be only one. |
| 1007 | } |
| 1008 | } |
| 1009 | if (!callbacks.empty()) |
| 1010 | return; |
| 1011 | |
| 1012 | // Last callback for |service_name| has been removed, remove match rule. |
| 1013 | const std::string name_owner_changed_match_rule = |
| 1014 | base::StringPrintf(kServiceNameOwnerChangeMatchRule, |
| 1015 | service_name.c_str()); |
| 1016 | ScopedDBusError error; |
| 1017 | RemoveMatch(name_owner_changed_match_rule, error.get()); |
| 1018 | // And remove |service_owner_changed_listener_map_| entry. |
| 1019 | service_owner_changed_listener_map_.erase(it); |
| 1020 | |
hashimoto | 46be6e9 | 2014-12-04 16:41:55 +0900 | [diff] [blame] | 1021 | if (service_owner_changed_listener_map_.empty()) |
| 1022 | RemoveFilterFunction(Bus::OnServiceOwnerChangedFilter, this); |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 1023 | } |
| 1024 | |
zqiu | a84d25f | 2015-07-08 11:08:30 +0900 | [diff] [blame] | 1025 | std::string Bus::GetConnectionName() { |
| 1026 | if (!connection_) |
| 1027 | return ""; |
| 1028 | return dbus_bus_get_unique_name(connection_); |
| 1029 | } |
| 1030 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 1031 | dbus_bool_t Bus::OnAddWatch(DBusWatch* raw_watch) { |
| 1032 | AssertOnDBusThread(); |
| 1033 | |
| 1034 | // watch will be deleted when raw_watch is removed in OnRemoveWatch(). |
| 1035 | Watch* watch = new Watch(raw_watch); |
| 1036 | if (watch->IsReadyToBeWatched()) { |
| 1037 | watch->StartWatching(); |
| 1038 | } |
| 1039 | ++num_pending_watches_; |
| 1040 | return true; |
| 1041 | } |
| 1042 | |
| 1043 | void Bus::OnRemoveWatch(DBusWatch* raw_watch) { |
| 1044 | AssertOnDBusThread(); |
| 1045 | |
| 1046 | Watch* watch = static_cast<Watch*>(dbus_watch_get_data(raw_watch)); |
| 1047 | delete watch; |
| 1048 | --num_pending_watches_; |
| 1049 | } |
| 1050 | |
| 1051 | void Bus::OnToggleWatch(DBusWatch* raw_watch) { |
| 1052 | AssertOnDBusThread(); |
| 1053 | |
| 1054 | Watch* watch = static_cast<Watch*>(dbus_watch_get_data(raw_watch)); |
| 1055 | if (watch->IsReadyToBeWatched()) { |
| 1056 | watch->StartWatching(); |
| 1057 | } else { |
| 1058 | // It's safe to call this if StartWatching() wasn't called, per |
| 1059 | // message_pump_libevent.h. |
| 1060 | watch->StopWatching(); |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | dbus_bool_t Bus::OnAddTimeout(DBusTimeout* raw_timeout) { |
| 1065 | AssertOnDBusThread(); |
| 1066 | |
| 1067 | // timeout will be deleted when raw_timeout is removed in |
| 1068 | // OnRemoveTimeoutThunk(). |
| 1069 | Timeout* timeout = new Timeout(raw_timeout); |
| 1070 | if (timeout->IsReadyToBeMonitored()) { |
| 1071 | timeout->StartMonitoring(this); |
| 1072 | } |
| 1073 | ++num_pending_timeouts_; |
| 1074 | return true; |
| 1075 | } |
| 1076 | |
| 1077 | void Bus::OnRemoveTimeout(DBusTimeout* raw_timeout) { |
| 1078 | AssertOnDBusThread(); |
| 1079 | |
| 1080 | Timeout* timeout = static_cast<Timeout*>(dbus_timeout_get_data(raw_timeout)); |
| 1081 | timeout->Complete(); |
| 1082 | --num_pending_timeouts_; |
| 1083 | } |
| 1084 | |
| 1085 | void Bus::OnToggleTimeout(DBusTimeout* raw_timeout) { |
| 1086 | AssertOnDBusThread(); |
| 1087 | |
| 1088 | Timeout* timeout = static_cast<Timeout*>(dbus_timeout_get_data(raw_timeout)); |
| 1089 | if (timeout->IsReadyToBeMonitored()) { |
| 1090 | timeout->StartMonitoring(this); |
| 1091 | } else { |
| 1092 | timeout->StopMonitoring(); |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | void Bus::OnDispatchStatusChanged(DBusConnection* connection, |
| 1097 | DBusDispatchStatus status) { |
| 1098 | DCHECK_EQ(connection, connection_); |
| 1099 | AssertOnDBusThread(); |
| 1100 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 1101 | // We cannot call ProcessAllIncomingDataIfAny() here, as calling |
| 1102 | // dbus_connection_dispatch() inside DBusDispatchStatusFunction is |
| 1103 | // prohibited by the D-Bus library. Hence, we post a task here instead. |
| 1104 | // See comments for dbus_connection_set_dispatch_status_function(). |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 1105 | GetDBusTaskRunner()->PostTask(FROM_HERE, |
| 1106 | base::Bind(&Bus::ProcessAllIncomingDataIfAny, |
| 1107 | this)); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 1108 | } |
| 1109 | |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 1110 | void Bus::OnServiceOwnerChanged(DBusMessage* message) { |
| 1111 | DCHECK(message); |
| 1112 | AssertOnDBusThread(); |
| 1113 | |
| 1114 | // |message| will be unrefed on exit of the function. Increment the |
| 1115 | // reference so we can use it in Signal::FromRawMessage() below. |
| 1116 | dbus_message_ref(message); |
| 1117 | scoped_ptr<Signal> signal(Signal::FromRawMessage(message)); |
| 1118 | |
| 1119 | // Confirm the validity of the NameOwnerChanged signal. |
| 1120 | if (signal->GetMember() != kNameOwnerChangedSignal || |
| 1121 | signal->GetInterface() != DBUS_INTERFACE_DBUS || |
| 1122 | signal->GetSender() != DBUS_SERVICE_DBUS) { |
| 1123 | return; |
| 1124 | } |
| 1125 | |
| 1126 | MessageReader reader(signal.get()); |
| 1127 | std::string service_name; |
| 1128 | std::string old_owner; |
| 1129 | std::string new_owner; |
| 1130 | if (!reader.PopString(&service_name) || |
| 1131 | !reader.PopString(&old_owner) || |
| 1132 | !reader.PopString(&new_owner)) { |
| 1133 | return; |
| 1134 | } |
| 1135 | |
| 1136 | ServiceOwnerChangedListenerMap::const_iterator it = |
| 1137 | service_owner_changed_listener_map_.find(service_name); |
| 1138 | if (it == service_owner_changed_listener_map_.end()) |
| 1139 | return; |
| 1140 | |
| 1141 | const std::vector<GetServiceOwnerCallback>& callbacks = it->second; |
| 1142 | for (size_t i = 0; i < callbacks.size(); ++i) { |
hashimoto@chromium.org | 955f648 | 2013-09-26 13:32:29 +0900 | [diff] [blame] | 1143 | GetOriginTaskRunner()->PostTask(FROM_HERE, |
| 1144 | base::Bind(callbacks[i], new_owner)); |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | // static |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 1149 | dbus_bool_t Bus::OnAddWatchThunk(DBusWatch* raw_watch, void* data) { |
| 1150 | Bus* self = static_cast<Bus*>(data); |
| 1151 | return self->OnAddWatch(raw_watch); |
| 1152 | } |
| 1153 | |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 1154 | // static |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 1155 | void Bus::OnRemoveWatchThunk(DBusWatch* raw_watch, void* data) { |
| 1156 | Bus* self = static_cast<Bus*>(data); |
jhawkins@chromium.org | 957fc4c | 2012-04-28 04:40:42 +0900 | [diff] [blame] | 1157 | self->OnRemoveWatch(raw_watch); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 1158 | } |
| 1159 | |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 1160 | // static |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 1161 | void Bus::OnToggleWatchThunk(DBusWatch* raw_watch, void* data) { |
| 1162 | Bus* self = static_cast<Bus*>(data); |
jhawkins@chromium.org | 957fc4c | 2012-04-28 04:40:42 +0900 | [diff] [blame] | 1163 | self->OnToggleWatch(raw_watch); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 1164 | } |
| 1165 | |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 1166 | // static |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 1167 | dbus_bool_t Bus::OnAddTimeoutThunk(DBusTimeout* raw_timeout, void* data) { |
| 1168 | Bus* self = static_cast<Bus*>(data); |
| 1169 | return self->OnAddTimeout(raw_timeout); |
| 1170 | } |
| 1171 | |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 1172 | // static |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 1173 | void Bus::OnRemoveTimeoutThunk(DBusTimeout* raw_timeout, void* data) { |
| 1174 | Bus* self = static_cast<Bus*>(data); |
jhawkins@chromium.org | 957fc4c | 2012-04-28 04:40:42 +0900 | [diff] [blame] | 1175 | self->OnRemoveTimeout(raw_timeout); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 1176 | } |
| 1177 | |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 1178 | // static |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 1179 | void Bus::OnToggleTimeoutThunk(DBusTimeout* raw_timeout, void* data) { |
| 1180 | Bus* self = static_cast<Bus*>(data); |
jhawkins@chromium.org | 957fc4c | 2012-04-28 04:40:42 +0900 | [diff] [blame] | 1181 | self->OnToggleTimeout(raw_timeout); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 1182 | } |
| 1183 | |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 1184 | // static |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 1185 | void Bus::OnDispatchStatusChangedThunk(DBusConnection* connection, |
| 1186 | DBusDispatchStatus status, |
| 1187 | void* data) { |
| 1188 | Bus* self = static_cast<Bus*>(data); |
jhawkins@chromium.org | 957fc4c | 2012-04-28 04:40:42 +0900 | [diff] [blame] | 1189 | self->OnDispatchStatusChanged(connection, status); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 1190 | } |
| 1191 | |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 1192 | // static |
nona@chromium.org | 5a44d2b | 2013-02-08 19:53:39 +0900 | [diff] [blame] | 1193 | DBusHandlerResult Bus::OnConnectionDisconnectedFilter( |
thestig@chromium.org | 074b1db | 2013-02-20 10:36:53 +0900 | [diff] [blame] | 1194 | DBusConnection* connection, |
| 1195 | DBusMessage* message, |
| 1196 | void* data) { |
nona@chromium.org | 5a44d2b | 2013-02-08 19:53:39 +0900 | [diff] [blame] | 1197 | if (dbus_message_is_signal(message, |
| 1198 | DBUS_INTERFACE_LOCAL, |
| 1199 | kDisconnectedSignal)) { |
hashimoto | 76b0cff | 2014-12-09 13:50:23 +0900 | [diff] [blame] | 1200 | // Abort when the connection is lost. |
| 1201 | LOG(FATAL) << "D-Bus connection was disconnected. Aborting."; |
nona@chromium.org | 5a44d2b | 2013-02-08 19:53:39 +0900 | [diff] [blame] | 1202 | } |
| 1203 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 1204 | } |
| 1205 | |
thestig@chromium.org | c2482f1 | 2013-06-11 07:52:34 +0900 | [diff] [blame] | 1206 | // static |
| 1207 | DBusHandlerResult Bus::OnServiceOwnerChangedFilter( |
| 1208 | DBusConnection* connection, |
| 1209 | DBusMessage* message, |
| 1210 | void* data) { |
| 1211 | if (dbus_message_is_signal(message, |
| 1212 | DBUS_INTERFACE_DBUS, |
| 1213 | kNameOwnerChangedSignal)) { |
| 1214 | Bus* self = static_cast<Bus*>(data); |
| 1215 | self->OnServiceOwnerChanged(message); |
| 1216 | } |
| 1217 | // Always return unhandled to let others, e.g. ObjectProxies, handle the same |
| 1218 | // signal. |
| 1219 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 1220 | } |
| 1221 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 1222 | } // namespace dbus |