Optimize shorthand CSS filter application. When the filter chain can be expressed...
[chromium-blink-merge.git] / dbus / test_service.cc
blobae67bf72794306896b2f0ea29f287a29a0371c7a
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() {
33 TestService::Options::~Options() {
36 TestService::TestService(const Options& options)
37 : base::Thread("TestService"),
38 dbus_task_runner_(options.dbus_task_runner),
39 on_all_methods_exported_(false, false),
40 num_exported_methods_(0) {
43 TestService::~TestService() {
44 Stop();
47 bool TestService::StartService() {
48 base::Thread::Options thread_options;
49 thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
50 return StartWithOptions(thread_options);
53 bool TestService::WaitUntilServiceIsStarted() {
54 const base::TimeDelta timeout(TestTimeouts::action_max_timeout());
55 // Wait until all methods are exported.
56 return on_all_methods_exported_.TimedWait(timeout);
59 void TestService::ShutdownAndBlock() {
60 message_loop()->PostTask(
61 FROM_HERE,
62 base::Bind(&TestService::ShutdownAndBlockInternal,
63 base::Unretained(this)));
66 bool TestService::HasDBusThread() {
67 return bus_->HasDBusThread();
70 void TestService::ShutdownAndBlockInternal() {
71 if (HasDBusThread())
72 bus_->ShutdownOnDBusThreadAndBlock();
73 else
74 bus_->ShutdownAndBlock();
77 void TestService::SendTestSignal(const std::string& message) {
78 message_loop()->PostTask(
79 FROM_HERE,
80 base::Bind(&TestService::SendTestSignalInternal,
81 base::Unretained(this),
82 message));
85 void TestService::SendTestSignalFromRoot(const std::string& message) {
86 message_loop()->PostTask(
87 FROM_HERE,
88 base::Bind(&TestService::SendTestSignalFromRootInternal,
89 base::Unretained(this),
90 message));
93 void TestService::SendTestSignalInternal(const std::string& message) {
94 Signal signal("org.chromium.TestInterface", "Test");
95 MessageWriter writer(&signal);
96 writer.AppendString(message);
97 exported_object_->SendSignal(&signal);
100 void TestService::SendTestSignalFromRootInternal(const std::string& message) {
101 Signal signal("org.chromium.TestInterface", "Test");
102 MessageWriter writer(&signal);
103 writer.AppendString(message);
105 bus_->RequestOwnership("org.chromium.TestService",
106 base::Bind(&TestService::OnOwnership,
107 base::Unretained(this),
108 base::Bind(&EmptyCallback)));
110 // Use "/" just like dbus-send does.
111 ExportedObject* root_object = bus_->GetExportedObject(ObjectPath("/"));
112 root_object->SendSignal(&signal);
115 void TestService::RequestOwnership(base::Callback<void(bool)> callback) {
116 message_loop()->PostTask(
117 FROM_HERE,
118 base::Bind(&TestService::RequestOwnershipInternal,
119 base::Unretained(this),
120 callback));
123 void TestService::RequestOwnershipInternal(
124 base::Callback<void(bool)> callback) {
125 bus_->RequestOwnership("org.chromium.TestService",
126 base::Bind(&TestService::OnOwnership,
127 base::Unretained(this),
128 callback));
131 void TestService::OnOwnership(base::Callback<void(bool)> callback,
132 const std::string& service_name,
133 bool success) {
134 has_ownership_ = success;
135 LOG_IF(ERROR, !success) << "Failed to own: " << service_name;
136 callback.Run(success);
139 void TestService::OnExported(const std::string& interface_name,
140 const std::string& method_name,
141 bool success) {
142 if (!success) {
143 LOG(ERROR) << "Failed to export: " << interface_name << "."
144 << method_name;
145 // Returning here will make WaitUntilServiceIsStarted() to time out
146 // and return false.
147 return;
150 ++num_exported_methods_;
151 if (num_exported_methods_ == kNumMethodsToExport)
152 on_all_methods_exported_.Signal();
155 void TestService::Run(base::MessageLoop* message_loop) {
156 Bus::Options bus_options;
157 bus_options.bus_type = Bus::SESSION;
158 bus_options.connection_type = Bus::PRIVATE;
159 bus_options.dbus_task_runner = dbus_task_runner_;
160 bus_ = new Bus(bus_options);
162 bus_->RequestOwnership("org.chromium.TestService",
163 base::Bind(&TestService::OnOwnership,
164 base::Unretained(this),
165 base::Bind(&EmptyCallback)));
167 exported_object_ = bus_->GetExportedObject(
168 ObjectPath("/org/chromium/TestObject"));
170 int num_methods = 0;
171 exported_object_->ExportMethod(
172 "org.chromium.TestInterface",
173 "Echo",
174 base::Bind(&TestService::Echo,
175 base::Unretained(this)),
176 base::Bind(&TestService::OnExported,
177 base::Unretained(this)));
178 ++num_methods;
180 exported_object_->ExportMethod(
181 "org.chromium.TestInterface",
182 "SlowEcho",
183 base::Bind(&TestService::SlowEcho,
184 base::Unretained(this)),
185 base::Bind(&TestService::OnExported,
186 base::Unretained(this)));
187 ++num_methods;
189 exported_object_->ExportMethod(
190 "org.chromium.TestInterface",
191 "AsyncEcho",
192 base::Bind(&TestService::AsyncEcho,
193 base::Unretained(this)),
194 base::Bind(&TestService::OnExported,
195 base::Unretained(this)));
196 ++num_methods;
198 exported_object_->ExportMethod(
199 "org.chromium.TestInterface",
200 "BrokenMethod",
201 base::Bind(&TestService::BrokenMethod,
202 base::Unretained(this)),
203 base::Bind(&TestService::OnExported,
204 base::Unretained(this)));
205 ++num_methods;
207 exported_object_->ExportMethod(
208 "org.chromium.TestInterface",
209 "PerformAction",
210 base::Bind(&TestService::PerformAction,
211 base::Unretained(this)),
212 base::Bind(&TestService::OnExported,
213 base::Unretained(this)));
214 ++num_methods;
216 exported_object_->ExportMethod(
217 kPropertiesInterface,
218 kPropertiesGetAll,
219 base::Bind(&TestService::GetAllProperties,
220 base::Unretained(this)),
221 base::Bind(&TestService::OnExported,
222 base::Unretained(this)));
223 ++num_methods;
225 exported_object_->ExportMethod(
226 kPropertiesInterface,
227 kPropertiesGet,
228 base::Bind(&TestService::GetProperty,
229 base::Unretained(this)),
230 base::Bind(&TestService::OnExported,
231 base::Unretained(this)));
232 ++num_methods;
234 exported_object_->ExportMethod(
235 kPropertiesInterface,
236 kPropertiesSet,
237 base::Bind(&TestService::SetProperty,
238 base::Unretained(this)),
239 base::Bind(&TestService::OnExported,
240 base::Unretained(this)));
241 ++num_methods;
243 exported_object_manager_ = bus_->GetExportedObject(
244 ObjectPath("/org/chromium/TestService"));
246 exported_object_manager_->ExportMethod(
247 kObjectManagerInterface,
248 kObjectManagerGetManagedObjects,
249 base::Bind(&TestService::GetManagedObjects,
250 base::Unretained(this)),
251 base::Bind(&TestService::OnExported,
252 base::Unretained(this)));
253 ++num_methods;
255 // Just print an error message as we don't want to crash tests.
256 // Tests will fail at a call to WaitUntilServiceIsStarted().
257 if (num_methods != kNumMethodsToExport) {
258 LOG(ERROR) << "The number of methods does not match";
260 message_loop->Run();
263 void TestService::Echo(MethodCall* method_call,
264 ExportedObject::ResponseSender response_sender) {
265 MessageReader reader(method_call);
266 std::string text_message;
267 if (!reader.PopString(&text_message)) {
268 response_sender.Run(scoped_ptr<Response>());
269 return;
272 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
273 MessageWriter writer(response.get());
274 writer.AppendString(text_message);
275 response_sender.Run(response.Pass());
278 void TestService::SlowEcho(MethodCall* method_call,
279 ExportedObject::ResponseSender response_sender) {
280 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
281 Echo(method_call, response_sender);
284 void TestService::AsyncEcho(MethodCall* method_call,
285 ExportedObject::ResponseSender response_sender) {
286 // Schedule a call to Echo() to send an asynchronous response after we return.
287 message_loop()->PostDelayedTask(FROM_HERE,
288 base::Bind(&TestService::Echo,
289 base::Unretained(this),
290 method_call,
291 response_sender),
292 TestTimeouts::tiny_timeout());
295 void TestService::BrokenMethod(MethodCall* method_call,
296 ExportedObject::ResponseSender response_sender) {
297 response_sender.Run(scoped_ptr<Response>());
301 void TestService::GetAllProperties(
302 MethodCall* method_call,
303 ExportedObject::ResponseSender response_sender) {
304 MessageReader reader(method_call);
305 std::string interface;
306 if (!reader.PopString(&interface)) {
307 response_sender.Run(scoped_ptr<Response>());
308 return;
311 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
312 MessageWriter writer(response.get());
314 AddPropertiesToWriter(&writer);
316 response_sender.Run(response.Pass());
319 void TestService::GetProperty(MethodCall* method_call,
320 ExportedObject::ResponseSender response_sender) {
321 MessageReader reader(method_call);
322 std::string interface;
323 if (!reader.PopString(&interface)) {
324 response_sender.Run(scoped_ptr<Response>());
325 return;
328 std::string name;
329 if (!reader.PopString(&name)) {
330 response_sender.Run(scoped_ptr<Response>());
331 return;
334 if (name == "Name") {
335 // Return the previous value for the "Name" property:
336 // Variant<"TestService">
337 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
338 MessageWriter writer(response.get());
340 writer.AppendVariantOfString("TestService");
342 response_sender.Run(response.Pass());
343 } else if (name == "Version") {
344 // Return a new value for the "Version" property:
345 // Variant<20>
346 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
347 MessageWriter writer(response.get());
349 writer.AppendVariantOfInt16(20);
351 response_sender.Run(response.Pass());
352 } else if (name == "Methods") {
353 // Return the previous value for the "Methods" property:
354 // Variant<["Echo", "SlowEcho", "AsyncEcho", "BrokenMethod"]>
355 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
356 MessageWriter writer(response.get());
357 MessageWriter variant_writer(NULL);
358 MessageWriter variant_array_writer(NULL);
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);
369 response_sender.Run(response.Pass());
370 } else if (name == "Objects") {
371 // Return the previous value for the "Objects" property:
372 // Variant<[objectpath:"/TestObjectPath"]>
373 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
374 MessageWriter writer(response.get());
375 MessageWriter variant_writer(NULL);
376 MessageWriter variant_array_writer(NULL);
378 writer.OpenVariant("ao", &variant_writer);
379 variant_writer.OpenArray("o", &variant_array_writer);
380 variant_array_writer.AppendObjectPath(ObjectPath("/TestObjectPath"));
381 variant_writer.CloseContainer(&variant_array_writer);
382 writer.CloseContainer(&variant_writer);
384 response_sender.Run(response.Pass());
385 } else {
386 // Return error.
387 response_sender.Run(scoped_ptr<Response>());
388 return;
392 void TestService::SetProperty(MethodCall* method_call,
393 ExportedObject::ResponseSender response_sender) {
394 MessageReader reader(method_call);
395 std::string interface;
396 if (!reader.PopString(&interface)) {
397 response_sender.Run(scoped_ptr<Response>());
398 return;
401 std::string name;
402 if (!reader.PopString(&name)) {
403 response_sender.Run(scoped_ptr<Response>());
404 return;
407 if (name != "Name") {
408 response_sender.Run(scoped_ptr<Response>());
409 return;
412 std::string value;
413 if (!reader.PopVariantOfString(&value)) {
414 response_sender.Run(scoped_ptr<Response>());
415 return;
418 SendPropertyChangedSignal(value);
420 response_sender.Run(Response::FromMethodCall(method_call));
423 void TestService::PerformAction(
424 MethodCall* method_call,
425 ExportedObject::ResponseSender response_sender) {
426 MessageReader reader(method_call);
427 std::string action;
428 ObjectPath object_path;
429 if (!reader.PopString(&action) || !reader.PopObjectPath(&object_path)) {
430 response_sender.Run(scoped_ptr<Response>());
431 return;
434 if (action == "AddObject")
435 AddObject(object_path);
436 else if (action == "RemoveObject")
437 RemoveObject(object_path);
439 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
440 response_sender.Run(response.Pass());
443 void TestService::GetManagedObjects(
444 MethodCall* method_call,
445 ExportedObject::ResponseSender response_sender) {
446 scoped_ptr<Response> response = Response::FromMethodCall(method_call);
447 MessageWriter writer(response.get());
449 // The managed objects response is a dictionary of object paths identifying
450 // the object(s) with a dictionary of strings identifying the interface(s)
451 // they implement and then a dictionary of property values.
453 // Thus this looks something like:
455 // {
456 // "/org/chromium/TestObject": {
457 // "org.chromium.TestInterface": { /* Properties */ }
458 // }
459 // }
462 MessageWriter array_writer(NULL);
463 MessageWriter dict_entry_writer(NULL);
464 MessageWriter object_array_writer(NULL);
465 MessageWriter object_dict_entry_writer(NULL);
467 writer.OpenArray("{oa{sa{sv}}}", &array_writer);
469 array_writer.OpenDictEntry(&dict_entry_writer);
470 dict_entry_writer.AppendObjectPath(ObjectPath("/org/chromium/TestObject"));
471 dict_entry_writer.OpenArray("{sa{sv}}", &object_array_writer);
473 object_array_writer.OpenDictEntry(&object_dict_entry_writer);
474 object_dict_entry_writer.AppendString("org.chromium.TestInterface");
475 AddPropertiesToWriter(&object_dict_entry_writer);
476 object_array_writer.CloseContainer(&object_dict_entry_writer);
478 dict_entry_writer.CloseContainer(&object_array_writer);
480 array_writer.CloseContainer(&dict_entry_writer);
481 writer.CloseContainer(&array_writer);
483 response_sender.Run(response.Pass());
486 void TestService::AddPropertiesToWriter(MessageWriter* writer) {
487 // The properties response is a dictionary of strings identifying the
488 // property and a variant containing the property value. We return all
489 // of the properties, thus the response is:
491 // {
492 // "Name": Variant<"TestService">,
493 // "Version": Variant<10>,
494 // "Methods": Variant<["Echo", "SlowEcho", "AsyncEcho", "BrokenMethod"]>,
495 // "Objects": Variant<[objectpath:"/TestObjectPath"]>
496 // }
498 MessageWriter array_writer(NULL);
499 MessageWriter dict_entry_writer(NULL);
500 MessageWriter variant_writer(NULL);
501 MessageWriter variant_array_writer(NULL);
503 writer->OpenArray("{sv}", &array_writer);
505 array_writer.OpenDictEntry(&dict_entry_writer);
506 dict_entry_writer.AppendString("Name");
507 dict_entry_writer.AppendVariantOfString("TestService");
508 array_writer.CloseContainer(&dict_entry_writer);
510 array_writer.OpenDictEntry(&dict_entry_writer);
511 dict_entry_writer.AppendString("Version");
512 dict_entry_writer.AppendVariantOfInt16(10);
513 array_writer.CloseContainer(&dict_entry_writer);
515 array_writer.OpenDictEntry(&dict_entry_writer);
516 dict_entry_writer.AppendString("Methods");
517 dict_entry_writer.OpenVariant("as", &variant_writer);
518 variant_writer.OpenArray("s", &variant_array_writer);
519 variant_array_writer.AppendString("Echo");
520 variant_array_writer.AppendString("SlowEcho");
521 variant_array_writer.AppendString("AsyncEcho");
522 variant_array_writer.AppendString("BrokenMethod");
523 variant_writer.CloseContainer(&variant_array_writer);
524 dict_entry_writer.CloseContainer(&variant_writer);
525 array_writer.CloseContainer(&dict_entry_writer);
527 array_writer.OpenDictEntry(&dict_entry_writer);
528 dict_entry_writer.AppendString("Objects");
529 dict_entry_writer.OpenVariant("ao", &variant_writer);
530 variant_writer.OpenArray("o", &variant_array_writer);
531 variant_array_writer.AppendObjectPath(ObjectPath("/TestObjectPath"));
532 variant_writer.CloseContainer(&variant_array_writer);
533 dict_entry_writer.CloseContainer(&variant_writer);
534 array_writer.CloseContainer(&dict_entry_writer);
536 writer->CloseContainer(&array_writer);
539 void TestService::AddObject(const ObjectPath& object_path) {
540 message_loop()->PostTask(
541 FROM_HERE,
542 base::Bind(&TestService::AddObjectInternal,
543 base::Unretained(this),
544 object_path));
547 void TestService::AddObjectInternal(const ObjectPath& object_path) {
548 Signal signal(kObjectManagerInterface, kObjectManagerInterfacesAdded);
549 MessageWriter writer(&signal);
550 writer.AppendObjectPath(object_path);
552 MessageWriter array_writer(NULL);
553 MessageWriter dict_entry_writer(NULL);
555 writer.OpenArray("{sa{sv}}", &array_writer);
556 array_writer.OpenDictEntry(&dict_entry_writer);
557 dict_entry_writer.AppendString("org.chromium.TestInterface");
558 AddPropertiesToWriter(&dict_entry_writer);
559 array_writer.CloseContainer(&dict_entry_writer);
560 writer.CloseContainer(&array_writer);
562 exported_object_manager_->SendSignal(&signal);
565 void TestService::RemoveObject(const ObjectPath& object_path) {
566 message_loop()->PostTask(FROM_HERE,
567 base::Bind(&TestService::RemoveObjectInternal,
568 base::Unretained(this),
569 object_path));
572 void TestService::RemoveObjectInternal(const ObjectPath& object_path) {
573 Signal signal(kObjectManagerInterface, kObjectManagerInterfacesRemoved);
574 MessageWriter writer(&signal);
576 writer.AppendObjectPath(object_path);
578 std::vector<std::string> interfaces;
579 interfaces.push_back("org.chromium.TestInterface");
580 writer.AppendArrayOfStrings(interfaces);
582 exported_object_manager_->SendSignal(&signal);
585 void TestService::SendPropertyChangedSignal(const std::string& name) {
586 message_loop()->PostTask(
587 FROM_HERE,
588 base::Bind(&TestService::SendPropertyChangedSignalInternal,
589 base::Unretained(this),
590 name));
593 void TestService::SendPropertyChangedSignalInternal(const std::string& name) {
594 Signal signal(kPropertiesInterface, kPropertiesChanged);
595 MessageWriter writer(&signal);
596 writer.AppendString("org.chromium.TestInterface");
598 MessageWriter array_writer(NULL);
599 MessageWriter dict_entry_writer(NULL);
601 writer.OpenArray("{sv}", &array_writer);
602 array_writer.OpenDictEntry(&dict_entry_writer);
603 dict_entry_writer.AppendString("Name");
604 dict_entry_writer.AppendVariantOfString(name);
605 array_writer.CloseContainer(&dict_entry_writer);
606 writer.CloseContainer(&array_writer);
608 exported_object_->SendSignal(&signal);
611 } // namespace dbus