1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "dbus/test_service.h"
#include "base/bind.h"
#include "base/test/test_timeouts.h"
#include "base/threading/platform_thread.h"
#include "dbus/bus.h"
#include "dbus/exported_object.h"
#include "dbus/message.h"
#include "dbus/object_path.h"
#include "dbus/property.h"
namespace dbus {
// Echo, SlowEcho, AsyncEcho, BrokenMethod, GetAll, Get, Set.
const int TestService::kNumMethodsToExport = 7;
TestService::Options::Options() {
}
TestService::Options::~Options() {
}
TestService::TestService(const Options& options)
: base::Thread("TestService"),
dbus_thread_message_loop_proxy_(options.dbus_thread_message_loop_proxy),
on_all_methods_exported_(false, false),
num_exported_methods_(0) {
}
TestService::~TestService() {
Stop();
}
bool TestService::StartService() {
base::Thread::Options thread_options;
thread_options.message_loop_type = MessageLoop::TYPE_IO;
return StartWithOptions(thread_options);
}
bool TestService::WaitUntilServiceIsStarted() {
const base::TimeDelta timeout(
base::TimeDelta::FromMilliseconds(
TestTimeouts::action_max_timeout_ms()));
// Wait until all methods are exported.
return on_all_methods_exported_.TimedWait(timeout);
}
void TestService::ShutdownAndBlock() {
message_loop()->PostTask(
FROM_HERE,
base::Bind(&TestService::ShutdownAndBlockInternal,
base::Unretained(this)));
}
bool TestService::HasDBusThread() {
return bus_->HasDBusThread();
}
void TestService::ShutdownAndBlockInternal() {
if (HasDBusThread())
bus_->ShutdownOnDBusThreadAndBlock();
else
bus_->ShutdownAndBlock();
}
void TestService::SendTestSignal(const std::string& message) {
message_loop()->PostTask(
FROM_HERE,
base::Bind(&TestService::SendTestSignalInternal,
base::Unretained(this),
message));
}
void TestService::SendTestSignalFromRoot(const std::string& message) {
message_loop()->PostTask(
FROM_HERE,
base::Bind(&TestService::SendTestSignalFromRootInternal,
base::Unretained(this),
message));
}
void TestService::SendTestSignalInternal(const std::string& message) {
dbus::Signal signal("org.chromium.TestInterface", "Test");
dbus::MessageWriter writer(&signal);
writer.AppendString(message);
exported_object_->SendSignal(&signal);
}
void TestService::SendTestSignalFromRootInternal(const std::string& message) {
dbus::Signal signal("org.chromium.TestInterface", "Test");
dbus::MessageWriter writer(&signal);
writer.AppendString(message);
bus_->RequestOwnership("org.chromium.TestService",
base::Bind(&TestService::OnOwnership,
base::Unretained(this)));
// Use "/" just like dbus-send does.
ExportedObject* root_object =
bus_->GetExportedObject(dbus::ObjectPath("/"));
root_object->SendSignal(&signal);
}
void TestService::OnOwnership(const std::string& service_name,
bool success) {
LOG_IF(ERROR, !success) << "Failed to own: " << service_name;
}
void TestService::OnExported(const std::string& interface_name,
const std::string& method_name,
bool success) {
if (!success) {
LOG(ERROR) << "Failed to export: " << interface_name << "."
<< method_name;
// Returning here will make WaitUntilServiceIsStarted() to time out
// and return false.
return;
}
++num_exported_methods_;
if (num_exported_methods_ == kNumMethodsToExport)
on_all_methods_exported_.Signal();
}
void TestService::Run(MessageLoop* message_loop) {
Bus::Options bus_options;
bus_options.bus_type = Bus::SESSION;
bus_options.connection_type = Bus::PRIVATE;
bus_options.dbus_thread_message_loop_proxy = dbus_thread_message_loop_proxy_;
bus_ = new Bus(bus_options);
bus_->RequestOwnership("org.chromium.TestService",
base::Bind(&TestService::OnOwnership,
base::Unretained(this)));
exported_object_ = bus_->GetExportedObject(
dbus::ObjectPath("/org/chromium/TestObject"));
int num_methods = 0;
exported_object_->ExportMethod(
"org.chromium.TestInterface",
"Echo",
base::Bind(&TestService::Echo,
base::Unretained(this)),
base::Bind(&TestService::OnExported,
base::Unretained(this)));
++num_methods;
exported_object_->ExportMethod(
"org.chromium.TestInterface",
"SlowEcho",
base::Bind(&TestService::SlowEcho,
base::Unretained(this)),
base::Bind(&TestService::OnExported,
base::Unretained(this)));
++num_methods;
exported_object_->ExportMethod(
"org.chromium.TestInterface",
"AsyncEcho",
base::Bind(&TestService::AsyncEcho,
base::Unretained(this)),
base::Bind(&TestService::OnExported,
base::Unretained(this)));
++num_methods;
exported_object_->ExportMethod(
"org.chromium.TestInterface",
"BrokenMethod",
base::Bind(&TestService::BrokenMethod,
base::Unretained(this)),
base::Bind(&TestService::OnExported,
base::Unretained(this)));
++num_methods;
exported_object_->ExportMethod(
kPropertiesInterface,
kPropertiesGetAll,
base::Bind(&TestService::GetAllProperties,
base::Unretained(this)),
base::Bind(&TestService::OnExported,
base::Unretained(this)));
++num_methods;
exported_object_->ExportMethod(
kPropertiesInterface,
kPropertiesGet,
base::Bind(&TestService::GetProperty,
base::Unretained(this)),
base::Bind(&TestService::OnExported,
base::Unretained(this)));
++num_methods;
exported_object_->ExportMethod(
kPropertiesInterface,
kPropertiesSet,
base::Bind(&TestService::SetProperty,
base::Unretained(this)),
base::Bind(&TestService::OnExported,
base::Unretained(this)));
++num_methods;
// Just print an error message as we don't want to crash tests.
// Tests will fail at a call to WaitUntilServiceIsStarted().
if (num_methods != kNumMethodsToExport) {
LOG(ERROR) << "The number of methods does not match";
}
message_loop->Run();
}
void TestService::Echo(MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender) {
MessageReader reader(method_call);
std::string text_message;
if (!reader.PopString(&text_message)) {
response_sender.Run(NULL);
return;
}
Response* response = Response::FromMethodCall(method_call);
MessageWriter writer(response);
writer.AppendString(text_message);
response_sender.Run(response);
}
void TestService::SlowEcho(
MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender) {
base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
Echo(method_call, response_sender);
}
void TestService::AsyncEcho(
MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender) {
// Schedule a call to Echo() to send an asynchronous response after we return.
message_loop()->PostDelayedTask(FROM_HERE,
base::Bind(&TestService::Echo,
base::Unretained(this),
method_call,
response_sender),
TestTimeouts::tiny_timeout());
}
void TestService::BrokenMethod(
MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender) {
response_sender.Run(NULL);
}
void TestService::GetAllProperties(
MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender) {
MessageReader reader(method_call);
std::string interface;
if (!reader.PopString(&interface)) {
response_sender.Run(NULL);
return;
}
// The properties response is a dictionary of strings identifying the
// property and a variant containing the property value. We return all
// of the properties, thus the response is:
//
// {
// "Name": Variant<"TestService">,
// "Version": Variant<10>,
// "Methods": Variant<["Echo", "SlowEcho", "AsyncEcho", "BrokenMethod"]>,
// "Objects": Variant<[objectpath:"/TestObjectPath"]>
// ]
Response* response = Response::FromMethodCall(method_call);
MessageWriter writer(response);
MessageWriter array_writer(NULL);
MessageWriter dict_entry_writer(NULL);
MessageWriter variant_writer(NULL);
MessageWriter variant_array_writer(NULL);
writer.OpenArray("{sv}", &array_writer);
array_writer.OpenDictEntry(&dict_entry_writer);
dict_entry_writer.AppendString("Name");
dict_entry_writer.AppendVariantOfString("TestService");
array_writer.CloseContainer(&dict_entry_writer);
array_writer.OpenDictEntry(&dict_entry_writer);
dict_entry_writer.AppendString("Version");
dict_entry_writer.AppendVariantOfInt16(10);
array_writer.CloseContainer(&dict_entry_writer);
array_writer.OpenDictEntry(&dict_entry_writer);
dict_entry_writer.AppendString("Methods");
dict_entry_writer.OpenVariant("as", &variant_writer);
variant_writer.OpenArray("s", &variant_array_writer);
variant_array_writer.AppendString("Echo");
variant_array_writer.AppendString("SlowEcho");
variant_array_writer.AppendString("AsyncEcho");
variant_array_writer.AppendString("BrokenMethod");
variant_writer.CloseContainer(&variant_array_writer);
dict_entry_writer.CloseContainer(&variant_writer);
array_writer.CloseContainer(&dict_entry_writer);
array_writer.OpenDictEntry(&dict_entry_writer);
dict_entry_writer.AppendString("Objects");
dict_entry_writer.OpenVariant("ao", &variant_writer);
variant_writer.OpenArray("o", &variant_array_writer);
variant_array_writer.AppendObjectPath(dbus::ObjectPath("/TestObjectPath"));
variant_writer.CloseContainer(&variant_array_writer);
dict_entry_writer.CloseContainer(&variant_writer);
array_writer.CloseContainer(&dict_entry_writer);
writer.CloseContainer(&array_writer);
response_sender.Run(response);
}
void TestService::GetProperty(
MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender) {
MessageReader reader(method_call);
std::string interface;
if (!reader.PopString(&interface)) {
response_sender.Run(NULL);
return;
}
std::string name;
if (!reader.PopString(&name)) {
response_sender.Run(NULL);
return;
}
if (name == "Name") {
// Return the previous value for the "Name" property:
// Variant<"TestService">
Response* response = Response::FromMethodCall(method_call);
MessageWriter writer(response);
writer.AppendVariantOfString("TestService");
response_sender.Run(response);
} else if (name == "Version") {
// Return a new value for the "Version" property:
// Variant<20>
Response* response = Response::FromMethodCall(method_call);
MessageWriter writer(response);
writer.AppendVariantOfInt16(20);
response_sender.Run(response);
} else if (name == "Methods") {
// Return the previous value for the "Methods" property:
// Variant<["Echo", "SlowEcho", "AsyncEcho", "BrokenMethod"]>
Response* response = Response::FromMethodCall(method_call);
MessageWriter writer(response);
MessageWriter variant_writer(NULL);
MessageWriter variant_array_writer(NULL);
writer.OpenVariant("as", &variant_writer);
variant_writer.OpenArray("s", &variant_array_writer);
variant_array_writer.AppendString("Echo");
variant_array_writer.AppendString("SlowEcho");
variant_array_writer.AppendString("AsyncEcho");
variant_array_writer.AppendString("BrokenMethod");
variant_writer.CloseContainer(&variant_array_writer);
writer.CloseContainer(&variant_writer);
response_sender.Run(response);
} else if (name == "Objects") {
// Return the previous value for the "Objects" property:
// Variant<[objectpath:"/TestObjectPath"]>
Response* response = Response::FromMethodCall(method_call);
MessageWriter writer(response);
MessageWriter variant_writer(NULL);
MessageWriter variant_array_writer(NULL);
writer.OpenVariant("ao", &variant_writer);
variant_writer.OpenArray("o", &variant_array_writer);
variant_array_writer.AppendObjectPath(dbus::ObjectPath("/TestObjectPath"));
variant_writer.CloseContainer(&variant_array_writer);
writer.CloseContainer(&variant_writer);
response_sender.Run(response);
} else {
// Return error.
response_sender.Run(NULL);
return;
}
}
void TestService::SetProperty(
MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender) {
MessageReader reader(method_call);
std::string interface;
if (!reader.PopString(&interface)) {
response_sender.Run(NULL);
return;
}
std::string name;
if (!reader.PopString(&name)) {
response_sender.Run(NULL);
return;
}
if (name != "Name") {
response_sender.Run(NULL);
return;
}
std::string value;
if (!reader.PopVariantOfString(&value)) {
response_sender.Run(NULL);
return;
}
SendPropertyChangedSignal(value);
Response* response = Response::FromMethodCall(method_call);
response_sender.Run(response);
}
void TestService::SendPropertyChangedSignal(const std::string& name) {
message_loop()->PostTask(
FROM_HERE,
base::Bind(&TestService::SendPropertyChangedSignalInternal,
base::Unretained(this),
name));
}
void TestService::SendPropertyChangedSignalInternal(const std::string& name) {
dbus::Signal signal(kPropertiesInterface, kPropertiesChanged);
dbus::MessageWriter writer(&signal);
writer.AppendString("org.chromium.TestService");
MessageWriter array_writer(NULL);
MessageWriter dict_entry_writer(NULL);
writer.OpenArray("{sv}", &array_writer);
array_writer.OpenDictEntry(&dict_entry_writer);
dict_entry_writer.AppendString("Name");
dict_entry_writer.AppendVariantOfString(name);
array_writer.CloseContainer(&dict_entry_writer);
writer.CloseContainer(&array_writer);
exported_object_->SendSignal(&signal);
}
} // namespace dbus
|