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/bluetooth_agent_manager_client.h" | 5 #include "device/bluetooth/dbus/bluetooth_agent_manager_client.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "dbus/bus.h" | 9 #include "dbus/bus.h" |
10 #include "dbus/message.h" | 10 #include "dbus/message.h" |
11 #include "dbus/object_proxy.h" | 11 #include "dbus/object_proxy.h" |
12 #include "third_party/cros_system_api/dbus/service_constants.h" | 12 #include "third_party/cros_system_api/dbus/service_constants.h" |
13 | 13 |
14 namespace chromeos { | 14 namespace bluez { |
15 | 15 |
16 const char BluetoothAgentManagerClient::kNoResponseError[] = | 16 const char BluetoothAgentManagerClient::kNoResponseError[] = |
17 "org.chromium.Error.NoResponse"; | 17 "org.chromium.Error.NoResponse"; |
18 | 18 |
19 // The BluetoothAgentManagerClient implementation used in production. | 19 // The BluetoothAgentManagerClient implementation used in production. |
20 class BluetoothAgentManagerClientImpl | 20 class BluetoothAgentManagerClientImpl : public BluetoothAgentManagerClient { |
21 : public BluetoothAgentManagerClient { | |
22 public: | 21 public: |
23 BluetoothAgentManagerClientImpl() : weak_ptr_factory_(this) {} | 22 BluetoothAgentManagerClientImpl() : weak_ptr_factory_(this) {} |
24 | 23 |
25 ~BluetoothAgentManagerClientImpl() override {} | 24 ~BluetoothAgentManagerClientImpl() override {} |
26 | 25 |
27 // BluetoothAgentManagerClient override. | 26 // BluetoothAgentManagerClient override. |
28 void RegisterAgent(const dbus::ObjectPath& agent_path, | 27 void RegisterAgent(const dbus::ObjectPath& agent_path, |
29 const std::string& capability, | 28 const std::string& capability, |
30 const base::Closure& callback, | 29 const base::Closure& callback, |
31 const ErrorCallback& error_callback) override { | 30 const ErrorCallback& error_callback) override { |
32 dbus::MethodCall method_call( | 31 dbus::MethodCall method_call( |
33 bluetooth_agent_manager::kBluetoothAgentManagerInterface, | 32 bluetooth_agent_manager::kBluetoothAgentManagerInterface, |
34 bluetooth_agent_manager::kRegisterAgent); | 33 bluetooth_agent_manager::kRegisterAgent); |
35 | 34 |
36 dbus::MessageWriter writer(&method_call); | 35 dbus::MessageWriter writer(&method_call); |
37 writer.AppendObjectPath(agent_path); | 36 writer.AppendObjectPath(agent_path); |
38 writer.AppendString(capability); | 37 writer.AppendString(capability); |
39 | 38 |
40 object_proxy_->CallMethodWithErrorCallback( | 39 object_proxy_->CallMethodWithErrorCallback( |
41 &method_call, | 40 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
42 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
43 base::Bind(&BluetoothAgentManagerClientImpl::OnSuccess, | 41 base::Bind(&BluetoothAgentManagerClientImpl::OnSuccess, |
44 weak_ptr_factory_.GetWeakPtr(), callback), | 42 weak_ptr_factory_.GetWeakPtr(), callback), |
45 base::Bind(&BluetoothAgentManagerClientImpl::OnError, | 43 base::Bind(&BluetoothAgentManagerClientImpl::OnError, |
46 weak_ptr_factory_.GetWeakPtr(), error_callback)); | 44 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
47 } | 45 } |
48 | 46 |
49 // BluetoothAgentManagerClient override. | 47 // BluetoothAgentManagerClient override. |
50 void UnregisterAgent(const dbus::ObjectPath& agent_path, | 48 void UnregisterAgent(const dbus::ObjectPath& agent_path, |
51 const base::Closure& callback, | 49 const base::Closure& callback, |
52 const ErrorCallback& error_callback) override { | 50 const ErrorCallback& error_callback) override { |
53 dbus::MethodCall method_call( | 51 dbus::MethodCall method_call( |
54 bluetooth_agent_manager::kBluetoothAgentManagerInterface, | 52 bluetooth_agent_manager::kBluetoothAgentManagerInterface, |
55 bluetooth_agent_manager::kUnregisterAgent); | 53 bluetooth_agent_manager::kUnregisterAgent); |
56 | 54 |
57 dbus::MessageWriter writer(&method_call); | 55 dbus::MessageWriter writer(&method_call); |
58 writer.AppendObjectPath(agent_path); | 56 writer.AppendObjectPath(agent_path); |
59 | 57 |
60 object_proxy_->CallMethodWithErrorCallback( | 58 object_proxy_->CallMethodWithErrorCallback( |
61 &method_call, | 59 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
62 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
63 base::Bind(&BluetoothAgentManagerClientImpl::OnSuccess, | 60 base::Bind(&BluetoothAgentManagerClientImpl::OnSuccess, |
64 weak_ptr_factory_.GetWeakPtr(), callback), | 61 weak_ptr_factory_.GetWeakPtr(), callback), |
65 base::Bind(&BluetoothAgentManagerClientImpl::OnError, | 62 base::Bind(&BluetoothAgentManagerClientImpl::OnError, |
66 weak_ptr_factory_.GetWeakPtr(), error_callback)); | 63 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
67 } | 64 } |
68 | 65 |
69 | |
70 // BluetoothAgentManagerClient override. | 66 // BluetoothAgentManagerClient override. |
71 void RequestDefaultAgent(const dbus::ObjectPath& agent_path, | 67 void RequestDefaultAgent(const dbus::ObjectPath& agent_path, |
72 const base::Closure& callback, | 68 const base::Closure& callback, |
73 const ErrorCallback& error_callback) override { | 69 const ErrorCallback& error_callback) override { |
74 dbus::MethodCall method_call( | 70 dbus::MethodCall method_call( |
75 bluetooth_agent_manager::kBluetoothAgentManagerInterface, | 71 bluetooth_agent_manager::kBluetoothAgentManagerInterface, |
76 bluetooth_agent_manager::kRequestDefaultAgent); | 72 bluetooth_agent_manager::kRequestDefaultAgent); |
77 | 73 |
78 dbus::MessageWriter writer(&method_call); | 74 dbus::MessageWriter writer(&method_call); |
79 writer.AppendObjectPath(agent_path); | 75 writer.AppendObjectPath(agent_path); |
80 | 76 |
81 object_proxy_->CallMethodWithErrorCallback( | 77 object_proxy_->CallMethodWithErrorCallback( |
82 &method_call, | 78 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
83 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
84 base::Bind(&BluetoothAgentManagerClientImpl::OnSuccess, | 79 base::Bind(&BluetoothAgentManagerClientImpl::OnSuccess, |
85 weak_ptr_factory_.GetWeakPtr(), callback), | 80 weak_ptr_factory_.GetWeakPtr(), callback), |
86 base::Bind(&BluetoothAgentManagerClientImpl::OnError, | 81 base::Bind(&BluetoothAgentManagerClientImpl::OnError, |
87 weak_ptr_factory_.GetWeakPtr(), error_callback)); | 82 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
88 } | 83 } |
89 | 84 |
90 protected: | 85 protected: |
91 void Init(dbus::Bus* bus) override { | 86 void Init(dbus::Bus* bus) override { |
92 DCHECK(bus); | 87 DCHECK(bus); |
93 object_proxy_ = bus->GetObjectProxy( | 88 object_proxy_ = bus->GetObjectProxy( |
94 bluetooth_agent_manager::kBluetoothAgentManagerServiceName, | 89 bluetooth_agent_manager::kBluetoothAgentManagerServiceName, |
95 dbus::ObjectPath( | 90 dbus::ObjectPath( |
96 bluetooth_agent_manager::kBluetoothAgentManagerServicePath)); | 91 bluetooth_agent_manager::kBluetoothAgentManagerServicePath)); |
97 } | 92 } |
98 | 93 |
99 private: | 94 private: |
100 // Called when a response for successful method call is received. | 95 // Called when a response for successful method call is received. |
101 void OnSuccess(const base::Closure& callback, | 96 void OnSuccess(const base::Closure& callback, dbus::Response* response) { |
102 dbus::Response* response) { | |
103 DCHECK(response); | 97 DCHECK(response); |
104 callback.Run(); | 98 callback.Run(); |
105 } | 99 } |
106 | 100 |
107 // Called when a response for a failed method call is received. | 101 // Called when a response for a failed method call is received. |
108 void OnError(const ErrorCallback& error_callback, | 102 void OnError(const ErrorCallback& error_callback, |
109 dbus::ErrorResponse* response) { | 103 dbus::ErrorResponse* response) { |
110 // Error response has optional error message argument. | 104 // Error response has optional error message argument. |
111 std::string error_name; | 105 std::string error_name; |
112 std::string error_message; | 106 std::string error_message; |
113 if (response) { | 107 if (response) { |
114 dbus::MessageReader reader(response); | 108 dbus::MessageReader reader(response); |
115 error_name = response->GetErrorName(); | 109 error_name = response->GetErrorName(); |
116 reader.PopString(&error_message); | 110 reader.PopString(&error_message); |
117 } else { | 111 } else { |
118 error_name = kNoResponseError; | 112 error_name = kNoResponseError; |
119 error_message = ""; | 113 error_message = ""; |
120 } | 114 } |
121 error_callback.Run(error_name, error_message); | 115 error_callback.Run(error_name, error_message); |
122 } | 116 } |
123 | 117 |
124 dbus::ObjectProxy* object_proxy_; | 118 dbus::ObjectProxy* object_proxy_; |
125 | 119 |
126 // Weak pointer factory for generating 'this' pointers that might live longer | 120 // Weak pointer factory for generating 'this' pointers that might live longer |
127 // than we do. | 121 // than we do. |
128 // Note: This should remain the last member so it'll be destroyed and | 122 // Note: This should remain the last member so it'll be destroyed and |
129 // invalidate its weak pointers before any other members are destroyed. | 123 // invalidate its weak pointers before any other members are destroyed. |
130 base::WeakPtrFactory<BluetoothAgentManagerClientImpl> | 124 base::WeakPtrFactory<BluetoothAgentManagerClientImpl> weak_ptr_factory_; |
131 weak_ptr_factory_; | |
132 | 125 |
133 DISALLOW_COPY_AND_ASSIGN(BluetoothAgentManagerClientImpl); | 126 DISALLOW_COPY_AND_ASSIGN(BluetoothAgentManagerClientImpl); |
134 }; | 127 }; |
135 | 128 |
136 BluetoothAgentManagerClient::BluetoothAgentManagerClient() { | 129 BluetoothAgentManagerClient::BluetoothAgentManagerClient() {} |
137 } | |
138 | 130 |
139 BluetoothAgentManagerClient::~BluetoothAgentManagerClient() { | 131 BluetoothAgentManagerClient::~BluetoothAgentManagerClient() {} |
140 } | |
141 | 132 |
142 BluetoothAgentManagerClient* BluetoothAgentManagerClient::Create() { | 133 BluetoothAgentManagerClient* BluetoothAgentManagerClient::Create() { |
143 return new BluetoothAgentManagerClientImpl(); | 134 return new BluetoothAgentManagerClientImpl(); |
144 } | 135 } |
145 | 136 |
146 } // namespace chromeos | 137 } // namespace bluez |
OLD | NEW |