OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 "chromeos/dbus/biod/biod_record_client.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/macros.h" |
| 9 #include "dbus/bus.h" |
| 10 #include "dbus/message.h" |
| 11 #include "dbus/object_path.h" |
| 12 #include "dbus/object_proxy.h" |
| 13 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 14 |
| 15 namespace chromeos { |
| 16 |
| 17 // The BiodRecordClient implementation used in production. |
| 18 class BiodRecordClientImpl : public BiodRecordClient { |
| 19 public: |
| 20 BiodRecordClientImpl() : weak_ptr_factory_(this) {} |
| 21 |
| 22 ~BiodRecordClientImpl() override {} |
| 23 |
| 24 void SetLabel(const dbus::ObjectPath& record_path, |
| 25 const std::string& label) override { |
| 26 dbus::MethodCall method_call(biod::kRecordInterface, |
| 27 biod::kRecordSetLabelMethod); |
| 28 dbus::MessageWriter writer(&method_call); |
| 29 writer.AppendString(label); |
| 30 |
| 31 dbus::ObjectProxy* record_proxy = |
| 32 bus_->GetObjectProxy(biod::kBiodServiceName, record_path); |
| 33 record_proxy->CallMethod(&method_call, |
| 34 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 35 dbus::ObjectProxy::EmptyResponseCallback()); |
| 36 } |
| 37 |
| 38 void Remove(const dbus::ObjectPath& record_path) override { |
| 39 dbus::MethodCall method_call(biod::kRecordInterface, |
| 40 biod::kRecordRemoveMethod); |
| 41 |
| 42 dbus::ObjectProxy* record_proxy = |
| 43 bus_->GetObjectProxy(biod::kBiodServiceName, record_path); |
| 44 record_proxy->CallMethod(&method_call, |
| 45 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 46 dbus::ObjectProxy::EmptyResponseCallback()); |
| 47 } |
| 48 |
| 49 void RequestLabel(const dbus::ObjectPath& record_path, |
| 50 const LabelCallback& callback) override { |
| 51 dbus::MethodCall method_call(dbus::kDBusPropertiesInterface, |
| 52 dbus::kDBusPropertiesGet); |
| 53 dbus::MessageWriter writer(&method_call); |
| 54 writer.AppendString(biod::kRecordInterface); |
| 55 writer.AppendString(biod::kRecordLabelProperty); |
| 56 |
| 57 dbus::ObjectProxy* record_proxy = |
| 58 bus_->GetObjectProxy(biod::kBiodServiceName, record_path); |
| 59 record_proxy->CallMethod( |
| 60 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 61 base::Bind(&BiodRecordClientImpl::OnRequestLabel, |
| 62 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 63 } |
| 64 |
| 65 protected: |
| 66 void Init(dbus::Bus* bus) override { bus_ = bus; } |
| 67 |
| 68 private: |
| 69 void OnRequestLabel(const LabelCallback& callback, dbus::Response* response) { |
| 70 std::string result; |
| 71 if (response) { |
| 72 dbus::MessageReader reader(response); |
| 73 if (!reader.PopVariantOfString(&result)) |
| 74 LOG(ERROR) << biod::kRecordLabelProperty << " had incorrect response."; |
| 75 } |
| 76 |
| 77 callback.Run(result); |
| 78 } |
| 79 |
| 80 dbus::Bus* bus_; |
| 81 |
| 82 // Note: This should remain the last member so it'll be destroyed and |
| 83 // invalidate its weak pointers before any other members are destroyed. |
| 84 base::WeakPtrFactory<BiodRecordClientImpl> weak_ptr_factory_; |
| 85 |
| 86 DISALLOW_COPY_AND_ASSIGN(BiodRecordClientImpl); |
| 87 }; |
| 88 |
| 89 BiodRecordClient::BiodRecordClient() {} |
| 90 |
| 91 BiodRecordClient::~BiodRecordClient() {} |
| 92 |
| 93 // static |
| 94 BiodRecordClient* BiodRecordClient::Create( |
| 95 DBusClientImplementationType /* type */) { |
| 96 return new BiodRecordClientImpl(); |
| 97 } |
| 98 |
| 99 } // namespace chromeos |
OLD | NEW |