test_service.cc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. // Copyright (c) 2012 The Chromium 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. #include "dbus/test_service.h"
  5. #include <stdint.h>
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include "base/bind.h"
  10. #include "base/callback_helpers.h"
  11. #include "base/guid.h"
  12. #include "base/logging.h"
  13. #include "base/message_loop/message_pump_type.h"
  14. #include "base/run_loop.h"
  15. #include "base/task/single_thread_task_runner.h"
  16. #include "base/test/test_timeouts.h"
  17. #include "base/threading/platform_thread.h"
  18. #include "base/time/time.h"
  19. #include "dbus/bus.h"
  20. #include "dbus/exported_object.h"
  21. #include "dbus/message.h"
  22. #include "dbus/object_manager.h"
  23. #include "dbus/object_path.h"
  24. #include "dbus/property.h"
  25. namespace dbus {
  26. // Echo, SlowEcho, AsyncEcho, BrokenMethod, GetAll, Get, Set, PerformAction,
  27. // GetManagedObjects
  28. const int TestService::kNumMethodsToExport = 9;
  29. TestService::Options::Options()
  30. : request_ownership_options(Bus::REQUIRE_PRIMARY) {
  31. }
  32. TestService::Options::~Options() = default;
  33. TestService::TestService(const Options& options)
  34. : base::Thread("TestService"),
  35. service_name_(options.service_name),
  36. request_ownership_options_(options.request_ownership_options),
  37. dbus_task_runner_(options.dbus_task_runner),
  38. on_name_obtained_(base::WaitableEvent::ResetPolicy::AUTOMATIC,
  39. base::WaitableEvent::InitialState::NOT_SIGNALED),
  40. num_exported_methods_(0),
  41. send_immediate_properties_changed_(false),
  42. has_ownership_(false),
  43. exported_object_(nullptr),
  44. exported_object_manager_(nullptr) {
  45. if (service_name_.empty()) {
  46. service_name_ = "org.chromium.TestService-" + base::GenerateGUID();
  47. }
  48. }
  49. TestService::~TestService() {
  50. Stop();
  51. }
  52. bool TestService::StartService() {
  53. base::Thread::Options thread_options;
  54. thread_options.message_pump_type = base::MessagePumpType::IO;
  55. return StartWithOptions(std::move(thread_options));
  56. }
  57. void TestService::WaitUntilServiceIsStarted() {
  58. // Wait until the ownership of the service name is obtained.
  59. on_name_obtained_.Wait();
  60. }
  61. void TestService::ShutdownAndBlock() {
  62. task_runner()->PostTask(FROM_HERE,
  63. base::BindOnce(&TestService::ShutdownAndBlockInternal,
  64. base::Unretained(this)));
  65. }
  66. bool TestService::HasDBusThread() {
  67. return bus_->HasDBusThread();
  68. }
  69. void TestService::ShutdownAndBlockInternal() {
  70. if (HasDBusThread())
  71. bus_->ShutdownOnDBusThreadAndBlock();
  72. else
  73. bus_->ShutdownAndBlock();
  74. }
  75. void TestService::SendTestSignal(const std::string& message) {
  76. task_runner()->PostTask(FROM_HERE,
  77. base::BindOnce(&TestService::SendTestSignalInternal,
  78. base::Unretained(this), message));
  79. }
  80. void TestService::SendTestSignalFromRoot(const std::string& message) {
  81. task_runner()->PostTask(
  82. FROM_HERE, base::BindOnce(&TestService::SendTestSignalFromRootInternal,
  83. base::Unretained(this), message));
  84. }
  85. void TestService::SendTestSignalInternal(const std::string& message) {
  86. Signal signal("org.chromium.TestInterface", "Test");
  87. MessageWriter writer(&signal);
  88. writer.AppendString(message);
  89. exported_object_->SendSignal(&signal);
  90. }
  91. void TestService::SendTestSignalFromRootInternal(const std::string& message) {
  92. Signal signal("org.chromium.TestInterface", "Test");
  93. MessageWriter writer(&signal);
  94. writer.AppendString(message);
  95. bus_->RequestOwnership(
  96. service_name_, request_ownership_options_,
  97. base::BindOnce(&TestService::OnOwnership, base::Unretained(this),
  98. base::DoNothing()));
  99. // Use "/" just like dbus-send does.
  100. ExportedObject* root_object = bus_->GetExportedObject(ObjectPath("/"));
  101. root_object->SendSignal(&signal);
  102. }
  103. void TestService::RequestOwnership(base::OnceCallback<void(bool)> callback) {
  104. task_runner()->PostTask(
  105. FROM_HERE, base::BindOnce(&TestService::RequestOwnershipInternal,
  106. base::Unretained(this), std::move(callback)));
  107. }
  108. void TestService::RequestOwnershipInternal(
  109. base::OnceCallback<void(bool)> callback) {
  110. bus_->RequestOwnership(
  111. service_name_, request_ownership_options_,
  112. base::BindOnce(&TestService::OnOwnership, base::Unretained(this),
  113. std::move(callback)));
  114. }
  115. void TestService::OnOwnership(base::OnceCallback<void(bool)> callback,
  116. const std::string& service_name,
  117. bool success) {
  118. has_ownership_ = success;
  119. LOG_IF(ERROR, !success) << "Failed to own: " << service_name;
  120. std::move(callback).Run(success);
  121. on_name_obtained_.Signal();
  122. }
  123. void TestService::ReleaseOwnership(base::OnceClosure callback) {
  124. bus_->GetDBusTaskRunner()->PostTask(
  125. FROM_HERE, base::BindOnce(&TestService::ReleaseOwnershipInternal,
  126. base::Unretained(this), std::move(callback)));
  127. }
  128. void TestService::ReleaseOwnershipInternal(base::OnceClosure callback) {
  129. bus_->ReleaseOwnership(service_name_);
  130. has_ownership_ = false;
  131. bus_->GetOriginTaskRunner()->PostTask(FROM_HERE, std::move(callback));
  132. }
  133. void TestService::SetSendImmediatePropertiesChanged() {
  134. send_immediate_properties_changed_ = true;
  135. }
  136. void TestService::OnExported(const std::string& interface_name,
  137. const std::string& method_name,
  138. bool success) {
  139. if (!success) {
  140. LOG(ERROR) << "Failed to export: " << interface_name << "."
  141. << method_name;
  142. // Returning here will make WaitUntilServiceIsStarted() to time out
  143. // and return false.
  144. return;
  145. }
  146. ++num_exported_methods_;
  147. if (num_exported_methods_ == kNumMethodsToExport) {
  148. // As documented in exported_object.h, the service name should be
  149. // requested after all methods are exposed.
  150. bus_->RequestOwnership(
  151. service_name_, request_ownership_options_,
  152. base::BindOnce(&TestService::OnOwnership, base::Unretained(this),
  153. base::DoNothing()));
  154. }
  155. }
  156. void TestService::Run(base::RunLoop* run_loop) {
  157. Bus::Options bus_options;
  158. bus_options.bus_type = Bus::SESSION;
  159. bus_options.connection_type = Bus::PRIVATE;
  160. bus_options.dbus_task_runner = dbus_task_runner_;
  161. bus_ = new Bus(bus_options);
  162. exported_object_ = bus_->GetExportedObject(
  163. ObjectPath("/org/chromium/TestObject"));
  164. int num_methods = 0;
  165. exported_object_->ExportMethod(
  166. "org.chromium.TestInterface", "Echo",
  167. base::BindRepeating(&TestService::Echo, base::Unretained(this)),
  168. base::BindOnce(&TestService::OnExported, base::Unretained(this)));
  169. ++num_methods;
  170. exported_object_->ExportMethod(
  171. "org.chromium.TestInterface", "SlowEcho",
  172. base::BindRepeating(&TestService::SlowEcho, base::Unretained(this)),
  173. base::BindOnce(&TestService::OnExported, base::Unretained(this)));
  174. ++num_methods;
  175. exported_object_->ExportMethod(
  176. "org.chromium.TestInterface", "AsyncEcho",
  177. base::BindRepeating(&TestService::AsyncEcho, base::Unretained(this)),
  178. base::BindOnce(&TestService::OnExported, base::Unretained(this)));
  179. ++num_methods;
  180. exported_object_->ExportMethod(
  181. "org.chromium.TestInterface", "BrokenMethod",
  182. base::BindRepeating(&TestService::BrokenMethod, base::Unretained(this)),
  183. base::BindOnce(&TestService::OnExported, base::Unretained(this)));
  184. ++num_methods;
  185. exported_object_->ExportMethod(
  186. "org.chromium.TestInterface", "PerformAction",
  187. base::BindRepeating(&TestService::PerformAction, base::Unretained(this)),
  188. base::BindOnce(&TestService::OnExported, base::Unretained(this)));
  189. ++num_methods;
  190. exported_object_->ExportMethod(
  191. kPropertiesInterface, kPropertiesGetAll,
  192. base::BindRepeating(&TestService::GetAllProperties,
  193. base::Unretained(this)),
  194. base::BindOnce(&TestService::OnExported, base::Unretained(this)));
  195. ++num_methods;
  196. exported_object_->ExportMethod(
  197. kPropertiesInterface, kPropertiesGet,
  198. base::BindRepeating(&TestService::GetProperty, base::Unretained(this)),
  199. base::BindOnce(&TestService::OnExported, base::Unretained(this)));
  200. ++num_methods;
  201. exported_object_->ExportMethod(
  202. kPropertiesInterface, kPropertiesSet,
  203. base::BindRepeating(&TestService::SetProperty, base::Unretained(this)),
  204. base::BindOnce(&TestService::OnExported, base::Unretained(this)));
  205. ++num_methods;
  206. exported_object_manager_ = bus_->GetExportedObject(
  207. ObjectPath("/org/chromium/TestService"));
  208. exported_object_manager_->ExportMethod(
  209. kObjectManagerInterface, kObjectManagerGetManagedObjects,
  210. base::BindRepeating(&TestService::GetManagedObjects,
  211. base::Unretained(this)),
  212. base::BindOnce(&TestService::OnExported, base::Unretained(this)));
  213. ++num_methods;
  214. // Just print an error message as we don't want to crash tests.
  215. // Tests will fail at a call to WaitUntilServiceIsStarted().
  216. if (num_methods != kNumMethodsToExport) {
  217. LOG(ERROR) << "The number of methods does not match";
  218. }
  219. run_loop->Run();
  220. }
  221. void TestService::Echo(MethodCall* method_call,
  222. ExportedObject::ResponseSender response_sender) {
  223. MessageReader reader(method_call);
  224. std::string text_message;
  225. if (!reader.PopString(&text_message)) {
  226. std::move(response_sender).Run(std::unique_ptr<Response>());
  227. return;
  228. }
  229. std::unique_ptr<Response> response = Response::FromMethodCall(method_call);
  230. MessageWriter writer(response.get());
  231. writer.AppendString(text_message);
  232. std::move(response_sender).Run(std::move(response));
  233. }
  234. void TestService::SlowEcho(MethodCall* method_call,
  235. ExportedObject::ResponseSender response_sender) {
  236. base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
  237. Echo(method_call, std::move(response_sender));
  238. }
  239. void TestService::AsyncEcho(MethodCall* method_call,
  240. ExportedObject::ResponseSender response_sender) {
  241. // Schedule a call to Echo() to send an asynchronous response after we return.
  242. task_runner()->PostDelayedTask(
  243. FROM_HERE,
  244. base::BindOnce(&TestService::Echo, base::Unretained(this), method_call,
  245. std::move(response_sender)),
  246. TestTimeouts::tiny_timeout());
  247. }
  248. void TestService::BrokenMethod(MethodCall* method_call,
  249. ExportedObject::ResponseSender response_sender) {
  250. std::move(response_sender).Run(std::unique_ptr<Response>());
  251. }
  252. void TestService::GetAllProperties(
  253. MethodCall* method_call,
  254. ExportedObject::ResponseSender response_sender) {
  255. MessageReader reader(method_call);
  256. std::string interface;
  257. if (!reader.PopString(&interface)) {
  258. std::move(response_sender).Run(std::unique_ptr<Response>());
  259. return;
  260. }
  261. std::unique_ptr<Response> response = Response::FromMethodCall(method_call);
  262. MessageWriter writer(response.get());
  263. AddPropertiesToWriter(&writer);
  264. std::move(response_sender).Run(std::move(response));
  265. }
  266. void TestService::GetProperty(MethodCall* method_call,
  267. ExportedObject::ResponseSender response_sender) {
  268. MessageReader reader(method_call);
  269. std::string interface;
  270. if (!reader.PopString(&interface)) {
  271. std::move(response_sender).Run(std::unique_ptr<Response>());
  272. return;
  273. }
  274. std::string name;
  275. if (!reader.PopString(&name)) {
  276. std::move(response_sender).Run(std::unique_ptr<Response>());
  277. return;
  278. }
  279. if (name == "Name") {
  280. // Return the previous value for the "Name" property:
  281. // Variant<"TestService">
  282. std::unique_ptr<Response> response = Response::FromMethodCall(method_call);
  283. MessageWriter writer(response.get());
  284. writer.AppendVariantOfString("TestService");
  285. std::move(response_sender).Run(std::move(response));
  286. } else if (name == "Version") {
  287. // Return a new value for the "Version" property:
  288. // Variant<20>
  289. std::unique_ptr<Response> response = Response::FromMethodCall(method_call);
  290. MessageWriter writer(response.get());
  291. writer.AppendVariantOfInt16(20);
  292. std::move(response_sender).Run(std::move(response));
  293. } else if (name == "Methods") {
  294. // Return the previous value for the "Methods" property:
  295. // Variant<["Echo", "SlowEcho", "AsyncEcho", "BrokenMethod"]>
  296. std::unique_ptr<Response> response = Response::FromMethodCall(method_call);
  297. MessageWriter writer(response.get());
  298. MessageWriter variant_writer(nullptr);
  299. MessageWriter variant_array_writer(nullptr);
  300. writer.OpenVariant("as", &variant_writer);
  301. variant_writer.OpenArray("s", &variant_array_writer);
  302. variant_array_writer.AppendString("Echo");
  303. variant_array_writer.AppendString("SlowEcho");
  304. variant_array_writer.AppendString("AsyncEcho");
  305. variant_array_writer.AppendString("BrokenMethod");
  306. variant_writer.CloseContainer(&variant_array_writer);
  307. writer.CloseContainer(&variant_writer);
  308. std::move(response_sender).Run(std::move(response));
  309. } else if (name == "Objects") {
  310. // Return the previous value for the "Objects" property:
  311. // Variant<[objectpath:"/TestObjectPath"]>
  312. std::unique_ptr<Response> response = Response::FromMethodCall(method_call);
  313. MessageWriter writer(response.get());
  314. MessageWriter variant_writer(nullptr);
  315. MessageWriter variant_array_writer(nullptr);
  316. writer.OpenVariant("ao", &variant_writer);
  317. variant_writer.OpenArray("o", &variant_array_writer);
  318. variant_array_writer.AppendObjectPath(ObjectPath("/TestObjectPath"));
  319. variant_writer.CloseContainer(&variant_array_writer);
  320. writer.CloseContainer(&variant_writer);
  321. std::move(response_sender).Run(std::move(response));
  322. } else if (name == "Bytes") {
  323. // Return the previous value for the "Bytes" property:
  324. // Variant<[0x54, 0x65, 0x73, 0x74]>
  325. std::unique_ptr<Response> response = Response::FromMethodCall(method_call);
  326. MessageWriter writer(response.get());
  327. MessageWriter variant_writer(nullptr);
  328. MessageWriter variant_array_writer(nullptr);
  329. writer.OpenVariant("ay", &variant_writer);
  330. const uint8_t bytes[] = {0x54, 0x65, 0x73, 0x74};
  331. variant_writer.AppendArrayOfBytes(bytes, sizeof(bytes));
  332. writer.CloseContainer(&variant_writer);
  333. std::move(response_sender).Run(std::move(response));
  334. } else {
  335. // Return error.
  336. std::move(response_sender).Run(std::unique_ptr<Response>());
  337. return;
  338. }
  339. }
  340. void TestService::SetProperty(MethodCall* method_call,
  341. ExportedObject::ResponseSender response_sender) {
  342. MessageReader reader(method_call);
  343. std::string interface;
  344. if (!reader.PopString(&interface)) {
  345. std::move(response_sender).Run(std::unique_ptr<Response>());
  346. return;
  347. }
  348. std::string name;
  349. if (!reader.PopString(&name)) {
  350. std::move(response_sender).Run(std::unique_ptr<Response>());
  351. return;
  352. }
  353. if (name != "Name") {
  354. std::move(response_sender).Run(std::unique_ptr<Response>());
  355. return;
  356. }
  357. std::string value;
  358. if (!reader.PopVariantOfString(&value)) {
  359. std::move(response_sender).Run(std::unique_ptr<Response>());
  360. return;
  361. }
  362. SendPropertyChangedSignal(value);
  363. std::move(response_sender).Run(Response::FromMethodCall(method_call));
  364. }
  365. void TestService::PerformAction(
  366. MethodCall* method_call,
  367. ExportedObject::ResponseSender response_sender) {
  368. MessageReader reader(method_call);
  369. std::string action;
  370. ObjectPath object_path;
  371. if (!reader.PopString(&action) || !reader.PopObjectPath(&object_path)) {
  372. std::move(response_sender).Run(std::unique_ptr<Response>());
  373. return;
  374. }
  375. if (action == "AddObject") {
  376. AddObject(object_path);
  377. } else if (action == "RemoveObject") {
  378. RemoveObject(object_path);
  379. } else if (action == "SetSendImmediatePropertiesChanged") {
  380. SetSendImmediatePropertiesChanged();
  381. } else if (action == "ReleaseOwnership") {
  382. ReleaseOwnership(base::BindOnce(&TestService::PerformActionResponse,
  383. base::Unretained(this), method_call,
  384. std::move(response_sender)));
  385. return;
  386. } else if (action == "Ownership") {
  387. ReleaseOwnership(base::BindOnce(&TestService::OwnershipReleased,
  388. base::Unretained(this), method_call,
  389. std::move(response_sender)));
  390. return;
  391. } else if (action == "InvalidateProperty") {
  392. SendPropertyInvalidatedSignal();
  393. }
  394. std::unique_ptr<Response> response = Response::FromMethodCall(method_call);
  395. std::move(response_sender).Run(std::move(response));
  396. }
  397. void TestService::PerformActionResponse(
  398. MethodCall* method_call,
  399. ExportedObject::ResponseSender response_sender) {
  400. std::unique_ptr<Response> response = Response::FromMethodCall(method_call);
  401. std::move(response_sender).Run(std::move(response));
  402. }
  403. void TestService::OwnershipReleased(
  404. MethodCall* method_call,
  405. ExportedObject::ResponseSender response_sender) {
  406. RequestOwnership(base::BindOnce(&TestService::OwnershipRegained,
  407. base::Unretained(this), method_call,
  408. std::move(response_sender)));
  409. }
  410. void TestService::OwnershipRegained(
  411. MethodCall* method_call,
  412. ExportedObject::ResponseSender response_sender,
  413. bool success) {
  414. PerformActionResponse(method_call, std::move(response_sender));
  415. }
  416. void TestService::GetManagedObjects(
  417. MethodCall* method_call,
  418. ExportedObject::ResponseSender response_sender) {
  419. std::unique_ptr<Response> response = Response::FromMethodCall(method_call);
  420. MessageWriter writer(response.get());
  421. // The managed objects response is a dictionary of object paths identifying
  422. // the object(s) with a dictionary of strings identifying the interface(s)
  423. // they implement and then a dictionary of property values.
  424. //
  425. // Thus this looks something like:
  426. //
  427. // {
  428. // "/org/chromium/TestObject": {
  429. // "org.chromium.TestInterface": { /* Properties */ }
  430. // }
  431. // }
  432. MessageWriter array_writer(nullptr);
  433. MessageWriter dict_entry_writer(nullptr);
  434. MessageWriter object_array_writer(nullptr);
  435. MessageWriter object_dict_entry_writer(nullptr);
  436. writer.OpenArray("{oa{sa{sv}}}", &array_writer);
  437. array_writer.OpenDictEntry(&dict_entry_writer);
  438. dict_entry_writer.AppendObjectPath(ObjectPath("/org/chromium/TestObject"));
  439. dict_entry_writer.OpenArray("{sa{sv}}", &object_array_writer);
  440. object_array_writer.OpenDictEntry(&object_dict_entry_writer);
  441. object_dict_entry_writer.AppendString("org.chromium.TestInterface");
  442. AddPropertiesToWriter(&object_dict_entry_writer);
  443. object_array_writer.CloseContainer(&object_dict_entry_writer);
  444. dict_entry_writer.CloseContainer(&object_array_writer);
  445. array_writer.CloseContainer(&dict_entry_writer);
  446. writer.CloseContainer(&array_writer);
  447. std::move(response_sender).Run(std::move(response));
  448. if (send_immediate_properties_changed_)
  449. SendPropertyChangedSignal("ChangedTestServiceName");
  450. }
  451. void TestService::AddPropertiesToWriter(MessageWriter* writer) {
  452. // The properties response is a dictionary of strings identifying the
  453. // property and a variant containing the property value. We return all
  454. // of the properties, thus the response is:
  455. //
  456. // {
  457. // "Name": Variant<"TestService">,
  458. // "Version": Variant<10>,
  459. // "Methods": Variant<["Echo", "SlowEcho", "AsyncEcho", "BrokenMethod"]>,
  460. // "Objects": Variant<[objectpath:"/TestObjectPath"]>
  461. // "Bytes": Variant<[0x54, 0x65, 0x73, 0x74]>
  462. // }
  463. MessageWriter array_writer(nullptr);
  464. MessageWriter dict_entry_writer(nullptr);
  465. MessageWriter variant_writer(nullptr);
  466. MessageWriter variant_array_writer(nullptr);
  467. writer->OpenArray("{sv}", &array_writer);
  468. array_writer.OpenDictEntry(&dict_entry_writer);
  469. dict_entry_writer.AppendString("Name");
  470. dict_entry_writer.AppendVariantOfString("TestService");
  471. array_writer.CloseContainer(&dict_entry_writer);
  472. array_writer.OpenDictEntry(&dict_entry_writer);
  473. dict_entry_writer.AppendString("Version");
  474. dict_entry_writer.AppendVariantOfInt16(10);
  475. array_writer.CloseContainer(&dict_entry_writer);
  476. array_writer.OpenDictEntry(&dict_entry_writer);
  477. dict_entry_writer.AppendString("Methods");
  478. dict_entry_writer.OpenVariant("as", &variant_writer);
  479. variant_writer.OpenArray("s", &variant_array_writer);
  480. variant_array_writer.AppendString("Echo");
  481. variant_array_writer.AppendString("SlowEcho");
  482. variant_array_writer.AppendString("AsyncEcho");
  483. variant_array_writer.AppendString("BrokenMethod");
  484. variant_writer.CloseContainer(&variant_array_writer);
  485. dict_entry_writer.CloseContainer(&variant_writer);
  486. array_writer.CloseContainer(&dict_entry_writer);
  487. array_writer.OpenDictEntry(&dict_entry_writer);
  488. dict_entry_writer.AppendString("Objects");
  489. dict_entry_writer.OpenVariant("ao", &variant_writer);
  490. variant_writer.OpenArray("o", &variant_array_writer);
  491. variant_array_writer.AppendObjectPath(ObjectPath("/TestObjectPath"));
  492. variant_writer.CloseContainer(&variant_array_writer);
  493. dict_entry_writer.CloseContainer(&variant_writer);
  494. array_writer.CloseContainer(&dict_entry_writer);
  495. array_writer.OpenDictEntry(&dict_entry_writer);
  496. dict_entry_writer.AppendString("Bytes");
  497. dict_entry_writer.OpenVariant("ay", &variant_writer);
  498. const uint8_t bytes[] = {0x54, 0x65, 0x73, 0x74};
  499. variant_writer.AppendArrayOfBytes(bytes, sizeof(bytes));
  500. dict_entry_writer.CloseContainer(&variant_writer);
  501. array_writer.CloseContainer(&dict_entry_writer);
  502. writer->CloseContainer(&array_writer);
  503. }
  504. void TestService::AddObject(const ObjectPath& object_path) {
  505. task_runner()->PostTask(FROM_HERE,
  506. base::BindOnce(&TestService::AddObjectInternal,
  507. base::Unretained(this), object_path));
  508. }
  509. void TestService::AddObjectInternal(const ObjectPath& object_path) {
  510. Signal signal(kObjectManagerInterface, kObjectManagerInterfacesAdded);
  511. MessageWriter writer(&signal);
  512. writer.AppendObjectPath(object_path);
  513. MessageWriter array_writer(nullptr);
  514. MessageWriter dict_entry_writer(nullptr);
  515. writer.OpenArray("{sa{sv}}", &array_writer);
  516. array_writer.OpenDictEntry(&dict_entry_writer);
  517. dict_entry_writer.AppendString("org.chromium.TestInterface");
  518. AddPropertiesToWriter(&dict_entry_writer);
  519. array_writer.CloseContainer(&dict_entry_writer);
  520. writer.CloseContainer(&array_writer);
  521. exported_object_manager_->SendSignal(&signal);
  522. }
  523. void TestService::RemoveObject(const ObjectPath& object_path) {
  524. task_runner()->PostTask(FROM_HERE,
  525. base::BindOnce(&TestService::RemoveObjectInternal,
  526. base::Unretained(this), object_path));
  527. }
  528. void TestService::RemoveObjectInternal(const ObjectPath& object_path) {
  529. Signal signal(kObjectManagerInterface, kObjectManagerInterfacesRemoved);
  530. MessageWriter writer(&signal);
  531. writer.AppendObjectPath(object_path);
  532. std::vector<std::string> interfaces;
  533. interfaces.push_back("org.chromium.TestInterface");
  534. writer.AppendArrayOfStrings(interfaces);
  535. exported_object_manager_->SendSignal(&signal);
  536. }
  537. void TestService::SendPropertyChangedSignal(const std::string& name) {
  538. task_runner()->PostTask(
  539. FROM_HERE, base::BindOnce(&TestService::SendPropertyChangedSignalInternal,
  540. base::Unretained(this), name));
  541. }
  542. void TestService::SendPropertyChangedSignalInternal(const std::string& name) {
  543. Signal signal(kPropertiesInterface, kPropertiesChanged);
  544. MessageWriter writer(&signal);
  545. writer.AppendString("org.chromium.TestInterface");
  546. MessageWriter array_writer(nullptr);
  547. MessageWriter dict_entry_writer(nullptr);
  548. writer.OpenArray("{sv}", &array_writer);
  549. array_writer.OpenDictEntry(&dict_entry_writer);
  550. dict_entry_writer.AppendString("Name");
  551. dict_entry_writer.AppendVariantOfString(name);
  552. array_writer.CloseContainer(&dict_entry_writer);
  553. writer.CloseContainer(&array_writer);
  554. MessageWriter invalidated_array_writer(nullptr);
  555. writer.OpenArray("s", &invalidated_array_writer);
  556. writer.CloseContainer(&invalidated_array_writer);
  557. exported_object_->SendSignal(&signal);
  558. }
  559. void TestService::SendPropertyInvalidatedSignal() {
  560. task_runner()->PostTask(
  561. FROM_HERE,
  562. base::BindOnce(&TestService::SendPropertyInvalidatedSignalInternal,
  563. base::Unretained(this)));
  564. }
  565. void TestService::SendPropertyInvalidatedSignalInternal() {
  566. Signal signal(kPropertiesInterface, kPropertiesChanged);
  567. MessageWriter writer(&signal);
  568. writer.AppendString("org.chromium.TestInterface");
  569. MessageWriter array_writer(nullptr);
  570. MessageWriter dict_entry_writer(nullptr);
  571. writer.OpenArray("{sv}", &array_writer);
  572. writer.CloseContainer(&array_writer);
  573. MessageWriter invalidated_array_writer(nullptr);
  574. writer.OpenArray("s", &invalidated_array_writer);
  575. invalidated_array_writer.AppendString("Name");
  576. writer.CloseContainer(&invalidated_array_writer);
  577. exported_object_->SendSignal(&signal);
  578. }
  579. } // namespace dbus