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