Add explicit |forceOnlineSignin| to user pod status
[chromium-blink-merge.git] / dbus / test_service.cc
blob21a885d0b67fa4a4ede39046fb3d8116cb26ecac
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.
5 #include "dbus/test_service.h"
7 #include "base/bind.h"
8 #include "base/test/test_timeouts.h"
9 #include "base/threading/platform_thread.h"
10 #include "dbus/bus.h"
11 #include "dbus/exported_object.h"
12 #include "dbus/message.h"
13 #include "dbus/object_manager.h"
14 #include "dbus/object_path.h"
15 #include "dbus/property.h"
17 namespace {
19 void EmptyCallback(bool /* success */) {
22 } // namespace
24 namespace dbus {
26 // Echo, SlowEcho, AsyncEcho, BrokenMethod, GetAll, Get, Set, PerformAction,
27 // GetManagedObjects.
28 const int TestService::kNumMethodsToExport = 9;
30 TestService::Options::Options()
31 : request_ownership_options(Bus::REQUIRE_PRIMARY) {
34 TestService::Options::~Options() {
37 TestService::TestService(const Options& options)
38 : base::Thread("TestService"),
39 request_ownership_options_(options.request_ownership_options),
40 dbus_task_runner_(options.dbus_task_runner),
41 on_name_obtained_(false, false),
42 num_exported_methods_(0) {
45 TestService::~TestService() {
46 Stop();
49 bool TestService::StartService() {
50 base::Thread::Options thread_options;
51 thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
52 return StartWithOptions(thread_options);
55 bool TestService::WaitUntilServiceIsStarted() {
56 const base::TimeDelta timeout(TestTimeouts::action_max_timeout());
57 // Wait until the ownership of the service name is obtained.
58 return on_name_obtained_.TimedWait(timeout);
61 void TestService::ShutdownAndBlock() {
62 message_loop()->PostTask(
63 FROM_HERE,
64 base::Bind(&TestService::ShutdownAndBlockInternal,
65 base::Unretained(this)));
68 bool TestService::HasDBusThread() {
69 return bus_->HasDBusThread();
72 void TestService::ShutdownAndBlockInternal() {
73 if (HasDBusThread())
74 bus_->ShutdownOnDBusThreadAndBlock();
75 else
76 bus_->ShutdownAndBlock();
79 void TestService::SendTestSignal(const std::string& message) {
80 message_loop()->PostTask(
81 FROM_HERE,
82 base::Bind(&TestService::SendTestSignalInternal,
83 base::Unretained(this),
84 message));
87 void TestService::SendTestSignalFromRoot(const std::string& message) {
88 message_loop()->PostTask(
89 FROM_HERE,
90 base::Bind(&TestService::SendTestSignalFromRootInternal,
91 base::Unretained(this),
92 message));
95 void TestService::SendTestSignalInternal(const std::string& message) {
96 Signal signal("org.chromium.TestInterface", "Test");
97 MessageWriter writer(&signal);
98 writer.AppendString(message);
99 exported_object_->SendSignal(&signal);
102 void TestService::SendTestSignalFromRootInternal(const std::string& message) {
103 Signal signal("org.chromium.TestInterface", "Test");
104 MessageWriter writer(&signal);
105 writer.AppendString(message);
107 bus_->RequestOwnership("org.chromium.TestService",
108 request_ownership_options_,
109 base::Bind(&TestService::OnOwnership,
110 base::Unretained(this),
111 base::Bind(&EmptyCallback)));
113 // Use "/" just like dbus-send does.
114 ExportedObject* root_object = bus_->GetExportedObject(ObjectPath("/"));
115 root_object->SendSignal(&signal);
118 void TestService::RequestOwnership(base::Callback<void(bool)> callback) {
119 message_loop()->PostTask(
120 FROM_HERE,
121 base::Bind(&TestService::RequestOwnershipInternal,
122 base::Unretained(this),
123 callback));
126 void TestService::RequestOwnershipInternal(
127 base::Callback<void(bool)> callback) {
128 bus_->RequestOwnership("org.chromium.TestService",
129 request_ownership_options_,
130 base::Bind(&TestService::OnOwnership,
131 base::Unretained(this),
132 callback));
135 void TestService::OnOwnership(base::Callback<void(bool)> callback,
136 const std::string& service_name,
137 bool success) {
138 has_ownership_ = success;
139 LOG_IF(ERROR, !success) << "Failed to own: " << service_name;
140 callback.Run(success);
142 on_name_obtained_.Signal();
145 void TestService::OnExported(const std::string& interface_name,
146 const std::string& method_name,
147 bool success) {
148 if (!success) {
149 LOG(ERROR) << "Failed to export: " << interface_name << "."
150 << method_name;
151 // Returning here will make WaitUntilServiceIsStarted() to time out
152 // and return false.
153 return;
156 ++num_exported_methods_;
157 if (num_exported_methods_ == kNumMethodsToExport) {
158 // As documented in exported_object.h, the service name should be
159 // requested after all methods are exposed.
160 bus_->RequestOwnership("org.chromium.TestService",
161 request_ownership_options_,
162 base::Bind(&TestService::OnOwnership,
163 base::Unretained(this),
164 base::Bind(&EmptyCallback)));
168 void TestService::Run(base::MessageLoop* message_loop) {
169 Bus::Options bus_options;
170 bus_options.bus_type = Bus::SESSION;
171 bus_options.connection_type = Bus::PRIVATE;
172 bus_options.dbus_task_runner = dbus_task_runner_;
173 bus_ = new Bus(bus_options);
175 exported_object_ = bus_->GetExportedObject(
176 ObjectPath("/org/chromium/TestObject"));
178 int num_methods = 0;
179 exported_object_->ExportMethod(
180 "org.chromium.TestInterface",
181 "Echo",
182 base::Bind(&TestService::Echo,
183 base::Unretained(this)),
184 base::Bind(&TestService::OnExported,
185 base::Unretained(this)));
186 ++num_methods;
188 exported_object_->ExportMethod(
189 "org.chromium.TestInterface",
190 "SlowEcho",
191 base::Bind(&TestService::SlowEcho,
192 base::Unretained(this)),
193 base::Bind(&TestService::OnExported,
194 base::Unretained(this)));
195 ++num_methods;
197 exported_object_->ExportMethod(
198 "org.chromium.TestInterface",
199 "AsyncEcho",
200 base::Bind(&TestService::AsyncEcho,
201 base::Unretained(this)),
202 base::Bind(&TestService::OnExported,
203 base::Unretained(this)));
204 ++num_methods;
206 exported_object_->ExportMethod(
207 "org.chromium.TestInterface",
208 "BrokenMethod",
209 base::Bind(&TestService::BrokenMethod,
210 base::Unretained(this)),
211 base::Bind(&TestService::OnExported,
212 base::Unretained(this)));
213 ++num_methods;
215 exported_object_->ExportMethod(
216 "org.chromium.TestInterface",
217 "PerformAction",
218 base::Bind(&TestService::PerformAction,
219 base::Unretained(this)),
220 base::Bind(&TestService::OnExported,
221 base::Unretained(this)));
222 ++num_methods;
224 exported_object_->ExportMethod(
225 kPropertiesInterface,
226 kPropertiesGetAll,
227 base::Bind(&TestService::GetAllProperties,
228 base::Unretained(this)),
229 base::Bind(&TestService::OnExported,
230 base::Unretained(this)));
231 ++num_methods;
233 exported_object_->ExportMethod(
234 kPropertiesInterface,
235 kPropertiesGet,
236 base::Bind(&TestService::GetProperty,
237 base::Unretained(this)),
238 base::Bind(&TestService::OnExported,
239 base::Unretained(this)));
240 ++num_methods;
242 exported_object_->ExportMethod(
243 kPropertiesInterface,
244 kPropertiesSet,
245 base::Bind(&TestService::SetProperty,
246 base::Unretained(this)),
247 base::Bind(&TestService::OnExported,
248 base::Unretained(this)));
249 ++num_methods;
251 exported_object_manager_ = bus_->GetExportedObject(
252 ObjectPath("/org/chromium/TestService"));
254 exported_object_manager_->ExportMethod(
255 kObjectManagerInterface,
256 kObjectManagerGetManagedObjects,
257 base::Bind(&TestService::GetManagedObjects,
258 base::Unretained(this)),
259 base::Bind(&TestService::OnExported,
260 base::Unretained(this)));
261 ++num_methods;
263 // Just print an error message as we don't want to crash tests.
264 // Tests will fail at a call to WaitUntilServiceIsStarted().
265 if (num_methods != kNumMethodsToExport) {
266 LOG(ERROR) << "The number of methods does not match";
268 message_loop->Run();
271 void TestService::Echo(MethodCall* method_call,
272 ExportedObject::ResponseSender response_sender) {
273 MessageReader reader(method_call);
274 std::string text_message;
275 if (!reader.PopString(&text_message)) {
276 response_sender.Run(scoped_ptr<Response>());
277 return;
280 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
281 MessageWriter writer(response.get());
282 writer.AppendString(text_message);
283 response_sender.Run(response.Pass());
286 void TestService::SlowEcho(MethodCall* method_call,
287 ExportedObject::ResponseSender response_sender) {
288 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
289 Echo(method_call, response_sender);
292 void TestService::AsyncEcho(MethodCall* method_call,
293 ExportedObject::ResponseSender response_sender) {
294 // Schedule a call to Echo() to send an asynchronous response after we return.
295 message_loop()->PostDelayedTask(FROM_HERE,
296 base::Bind(&TestService::Echo,
297 base::Unretained(this),
298 method_call,
299 response_sender),
300 TestTimeouts::tiny_timeout());
303 void TestService::BrokenMethod(MethodCall* method_call,
304 ExportedObject::ResponseSender response_sender) {
305 response_sender.Run(scoped_ptr<Response>());
309 void TestService::GetAllProperties(
310 MethodCall* method_call,
311 ExportedObject::ResponseSender response_sender) {
312 MessageReader reader(method_call);
313 std::string interface;
314 if (!reader.PopString(&interface)) {
315 response_sender.Run(scoped_ptr<Response>());
316 return;
319 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
320 MessageWriter writer(response.get());
322 AddPropertiesToWriter(&writer);
324 response_sender.Run(response.Pass());
327 void TestService::GetProperty(MethodCall* method_call,
328 ExportedObject::ResponseSender response_sender) {
329 MessageReader reader(method_call);
330 std::string interface;
331 if (!reader.PopString(&interface)) {
332 response_sender.Run(scoped_ptr<Response>());
333 return;
336 std::string name;
337 if (!reader.PopString(&name)) {
338 response_sender.Run(scoped_ptr<Response>());
339 return;
342 if (name == "Name") {
343 // Return the previous value for the "Name" property:
344 // Variant<"TestService">
345 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
346 MessageWriter writer(response.get());
348 writer.AppendVariantOfString("TestService");
350 response_sender.Run(response.Pass());
351 } else if (name == "Version") {
352 // Return a new value for the "Version" property:
353 // Variant<20>
354 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
355 MessageWriter writer(response.get());
357 writer.AppendVariantOfInt16(20);
359 response_sender.Run(response.Pass());
360 } else if (name == "Methods") {
361 // Return the previous value for the "Methods" property:
362 // Variant<["Echo", "SlowEcho", "AsyncEcho", "BrokenMethod"]>
363 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
364 MessageWriter writer(response.get());
365 MessageWriter variant_writer(NULL);
366 MessageWriter variant_array_writer(NULL);
368 writer.OpenVariant("as", &variant_writer);
369 variant_writer.OpenArray("s", &variant_array_writer);
370 variant_array_writer.AppendString("Echo");
371 variant_array_writer.AppendString("SlowEcho");
372 variant_array_writer.AppendString("AsyncEcho");
373 variant_array_writer.AppendString("BrokenMethod");
374 variant_writer.CloseContainer(&variant_array_writer);
375 writer.CloseContainer(&variant_writer);
377 response_sender.Run(response.Pass());
378 } else if (name == "Objects") {
379 // Return the previous value for the "Objects" property:
380 // Variant<[objectpath:"/TestObjectPath"]>
381 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
382 MessageWriter writer(response.get());
383 MessageWriter variant_writer(NULL);
384 MessageWriter variant_array_writer(NULL);
386 writer.OpenVariant("ao", &variant_writer);
387 variant_writer.OpenArray("o", &variant_array_writer);
388 variant_array_writer.AppendObjectPath(ObjectPath("/TestObjectPath"));
389 variant_writer.CloseContainer(&variant_array_writer);
390 writer.CloseContainer(&variant_writer);
392 response_sender.Run(response.Pass());
393 } else {
394 // Return error.
395 response_sender.Run(scoped_ptr<Response>());
396 return;
400 void TestService::SetProperty(MethodCall* method_call,
401 ExportedObject::ResponseSender response_sender) {
402 MessageReader reader(method_call);
403 std::string interface;
404 if (!reader.PopString(&interface)) {
405 response_sender.Run(scoped_ptr<Response>());
406 return;
409 std::string name;
410 if (!reader.PopString(&name)) {
411 response_sender.Run(scoped_ptr<Response>());
412 return;
415 if (name != "Name") {
416 response_sender.Run(scoped_ptr<Response>());
417 return;
420 std::string value;
421 if (!reader.PopVariantOfString(&value)) {
422 response_sender.Run(scoped_ptr<Response>());
423 return;
426 SendPropertyChangedSignal(value);
428 response_sender.Run(Response::FromMethodCall(method_call));
431 void TestService::PerformAction(
432 MethodCall* method_call,
433 ExportedObject::ResponseSender response_sender) {
434 MessageReader reader(method_call);
435 std::string action;
436 ObjectPath object_path;
437 if (!reader.PopString(&action) || !reader.PopObjectPath(&object_path)) {
438 response_sender.Run(scoped_ptr<Response>());
439 return;
442 if (action == "AddObject")
443 AddObject(object_path);
444 else if (action == "RemoveObject")
445 RemoveObject(object_path);
447 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
448 response_sender.Run(response.Pass());
451 void TestService::GetManagedObjects(
452 MethodCall* method_call,
453 ExportedObject::ResponseSender response_sender) {
454 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
455 MessageWriter writer(response.get());
457 // The managed objects response is a dictionary of object paths identifying
458 // the object(s) with a dictionary of strings identifying the interface(s)
459 // they implement and then a dictionary of property values.
461 // Thus this looks something like:
463 // {
464 // "/org/chromium/TestObject": {
465 // "org.chromium.TestInterface": { /* Properties */ }
466 // }
467 // }
470 MessageWriter array_writer(NULL);
471 MessageWriter dict_entry_writer(NULL);
472 MessageWriter object_array_writer(NULL);
473 MessageWriter object_dict_entry_writer(NULL);
475 writer.OpenArray("{oa{sa{sv}}}", &array_writer);
477 array_writer.OpenDictEntry(&dict_entry_writer);
478 dict_entry_writer.AppendObjectPath(ObjectPath("/org/chromium/TestObject"));
479 dict_entry_writer.OpenArray("{sa{sv}}", &object_array_writer);
481 object_array_writer.OpenDictEntry(&object_dict_entry_writer);
482 object_dict_entry_writer.AppendString("org.chromium.TestInterface");
483 AddPropertiesToWriter(&object_dict_entry_writer);
484 object_array_writer.CloseContainer(&object_dict_entry_writer);
486 dict_entry_writer.CloseContainer(&object_array_writer);
488 array_writer.CloseContainer(&dict_entry_writer);
489 writer.CloseContainer(&array_writer);
491 response_sender.Run(response.Pass());
494 void TestService::AddPropertiesToWriter(MessageWriter* writer) {
495 // The properties response is a dictionary of strings identifying the
496 // property and a variant containing the property value. We return all
497 // of the properties, thus the response is:
499 // {
500 // "Name": Variant<"TestService">,
501 // "Version": Variant<10>,
502 // "Methods": Variant<["Echo", "SlowEcho", "AsyncEcho", "BrokenMethod"]>,
503 // "Objects": Variant<[objectpath:"/TestObjectPath"]>
504 // }
506 MessageWriter array_writer(NULL);
507 MessageWriter dict_entry_writer(NULL);
508 MessageWriter variant_writer(NULL);
509 MessageWriter variant_array_writer(NULL);
511 writer->OpenArray("{sv}", &array_writer);
513 array_writer.OpenDictEntry(&dict_entry_writer);
514 dict_entry_writer.AppendString("Name");
515 dict_entry_writer.AppendVariantOfString("TestService");
516 array_writer.CloseContainer(&dict_entry_writer);
518 array_writer.OpenDictEntry(&dict_entry_writer);
519 dict_entry_writer.AppendString("Version");
520 dict_entry_writer.AppendVariantOfInt16(10);
521 array_writer.CloseContainer(&dict_entry_writer);
523 array_writer.OpenDictEntry(&dict_entry_writer);
524 dict_entry_writer.AppendString("Methods");
525 dict_entry_writer.OpenVariant("as", &variant_writer);
526 variant_writer.OpenArray("s", &variant_array_writer);
527 variant_array_writer.AppendString("Echo");
528 variant_array_writer.AppendString("SlowEcho");
529 variant_array_writer.AppendString("AsyncEcho");
530 variant_array_writer.AppendString("BrokenMethod");
531 variant_writer.CloseContainer(&variant_array_writer);
532 dict_entry_writer.CloseContainer(&variant_writer);
533 array_writer.CloseContainer(&dict_entry_writer);
535 array_writer.OpenDictEntry(&dict_entry_writer);
536 dict_entry_writer.AppendString("Objects");
537 dict_entry_writer.OpenVariant("ao", &variant_writer);
538 variant_writer.OpenArray("o", &variant_array_writer);
539 variant_array_writer.AppendObjectPath(ObjectPath("/TestObjectPath"));
540 variant_writer.CloseContainer(&variant_array_writer);
541 dict_entry_writer.CloseContainer(&variant_writer);
542 array_writer.CloseContainer(&dict_entry_writer);
544 writer->CloseContainer(&array_writer);
547 void TestService::AddObject(const ObjectPath& object_path) {
548 message_loop()->PostTask(
549 FROM_HERE,
550 base::Bind(&TestService::AddObjectInternal,
551 base::Unretained(this),
552 object_path));
555 void TestService::AddObjectInternal(const ObjectPath& object_path) {
556 Signal signal(kObjectManagerInterface, kObjectManagerInterfacesAdded);
557 MessageWriter writer(&signal);
558 writer.AppendObjectPath(object_path);
560 MessageWriter array_writer(NULL);
561 MessageWriter dict_entry_writer(NULL);
563 writer.OpenArray("{sa{sv}}", &array_writer);
564 array_writer.OpenDictEntry(&dict_entry_writer);
565 dict_entry_writer.AppendString("org.chromium.TestInterface");
566 AddPropertiesToWriter(&dict_entry_writer);
567 array_writer.CloseContainer(&dict_entry_writer);
568 writer.CloseContainer(&array_writer);
570 exported_object_manager_->SendSignal(&signal);
573 void TestService::RemoveObject(const ObjectPath& object_path) {
574 message_loop()->PostTask(FROM_HERE,
575 base::Bind(&TestService::RemoveObjectInternal,
576 base::Unretained(this),
577 object_path));
580 void TestService::RemoveObjectInternal(const ObjectPath& object_path) {
581 Signal signal(kObjectManagerInterface, kObjectManagerInterfacesRemoved);
582 MessageWriter writer(&signal);
584 writer.AppendObjectPath(object_path);
586 std::vector<std::string> interfaces;
587 interfaces.push_back("org.chromium.TestInterface");
588 writer.AppendArrayOfStrings(interfaces);
590 exported_object_manager_->SendSignal(&signal);
593 void TestService::SendPropertyChangedSignal(const std::string& name) {
594 message_loop()->PostTask(
595 FROM_HERE,
596 base::Bind(&TestService::SendPropertyChangedSignalInternal,
597 base::Unretained(this),
598 name));
601 void TestService::SendPropertyChangedSignalInternal(const std::string& name) {
602 Signal signal(kPropertiesInterface, kPropertiesChanged);
603 MessageWriter writer(&signal);
604 writer.AppendString("org.chromium.TestInterface");
606 MessageWriter array_writer(NULL);
607 MessageWriter dict_entry_writer(NULL);
609 writer.OpenArray("{sv}", &array_writer);
610 array_writer.OpenDictEntry(&dict_entry_writer);
611 dict_entry_writer.AppendString("Name");
612 dict_entry_writer.AppendVariantOfString(name);
613 array_writer.CloseContainer(&dict_entry_writer);
614 writer.CloseContainer(&array_writer);
616 exported_object_->SendSignal(&signal);
619 } // namespace dbus