blob: ff73e7a18e608e34c021fbf5489c368de056b5b3 [file] [log] [blame]
[email protected]02798a982012-01-27 00:45:331// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]a51076112011-08-17 20:58:122// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "dbus/test_service.h"
6
avi22437c692015-12-22 18:12:457#include <stdint.h>
dtapuska1d622f02015-02-09 22:51:408#include <string>
dchenge48600452015-12-28 02:24:509#include <utility>
dtapuska1d622f02015-02-09 22:51:4010#include <vector>
11
[email protected]a51076112011-08-17 20:58:1212#include "base/bind.h"
Peter Kasting341e1fb2018-02-24 00:03:0113#include "base/bind_helpers.h"
hashimoto067d84f522016-01-05 08:48:0314#include "base/guid.h"
Hans Wennborg164609132020-06-17 15:43:1215#include "base/logging.h"
Carlos Caballerodd8bf7b042019-07-30 14:14:1516#include "base/message_loop/message_pump_type.h"
fdoraye1e050c52016-07-19 21:05:5417#include "base/run_loop.h"
fdoray6ef45cf2016-08-25 15:36:3718#include "base/single_thread_task_runner.h"
[email protected]12f97662011-08-20 01:07:1719#include "base/test/test_timeouts.h"
[email protected]a51076112011-08-17 20:58:1220#include "base/threading/platform_thread.h"
21#include "dbus/bus.h"
22#include "dbus/exported_object.h"
23#include "dbus/message.h"
[email protected]9cc40cb2013-03-25 18:20:0824#include "dbus/object_manager.h"
[email protected]216ed0b2012-02-14 21:29:0625#include "dbus/object_path.h"
[email protected]cf910da22012-02-15 04:21:0826#include "dbus/property.h"
[email protected]a51076112011-08-17 20:58:1227
28namespace dbus {
29
[email protected]9cc40cb2013-03-25 18:20:0830// Echo, SlowEcho, AsyncEcho, BrokenMethod, GetAll, Get, Set, PerformAction,
armansitoebff093d2014-09-05 17:49:3431// GetManagedObjects
[email protected]9cc40cb2013-03-25 18:20:0832const int TestService::kNumMethodsToExport = 9;
[email protected]a51076112011-08-17 20:58:1233
[email protected]e2824902013-07-31 06:34:5934TestService::Options::Options()
35 : request_ownership_options(Bus::REQUIRE_PRIMARY) {
[email protected]12f97662011-08-20 01:07:1736}
37
Chris Watkins3740aae2017-11-29 07:44:1138TestService::Options::~Options() = default;
[email protected]12f97662011-08-20 01:07:1739
40TestService::TestService(const Options& options)
[email protected]a51076112011-08-17 20:58:1241 : base::Thread("TestService"),
hashimoto067d84f522016-01-05 08:48:0342 service_name_(options.service_name),
[email protected]e2824902013-07-31 06:34:5943 request_ownership_options_(options.request_ownership_options),
[email protected]200328a2013-02-20 01:36:5344 dbus_task_runner_(options.dbus_task_runner),
gab0faddf482016-06-02 12:26:5545 on_name_obtained_(base::WaitableEvent::ResetPolicy::AUTOMATIC,
46 base::WaitableEvent::InitialState::NOT_SIGNALED),
earthdok340dde22014-10-08 13:00:2047 num_exported_methods_(0),
48 send_immediate_properties_changed_(false),
49 has_ownership_(false),
Ben Chan14d500372017-11-09 20:20:1650 exported_object_(nullptr),
51 exported_object_manager_(nullptr) {
hashimoto067d84f522016-01-05 08:48:0352 if (service_name_.empty()) {
53 service_name_ = "org.chromium.TestService-" + base::GenerateGUID();
54 }
[email protected]a51076112011-08-17 20:58:1255}
56
57TestService::~TestService() {
[email protected]d583c3a2011-11-02 15:31:5658 Stop();
[email protected]a51076112011-08-17 20:58:1259}
60
[email protected]12f97662011-08-20 01:07:1761bool TestService::StartService() {
[email protected]a51076112011-08-17 20:58:1262 base::Thread::Options thread_options;
Carlos Caballerodd8bf7b042019-07-30 14:14:1563 thread_options.message_pump_type = base::MessagePumpType::IO;
[email protected]12f97662011-08-20 01:07:1764 return StartWithOptions(thread_options);
[email protected]a51076112011-08-17 20:58:1265}
66
Ryo Hashimoto3bbeb5c2018-08-13 04:58:3967void TestService::WaitUntilServiceIsStarted() {
[email protected]332577d2014-01-09 04:39:1768 // Wait until the ownership of the service name is obtained.
Ryo Hashimoto3bbeb5c2018-08-13 04:58:3969 on_name_obtained_.Wait();
[email protected]12f97662011-08-20 01:07:1770}
71
[email protected]e20d39fe2011-09-02 06:56:2372void TestService::ShutdownAndBlock() {
Alexander Timin0439ffe2018-10-30 18:10:0073 task_runner()->PostTask(FROM_HERE,
kylechar39c39282019-02-19 19:04:0474 base::BindOnce(&TestService::ShutdownAndBlockInternal,
75 base::Unretained(this)));
[email protected]a51076112011-08-17 20:58:1276}
77
[email protected]12f97662011-08-20 01:07:1778bool TestService::HasDBusThread() {
79 return bus_->HasDBusThread();
80}
81
[email protected]e20d39fe2011-09-02 06:56:2382void TestService::ShutdownAndBlockInternal() {
83 if (HasDBusThread())
84 bus_->ShutdownOnDBusThreadAndBlock();
85 else
86 bus_->ShutdownAndBlock();
87}
88
[email protected]3beaaa4e2011-08-23 07:29:2189void TestService::SendTestSignal(const std::string& message) {
Alexander Timin0439ffe2018-10-30 18:10:0090 task_runner()->PostTask(FROM_HERE,
kylechar39c39282019-02-19 19:04:0491 base::BindOnce(&TestService::SendTestSignalInternal,
92 base::Unretained(this), message));
[email protected]3beaaa4e2011-08-23 07:29:2193}
94
[email protected]df159b22011-09-14 22:10:2495void TestService::SendTestSignalFromRoot(const std::string& message) {
Alexander Timin0439ffe2018-10-30 18:10:0096 task_runner()->PostTask(
kylechar39c39282019-02-19 19:04:0497 FROM_HERE, base::BindOnce(&TestService::SendTestSignalFromRootInternal,
98 base::Unretained(this), message));
[email protected]df159b22011-09-14 22:10:2499}
100
[email protected]3beaaa4e2011-08-23 07:29:21101void TestService::SendTestSignalInternal(const std::string& message) {
[email protected]2a57ca642013-06-13 06:37:19102 Signal signal("org.chromium.TestInterface", "Test");
103 MessageWriter writer(&signal);
[email protected]3beaaa4e2011-08-23 07:29:21104 writer.AppendString(message);
105 exported_object_->SendSignal(&signal);
106}
107
[email protected]df159b22011-09-14 22:10:24108void TestService::SendTestSignalFromRootInternal(const std::string& message) {
[email protected]2a57ca642013-06-13 06:37:19109 Signal signal("org.chromium.TestInterface", "Test");
110 MessageWriter writer(&signal);
[email protected]df159b22011-09-14 22:10:24111 writer.AppendString(message);
112
Reilly Grantd4e66132019-11-22 17:14:50113 bus_->RequestOwnership(
114 service_name_, request_ownership_options_,
115 base::BindOnce(&TestService::OnOwnership, base::Unretained(this),
116 base::DoNothing::Once<bool>()));
[email protected]15e7b162012-03-10 01:12:52117
[email protected]df159b22011-09-14 22:10:24118 // Use "/" just like dbus-send does.
[email protected]2a57ca642013-06-13 06:37:19119 ExportedObject* root_object = bus_->GetExportedObject(ObjectPath("/"));
[email protected]df159b22011-09-14 22:10:24120 root_object->SendSignal(&signal);
121}
122
Reilly Grantd4e66132019-11-22 17:14:50123void TestService::RequestOwnership(base::OnceCallback<void(bool)> callback) {
124 task_runner()->PostTask(
125 FROM_HERE, base::BindOnce(&TestService::RequestOwnershipInternal,
126 base::Unretained(this), std::move(callback)));
[email protected]8bbe31e2012-10-29 06:27:33127}
128
[email protected]6d36c0c2012-11-14 11:02:59129void TestService::RequestOwnershipInternal(
Reilly Grantd4e66132019-11-22 17:14:50130 base::OnceCallback<void(bool)> callback) {
131 bus_->RequestOwnership(
132 service_name_, request_ownership_options_,
133 base::BindOnce(&TestService::OnOwnership, base::Unretained(this),
134 std::move(callback)));
[email protected]8bbe31e2012-10-29 06:27:33135}
136
Reilly Grantd4e66132019-11-22 17:14:50137void TestService::OnOwnership(base::OnceCallback<void(bool)> callback,
[email protected]6d36c0c2012-11-14 11:02:59138 const std::string& service_name,
[email protected]15e7b162012-03-10 01:12:52139 bool success) {
[email protected]6d36c0c2012-11-14 11:02:59140 has_ownership_ = success;
[email protected]15e7b162012-03-10 01:12:52141 LOG_IF(ERROR, !success) << "Failed to own: " << service_name;
Reilly Grantd4e66132019-11-22 17:14:50142 std::move(callback).Run(success);
[email protected]332577d2014-01-09 04:39:17143
144 on_name_obtained_.Signal();
[email protected]15e7b162012-03-10 01:12:52145}
146
Reilly Grantd4e66132019-11-22 17:14:50147void TestService::ReleaseOwnership(base::OnceClosure callback) {
[email protected]043fb8c2014-03-07 02:24:33148 bus_->GetDBusTaskRunner()->PostTask(
kylechar39c39282019-02-19 19:04:04149 FROM_HERE, base::BindOnce(&TestService::ReleaseOwnershipInternal,
Reilly Grantd4e66132019-11-22 17:14:50150 base::Unretained(this), std::move(callback)));
[email protected]043fb8c2014-03-07 02:24:33151}
152
Reilly Grantd4e66132019-11-22 17:14:50153void TestService::ReleaseOwnershipInternal(base::OnceClosure callback) {
hashimoto067d84f522016-01-05 08:48:03154 bus_->ReleaseOwnership(service_name_);
[email protected]043fb8c2014-03-07 02:24:33155 has_ownership_ = false;
156
Reilly Grantd4e66132019-11-22 17:14:50157 bus_->GetOriginTaskRunner()->PostTask(FROM_HERE, std::move(callback));
[email protected]043fb8c2014-03-07 02:24:33158}
159
armansitoebff093d2014-09-05 17:49:34160void TestService::SetSendImmediatePropertiesChanged() {
161 send_immediate_properties_changed_ = true;
162}
163
[email protected]12f97662011-08-20 01:07:17164void TestService::OnExported(const std::string& interface_name,
165 const std::string& method_name,
166 bool success) {
167 if (!success) {
168 LOG(ERROR) << "Failed to export: " << interface_name << "."
169 << method_name;
170 // Returning here will make WaitUntilServiceIsStarted() to time out
171 // and return false.
172 return;
173 }
174
175 ++num_exported_methods_;
[email protected]332577d2014-01-09 04:39:17176 if (num_exported_methods_ == kNumMethodsToExport) {
177 // As documented in exported_object.h, the service name should be
178 // requested after all methods are exposed.
Peter Kasting341e1fb2018-02-24 00:03:01179 bus_->RequestOwnership(
180 service_name_, request_ownership_options_,
Reilly Grantd4e66132019-11-22 17:14:50181 base::BindOnce(&TestService::OnOwnership, base::Unretained(this),
182 base::DoNothing::Once<bool>()));
[email protected]332577d2014-01-09 04:39:17183 }
[email protected]12f97662011-08-20 01:07:17184}
185
fdoraye1e050c52016-07-19 21:05:54186void TestService::Run(base::RunLoop* run_loop) {
[email protected]a51076112011-08-17 20:58:12187 Bus::Options bus_options;
188 bus_options.bus_type = Bus::SESSION;
189 bus_options.connection_type = Bus::PRIVATE;
[email protected]200328a2013-02-20 01:36:53190 bus_options.dbus_task_runner = dbus_task_runner_;
[email protected]a51076112011-08-17 20:58:12191 bus_ = new Bus(bus_options);
192
193 exported_object_ = bus_->GetExportedObject(
[email protected]2a57ca642013-06-13 06:37:19194 ObjectPath("/org/chromium/TestObject"));
[email protected]12f97662011-08-20 01:07:17195
196 int num_methods = 0;
197 exported_object_->ExportMethod(
Reilly Grantd4e66132019-11-22 17:14:50198 "org.chromium.TestInterface", "Echo",
199 base::BindRepeating(&TestService::Echo, base::Unretained(this)),
200 base::BindOnce(&TestService::OnExported, base::Unretained(this)));
[email protected]12f97662011-08-20 01:07:17201 ++num_methods;
202
203 exported_object_->ExportMethod(
Reilly Grantd4e66132019-11-22 17:14:50204 "org.chromium.TestInterface", "SlowEcho",
205 base::BindRepeating(&TestService::SlowEcho, base::Unretained(this)),
206 base::BindOnce(&TestService::OnExported, base::Unretained(this)));
[email protected]12f97662011-08-20 01:07:17207 ++num_methods;
208
209 exported_object_->ExportMethod(
Reilly Grantd4e66132019-11-22 17:14:50210 "org.chromium.TestInterface", "AsyncEcho",
211 base::BindRepeating(&TestService::AsyncEcho, base::Unretained(this)),
212 base::BindOnce(&TestService::OnExported, base::Unretained(this)));
[email protected]9aa74cc2011-11-30 04:57:42213 ++num_methods;
214
215 exported_object_->ExportMethod(
Reilly Grantd4e66132019-11-22 17:14:50216 "org.chromium.TestInterface", "BrokenMethod",
217 base::BindRepeating(&TestService::BrokenMethod, base::Unretained(this)),
218 base::BindOnce(&TestService::OnExported, base::Unretained(this)));
[email protected]12f97662011-08-20 01:07:17219 ++num_methods;
[email protected]a51076112011-08-17 20:58:12220
[email protected]cf910da22012-02-15 04:21:08221 exported_object_->ExportMethod(
Reilly Grantd4e66132019-11-22 17:14:50222 "org.chromium.TestInterface", "PerformAction",
223 base::BindRepeating(&TestService::PerformAction, base::Unretained(this)),
224 base::BindOnce(&TestService::OnExported, base::Unretained(this)));
[email protected]9cc40cb2013-03-25 18:20:08225 ++num_methods;
226
227 exported_object_->ExportMethod(
Reilly Grantd4e66132019-11-22 17:14:50228 kPropertiesInterface, kPropertiesGetAll,
229 base::BindRepeating(&TestService::GetAllProperties,
230 base::Unretained(this)),
231 base::BindOnce(&TestService::OnExported, base::Unretained(this)));
[email protected]cf910da22012-02-15 04:21:08232 ++num_methods;
233
234 exported_object_->ExportMethod(
Reilly Grantd4e66132019-11-22 17:14:50235 kPropertiesInterface, kPropertiesGet,
236 base::BindRepeating(&TestService::GetProperty, base::Unretained(this)),
237 base::BindOnce(&TestService::OnExported, base::Unretained(this)));
[email protected]cf910da22012-02-15 04:21:08238 ++num_methods;
239
240 exported_object_->ExportMethod(
Reilly Grantd4e66132019-11-22 17:14:50241 kPropertiesInterface, kPropertiesSet,
242 base::BindRepeating(&TestService::SetProperty, base::Unretained(this)),
243 base::BindOnce(&TestService::OnExported, base::Unretained(this)));
[email protected]cf910da22012-02-15 04:21:08244 ++num_methods;
245
[email protected]9cc40cb2013-03-25 18:20:08246 exported_object_manager_ = bus_->GetExportedObject(
[email protected]2a57ca642013-06-13 06:37:19247 ObjectPath("/org/chromium/TestService"));
[email protected]9cc40cb2013-03-25 18:20:08248
249 exported_object_manager_->ExportMethod(
Reilly Grantd4e66132019-11-22 17:14:50250 kObjectManagerInterface, kObjectManagerGetManagedObjects,
251 base::BindRepeating(&TestService::GetManagedObjects,
252 base::Unretained(this)),
253 base::BindOnce(&TestService::OnExported, base::Unretained(this)));
[email protected]9cc40cb2013-03-25 18:20:08254 ++num_methods;
255
[email protected]12f97662011-08-20 01:07:17256 // Just print an error message as we don't want to crash tests.
257 // Tests will fail at a call to WaitUntilServiceIsStarted().
258 if (num_methods != kNumMethodsToExport) {
259 LOG(ERROR) << "The number of methods does not match";
260 }
fdoraye1e050c52016-07-19 21:05:54261 run_loop->Run();
[email protected]a51076112011-08-17 20:58:12262}
263
[email protected]9aa74cc2011-11-30 04:57:42264void TestService::Echo(MethodCall* method_call,
[email protected]2a57ca642013-06-13 06:37:19265 ExportedObject::ResponseSender response_sender) {
[email protected]a51076112011-08-17 20:58:12266 MessageReader reader(method_call);
267 std::string text_message;
[email protected]9aa74cc2011-11-30 04:57:42268 if (!reader.PopString(&text_message)) {
Reilly Grantd4e66132019-11-22 17:14:50269 std::move(response_sender).Run(std::unique_ptr<Response>());
[email protected]9aa74cc2011-11-30 04:57:42270 return;
271 }
[email protected]a51076112011-08-17 20:58:12272
dcheng2a193282016-04-08 22:55:04273 std::unique_ptr<Response> response = Response::FromMethodCall(method_call);
[email protected]9b25d452013-02-07 09:46:24274 MessageWriter writer(response.get());
[email protected]a51076112011-08-17 20:58:12275 writer.AppendString(text_message);
Reilly Grantd4e66132019-11-22 17:14:50276 std::move(response_sender).Run(std::move(response));
[email protected]a51076112011-08-17 20:58:12277}
278
[email protected]2a57ca642013-06-13 06:37:19279void TestService::SlowEcho(MethodCall* method_call,
280 ExportedObject::ResponseSender response_sender) {
[email protected]02798a982012-01-27 00:45:33281 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
Reilly Grantd4e66132019-11-22 17:14:50282 Echo(method_call, std::move(response_sender));
[email protected]a51076112011-08-17 20:58:12283}
284
[email protected]2a57ca642013-06-13 06:37:19285void TestService::AsyncEcho(MethodCall* method_call,
286 ExportedObject::ResponseSender response_sender) {
[email protected]9aa74cc2011-11-30 04:57:42287 // Schedule a call to Echo() to send an asynchronous response after we return.
Alexander Timin0439ffe2018-10-30 18:10:00288 task_runner()->PostDelayedTask(
289 FROM_HERE,
kylechar39c39282019-02-19 19:04:04290 base::BindOnce(&TestService::Echo, base::Unretained(this), method_call,
Reilly Grantd4e66132019-11-22 17:14:50291 std::move(response_sender)),
fdoray6ef45cf2016-08-25 15:36:37292 TestTimeouts::tiny_timeout());
[email protected]9aa74cc2011-11-30 04:57:42293}
294
[email protected]2a57ca642013-06-13 06:37:19295void TestService::BrokenMethod(MethodCall* method_call,
296 ExportedObject::ResponseSender response_sender) {
Reilly Grantd4e66132019-11-22 17:14:50297 std::move(response_sender).Run(std::unique_ptr<Response>());
[email protected]a51076112011-08-17 20:58:12298}
299
[email protected]cf910da22012-02-15 04:21:08300
301void TestService::GetAllProperties(
302 MethodCall* method_call,
[email protected]2a57ca642013-06-13 06:37:19303 ExportedObject::ResponseSender response_sender) {
[email protected]cf910da22012-02-15 04:21:08304 MessageReader reader(method_call);
305 std::string interface;
306 if (!reader.PopString(&interface)) {
Reilly Grantd4e66132019-11-22 17:14:50307 std::move(response_sender).Run(std::unique_ptr<Response>());
[email protected]cf910da22012-02-15 04:21:08308 return;
309 }
310
dcheng2a193282016-04-08 22:55:04311 std::unique_ptr<Response> response = Response::FromMethodCall(method_call);
[email protected]9b25d452013-02-07 09:46:24312 MessageWriter writer(response.get());
[email protected]cf910da22012-02-15 04:21:08313
[email protected]9cc40cb2013-03-25 18:20:08314 AddPropertiesToWriter(&writer);
[email protected]cf910da22012-02-15 04:21:08315
Reilly Grantd4e66132019-11-22 17:14:50316 std::move(response_sender).Run(std::move(response));
[email protected]cf910da22012-02-15 04:21:08317}
318
[email protected]2a57ca642013-06-13 06:37:19319void TestService::GetProperty(MethodCall* method_call,
320 ExportedObject::ResponseSender response_sender) {
[email protected]cf910da22012-02-15 04:21:08321 MessageReader reader(method_call);
322 std::string interface;
323 if (!reader.PopString(&interface)) {
Reilly Grantd4e66132019-11-22 17:14:50324 std::move(response_sender).Run(std::unique_ptr<Response>());
[email protected]cf910da22012-02-15 04:21:08325 return;
326 }
327
328 std::string name;
329 if (!reader.PopString(&name)) {
Reilly Grantd4e66132019-11-22 17:14:50330 std::move(response_sender).Run(std::unique_ptr<Response>());
[email protected]cf910da22012-02-15 04:21:08331 return;
332 }
333
[email protected]72bbacc2012-03-21 23:43:45334 if (name == "Name") {
335 // Return the previous value for the "Name" property:
336 // Variant<"TestService">
dcheng2a193282016-04-08 22:55:04337 std::unique_ptr<Response> response = Response::FromMethodCall(method_call);
[email protected]9b25d452013-02-07 09:46:24338 MessageWriter writer(response.get());
[email protected]72bbacc2012-03-21 23:43:45339
340 writer.AppendVariantOfString("TestService");
341
Reilly Grantd4e66132019-11-22 17:14:50342 std::move(response_sender).Run(std::move(response));
[email protected]72bbacc2012-03-21 23:43:45343 } else if (name == "Version") {
344 // Return a new value for the "Version" property:
345 // Variant<20>
dcheng2a193282016-04-08 22:55:04346 std::unique_ptr<Response> response = Response::FromMethodCall(method_call);
[email protected]9b25d452013-02-07 09:46:24347 MessageWriter writer(response.get());
[email protected]72bbacc2012-03-21 23:43:45348
349 writer.AppendVariantOfInt16(20);
350
Reilly Grantd4e66132019-11-22 17:14:50351 std::move(response_sender).Run(std::move(response));
[email protected]72bbacc2012-03-21 23:43:45352 } else if (name == "Methods") {
353 // Return the previous value for the "Methods" property:
354 // Variant<["Echo", "SlowEcho", "AsyncEcho", "BrokenMethod"]>
dcheng2a193282016-04-08 22:55:04355 std::unique_ptr<Response> response = Response::FromMethodCall(method_call);
[email protected]9b25d452013-02-07 09:46:24356 MessageWriter writer(response.get());
Ben Chan14d500372017-11-09 20:20:16357 MessageWriter variant_writer(nullptr);
358 MessageWriter variant_array_writer(nullptr);
[email protected]72bbacc2012-03-21 23:43:45359
360 writer.OpenVariant("as", &variant_writer);
361 variant_writer.OpenArray("s", &variant_array_writer);
362 variant_array_writer.AppendString("Echo");
363 variant_array_writer.AppendString("SlowEcho");
364 variant_array_writer.AppendString("AsyncEcho");
365 variant_array_writer.AppendString("BrokenMethod");
366 variant_writer.CloseContainer(&variant_array_writer);
367 writer.CloseContainer(&variant_writer);
368
Reilly Grantd4e66132019-11-22 17:14:50369 std::move(response_sender).Run(std::move(response));
[email protected]72bbacc2012-03-21 23:43:45370 } else if (name == "Objects") {
371 // Return the previous value for the "Objects" property:
372 // Variant<[objectpath:"/TestObjectPath"]>
dcheng2a193282016-04-08 22:55:04373 std::unique_ptr<Response> response = Response::FromMethodCall(method_call);
[email protected]9b25d452013-02-07 09:46:24374 MessageWriter writer(response.get());
Ben Chan14d500372017-11-09 20:20:16375 MessageWriter variant_writer(nullptr);
376 MessageWriter variant_array_writer(nullptr);
[email protected]72bbacc2012-03-21 23:43:45377
378 writer.OpenVariant("ao", &variant_writer);
379 variant_writer.OpenArray("o", &variant_array_writer);
[email protected]2a57ca642013-06-13 06:37:19380 variant_array_writer.AppendObjectPath(ObjectPath("/TestObjectPath"));
[email protected]72bbacc2012-03-21 23:43:45381 variant_writer.CloseContainer(&variant_array_writer);
382 writer.CloseContainer(&variant_writer);
383
Reilly Grantd4e66132019-11-22 17:14:50384 std::move(response_sender).Run(std::move(response));
[email protected]ebbfffa22014-03-15 07:40:49385 } else if (name == "Bytes") {
386 // Return the previous value for the "Bytes" property:
387 // Variant<[0x54, 0x65, 0x73, 0x74]>
dcheng2a193282016-04-08 22:55:04388 std::unique_ptr<Response> response = Response::FromMethodCall(method_call);
[email protected]ebbfffa22014-03-15 07:40:49389 MessageWriter writer(response.get());
Ben Chan14d500372017-11-09 20:20:16390 MessageWriter variant_writer(nullptr);
391 MessageWriter variant_array_writer(nullptr);
[email protected]ebbfffa22014-03-15 07:40:49392
393 writer.OpenVariant("ay", &variant_writer);
avi22437c692015-12-22 18:12:45394 const uint8_t bytes[] = {0x54, 0x65, 0x73, 0x74};
[email protected]ebbfffa22014-03-15 07:40:49395 variant_writer.AppendArrayOfBytes(bytes, sizeof(bytes));
396 writer.CloseContainer(&variant_writer);
397
Reilly Grantd4e66132019-11-22 17:14:50398 std::move(response_sender).Run(std::move(response));
[email protected]72bbacc2012-03-21 23:43:45399 } else {
400 // Return error.
Reilly Grantd4e66132019-11-22 17:14:50401 std::move(response_sender).Run(std::unique_ptr<Response>());
[email protected]cf910da22012-02-15 04:21:08402 return;
403 }
[email protected]cf910da22012-02-15 04:21:08404}
405
[email protected]2a57ca642013-06-13 06:37:19406void TestService::SetProperty(MethodCall* method_call,
407 ExportedObject::ResponseSender response_sender) {
[email protected]cf910da22012-02-15 04:21:08408 MessageReader reader(method_call);
409 std::string interface;
410 if (!reader.PopString(&interface)) {
Reilly Grantd4e66132019-11-22 17:14:50411 std::move(response_sender).Run(std::unique_ptr<Response>());
[email protected]cf910da22012-02-15 04:21:08412 return;
413 }
414
415 std::string name;
416 if (!reader.PopString(&name)) {
Reilly Grantd4e66132019-11-22 17:14:50417 std::move(response_sender).Run(std::unique_ptr<Response>());
[email protected]cf910da22012-02-15 04:21:08418 return;
419 }
420
421 if (name != "Name") {
Reilly Grantd4e66132019-11-22 17:14:50422 std::move(response_sender).Run(std::unique_ptr<Response>());
[email protected]cf910da22012-02-15 04:21:08423 return;
424 }
425
426 std::string value;
427 if (!reader.PopVariantOfString(&value)) {
Reilly Grantd4e66132019-11-22 17:14:50428 std::move(response_sender).Run(std::unique_ptr<Response>());
[email protected]cf910da22012-02-15 04:21:08429 return;
430 }
431
432 SendPropertyChangedSignal(value);
433
Reilly Grantd4e66132019-11-22 17:14:50434 std::move(response_sender).Run(Response::FromMethodCall(method_call));
[email protected]cf910da22012-02-15 04:21:08435}
436
[email protected]9cc40cb2013-03-25 18:20:08437void TestService::PerformAction(
438 MethodCall* method_call,
[email protected]2a57ca642013-06-13 06:37:19439 ExportedObject::ResponseSender response_sender) {
[email protected]9cc40cb2013-03-25 18:20:08440 MessageReader reader(method_call);
441 std::string action;
[email protected]2a57ca642013-06-13 06:37:19442 ObjectPath object_path;
[email protected]9cc40cb2013-03-25 18:20:08443 if (!reader.PopString(&action) || !reader.PopObjectPath(&object_path)) {
Reilly Grantd4e66132019-11-22 17:14:50444 std::move(response_sender).Run(std::unique_ptr<Response>());
[email protected]9cc40cb2013-03-25 18:20:08445 return;
446 }
447
armansitoebff093d2014-09-05 17:49:34448 if (action == "AddObject") {
[email protected]9cc40cb2013-03-25 18:20:08449 AddObject(object_path);
armansitoebff093d2014-09-05 17:49:34450 } else if (action == "RemoveObject") {
[email protected]9cc40cb2013-03-25 18:20:08451 RemoveObject(object_path);
armansitoebff093d2014-09-05 17:49:34452 } else if (action == "SetSendImmediatePropertiesChanged") {
453 SetSendImmediatePropertiesChanged();
dtapuska1d622f02015-02-09 22:51:40454 } else if (action == "ReleaseOwnership") {
Reilly Grantd4e66132019-11-22 17:14:50455 ReleaseOwnership(base::BindOnce(&TestService::PerformActionResponse,
456 base::Unretained(this), method_call,
457 std::move(response_sender)));
[email protected]043fb8c2014-03-07 02:24:33458 return;
459 } else if (action == "Ownership") {
Reilly Grantd4e66132019-11-22 17:14:50460 ReleaseOwnership(base::BindOnce(&TestService::OwnershipReleased,
461 base::Unretained(this), method_call,
462 std::move(response_sender)));
[email protected]043fb8c2014-03-07 02:24:33463 return;
jpawlowskied276542015-05-11 11:07:04464 } else if (action == "InvalidateProperty") {
465 SendPropertyInvalidatedSignal();
[email protected]043fb8c2014-03-07 02:24:33466 }
[email protected]9cc40cb2013-03-25 18:20:08467
dcheng2a193282016-04-08 22:55:04468 std::unique_ptr<Response> response = Response::FromMethodCall(method_call);
Reilly Grantd4e66132019-11-22 17:14:50469 std::move(response_sender).Run(std::move(response));
[email protected]9cc40cb2013-03-25 18:20:08470}
471
[email protected]043fb8c2014-03-07 02:24:33472void TestService::PerformActionResponse(
473 MethodCall* method_call,
474 ExportedObject::ResponseSender response_sender) {
dcheng2a193282016-04-08 22:55:04475 std::unique_ptr<Response> response = Response::FromMethodCall(method_call);
Reilly Grantd4e66132019-11-22 17:14:50476 std::move(response_sender).Run(std::move(response));
[email protected]043fb8c2014-03-07 02:24:33477}
478
479void TestService::OwnershipReleased(
480 MethodCall* method_call,
481 ExportedObject::ResponseSender response_sender) {
Reilly Grantd4e66132019-11-22 17:14:50482 RequestOwnership(base::BindOnce(&TestService::OwnershipRegained,
483 base::Unretained(this), method_call,
484 std::move(response_sender)));
[email protected]043fb8c2014-03-07 02:24:33485}
486
487
488void TestService::OwnershipRegained(
489 MethodCall* method_call,
490 ExportedObject::ResponseSender response_sender,
491 bool success) {
Reilly Grantd4e66132019-11-22 17:14:50492 PerformActionResponse(method_call, std::move(response_sender));
[email protected]043fb8c2014-03-07 02:24:33493}
494
495
[email protected]9cc40cb2013-03-25 18:20:08496void TestService::GetManagedObjects(
497 MethodCall* method_call,
[email protected]2a57ca642013-06-13 06:37:19498 ExportedObject::ResponseSender response_sender) {
dcheng2a193282016-04-08 22:55:04499 std::unique_ptr<Response> response = Response::FromMethodCall(method_call);
[email protected]9cc40cb2013-03-25 18:20:08500 MessageWriter writer(response.get());
501
502 // The managed objects response is a dictionary of object paths identifying
503 // the object(s) with a dictionary of strings identifying the interface(s)
504 // they implement and then a dictionary of property values.
505 //
506 // Thus this looks something like:
507 //
508 // {
509 // "/org/chromium/TestObject": {
510 // "org.chromium.TestInterface": { /* Properties */ }
511 // }
512 // }
513
514
Ben Chan14d500372017-11-09 20:20:16515 MessageWriter array_writer(nullptr);
516 MessageWriter dict_entry_writer(nullptr);
517 MessageWriter object_array_writer(nullptr);
518 MessageWriter object_dict_entry_writer(nullptr);
[email protected]9cc40cb2013-03-25 18:20:08519
520 writer.OpenArray("{oa{sa{sv}}}", &array_writer);
521
522 array_writer.OpenDictEntry(&dict_entry_writer);
[email protected]2a57ca642013-06-13 06:37:19523 dict_entry_writer.AppendObjectPath(ObjectPath("/org/chromium/TestObject"));
[email protected]9cc40cb2013-03-25 18:20:08524 dict_entry_writer.OpenArray("{sa{sv}}", &object_array_writer);
525
526 object_array_writer.OpenDictEntry(&object_dict_entry_writer);
527 object_dict_entry_writer.AppendString("org.chromium.TestInterface");
528 AddPropertiesToWriter(&object_dict_entry_writer);
529 object_array_writer.CloseContainer(&object_dict_entry_writer);
530
531 dict_entry_writer.CloseContainer(&object_array_writer);
532
533 array_writer.CloseContainer(&dict_entry_writer);
534 writer.CloseContainer(&array_writer);
535
Reilly Grantd4e66132019-11-22 17:14:50536 std::move(response_sender).Run(std::move(response));
armansitoebff093d2014-09-05 17:49:34537
538 if (send_immediate_properties_changed_)
539 SendPropertyChangedSignal("ChangedTestServiceName");
[email protected]9cc40cb2013-03-25 18:20:08540}
541
[email protected]2a57ca642013-06-13 06:37:19542void TestService::AddPropertiesToWriter(MessageWriter* writer) {
[email protected]9cc40cb2013-03-25 18:20:08543 // The properties response is a dictionary of strings identifying the
544 // property and a variant containing the property value. We return all
545 // of the properties, thus the response is:
546 //
547 // {
548 // "Name": Variant<"TestService">,
549 // "Version": Variant<10>,
550 // "Methods": Variant<["Echo", "SlowEcho", "AsyncEcho", "BrokenMethod"]>,
551 // "Objects": Variant<[objectpath:"/TestObjectPath"]>
[email protected]ebbfffa22014-03-15 07:40:49552 // "Bytes": Variant<[0x54, 0x65, 0x73, 0x74]>
[email protected]2a57ca642013-06-13 06:37:19553 // }
[email protected]9cc40cb2013-03-25 18:20:08554
Ben Chan14d500372017-11-09 20:20:16555 MessageWriter array_writer(nullptr);
556 MessageWriter dict_entry_writer(nullptr);
557 MessageWriter variant_writer(nullptr);
558 MessageWriter variant_array_writer(nullptr);
[email protected]9cc40cb2013-03-25 18:20:08559
560 writer->OpenArray("{sv}", &array_writer);
561
562 array_writer.OpenDictEntry(&dict_entry_writer);
563 dict_entry_writer.AppendString("Name");
564 dict_entry_writer.AppendVariantOfString("TestService");
565 array_writer.CloseContainer(&dict_entry_writer);
566
567 array_writer.OpenDictEntry(&dict_entry_writer);
568 dict_entry_writer.AppendString("Version");
569 dict_entry_writer.AppendVariantOfInt16(10);
570 array_writer.CloseContainer(&dict_entry_writer);
571
572 array_writer.OpenDictEntry(&dict_entry_writer);
573 dict_entry_writer.AppendString("Methods");
574 dict_entry_writer.OpenVariant("as", &variant_writer);
575 variant_writer.OpenArray("s", &variant_array_writer);
576 variant_array_writer.AppendString("Echo");
577 variant_array_writer.AppendString("SlowEcho");
578 variant_array_writer.AppendString("AsyncEcho");
579 variant_array_writer.AppendString("BrokenMethod");
580 variant_writer.CloseContainer(&variant_array_writer);
581 dict_entry_writer.CloseContainer(&variant_writer);
582 array_writer.CloseContainer(&dict_entry_writer);
583
584 array_writer.OpenDictEntry(&dict_entry_writer);
585 dict_entry_writer.AppendString("Objects");
586 dict_entry_writer.OpenVariant("ao", &variant_writer);
587 variant_writer.OpenArray("o", &variant_array_writer);
[email protected]2a57ca642013-06-13 06:37:19588 variant_array_writer.AppendObjectPath(ObjectPath("/TestObjectPath"));
[email protected]9cc40cb2013-03-25 18:20:08589 variant_writer.CloseContainer(&variant_array_writer);
590 dict_entry_writer.CloseContainer(&variant_writer);
591 array_writer.CloseContainer(&dict_entry_writer);
592
[email protected]ebbfffa22014-03-15 07:40:49593 array_writer.OpenDictEntry(&dict_entry_writer);
594 dict_entry_writer.AppendString("Bytes");
595 dict_entry_writer.OpenVariant("ay", &variant_writer);
avi22437c692015-12-22 18:12:45596 const uint8_t bytes[] = {0x54, 0x65, 0x73, 0x74};
[email protected]ebbfffa22014-03-15 07:40:49597 variant_writer.AppendArrayOfBytes(bytes, sizeof(bytes));
598 dict_entry_writer.CloseContainer(&variant_writer);
599 array_writer.CloseContainer(&dict_entry_writer);
600
[email protected]9cc40cb2013-03-25 18:20:08601 writer->CloseContainer(&array_writer);
602}
603
[email protected]2a57ca642013-06-13 06:37:19604void TestService::AddObject(const ObjectPath& object_path) {
Alexander Timin0439ffe2018-10-30 18:10:00605 task_runner()->PostTask(FROM_HERE,
kylechar39c39282019-02-19 19:04:04606 base::BindOnce(&TestService::AddObjectInternal,
607 base::Unretained(this), object_path));
[email protected]9cc40cb2013-03-25 18:20:08608}
609
[email protected]2a57ca642013-06-13 06:37:19610void TestService::AddObjectInternal(const ObjectPath& object_path) {
611 Signal signal(kObjectManagerInterface, kObjectManagerInterfacesAdded);
612 MessageWriter writer(&signal);
[email protected]9cc40cb2013-03-25 18:20:08613 writer.AppendObjectPath(object_path);
614
Ben Chan14d500372017-11-09 20:20:16615 MessageWriter array_writer(nullptr);
616 MessageWriter dict_entry_writer(nullptr);
[email protected]9cc40cb2013-03-25 18:20:08617
618 writer.OpenArray("{sa{sv}}", &array_writer);
619 array_writer.OpenDictEntry(&dict_entry_writer);
620 dict_entry_writer.AppendString("org.chromium.TestInterface");
621 AddPropertiesToWriter(&dict_entry_writer);
622 array_writer.CloseContainer(&dict_entry_writer);
623 writer.CloseContainer(&array_writer);
624
625 exported_object_manager_->SendSignal(&signal);
626}
627
[email protected]2a57ca642013-06-13 06:37:19628void TestService::RemoveObject(const ObjectPath& object_path) {
Alexander Timin0439ffe2018-10-30 18:10:00629 task_runner()->PostTask(FROM_HERE,
kylechar39c39282019-02-19 19:04:04630 base::BindOnce(&TestService::RemoveObjectInternal,
631 base::Unretained(this), object_path));
[email protected]9cc40cb2013-03-25 18:20:08632}
633
[email protected]2a57ca642013-06-13 06:37:19634void TestService::RemoveObjectInternal(const ObjectPath& object_path) {
635 Signal signal(kObjectManagerInterface, kObjectManagerInterfacesRemoved);
636 MessageWriter writer(&signal);
[email protected]9cc40cb2013-03-25 18:20:08637
638 writer.AppendObjectPath(object_path);
639
640 std::vector<std::string> interfaces;
641 interfaces.push_back("org.chromium.TestInterface");
642 writer.AppendArrayOfStrings(interfaces);
643
644 exported_object_manager_->SendSignal(&signal);
645}
646
[email protected]cf910da22012-02-15 04:21:08647void TestService::SendPropertyChangedSignal(const std::string& name) {
Alexander Timin0439ffe2018-10-30 18:10:00648 task_runner()->PostTask(
kylechar39c39282019-02-19 19:04:04649 FROM_HERE, base::BindOnce(&TestService::SendPropertyChangedSignalInternal,
650 base::Unretained(this), name));
[email protected]cf910da22012-02-15 04:21:08651}
652
653void TestService::SendPropertyChangedSignalInternal(const std::string& name) {
[email protected]2a57ca642013-06-13 06:37:19654 Signal signal(kPropertiesInterface, kPropertiesChanged);
655 MessageWriter writer(&signal);
[email protected]9cc40cb2013-03-25 18:20:08656 writer.AppendString("org.chromium.TestInterface");
[email protected]cf910da22012-02-15 04:21:08657
Ben Chan14d500372017-11-09 20:20:16658 MessageWriter array_writer(nullptr);
659 MessageWriter dict_entry_writer(nullptr);
[email protected]cf910da22012-02-15 04:21:08660
661 writer.OpenArray("{sv}", &array_writer);
662 array_writer.OpenDictEntry(&dict_entry_writer);
663 dict_entry_writer.AppendString("Name");
664 dict_entry_writer.AppendVariantOfString(name);
665 array_writer.CloseContainer(&dict_entry_writer);
666 writer.CloseContainer(&array_writer);
667
Ben Chan14d500372017-11-09 20:20:16668 MessageWriter invalidated_array_writer(nullptr);
jpawlowskied276542015-05-11 11:07:04669
670 writer.OpenArray("s", &invalidated_array_writer);
671 writer.CloseContainer(&invalidated_array_writer);
672
673 exported_object_->SendSignal(&signal);
674}
675
676void TestService::SendPropertyInvalidatedSignal() {
Alexander Timin0439ffe2018-10-30 18:10:00677 task_runner()->PostTask(
kylechar39c39282019-02-19 19:04:04678 FROM_HERE,
679 base::BindOnce(&TestService::SendPropertyInvalidatedSignalInternal,
680 base::Unretained(this)));
jpawlowskied276542015-05-11 11:07:04681}
682
683void TestService::SendPropertyInvalidatedSignalInternal() {
684 Signal signal(kPropertiesInterface, kPropertiesChanged);
685 MessageWriter writer(&signal);
686 writer.AppendString("org.chromium.TestInterface");
687
Ben Chan14d500372017-11-09 20:20:16688 MessageWriter array_writer(nullptr);
689 MessageWriter dict_entry_writer(nullptr);
jpawlowskied276542015-05-11 11:07:04690
691 writer.OpenArray("{sv}", &array_writer);
692 writer.CloseContainer(&array_writer);
693
Ben Chan14d500372017-11-09 20:20:16694 MessageWriter invalidated_array_writer(nullptr);
jpawlowskied276542015-05-11 11:07:04695
696 writer.OpenArray("s", &invalidated_array_writer);
697 invalidated_array_writer.AppendString("Name");
698 writer.CloseContainer(&invalidated_array_writer);
699
[email protected]cf910da22012-02-15 04:21:08700 exported_object_->SendSignal(&signal);
701}
702
[email protected]a51076112011-08-17 20:58:12703} // namespace dbus