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