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