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