OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/dbus/nfc_property_set.h" | 5 #include "chromeos/dbus/nfc_property_set.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "third_party/cros_system_api/dbus/service_constants.h" | 8 #include "third_party/cros_system_api/dbus/service_constants.h" |
9 | 9 |
10 namespace chromeos { | 10 namespace chromeos { |
11 | 11 |
12 NfcPropertySet::NfcPropertySet(dbus::ObjectProxy* object_proxy, | 12 NfcPropertySet::NfcPropertySet(dbus::ObjectProxy* object_proxy, |
13 const std::string& interface, | 13 const std::string& interface, |
14 const PropertyChangedCallback& callback) | 14 const PropertyChangedCallback& callback) |
15 : dbus::PropertySet(object_proxy, interface, callback) { | 15 : dbus::PropertySet(object_proxy, interface, callback), |
16 } | 16 weak_ptr_factory_(this) {} |
17 | 17 |
18 NfcPropertySet::~NfcPropertySet() { | 18 NfcPropertySet::~NfcPropertySet() { |
19 } | 19 } |
20 | 20 |
21 void NfcPropertySet::ConnectSignals() { | 21 void NfcPropertySet::ConnectSignals() { |
22 object_proxy()->ConnectToSignal( | 22 object_proxy()->ConnectToSignal( |
23 interface(), | 23 interface(), nfc_common::kPropertyChangedSignal, |
24 nfc_common::kPropertyChangedSignal, | 24 base::Bind(&dbus::PropertySet::ChangedReceived, |
25 base::Bind(&dbus::PropertySet::ChangedReceived, GetWeakPtr()), | 25 weak_ptr_factory_.GetWeakPtr()), |
26 base::Bind(&dbus::PropertySet::ChangedConnected, GetWeakPtr())); | 26 base::Bind(&dbus::PropertySet::ChangedConnected, |
| 27 weak_ptr_factory_.GetWeakPtr())); |
27 } | 28 } |
28 | 29 |
29 void NfcPropertySet::SetAllPropertiesReceivedCallback( | 30 void NfcPropertySet::SetAllPropertiesReceivedCallback( |
30 const base::Closure& callback) { | 31 const base::Closure& callback) { |
31 on_get_all_callback_ = callback; | 32 on_get_all_callback_ = callback; |
32 } | 33 } |
33 | 34 |
34 void NfcPropertySet::Get(dbus::PropertyBase* property, | 35 void NfcPropertySet::Get(dbus::PropertyBase* property, |
35 GetCallback callback) { | 36 GetCallback callback) { |
36 NOTREACHED() << "neard does not implement Get for properties."; | 37 NOTREACHED() << "neard does not implement Get for properties."; |
37 } | 38 } |
38 | 39 |
39 void NfcPropertySet::GetAll() { | 40 void NfcPropertySet::GetAll() { |
40 dbus::MethodCall method_call( | 41 dbus::MethodCall method_call( |
41 interface(), nfc_common::kGetProperties); | 42 interface(), nfc_common::kGetProperties); |
42 object_proxy()->CallMethod(&method_call, | 43 object_proxy()->CallMethodWithErrorCallback( |
43 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 44 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
44 base::Bind(&dbus::PropertySet::OnGetAll, | 45 base::Bind(&dbus::PropertySet::OnGetAll, weak_ptr_factory_.GetWeakPtr()), |
45 GetWeakPtr())); | 46 base::Bind(&NfcPropertySet::OnGetAllError, |
| 47 weak_ptr_factory_.GetWeakPtr())); |
46 } | 48 } |
47 | 49 |
48 void NfcPropertySet::OnGetAll(dbus::Response* response) { | 50 void NfcPropertySet::OnGetAll(dbus::Response* response) { |
49 // First invoke the superclass implementation. If the call to GetAll was | 51 // First invoke the superclass implementation. If the call to GetAll was |
50 // successful, this will invoke the PropertyChangedCallback passed to the | 52 // successful, this will invoke the PropertyChangedCallback passed to the |
51 // constructor for each individual property received through the call and | 53 // constructor for each individual property received through the call and |
52 // make sure that the values of the properties have been cached. This way, | 54 // make sure that the values of the properties have been cached. This way, |
53 // all received properties will be available when |on_get_all_callback_| is | 55 // all received properties will be available when |on_get_all_callback_| is |
54 // run. | 56 // run. |
55 dbus::PropertySet::OnGetAll(response); | 57 dbus::PropertySet::OnGetAll(response); |
56 if (response) { | 58 if (response) { |
57 VLOG(2) << "NfcPropertySet::GetAll returned successfully."; | 59 VLOG(2) << "NfcPropertySet::GetAll returned successfully."; |
58 if (!on_get_all_callback_.is_null()) | 60 if (!on_get_all_callback_.is_null()) |
59 on_get_all_callback_.Run(); | 61 on_get_all_callback_.Run(); |
60 } | 62 } |
61 } | 63 } |
62 | 64 |
63 void NfcPropertySet::Set(dbus::PropertyBase* property, | 65 void NfcPropertySet::Set(dbus::PropertyBase* property, |
64 SetCallback callback) { | 66 SetCallback callback) { |
65 dbus::MethodCall method_call( | 67 dbus::MethodCall method_call( |
66 interface(), nfc_common::kSetProperty); | 68 interface(), nfc_common::kSetProperty); |
67 dbus::MessageWriter writer(&method_call); | 69 dbus::MessageWriter writer(&method_call); |
68 writer.AppendString(property->name()); | 70 writer.AppendString(property->name()); |
69 property->AppendSetValueToWriter(&writer); | 71 property->AppendSetValueToWriter(&writer); |
70 object_proxy()->CallMethod(&method_call, | 72 object_proxy()->CallMethod( |
71 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 73 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
72 base::Bind(&dbus::PropertySet::OnSet, | 74 base::Bind(&dbus::PropertySet::OnSet, weak_ptr_factory_.GetWeakPtr(), |
73 GetWeakPtr(), | 75 property, callback)); |
74 property, | |
75 callback)); | |
76 } | 76 } |
77 | 77 |
78 void NfcPropertySet::ChangedReceived(dbus::Signal* signal) { | 78 void NfcPropertySet::ChangedReceived(dbus::Signal* signal) { |
79 DCHECK(signal); | 79 DCHECK(signal); |
80 dbus::MessageReader reader(signal); | 80 dbus::MessageReader reader(signal); |
81 UpdatePropertyFromReader(&reader); | 81 UpdatePropertyFromReader(&reader); |
82 } | 82 } |
83 | 83 |
| 84 void NfcPropertySet::OnGetAllError(dbus::ErrorResponse* response) { |
| 85 if (response) { |
| 86 dbus::MessageReader reader(response); |
| 87 std::string error_message; |
| 88 reader.PopString(&error_message); |
| 89 |
| 90 if (response->GetErrorName() == DBUS_ERROR_SERVICE_UNKNOWN) { |
| 91 // Do not LOG(ERROR) if service is unknown. crbug.com/393311. |
| 92 VLOG(2) << "NfcPropertySet::GetAll failed because the service is unknown." |
| 93 << " NFC not enabled on this device? : " << error_message; |
| 94 } else { |
| 95 LOG(ERROR) << "NfcPropertySet::GetAll failed: " << error_message; |
| 96 } |
| 97 } |
| 98 OnGetAll(nullptr); |
| 99 } |
| 100 |
84 } // namespace chromeos | 101 } // namespace chromeos |
OLD | NEW |