Alex Vakulenko | 30477dd | 2014-08-15 11:44:26 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <chromeos/dbus/dbus_object.h> |
| 6 | |
| 7 | #include <vector> |
| 8 | |
| 9 | #include <base/bind.h> |
Alex Vakulenko | e2f969f | 2014-08-24 15:05:06 -0700 | [diff] [blame] | 10 | #include <base/logging.h> |
Alex Vakulenko | 1510466 | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 11 | #include <chromeos/dbus/async_event_sequencer.h> |
| 12 | #include <chromeos/dbus/exported_object_manager.h> |
| 13 | #include <chromeos/dbus/exported_property_set.h> |
Alex Vakulenko | 520fc15 | 2014-08-19 10:31:54 -0700 | [diff] [blame] | 14 | #include <dbus/property.h> |
Alex Vakulenko | 30477dd | 2014-08-15 11:44:26 -0700 | [diff] [blame] | 15 | |
| 16 | namespace chromeos { |
| 17 | namespace dbus_utils { |
| 18 | |
Alex Vakulenko | 30477dd | 2014-08-15 11:44:26 -0700 | [diff] [blame] | 19 | ////////////////////////////////////////////////////////////////////////////// |
| 20 | |
| 21 | DBusInterface::DBusInterface(DBusObject* dbus_object, |
| 22 | const std::string& interface_name) |
Alex Vakulenko | 05d2904 | 2015-01-13 09:39:25 -0800 | [diff] [blame^] | 23 | : dbus_object_(dbus_object), interface_name_(interface_name) { |
| 24 | } |
Alex Vakulenko | 30477dd | 2014-08-15 11:44:26 -0700 | [diff] [blame] | 25 | |
| 26 | void DBusInterface::AddProperty(const std::string& property_name, |
| 27 | ExportedPropertyBase* prop_base) { |
Alex Vakulenko | 05d2904 | 2015-01-13 09:39:25 -0800 | [diff] [blame^] | 28 | dbus_object_->property_set_.RegisterProperty( |
| 29 | interface_name_, property_name, prop_base); |
Alex Vakulenko | 30477dd | 2014-08-15 11:44:26 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | void DBusInterface::ExportAsync( |
| 33 | ExportedObjectManager* object_manager, |
| 34 | dbus::Bus* bus, |
| 35 | dbus::ExportedObject* exported_object, |
| 36 | const dbus::ObjectPath& object_path, |
| 37 | const AsyncEventSequencer::CompletionAction& completion_callback) { |
Alex Vakulenko | 3e007ac | 2014-09-11 15:56:19 -0700 | [diff] [blame] | 38 | VLOG(1) << "Registering D-Bus interface '" << interface_name_ << "' for '" |
| 39 | << object_path.value() << "'"; |
Alex Vakulenko | 30477dd | 2014-08-15 11:44:26 -0700 | [diff] [blame] | 40 | scoped_refptr<AsyncEventSequencer> sequencer(new AsyncEventSequencer()); |
| 41 | for (const auto& pair : handlers_) { |
| 42 | std::string method_name = pair.first; |
Alex Vakulenko | 3e007ac | 2014-09-11 15:56:19 -0700 | [diff] [blame] | 43 | VLOG(1) << "Exporting method: " << interface_name_ << "." << method_name; |
Alex Vakulenko | 30477dd | 2014-08-15 11:44:26 -0700 | [diff] [blame] | 44 | std::string export_error = "Failed exporting " + method_name + " method"; |
| 45 | auto export_handler = sequencer->GetExportHandler( |
| 46 | interface_name_, method_name, export_error, true); |
Alex Vakulenko | 05d2904 | 2015-01-13 09:39:25 -0800 | [diff] [blame^] | 47 | auto method_handler = |
| 48 | base::Bind(&DBusInterface::HandleMethodCall, base::Unretained(this)); |
Alex Vakulenko | 30477dd | 2014-08-15 11:44:26 -0700 | [diff] [blame] | 49 | exported_object->ExportMethod( |
| 50 | interface_name_, method_name, method_handler, export_handler); |
| 51 | } |
| 52 | |
Alex Vakulenko | 30477dd | 2014-08-15 11:44:26 -0700 | [diff] [blame] | 53 | std::vector<AsyncEventSequencer::CompletionAction> actions; |
| 54 | if (object_manager) { |
Alex Vakulenko | 520fc15 | 2014-08-19 10:31:54 -0700 | [diff] [blame] | 55 | auto property_writer_callback = |
| 56 | dbus_object_->property_set_.GetPropertyWriter(interface_name_); |
Alex Vakulenko | 30477dd | 2014-08-15 11:44:26 -0700 | [diff] [blame] | 57 | actions.push_back(sequencer->WrapCompletionTask( |
| 58 | base::Bind(&ExportedObjectManager::ClaimInterface, |
| 59 | object_manager->AsWeakPtr(), |
| 60 | object_path, |
| 61 | interface_name_, |
Alex Vakulenko | 520fc15 | 2014-08-19 10:31:54 -0700 | [diff] [blame] | 62 | property_writer_callback))); |
Alex Vakulenko | 30477dd | 2014-08-15 11:44:26 -0700 | [diff] [blame] | 63 | } |
| 64 | actions.push_back(completion_callback); |
| 65 | sequencer->OnAllTasksCompletedCall(actions); |
| 66 | } |
| 67 | |
Alex Vakulenko | 05d2904 | 2015-01-13 09:39:25 -0800 | [diff] [blame^] | 68 | void DBusInterface::HandleMethodCall(dbus::MethodCall* method_call, |
| 69 | ResponseSender sender) { |
Alex Vakulenko | 30477dd | 2014-08-15 11:44:26 -0700 | [diff] [blame] | 70 | std::string method_name = method_call->GetMember(); |
Alex Vakulenko | 3e007ac | 2014-09-11 15:56:19 -0700 | [diff] [blame] | 71 | // Make a local copy of |interface_name_| because calling HandleMethod() |
| 72 | // can potentially kill this interface object... |
| 73 | std::string interface_name = interface_name_; |
Alex Vakulenko | 05d2904 | 2015-01-13 09:39:25 -0800 | [diff] [blame^] | 74 | VLOG(1) << "Received method call request: " << interface_name << "." |
| 75 | << method_name << "(" << method_call->GetSignature() << ")"; |
Alex Vakulenko | 30477dd | 2014-08-15 11:44:26 -0700 | [diff] [blame] | 76 | auto pair = handlers_.find(method_name); |
Alex Vakulenko | c60df90 | 2014-11-07 07:31:07 -0800 | [diff] [blame] | 77 | if (pair == handlers_.end()) { |
Alex Vakulenko | 05d2904 | 2015-01-13 09:39:25 -0800 | [diff] [blame^] | 78 | auto response = |
| 79 | dbus::ErrorResponse::FromMethodCall(method_call, |
| 80 | DBUS_ERROR_UNKNOWN_METHOD, |
| 81 | "Unknown method: " + method_name); |
Alex Vakulenko | 7fb5e54 | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 82 | sender.Run(response.Pass()); |
Alex Vakulenko | f437e3b | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 83 | return; |
Alex Vakulenko | 30477dd | 2014-08-15 11:44:26 -0700 | [diff] [blame] | 84 | } |
Alex Vakulenko | 3e007ac | 2014-09-11 15:56:19 -0700 | [diff] [blame] | 85 | LOG(INFO) << "Dispatching DBus method call: " << method_name; |
Alex Vakulenko | f437e3b | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 86 | pair->second->HandleMethod(method_call, sender); |
Alex Vakulenko | 30477dd | 2014-08-15 11:44:26 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void DBusInterface::AddHandlerImpl( |
| 90 | const std::string& method_name, |
Alex Vakulenko | f437e3b | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 91 | std::unique_ptr<DBusInterfaceMethodHandlerInterface> handler) { |
Alex Vakulenko | 05d2904 | 2015-01-13 09:39:25 -0800 | [diff] [blame^] | 92 | VLOG(1) << "Declaring method handler: " << interface_name_ << "." |
| 93 | << method_name; |
Alex Vakulenko | 30477dd | 2014-08-15 11:44:26 -0700 | [diff] [blame] | 94 | auto res = handlers_.insert(std::make_pair(method_name, std::move(handler))); |
| 95 | CHECK(res.second) << "Method '" << method_name << "' already exists"; |
| 96 | } |
| 97 | |
Alex Vakulenko | 3e007ac | 2014-09-11 15:56:19 -0700 | [diff] [blame] | 98 | void DBusInterface::AddSignalImpl( |
| 99 | const std::string& signal_name, |
| 100 | const std::shared_ptr<DBusSignalBase>& signal) { |
Alex Vakulenko | 05d2904 | 2015-01-13 09:39:25 -0800 | [diff] [blame^] | 101 | VLOG(1) << "Declaring a signal sink: " << interface_name_ << "." |
| 102 | << signal_name; |
Alex Vakulenko | 3e007ac | 2014-09-11 15:56:19 -0700 | [diff] [blame] | 103 | CHECK(signals_.insert(std::make_pair(signal_name, signal)).second) |
| 104 | << "The signal '" << signal_name << "' is already registered"; |
| 105 | } |
| 106 | |
Alex Vakulenko | 30477dd | 2014-08-15 11:44:26 -0700 | [diff] [blame] | 107 | /////////////////////////////////////////////////////////////////////////////// |
| 108 | |
| 109 | DBusObject::DBusObject(ExportedObjectManager* object_manager, |
| 110 | const scoped_refptr<dbus::Bus>& bus, |
| 111 | const dbus::ObjectPath& object_path) |
Alex Vakulenko | 05d2904 | 2015-01-13 09:39:25 -0800 | [diff] [blame^] | 112 | : property_set_(bus.get()), bus_(bus), object_path_(object_path) { |
Alex Vakulenko | 30477dd | 2014-08-15 11:44:26 -0700 | [diff] [blame] | 113 | if (object_manager) |
| 114 | object_manager_ = object_manager->AsWeakPtr(); |
| 115 | } |
| 116 | |
| 117 | DBusObject::~DBusObject() { |
| 118 | if (object_manager_) { |
| 119 | for (const auto& pair : interfaces_) |
| 120 | object_manager_->ReleaseInterface(object_path_, pair.first); |
| 121 | } |
| 122 | |
| 123 | if (exported_object_) |
| 124 | exported_object_->Unregister(); |
| 125 | } |
| 126 | |
| 127 | DBusInterface* DBusObject::AddOrGetInterface( |
| 128 | const std::string& interface_name) { |
| 129 | auto iter = interfaces_.find(interface_name); |
| 130 | if (iter == interfaces_.end()) { |
Alex Vakulenko | 3e007ac | 2014-09-11 15:56:19 -0700 | [diff] [blame] | 131 | VLOG(1) << "Adding an interface '" << interface_name << "' to object '" |
| 132 | << object_path_.value() << "'."; |
Alex Vakulenko | 30477dd | 2014-08-15 11:44:26 -0700 | [diff] [blame] | 133 | // Interface doesn't exist yet. Create one... |
| 134 | std::unique_ptr<DBusInterface> new_itf( |
| 135 | new DBusInterface(this, interface_name)); |
Alex Vakulenko | 05d2904 | 2015-01-13 09:39:25 -0800 | [diff] [blame^] | 136 | iter = interfaces_.insert(std::make_pair(interface_name, |
| 137 | std::move(new_itf))).first; |
Alex Vakulenko | 30477dd | 2014-08-15 11:44:26 -0700 | [diff] [blame] | 138 | } |
| 139 | return iter->second.get(); |
| 140 | } |
| 141 | |
Alex Vakulenko | f02e379 | 2014-11-07 08:01:35 -0800 | [diff] [blame] | 142 | DBusInterface* DBusObject::FindInterface( |
Alex Vakulenko | 05d2904 | 2015-01-13 09:39:25 -0800 | [diff] [blame^] | 143 | const std::string& interface_name) const { |
Alex Vakulenko | 30477dd | 2014-08-15 11:44:26 -0700 | [diff] [blame] | 144 | auto itf_iter = interfaces_.find(interface_name); |
Alex Vakulenko | f02e379 | 2014-11-07 08:01:35 -0800 | [diff] [blame] | 145 | return (itf_iter == interfaces_.end()) ? nullptr : itf_iter->second.get(); |
Alex Vakulenko | 30477dd | 2014-08-15 11:44:26 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void DBusObject::RegisterAsync( |
| 149 | const AsyncEventSequencer::CompletionAction& completion_callback) { |
Alex Vakulenko | 05d2904 | 2015-01-13 09:39:25 -0800 | [diff] [blame^] | 150 | VLOG(1) << "Registering D-Bus object '" << object_path_.value() << "'."; |
Alex Vakulenko | 30477dd | 2014-08-15 11:44:26 -0700 | [diff] [blame] | 151 | CHECK(exported_object_ == nullptr) << "Object already registered."; |
| 152 | scoped_refptr<AsyncEventSequencer> sequencer(new AsyncEventSequencer()); |
| 153 | exported_object_ = bus_->GetExportedObject(object_path_); |
Alex Vakulenko | 520fc15 | 2014-08-19 10:31:54 -0700 | [diff] [blame] | 154 | |
| 155 | // Add the org.freedesktop.DBus.Properties interface to the object. |
| 156 | DBusInterface* prop_interface = AddOrGetInterface(dbus::kPropertiesInterface); |
Alex Vakulenko | f437e3b | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 157 | prop_interface->AddSimpleMethodHandler( |
| 158 | dbus::kPropertiesGetAll, |
| 159 | base::Unretained(&property_set_), |
| 160 | &ExportedPropertySet::HandleGetAll); |
| 161 | prop_interface->AddSimpleMethodHandlerWithError( |
| 162 | dbus::kPropertiesGet, |
| 163 | base::Unretained(&property_set_), |
| 164 | &ExportedPropertySet::HandleGet); |
| 165 | prop_interface->AddSimpleMethodHandlerWithError( |
| 166 | dbus::kPropertiesSet, |
| 167 | base::Unretained(&property_set_), |
| 168 | &ExportedPropertySet::HandleSet); |
Alex Vakulenko | fa8f944 | 2014-08-27 14:50:36 -0700 | [diff] [blame] | 169 | property_set_.OnPropertiesInterfaceExported(prop_interface); |
Alex Vakulenko | 30477dd | 2014-08-15 11:44:26 -0700 | [diff] [blame] | 170 | |
| 171 | // Export interface methods |
| 172 | for (const auto& pair : interfaces_) { |
| 173 | pair.second->ExportAsync( |
| 174 | object_manager_.get(), |
| 175 | bus_.get(), |
| 176 | exported_object_, |
| 177 | object_path_, |
| 178 | sequencer->GetHandler("Failed to export interface " + pair.first, |
| 179 | false)); |
| 180 | } |
| 181 | |
| 182 | sequencer->OnAllTasksCompletedCall({completion_callback}); |
| 183 | } |
| 184 | |
Alex Vakulenko | fa8f944 | 2014-08-27 14:50:36 -0700 | [diff] [blame] | 185 | bool DBusObject::SendSignal(dbus::Signal* signal) { |
Alex Vakulenko | e2f969f | 2014-08-24 15:05:06 -0700 | [diff] [blame] | 186 | if (exported_object_) { |
Alex Vakulenko | e2f969f | 2014-08-24 15:05:06 -0700 | [diff] [blame] | 187 | exported_object_->SendSignal(signal); |
Alex Vakulenko | fa8f944 | 2014-08-27 14:50:36 -0700 | [diff] [blame] | 188 | return true; |
Alex Vakulenko | e2f969f | 2014-08-24 15:05:06 -0700 | [diff] [blame] | 189 | } |
Alex Vakulenko | fa8f944 | 2014-08-27 14:50:36 -0700 | [diff] [blame] | 190 | LOG(ERROR) << "Trying to send a signal from an object that is not exported"; |
| 191 | return false; |
Alex Vakulenko | e2f969f | 2014-08-24 15:05:06 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Alex Vakulenko | 30477dd | 2014-08-15 11:44:26 -0700 | [diff] [blame] | 194 | } // namespace dbus_utils |
| 195 | } // namespace chromeos |