blob: 41975898c03c5f866120be5ac7946592c47460fa [file] [log] [blame]
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +09001// 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.
4
5#include "dbus/property.h"
6
avi0ad0ce02015-12-23 03:12:45 +09007#include <stddef.h>
8
puthik90fd20c2016-10-06 14:03:03 +09009#include <memory>
10
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090011#include "base/bind.h"
12#include "base/logging.h"
13
14#include "dbus/message.h"
15#include "dbus/object_path.h"
16#include "dbus/object_proxy.h"
17
18namespace dbus {
19
20//
21// PropertyBase implementation.
22//
23
derat3f868a42015-07-17 00:06:44 +090024PropertyBase::PropertyBase() : property_set_(nullptr), is_valid_(false) {}
25
Chris Watkins635e8902017-11-29 16:44:11 +090026PropertyBase::~PropertyBase() = default;
derat3f868a42015-07-17 00:06:44 +090027
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090028void PropertyBase::Init(PropertySet* property_set, const std::string& name) {
29 DCHECK(!property_set_);
30 property_set_ = property_set;
jpawlowski77e73fc2015-05-11 20:07:04 +090031 is_valid_ = false;
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090032 name_ = name;
33}
34
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090035//
36// PropertySet implementation.
37//
38
keybuk@chromium.orgb45e5292012-08-15 10:03:30 +090039PropertySet::PropertySet(
40 ObjectProxy* object_proxy,
41 const std::string& interface,
42 const PropertyChangedCallback& property_changed_callback)
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090043 : object_proxy_(object_proxy),
44 interface_(interface),
45 property_changed_callback_(property_changed_callback),
46 weak_ptr_factory_(this) {}
47
Chris Watkins635e8902017-11-29 16:44:11 +090048PropertySet::~PropertySet() = default;
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090049
50void PropertySet::RegisterProperty(const std::string& name,
51 PropertyBase* property) {
52 property->Init(this, name);
53 properties_map_[name] = property;
54}
55
56void PropertySet::ConnectSignals() {
57 DCHECK(object_proxy_);
58 object_proxy_->ConnectToSignal(
59 kPropertiesInterface,
60 kPropertiesChanged,
61 base::Bind(&PropertySet::ChangedReceived,
62 weak_ptr_factory_.GetWeakPtr()),
63 base::Bind(&PropertySet::ChangedConnected,
64 weak_ptr_factory_.GetWeakPtr()));
65}
66
67
68void PropertySet::ChangedReceived(Signal* signal) {
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090069 DCHECK(signal);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090070 MessageReader reader(signal);
71
72 std::string interface;
73 if (!reader.PopString(&interface)) {
74 LOG(WARNING) << "Property changed signal has wrong parameters: "
75 << "expected interface name: " << signal->ToString();
76 return;
77 }
78
79 if (interface != this->interface())
80 return;
81
82 if (!UpdatePropertiesFromReader(&reader)) {
83 LOG(WARNING) << "Property changed signal has wrong parameters: "
84 << "expected dictionary: " << signal->ToString();
85 }
86
jpawlowski77e73fc2015-05-11 20:07:04 +090087 if (!InvalidatePropertiesFromReader(&reader)) {
88 LOG(WARNING) << "Property changed signal has wrong parameters: "
89 << "expected array to invalidate: " << signal->ToString();
90 }
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +090091}
92
93void PropertySet::ChangedConnected(const std::string& interface_name,
94 const std::string& signal_name,
95 bool success) {
96 LOG_IF(WARNING, !success) << "Failed to connect to " << signal_name
97 << "signal.";
98}
99
100
keybuk@chromium.org56028ac2012-06-29 03:43:30 +0900101void PropertySet::Get(PropertyBase* property, GetCallback callback) {
102 MethodCall method_call(kPropertiesInterface, kPropertiesGet);
103 MessageWriter writer(&method_call);
104 writer.AppendString(interface());
105 writer.AppendString(property->name());
106
107 DCHECK(object_proxy_);
108 object_proxy_->CallMethod(&method_call,
109 ObjectProxy::TIMEOUT_USE_DEFAULT,
110 base::Bind(&PropertySet::OnGet,
111 GetWeakPtr(),
112 property,
113 callback));
114}
115
116void PropertySet::OnGet(PropertyBase* property, GetCallback callback,
117 Response* response) {
118 if (!response) {
119 LOG(WARNING) << property->name() << ": Get: failed.";
120 return;
121 }
122
123 MessageReader reader(response);
jpawlowski77e73fc2015-05-11 20:07:04 +0900124 if (property->PopValueFromReader(&reader)) {
125 property->set_valid(true);
keybuk@chromium.org56028ac2012-06-29 03:43:30 +0900126 NotifyPropertyChanged(property->name());
jpawlowski77e73fc2015-05-11 20:07:04 +0900127 } else {
128 if (property->is_valid()) {
129 property->set_valid(false);
130 NotifyPropertyChanged(property->name());
131 }
132 }
keybuk@chromium.org56028ac2012-06-29 03:43:30 +0900133
134 if (!callback.is_null())
135 callback.Run(response);
136}
137
nywang80f27162015-09-29 07:46:36 +0900138bool PropertySet::GetAndBlock(PropertyBase* property) {
139 MethodCall method_call(kPropertiesInterface, kPropertiesGet);
140 MessageWriter writer(&method_call);
141 writer.AppendString(interface());
142 writer.AppendString(property->name());
143
144 DCHECK(object_proxy_);
dcheng30c5a172016-04-09 07:55:04 +0900145 std::unique_ptr<dbus::Response> response(object_proxy_->CallMethodAndBlock(
146 &method_call, ObjectProxy::TIMEOUT_USE_DEFAULT));
nywang80f27162015-09-29 07:46:36 +0900147
148 if (!response.get()) {
149 LOG(WARNING) << property->name() << ": GetAndBlock: failed.";
150 return false;
151 }
152
153 MessageReader reader(response.get());
154 if (property->PopValueFromReader(&reader)) {
155 property->set_valid(true);
156 NotifyPropertyChanged(property->name());
157 } else {
158 if (property->is_valid()) {
159 property->set_valid(false);
160 NotifyPropertyChanged(property->name());
161 }
162 }
163 return true;
164}
165
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900166void PropertySet::GetAll() {
167 MethodCall method_call(kPropertiesInterface, kPropertiesGetAll);
168 MessageWriter writer(&method_call);
169 writer.AppendString(interface());
170
171 DCHECK(object_proxy_);
172 object_proxy_->CallMethod(&method_call,
173 ObjectProxy::TIMEOUT_USE_DEFAULT,
174 base::Bind(&PropertySet::OnGetAll,
175 weak_ptr_factory_.GetWeakPtr()));
176}
177
178void PropertySet::OnGetAll(Response* response) {
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900179 if (!response) {
stevenjb@chromium.orgfa639cc2014-07-17 14:03:53 +0900180 LOG(WARNING) << "GetAll request failed for: " << interface_;
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900181 return;
182 }
183
184 MessageReader reader(response);
185 if (!UpdatePropertiesFromReader(&reader)) {
186 LOG(WARNING) << "GetAll response has wrong parameters: "
187 << "expected dictionary: " << response->ToString();
188 }
189}
190
keybuk@chromium.org56028ac2012-06-29 03:43:30 +0900191void PropertySet::Set(PropertyBase* property, SetCallback callback) {
192 MethodCall method_call(kPropertiesInterface, kPropertiesSet);
193 MessageWriter writer(&method_call);
194 writer.AppendString(interface());
195 writer.AppendString(property->name());
196 property->AppendSetValueToWriter(&writer);
197
198 DCHECK(object_proxy_);
199 object_proxy_->CallMethod(&method_call,
200 ObjectProxy::TIMEOUT_USE_DEFAULT,
201 base::Bind(&PropertySet::OnSet,
202 GetWeakPtr(),
203 property,
204 callback));
205}
206
nywangf1196812015-09-23 09:06:40 +0900207bool PropertySet::SetAndBlock(PropertyBase* property) {
208 MethodCall method_call(kPropertiesInterface, kPropertiesSet);
209 MessageWriter writer(&method_call);
210 writer.AppendString(interface());
211 writer.AppendString(property->name());
212 property->AppendSetValueToWriter(&writer);
213
214 DCHECK(object_proxy_);
dcheng30c5a172016-04-09 07:55:04 +0900215 std::unique_ptr<dbus::Response> response(object_proxy_->CallMethodAndBlock(
216 &method_call, ObjectProxy::TIMEOUT_USE_DEFAULT));
nywangf1196812015-09-23 09:06:40 +0900217 if (response.get())
218 return true;
219 return false;
220}
221
stevenjb@chromium.orgfa639cc2014-07-17 14:03:53 +0900222void PropertySet::OnSet(PropertyBase* property,
223 SetCallback callback,
keybuk@chromium.org56028ac2012-06-29 03:43:30 +0900224 Response* response) {
225 LOG_IF(WARNING, !response) << property->name() << ": Set: failed.";
226 if (!callback.is_null())
227 callback.Run(response);
228}
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900229
230bool PropertySet::UpdatePropertiesFromReader(MessageReader* reader) {
231 DCHECK(reader);
Ben Chan333256c2017-11-10 05:20:16 +0900232 MessageReader array_reader(nullptr);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900233 if (!reader->PopArray(&array_reader))
234 return false;
235
236 while (array_reader.HasMoreData()) {
Ben Chan333256c2017-11-10 05:20:16 +0900237 MessageReader dict_entry_reader(nullptr);
keybuk@chromium.org683dd8c2012-03-23 05:34:05 +0900238 if (array_reader.PopDictEntry(&dict_entry_reader))
239 UpdatePropertyFromReader(&dict_entry_reader);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900240 }
241
242 return true;
243}
244
245bool PropertySet::UpdatePropertyFromReader(MessageReader* reader) {
246 DCHECK(reader);
247
248 std::string name;
249 if (!reader->PopString(&name))
250 return false;
251
252 PropertiesMap::iterator it = properties_map_.find(name);
253 if (it == properties_map_.end())
254 return false;
255
256 PropertyBase* property = it->second;
257 if (property->PopValueFromReader(reader)) {
jpawlowski77e73fc2015-05-11 20:07:04 +0900258 property->set_valid(true);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900259 NotifyPropertyChanged(name);
260 return true;
261 } else {
jpawlowski77e73fc2015-05-11 20:07:04 +0900262 if (property->is_valid()) {
263 property->set_valid(false);
264 NotifyPropertyChanged(property->name());
265 }
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900266 return false;
267 }
268}
269
jpawlowski77e73fc2015-05-11 20:07:04 +0900270bool PropertySet::InvalidatePropertiesFromReader(MessageReader* reader) {
271 DCHECK(reader);
Ben Chan333256c2017-11-10 05:20:16 +0900272 MessageReader array_reader(nullptr);
jpawlowski77e73fc2015-05-11 20:07:04 +0900273 if (!reader->PopArray(&array_reader))
274 return false;
275
276 while (array_reader.HasMoreData()) {
277 std::string name;
278 if (!array_reader.PopString(&name))
279 return false;
280
281 PropertiesMap::iterator it = properties_map_.find(name);
282 if (it == properties_map_.end())
283 continue;
284
285 PropertyBase* property = it->second;
286 if (property->is_valid()) {
287 property->set_valid(false);
288 NotifyPropertyChanged(property->name());
289 }
290 }
291
292 return true;
293}
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900294
295void PropertySet::NotifyPropertyChanged(const std::string& name) {
296 if (!property_changed_callback_.is_null())
297 property_changed_callback_.Run(name);
298}
299
300//
301// Property<Byte> specialization.
302//
303
304template <>
avi0ad0ce02015-12-23 03:12:45 +0900305Property<uint8_t>::Property()
306 : value_(0) {}
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900307
308template <>
avi0ad0ce02015-12-23 03:12:45 +0900309bool Property<uint8_t>::PopValueFromReader(MessageReader* reader) {
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900310 return reader->PopVariantOfByte(&value_);
311}
312
313template <>
avi0ad0ce02015-12-23 03:12:45 +0900314void Property<uint8_t>::AppendSetValueToWriter(MessageWriter* writer) {
keybuk@chromium.org56028ac2012-06-29 03:43:30 +0900315 writer->AppendVariantOfByte(set_value_);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900316}
317
318//
319// Property<bool> specialization.
320//
321
322template <>
keybuk@chromium.org56028ac2012-06-29 03:43:30 +0900323Property<bool>::Property() : value_(false) {
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900324}
325
326template <>
327bool Property<bool>::PopValueFromReader(MessageReader* reader) {
328 return reader->PopVariantOfBool(&value_);
329}
330
331template <>
keybuk@chromium.org56028ac2012-06-29 03:43:30 +0900332void Property<bool>::AppendSetValueToWriter(MessageWriter* writer) {
333 writer->AppendVariantOfBool(set_value_);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900334}
335
336//
avi0ad0ce02015-12-23 03:12:45 +0900337// Property<int16_t> specialization.
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900338//
339
340template <>
avi0ad0ce02015-12-23 03:12:45 +0900341Property<int16_t>::Property()
342 : value_(0) {}
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900343
344template <>
avi0ad0ce02015-12-23 03:12:45 +0900345bool Property<int16_t>::PopValueFromReader(MessageReader* reader) {
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900346 return reader->PopVariantOfInt16(&value_);
347}
348
349template <>
avi0ad0ce02015-12-23 03:12:45 +0900350void Property<int16_t>::AppendSetValueToWriter(MessageWriter* writer) {
keybuk@chromium.org56028ac2012-06-29 03:43:30 +0900351 writer->AppendVariantOfInt16(set_value_);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900352}
353
354//
avi0ad0ce02015-12-23 03:12:45 +0900355// Property<uint16_t> specialization.
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900356//
357
358template <>
avi0ad0ce02015-12-23 03:12:45 +0900359Property<uint16_t>::Property()
360 : value_(0) {}
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900361
362template <>
avi0ad0ce02015-12-23 03:12:45 +0900363bool Property<uint16_t>::PopValueFromReader(MessageReader* reader) {
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900364 return reader->PopVariantOfUint16(&value_);
365}
366
367template <>
avi0ad0ce02015-12-23 03:12:45 +0900368void Property<uint16_t>::AppendSetValueToWriter(MessageWriter* writer) {
keybuk@chromium.org56028ac2012-06-29 03:43:30 +0900369 writer->AppendVariantOfUint16(set_value_);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900370}
371
372//
avi0ad0ce02015-12-23 03:12:45 +0900373// Property<int32_t> specialization.
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900374//
375
376template <>
avi0ad0ce02015-12-23 03:12:45 +0900377Property<int32_t>::Property()
378 : value_(0) {}
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900379
380template <>
avi0ad0ce02015-12-23 03:12:45 +0900381bool Property<int32_t>::PopValueFromReader(MessageReader* reader) {
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900382 return reader->PopVariantOfInt32(&value_);
383}
384
385template <>
avi0ad0ce02015-12-23 03:12:45 +0900386void Property<int32_t>::AppendSetValueToWriter(MessageWriter* writer) {
keybuk@chromium.org56028ac2012-06-29 03:43:30 +0900387 writer->AppendVariantOfInt32(set_value_);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900388}
389
390//
avi0ad0ce02015-12-23 03:12:45 +0900391// Property<uint32_t> specialization.
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900392//
393
394template <>
avi0ad0ce02015-12-23 03:12:45 +0900395Property<uint32_t>::Property()
396 : value_(0) {}
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900397
398template <>
avi0ad0ce02015-12-23 03:12:45 +0900399bool Property<uint32_t>::PopValueFromReader(MessageReader* reader) {
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900400 return reader->PopVariantOfUint32(&value_);
401}
402
403template <>
avi0ad0ce02015-12-23 03:12:45 +0900404void Property<uint32_t>::AppendSetValueToWriter(MessageWriter* writer) {
keybuk@chromium.org56028ac2012-06-29 03:43:30 +0900405 writer->AppendVariantOfUint32(set_value_);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900406}
407
408//
avi0ad0ce02015-12-23 03:12:45 +0900409// Property<int64_t> specialization.
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900410//
411
412template <>
avi0ad0ce02015-12-23 03:12:45 +0900413Property<int64_t>::Property()
414 : value_(0), set_value_(0) {}
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900415
416template <>
avi0ad0ce02015-12-23 03:12:45 +0900417bool Property<int64_t>::PopValueFromReader(MessageReader* reader) {
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900418 return reader->PopVariantOfInt64(&value_);
419}
420
421template <>
avi0ad0ce02015-12-23 03:12:45 +0900422void Property<int64_t>::AppendSetValueToWriter(MessageWriter* writer) {
keybuk@chromium.org56028ac2012-06-29 03:43:30 +0900423 writer->AppendVariantOfInt64(set_value_);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900424}
425
426//
avi0ad0ce02015-12-23 03:12:45 +0900427// Property<uint64_t> specialization.
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900428//
429
430template <>
avi0ad0ce02015-12-23 03:12:45 +0900431Property<uint64_t>::Property()
432 : value_(0) {}
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900433
434template <>
avi0ad0ce02015-12-23 03:12:45 +0900435bool Property<uint64_t>::PopValueFromReader(MessageReader* reader) {
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900436 return reader->PopVariantOfUint64(&value_);
437}
438
439template <>
avi0ad0ce02015-12-23 03:12:45 +0900440void Property<uint64_t>::AppendSetValueToWriter(MessageWriter* writer) {
keybuk@chromium.org56028ac2012-06-29 03:43:30 +0900441 writer->AppendVariantOfUint64(set_value_);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900442}
443
444//
445// Property<double> specialization.
446//
447
448template <>
keybuk@chromium.org56028ac2012-06-29 03:43:30 +0900449Property<double>::Property() : value_(0.0) {
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900450}
451
452template <>
453bool Property<double>::PopValueFromReader(MessageReader* reader) {
454 return reader->PopVariantOfDouble(&value_);
455}
456
457template <>
keybuk@chromium.org56028ac2012-06-29 03:43:30 +0900458void Property<double>::AppendSetValueToWriter(MessageWriter* writer) {
459 writer->AppendVariantOfDouble(set_value_);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900460}
461
462//
463// Property<std::string> specialization.
464//
465
466template <>
467bool Property<std::string>::PopValueFromReader(MessageReader* reader) {
468 return reader->PopVariantOfString(&value_);
469}
470
471template <>
keybuk@chromium.org56028ac2012-06-29 03:43:30 +0900472void Property<std::string>::AppendSetValueToWriter(MessageWriter* writer) {
473 writer->AppendVariantOfString(set_value_);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900474}
475
476//
477// Property<ObjectPath> specialization.
478//
479
480template <>
481bool Property<ObjectPath>::PopValueFromReader(MessageReader* reader) {
482 return reader->PopVariantOfObjectPath(&value_);
483}
484
485template <>
keybuk@chromium.org56028ac2012-06-29 03:43:30 +0900486void Property<ObjectPath>::AppendSetValueToWriter(MessageWriter* writer) {
487 writer->AppendVariantOfObjectPath(set_value_);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900488}
489
490//
Ben Chandf19e682017-11-08 10:50:21 +0900491// Property<std::vector<std::string>> specialization.
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900492//
493
494template <>
Ben Chandf19e682017-11-08 10:50:21 +0900495bool Property<std::vector<std::string>>::PopValueFromReader(
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900496 MessageReader* reader) {
Ben Chan333256c2017-11-10 05:20:16 +0900497 MessageReader variant_reader(nullptr);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900498 if (!reader->PopVariant(&variant_reader))
499 return false;
500
keybuk@google.comf056b962012-03-22 08:43:45 +0900501 value_.clear();
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900502 return variant_reader.PopArrayOfStrings(&value_);
503}
504
505template <>
Ben Chandf19e682017-11-08 10:50:21 +0900506void Property<std::vector<std::string>>::AppendSetValueToWriter(
keybuk@chromium.org56028ac2012-06-29 03:43:30 +0900507 MessageWriter* writer) {
Ben Chan333256c2017-11-10 05:20:16 +0900508 MessageWriter variant_writer(nullptr);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900509 writer->OpenVariant("as", &variant_writer);
keybuk@chromium.org56028ac2012-06-29 03:43:30 +0900510 variant_writer.AppendArrayOfStrings(set_value_);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900511 writer->CloseContainer(&variant_writer);
512}
513
514//
Ben Chandf19e682017-11-08 10:50:21 +0900515// Property<std::vector<ObjectPath>> specialization.
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900516//
517
518template <>
Ben Chandf19e682017-11-08 10:50:21 +0900519bool Property<std::vector<ObjectPath>>::PopValueFromReader(
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900520 MessageReader* reader) {
Ben Chan333256c2017-11-10 05:20:16 +0900521 MessageReader variant_reader(nullptr);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900522 if (!reader->PopVariant(&variant_reader))
523 return false;
524
keybuk@google.comf056b962012-03-22 08:43:45 +0900525 value_.clear();
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900526 return variant_reader.PopArrayOfObjectPaths(&value_);
527}
528
529template <>
Ben Chandf19e682017-11-08 10:50:21 +0900530void Property<std::vector<ObjectPath>>::AppendSetValueToWriter(
keybuk@chromium.org56028ac2012-06-29 03:43:30 +0900531 MessageWriter* writer) {
Ben Chan333256c2017-11-10 05:20:16 +0900532 MessageWriter variant_writer(nullptr);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900533 writer->OpenVariant("ao", &variant_writer);
keybuk@chromium.org56028ac2012-06-29 03:43:30 +0900534 variant_writer.AppendArrayOfObjectPaths(set_value_);
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900535 writer->CloseContainer(&variant_writer);
536}
537
armansito@chromium.org11e12742014-03-15 16:40:49 +0900538//
Ben Chandf19e682017-11-08 10:50:21 +0900539// Property<std::vector<uint8_t>> specialization.
armansito@chromium.org11e12742014-03-15 16:40:49 +0900540//
541
542template <>
avi0ad0ce02015-12-23 03:12:45 +0900543bool Property<std::vector<uint8_t>>::PopValueFromReader(MessageReader* reader) {
Ben Chan333256c2017-11-10 05:20:16 +0900544 MessageReader variant_reader(nullptr);
armansito@chromium.org11e12742014-03-15 16:40:49 +0900545 if (!reader->PopVariant(&variant_reader))
546 return false;
547
548 value_.clear();
Ben Chan333256c2017-11-10 05:20:16 +0900549 const uint8_t* bytes = nullptr;
armansito@chromium.org11e12742014-03-15 16:40:49 +0900550 size_t length = 0;
551 if (!variant_reader.PopArrayOfBytes(&bytes, &length))
552 return false;
553 value_.assign(bytes, bytes + length);
554 return true;
555}
556
557template <>
avi0ad0ce02015-12-23 03:12:45 +0900558void Property<std::vector<uint8_t>>::AppendSetValueToWriter(
armansito@chromium.org11e12742014-03-15 16:40:49 +0900559 MessageWriter* writer) {
Ben Chan333256c2017-11-10 05:20:16 +0900560 MessageWriter variant_writer(nullptr);
armansito@chromium.org11e12742014-03-15 16:40:49 +0900561 writer->OpenVariant("ay", &variant_writer);
562 variant_writer.AppendArrayOfBytes(set_value_.data(), set_value_.size());
563 writer->CloseContainer(&variant_writer);
564}
565
dtapuska95f71722015-02-10 01:02:55 +0900566//
567// Property<std::map<std::string, std::string>> specialization.
568//
569
570template <>
571bool Property<std::map<std::string, std::string>>::PopValueFromReader(
572 MessageReader* reader) {
Ben Chan333256c2017-11-10 05:20:16 +0900573 MessageReader variant_reader(nullptr);
574 MessageReader array_reader(nullptr);
dtapuska95f71722015-02-10 01:02:55 +0900575 if (!reader->PopVariant(&variant_reader) ||
576 !variant_reader.PopArray(&array_reader))
577 return false;
578 value_.clear();
579 while (array_reader.HasMoreData()) {
Ben Chan333256c2017-11-10 05:20:16 +0900580 dbus::MessageReader dict_entry_reader(nullptr);
dtapuska95f71722015-02-10 01:02:55 +0900581 if (!array_reader.PopDictEntry(&dict_entry_reader))
582 return false;
583 std::string key;
584 std::string value;
585 if (!dict_entry_reader.PopString(&key) ||
586 !dict_entry_reader.PopString(&value))
587 return false;
588 value_[key] = value;
589 }
590 return true;
591}
592
593template <>
594void Property<std::map<std::string, std::string>>::AppendSetValueToWriter(
595 MessageWriter* writer) {
Ben Chan333256c2017-11-10 05:20:16 +0900596 MessageWriter variant_writer(nullptr);
597 MessageWriter dict_writer(nullptr);
dtapuska95f71722015-02-10 01:02:55 +0900598 writer->OpenVariant("a{ss}", &variant_writer);
599 variant_writer.OpenArray("{ss}", &dict_writer);
600 for (const auto& pair : set_value_) {
Ben Chan333256c2017-11-10 05:20:16 +0900601 dbus::MessageWriter entry_writer(nullptr);
dtapuska95f71722015-02-10 01:02:55 +0900602 dict_writer.OpenDictEntry(&entry_writer);
603 entry_writer.AppendString(pair.first);
604 entry_writer.AppendString(pair.second);
605 dict_writer.CloseContainer(&entry_writer);
606 }
607 variant_writer.CloseContainer(&dict_writer);
608 writer->CloseContainer(&variant_writer);
609}
610
611//
612// Property<std::vector<std::pair<std::vector<uint8_t>, uint16_t>>>
613// specialization.
614//
615
616template <>
617bool Property<std::vector<std::pair<std::vector<uint8_t>, uint16_t>>>::
618 PopValueFromReader(MessageReader* reader) {
Ben Chan333256c2017-11-10 05:20:16 +0900619 MessageReader variant_reader(nullptr);
620 MessageReader array_reader(nullptr);
dtapuska95f71722015-02-10 01:02:55 +0900621 if (!reader->PopVariant(&variant_reader) ||
622 !variant_reader.PopArray(&array_reader))
623 return false;
624
625 value_.clear();
626 while (array_reader.HasMoreData()) {
Ben Chan333256c2017-11-10 05:20:16 +0900627 dbus::MessageReader struct_reader(nullptr);
dtapuska95f71722015-02-10 01:02:55 +0900628 if (!array_reader.PopStruct(&struct_reader))
629 return false;
630
631 std::pair<std::vector<uint8_t>, uint16_t> entry;
Ben Chan333256c2017-11-10 05:20:16 +0900632 const uint8_t* bytes = nullptr;
dtapuska95f71722015-02-10 01:02:55 +0900633 size_t length = 0;
634 if (!struct_reader.PopArrayOfBytes(&bytes, &length))
635 return false;
636 entry.first.assign(bytes, bytes + length);
637 if (!struct_reader.PopUint16(&entry.second))
638 return false;
639 value_.push_back(entry);
640 }
641 return true;
642}
643
644template <>
645void Property<std::vector<std::pair<std::vector<uint8_t>, uint16_t>>>::
646 AppendSetValueToWriter(MessageWriter* writer) {
Ben Chan333256c2017-11-10 05:20:16 +0900647 MessageWriter variant_writer(nullptr);
648 MessageWriter array_writer(nullptr);
dtapuska95f71722015-02-10 01:02:55 +0900649 writer->OpenVariant("a(ayq)", &variant_writer);
650 variant_writer.OpenArray("(ayq)", &array_writer);
651 for (const auto& pair : set_value_) {
652 dbus::MessageWriter struct_writer(nullptr);
653 array_writer.OpenStruct(&struct_writer);
654 struct_writer.AppendArrayOfBytes(std::get<0>(pair).data(),
655 std::get<0>(pair).size());
656 struct_writer.AppendUint16(std::get<1>(pair));
657 array_writer.CloseContainer(&struct_writer);
658 }
659 variant_writer.CloseContainer(&array_writer);
660 writer->CloseContainer(&variant_writer);
661}
662
puthik90fd20c2016-10-06 14:03:03 +0900663//
puthik6558aab2016-11-05 04:38:17 +0900664// Property<std::unordered_map<std::string, std::vector<uint8_t>>>
puthik90fd20c2016-10-06 14:03:03 +0900665// specialization.
666//
667
668template <>
669bool Property<std::unordered_map<std::string, std::vector<uint8_t>>>::
670 PopValueFromReader(MessageReader* reader) {
671 MessageReader variant_reader(nullptr);
672 MessageReader dict_reader(nullptr);
673 if (!reader->PopVariant(&variant_reader) ||
674 !variant_reader.PopArray(&dict_reader))
675 return false;
676
677 value_.clear();
678 while (dict_reader.HasMoreData()) {
679 MessageReader entry_reader(nullptr);
680 if (!dict_reader.PopDictEntry(&entry_reader))
681 return false;
682
683 std::string key;
684 MessageReader value_varient_reader(nullptr);
685 if (!entry_reader.PopString(&key) ||
686 !entry_reader.PopVariant(&value_varient_reader))
687 return false;
688
689 const uint8_t* bytes = nullptr;
690 size_t length = 0;
691 if (!value_varient_reader.PopArrayOfBytes(&bytes, &length))
692 return false;
693
694 value_[key].assign(bytes, bytes + length);
695 }
696 return true;
697}
698
699template <>
700void Property<std::unordered_map<std::string, std::vector<uint8_t>>>::
701 AppendSetValueToWriter(MessageWriter* writer) {
702 MessageWriter variant_writer(nullptr);
703 MessageWriter dict_writer(nullptr);
704
705 writer->OpenVariant("a{sv}", &variant_writer);
706 variant_writer.OpenArray("{sv}", &dict_writer);
707
708 for (const auto& pair : set_value_) {
709 MessageWriter entry_writer(nullptr);
710 dict_writer.OpenDictEntry(&entry_writer);
711
712 entry_writer.AppendString(pair.first);
713
714 MessageWriter value_varient_writer(nullptr);
715 entry_writer.OpenVariant("ay", &value_varient_writer);
716 value_varient_writer.AppendArrayOfBytes(pair.second.data(),
717 pair.second.size());
718 entry_writer.CloseContainer(&value_varient_writer);
719
720 dict_writer.CloseContainer(&entry_writer);
721 }
722
723 variant_writer.CloseContainer(&dict_writer);
724 writer->CloseContainer(&variant_writer);
725}
726
puthik6558aab2016-11-05 04:38:17 +0900727//
728// Property<std::unordered_map<uint16_t, std::vector<uint8_t>>>
729// specialization.
730//
731
732template <>
733bool Property<std::unordered_map<uint16_t, std::vector<uint8_t>>>::
734 PopValueFromReader(MessageReader* reader) {
735 MessageReader variant_reader(nullptr);
736 MessageReader dict_reader(nullptr);
737 if (!reader->PopVariant(&variant_reader) ||
738 !variant_reader.PopArray(&dict_reader))
739 return false;
740
741 value_.clear();
742 while (dict_reader.HasMoreData()) {
743 MessageReader entry_reader(nullptr);
744 if (!dict_reader.PopDictEntry(&entry_reader))
745 return false;
746
747 uint16_t key;
748 MessageReader value_varient_reader(nullptr);
749 if (!entry_reader.PopUint16(&key) ||
750 !entry_reader.PopVariant(&value_varient_reader))
751 return false;
752
753 const uint8_t* bytes = nullptr;
754 size_t length = 0;
755 if (!value_varient_reader.PopArrayOfBytes(&bytes, &length))
756 return false;
757
758 value_[key].assign(bytes, bytes + length);
759 }
760 return true;
761}
762
763template <>
764void Property<std::unordered_map<uint16_t, std::vector<uint8_t>>>::
765 AppendSetValueToWriter(MessageWriter* writer) {
766 MessageWriter variant_writer(nullptr);
767 MessageWriter dict_writer(nullptr);
768
769 writer->OpenVariant("a{qv}", &variant_writer);
770 variant_writer.OpenArray("{qv}", &dict_writer);
771
772 for (const auto& pair : set_value_) {
773 MessageWriter entry_writer(nullptr);
774 dict_writer.OpenDictEntry(&entry_writer);
775
776 entry_writer.AppendUint16(pair.first);
777
778 MessageWriter value_varient_writer(nullptr);
779 entry_writer.OpenVariant("ay", &value_varient_writer);
780 value_varient_writer.AppendArrayOfBytes(pair.second.data(),
781 pair.second.size());
782 entry_writer.CloseContainer(&value_varient_writer);
783
784 dict_writer.CloseContainer(&entry_writer);
785 }
786
787 variant_writer.CloseContainer(&dict_writer);
788 writer->CloseContainer(&variant_writer);
789}
790
avi0ad0ce02015-12-23 03:12:45 +0900791template class Property<uint8_t>;
tzik@chromium.org1dc29432014-07-25 13:04:27 +0900792template class Property<bool>;
avi0ad0ce02015-12-23 03:12:45 +0900793template class Property<int16_t>;
794template class Property<uint16_t>;
795template class Property<int32_t>;
796template class Property<uint32_t>;
797template class Property<int64_t>;
798template class Property<uint64_t>;
tzik@chromium.org1dc29432014-07-25 13:04:27 +0900799template class Property<double>;
800template class Property<std::string>;
801template class Property<ObjectPath>;
Ben Chandf19e682017-11-08 10:50:21 +0900802template class Property<std::vector<std::string>>;
803template class Property<std::vector<ObjectPath>>;
avi0ad0ce02015-12-23 03:12:45 +0900804template class Property<std::vector<uint8_t>>;
dtapuska95f71722015-02-10 01:02:55 +0900805template class Property<std::map<std::string, std::string>>;
806template class Property<std::vector<std::pair<std::vector<uint8_t>, uint16_t>>>;
puthik90fd20c2016-10-06 14:03:03 +0900807template class Property<std::unordered_map<std::string, std::vector<uint8_t>>>;
puthik6558aab2016-11-05 04:38:17 +0900808template class Property<std::unordered_map<uint16_t, std::vector<uint8_t>>>;
tzik@chromium.org1dc29432014-07-25 13:04:27 +0900809
keybuk@chromium.org7e0c4932012-02-15 13:21:08 +0900810} // namespace dbus