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
3 // found in the LICENSE file.
5 #include "chromeos/dbus/bluetooth_profile_manager_client.h"
8 #include "base/logging.h"
10 #include "dbus/message.h"
11 #include "dbus/object_proxy.h"
12 #include "third_party/cros_system_api/dbus/service_constants.h"
16 const char BluetoothProfileManagerClient::kNoResponseError
[] =
17 "org.chromium.Error.NoResponse";
20 BluetoothProfileManagerClient::Options::Options() {
23 BluetoothProfileManagerClient::Options::~Options() {
26 // The BluetoothProfileManagerClient implementation used in production.
27 class BluetoothProfileManagerClientImpl
28 : public BluetoothProfileManagerClient
{
30 BluetoothProfileManagerClientImpl() : weak_ptr_factory_(this) {}
32 ~BluetoothProfileManagerClientImpl() override
{}
34 // BluetoothProfileManagerClient override.
35 void RegisterProfile(const dbus::ObjectPath
& profile_path
,
36 const std::string
& uuid
,
37 const Options
& options
,
38 const base::Closure
& callback
,
39 const ErrorCallback
& error_callback
) override
{
40 dbus::MethodCall
method_call(
41 bluetooth_profile_manager::kBluetoothProfileManagerInterface
,
42 bluetooth_profile_manager::kRegisterProfile
);
44 dbus::MessageWriter
writer(&method_call
);
45 writer
.AppendObjectPath(profile_path
);
46 writer
.AppendString(uuid
);
48 dbus::MessageWriter
array_writer(NULL
);
49 writer
.OpenArray("{sv}", &array_writer
);
51 dbus::MessageWriter
dict_writer(NULL
);
53 // Send Name if provided.
54 if (options
.name
.get() != NULL
) {
55 array_writer
.OpenDictEntry(&dict_writer
);
56 dict_writer
.AppendString(bluetooth_profile_manager::kNameOption
);
57 dict_writer
.AppendVariantOfString(*(options
.name
));
58 array_writer
.CloseContainer(&dict_writer
);
61 // Send Service if provided.
62 if (options
.service
.get() != NULL
) {
63 dbus::MessageWriter
dict_writer(NULL
);
64 array_writer
.OpenDictEntry(&dict_writer
);
65 dict_writer
.AppendString(bluetooth_profile_manager::kServiceOption
);
66 dict_writer
.AppendVariantOfString(*(options
.service
));
67 array_writer
.CloseContainer(&dict_writer
);
70 // Send Role if not the default value.
71 if (options
.role
!= SYMMETRIC
) {
72 dbus::MessageWriter
dict_writer(NULL
);
73 array_writer
.OpenDictEntry(&dict_writer
);
74 dict_writer
.AppendString(bluetooth_profile_manager::kRoleOption
);
75 if (options
.role
== CLIENT
)
76 dict_writer
.AppendVariantOfString(
77 bluetooth_profile_manager::kClientRoleOption
);
78 else if (options
.role
== SERVER
)
79 dict_writer
.AppendVariantOfString(
80 bluetooth_profile_manager::kServerRoleOption
);
82 dict_writer
.AppendVariantOfString("");
83 array_writer
.CloseContainer(&dict_writer
);
86 // Send Channel if provided.
87 if (options
.channel
.get() != NULL
) {
88 dbus::MessageWriter
dict_writer(NULL
);
89 array_writer
.OpenDictEntry(&dict_writer
);
90 dict_writer
.AppendString(bluetooth_profile_manager::kChannelOption
);
91 dict_writer
.AppendVariantOfUint16(*(options
.channel
));
92 array_writer
.CloseContainer(&dict_writer
);
95 // Send PSM if provided.
96 if (options
.psm
.get() != NULL
) {
97 dbus::MessageWriter
dict_writer(NULL
);
98 array_writer
.OpenDictEntry(&dict_writer
);
99 dict_writer
.AppendString(bluetooth_profile_manager::kPSMOption
);
100 dict_writer
.AppendVariantOfUint16(*(options
.psm
));
101 array_writer
.CloseContainer(&dict_writer
);
104 // Send RequireAuthentication if provided.
105 if (options
.require_authentication
.get() != NULL
) {
106 array_writer
.OpenDictEntry(&dict_writer
);
107 dict_writer
.AppendString(
108 bluetooth_profile_manager::kRequireAuthenticationOption
);
109 dict_writer
.AppendVariantOfBool(*(options
.require_authentication
));
110 array_writer
.CloseContainer(&dict_writer
);
113 // Send RequireAuthorization if provided.
114 if (options
.require_authorization
.get() != NULL
) {
115 array_writer
.OpenDictEntry(&dict_writer
);
116 dict_writer
.AppendString(
117 bluetooth_profile_manager::kRequireAuthorizationOption
);
118 dict_writer
.AppendVariantOfBool(*(options
.require_authorization
));
119 array_writer
.CloseContainer(&dict_writer
);
122 // Send AutoConnect if provided.
123 if (options
.auto_connect
.get() != NULL
) {
124 array_writer
.OpenDictEntry(&dict_writer
);
125 dict_writer
.AppendString(
126 bluetooth_profile_manager::kAutoConnectOption
);
127 dict_writer
.AppendVariantOfBool(*(options
.auto_connect
));
128 array_writer
.CloseContainer(&dict_writer
);
131 // Send ServiceRecord if provided.
132 if (options
.service_record
.get() != NULL
) {
133 dbus::MessageWriter
dict_writer(NULL
);
134 array_writer
.OpenDictEntry(&dict_writer
);
135 dict_writer
.AppendString(bluetooth_profile_manager::kServiceRecordOption
);
136 dict_writer
.AppendVariantOfString(*(options
.service_record
));
137 array_writer
.CloseContainer(&dict_writer
);
140 // Send Version if provided.
141 if (options
.version
.get() != NULL
) {
142 dbus::MessageWriter
dict_writer(NULL
);
143 array_writer
.OpenDictEntry(&dict_writer
);
144 dict_writer
.AppendString(bluetooth_profile_manager::kVersionOption
);
145 dict_writer
.AppendVariantOfUint16(*(options
.version
));
146 array_writer
.CloseContainer(&dict_writer
);
149 // Send Features if provided.
150 if (options
.features
.get() != NULL
) {
151 dbus::MessageWriter
dict_writer(NULL
);
152 array_writer
.OpenDictEntry(&dict_writer
);
153 dict_writer
.AppendString(bluetooth_profile_manager::kFeaturesOption
);
154 dict_writer
.AppendVariantOfUint16(*(options
.features
));
155 array_writer
.CloseContainer(&dict_writer
);
158 writer
.CloseContainer(&array_writer
);
160 object_proxy_
->CallMethodWithErrorCallback(
162 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
163 base::Bind(&BluetoothProfileManagerClientImpl::OnSuccess
,
164 weak_ptr_factory_
.GetWeakPtr(), callback
),
165 base::Bind(&BluetoothProfileManagerClientImpl::OnError
,
166 weak_ptr_factory_
.GetWeakPtr(), error_callback
));
169 // BluetoothProfileManagerClient override.
170 void UnregisterProfile(const dbus::ObjectPath
& profile_path
,
171 const base::Closure
& callback
,
172 const ErrorCallback
& error_callback
) override
{
173 dbus::MethodCall
method_call(
174 bluetooth_profile_manager::kBluetoothProfileManagerInterface
,
175 bluetooth_profile_manager::kUnregisterProfile
);
177 dbus::MessageWriter
writer(&method_call
);
178 writer
.AppendObjectPath(profile_path
);
180 object_proxy_
->CallMethodWithErrorCallback(
182 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
183 base::Bind(&BluetoothProfileManagerClientImpl::OnSuccess
,
184 weak_ptr_factory_
.GetWeakPtr(), callback
),
185 base::Bind(&BluetoothProfileManagerClientImpl::OnError
,
186 weak_ptr_factory_
.GetWeakPtr(), error_callback
));
190 void Init(dbus::Bus
* bus
) override
{
192 object_proxy_
= bus
->GetObjectProxy(
193 bluetooth_profile_manager::kBluetoothProfileManagerServiceName
,
195 bluetooth_profile_manager::kBluetoothProfileManagerServicePath
));
199 // Called when a response for successful method call is received.
200 void OnSuccess(const base::Closure
& callback
,
201 dbus::Response
* response
) {
206 // Called when a response for a failed method call is received.
207 void OnError(const ErrorCallback
& error_callback
,
208 dbus::ErrorResponse
* response
) {
209 // Error response has optional error message argument.
210 std::string error_name
;
211 std::string error_message
;
213 dbus::MessageReader
reader(response
);
214 error_name
= response
->GetErrorName();
215 reader
.PopString(&error_message
);
217 error_name
= kNoResponseError
;
220 error_callback
.Run(error_name
, error_message
);
223 dbus::ObjectProxy
* object_proxy_
;
225 // Weak pointer factory for generating 'this' pointers that might live longer
227 // Note: This should remain the last member so it'll be destroyed and
228 // invalidate its weak pointers before any other members are destroyed.
229 base::WeakPtrFactory
<BluetoothProfileManagerClientImpl
> weak_ptr_factory_
;
231 DISALLOW_COPY_AND_ASSIGN(BluetoothProfileManagerClientImpl
);
234 BluetoothProfileManagerClient::BluetoothProfileManagerClient() {
237 BluetoothProfileManagerClient::~BluetoothProfileManagerClient() {
240 BluetoothProfileManagerClient
* BluetoothProfileManagerClient::Create() {
241 return new BluetoothProfileManagerClientImpl();
244 } // namespace chromeos