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
ash / dbus / gesture_properties_service_provider.cc [blame]
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/dbus/gesture_properties_service_provider.h"
#include <memory>
#include <utility>
#include "base/functional/bind.h"
#include "base/logging.h"
#include "dbus/message.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
#include "ui/ozone/public/input_controller.h"
#include "ui/ozone/public/ozone_platform.h"
namespace ash {
GesturePropertiesServiceProvider::GesturePropertiesServiceProvider()
: weak_ptr_factory_(this) {}
GesturePropertiesServiceProvider::~GesturePropertiesServiceProvider() = default;
void GesturePropertiesServiceProvider::Start(
scoped_refptr<dbus::ExportedObject> exported_object) {
auto on_exported =
base::BindRepeating(&GesturePropertiesServiceProvider::OnExported,
weak_ptr_factory_.GetWeakPtr());
exported_object->ExportMethod(
chromeos::kGesturePropertiesServiceInterface,
chromeos::kGesturePropertiesServiceListDevicesMethod,
base::BindRepeating(&GesturePropertiesServiceProvider::ListDevices,
weak_ptr_factory_.GetWeakPtr()),
on_exported);
exported_object->ExportMethod(
chromeos::kGesturePropertiesServiceInterface,
chromeos::kGesturePropertiesServiceListPropertiesMethod,
base::BindRepeating(&GesturePropertiesServiceProvider::ListProperties,
weak_ptr_factory_.GetWeakPtr()),
on_exported);
exported_object->ExportMethod(
chromeos::kGesturePropertiesServiceInterface,
chromeos::kGesturePropertiesServiceGetPropertyMethod,
base::BindRepeating(&GesturePropertiesServiceProvider::GetProperty,
weak_ptr_factory_.GetWeakPtr()),
on_exported);
exported_object->ExportMethod(
chromeos::kGesturePropertiesServiceInterface,
chromeos::kGesturePropertiesServiceSetPropertyMethod,
base::BindRepeating(&GesturePropertiesServiceProvider::SetProperty,
weak_ptr_factory_.GetWeakPtr()),
on_exported);
}
void GesturePropertiesServiceProvider::OnExported(
const std::string& interface_name,
const std::string& method_name,
bool success) {
if (!success)
LOG(ERROR) << "Failed to export " << interface_name << "." << method_name;
}
namespace {
void GetPropertyCallback(dbus::MethodCall* method_call,
std::unique_ptr<dbus::Response> response,
dbus::ExportedObject::ResponseSender response_sender,
bool is_read_only,
ui::ozone::mojom::GesturePropValuePtr values) {
if (values.is_null()) {
std::move(response_sender)
.Run(dbus::ErrorResponse::FromMethodCall(
method_call, DBUS_ERROR_INVALID_ARGS,
"The device ID or property name specified was not found."));
return;
}
dbus::MessageWriter writer(response.get());
dbus::MessageWriter variant_writer(nullptr);
dbus::MessageWriter array_writer(nullptr);
writer.AppendBool(is_read_only);
switch (values->which()) {
case ui::ozone::mojom::GesturePropValue::Tag::kInts: {
writer.AppendUint32(values->get_ints().size());
writer.OpenVariant("ai", &variant_writer);
variant_writer.AppendArrayOfInt32s(values->get_ints());
writer.CloseContainer(&variant_writer);
break;
}
case ui::ozone::mojom::GesturePropValue::Tag::kShorts: {
writer.AppendUint32(values->get_shorts().size());
writer.OpenVariant("an", &variant_writer);
variant_writer.OpenArray("n", &array_writer);
for (int16_t value : values->get_shorts()) {
array_writer.AppendInt16(value);
}
variant_writer.CloseContainer(&array_writer);
writer.CloseContainer(&variant_writer);
break;
}
case ui::ozone::mojom::GesturePropValue::Tag::kBools: {
writer.AppendUint32(values->get_bools().size());
writer.OpenVariant("ab", &variant_writer);
variant_writer.OpenArray("b", &array_writer);
for (bool value : values->get_bools()) {
array_writer.AppendBool(value);
}
variant_writer.CloseContainer(&array_writer);
writer.CloseContainer(&variant_writer);
break;
}
case ui::ozone::mojom::GesturePropValue::Tag::kStr: {
writer.AppendUint32(1);
writer.AppendVariantOfString(values->get_str());
break;
}
case ui::ozone::mojom::GesturePropValue::Tag::kReals: {
writer.AppendUint32(values->get_reals().size());
writer.OpenVariant("ad", &variant_writer);
variant_writer.AppendArrayOfDoubles(values->get_reals());
writer.CloseContainer(&variant_writer);
break;
}
default: {
// This should never happen.
LOG(WARNING) << "No value set on GesturePropValue union; not returning "
"values to GetProperty call.";
writer.AppendUint32(0);
break;
}
}
std::move(response_sender).Run(std::move(response));
}
void SetPropertyCallback(dbus::MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender,
ui::ozone::mojom::SetGesturePropErrorCode error) {
std::string error_message;
switch (error) {
case ui::ozone::mojom::SetGesturePropErrorCode::SUCCESS:
std::move(response_sender)
.Run(dbus::Response::FromMethodCall(method_call));
return;
case ui::ozone::mojom::SetGesturePropErrorCode::NOT_FOUND:
error_message = "The device ID or property name specified was not found.";
break;
case ui::ozone::mojom::SetGesturePropErrorCode::READ_ONLY:
error_message = "That property is read-only.";
break;
case ui::ozone::mojom::SetGesturePropErrorCode::TYPE_MISMATCH:
error_message =
"The property is of a different type than the value(s) "
"provided.";
break;
case ui::ozone::mojom::SetGesturePropErrorCode::SIZE_MISMATCH:
error_message =
"The property has a different number of values to that "
"provided.";
break;
case ui::ozone::mojom::SetGesturePropErrorCode::UNKNOWN_ERROR:
default:
error_message = "An unknown error occurred.";
break;
}
LOG(ERROR) << "SetProperty error: " << error_message;
std::move(response_sender)
.Run(dbus::ErrorResponse::FromMethodCall(
method_call, DBUS_ERROR_INVALID_ARGS, error_message));
}
void ListDevicesCallback(std::unique_ptr<dbus::Response> response,
dbus::ExportedObject::ResponseSender response_sender,
const base::flat_map<int, std::string>& result) {
dbus::MessageWriter writer(response.get());
writer.AppendUint32(result.size());
dbus::MessageWriter dict_writer(nullptr);
writer.OpenArray("{is}", &dict_writer);
for (const auto& pair : result) {
dbus::MessageWriter dict_entry_writer(nullptr);
dict_writer.OpenDictEntry(&dict_entry_writer);
dict_entry_writer.AppendInt32(pair.first);
dict_entry_writer.AppendString(pair.second);
dict_writer.CloseContainer(&dict_entry_writer);
}
writer.CloseContainer(&dict_writer);
std::move(response_sender).Run(std::move(response));
}
void ListPropertiesCallback(
std::unique_ptr<dbus::Response> response,
dbus::ExportedObject::ResponseSender response_sender,
const std::vector<std::string>& result) {
dbus::MessageWriter writer(response.get());
writer.AppendUint32(result.size());
writer.AppendArrayOfStrings(result);
std::move(response_sender).Run(std::move(response));
}
} // namespace
void GesturePropertiesServiceProvider::ListDevices(
dbus::MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender) {
std::unique_ptr<dbus::Response> response =
dbus::Response::FromMethodCall(method_call);
GetService()->ListDevices(base::BindOnce(
&ListDevicesCallback, std::move(response), std::move(response_sender)));
}
void GesturePropertiesServiceProvider::ListProperties(
dbus::MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender) {
dbus::MessageReader reader(method_call);
int32_t device_id;
if (!reader.PopInt32(&device_id)) {
std::move(response_sender)
.Run(dbus::ErrorResponse::FromMethodCall(
method_call, DBUS_ERROR_INVALID_ARGS,
"The device ID (int32) is missing."));
return;
}
std::unique_ptr<dbus::Response> response =
dbus::Response::FromMethodCall(method_call);
GetService()->ListProperties(
device_id, base::BindOnce(&ListPropertiesCallback, std::move(response),
std::move(response_sender)));
}
void GesturePropertiesServiceProvider::GetProperty(
dbus::MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender) {
dbus::MessageReader reader(method_call);
int32_t device_id;
std::string property_name;
if (!reader.PopInt32(&device_id) || !reader.PopString(&property_name)) {
std::move(response_sender)
.Run(dbus::ErrorResponse::FromMethodCall(
method_call, DBUS_ERROR_INVALID_ARGS,
"The device ID (int32) and/or property name (string) is missing."));
return;
}
std::unique_ptr<dbus::Response> response =
dbus::Response::FromMethodCall(method_call);
GetService()->GetProperty(
device_id, property_name,
base::BindOnce(&GetPropertyCallback, method_call, std::move(response),
std::move(response_sender)));
}
static ui::ozone::mojom::GesturePropValuePtr GesturePropValueFromVariant(
dbus::MessageReader* variant_reader,
std::string* error_message) {
std::string string_value;
if (variant_reader->PopString(&string_value)) {
return ui::ozone::mojom::GesturePropValue::NewStr(string_value);
}
dbus::MessageReader array_reader(nullptr);
if (!variant_reader->PopArray(&array_reader)) {
*error_message =
"Value(s) should be specified either as a string or an "
"array of the appropriate type.";
return nullptr;
}
switch (array_reader.GetDataType()) {
case dbus::Message::DataType::INT32: {
std::vector<int32_t> values = {};
int32_t value;
while (array_reader.PopInt32(&value)) {
values.push_back(value);
}
return ui::ozone::mojom::GesturePropValue::NewInts(values);
}
case dbus::Message::DataType::INT16: {
std::vector<int16_t> values = {};
int16_t value;
while (array_reader.PopInt16(&value)) {
values.push_back(value);
}
return ui::ozone::mojom::GesturePropValue::NewShorts(values);
}
case dbus::Message::DataType::BOOL: {
std::vector<bool> values = {};
bool value;
while (array_reader.PopBool(&value)) {
values.push_back(value);
}
return ui::ozone::mojom::GesturePropValue::NewBools(values);
}
case dbus::Message::DataType::DOUBLE: {
std::vector<double> values = {};
double value;
while (array_reader.PopDouble(&value)) {
values.push_back(value);
}
return ui::ozone::mojom::GesturePropValue::NewReals(values);
}
case dbus::Message::DataType::STRING: {
*error_message =
"String properties can only have one value, and so "
"should not be specified as arrays.";
return nullptr;
}
default: {
*error_message =
"Unsupported D-Bus value type; supported types are "
"int32, int16, bool, double, and string.";
return nullptr;
}
}
}
void GesturePropertiesServiceProvider::SetProperty(
dbus::MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender) {
dbus::MessageReader reader(method_call);
int32_t device_id;
std::string property_name;
if (!reader.PopInt32(&device_id) || !reader.PopString(&property_name)) {
std::move(response_sender)
.Run(dbus::ErrorResponse::FromMethodCall(
method_call, DBUS_ERROR_INVALID_ARGS,
"The device ID (int32) and/or property name (string) is missing."));
return;
}
if (!reader.HasMoreData()) {
std::move(response_sender)
.Run(dbus::ErrorResponse::FromMethodCall(
method_call, DBUS_ERROR_INVALID_ARGS, "No value(s) specified."));
return;
}
std::string error_message;
ui::ozone::mojom::GesturePropValuePtr values =
GesturePropValueFromVariant(&reader, &error_message);
if (values.is_null()) {
std::move(response_sender)
.Run(dbus::ErrorResponse::FromMethodCall(
method_call, DBUS_ERROR_INVALID_ARGS, error_message));
return;
}
GetService()->SetProperty(device_id, property_name, std::move(values),
base::BindOnce(&SetPropertyCallback, method_call,
std::move(response_sender)));
}
ui::ozone::mojom::GesturePropertiesService*
GesturePropertiesServiceProvider::GetService() {
if (service_for_test_ != nullptr)
return service_for_test_;
if (!service_.is_bound()) {
ui::OzonePlatform::GetInstance()
->GetInputController()
->GetGesturePropertiesService(service_.BindNewPipeAndPassReceiver());
}
return service_.get();
}
} // namespace ash