bluetooth_profile_manager_client.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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.
  4. #include "device/bluetooth/dbus/bluetooth_profile_manager_client.h"
  5. #include "base/bind.h"
  6. #include "base/check.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/time/time.h"
  9. #include "dbus/bus.h"
  10. #include "dbus/message.h"
  11. #include "dbus/object_proxy.h"
  12. #include "device/bluetooth/dbus/bluetooth_metrics_helper.h"
  13. #include "third_party/cros_system_api/dbus/service_constants.h"
  14. namespace bluez {
  15. const char BluetoothProfileManagerClient::kNoResponseError[] =
  16. "org.chromium.Error.NoResponse";
  17. BluetoothProfileManagerClient::Options::Options() = default;
  18. BluetoothProfileManagerClient::Options::~Options() = default;
  19. // The BluetoothProfileManagerClient implementation used in production.
  20. class BluetoothProfileManagerClientImpl : public BluetoothProfileManagerClient {
  21. public:
  22. BluetoothProfileManagerClientImpl() {}
  23. BluetoothProfileManagerClientImpl(const BluetoothProfileManagerClientImpl&) =
  24. delete;
  25. BluetoothProfileManagerClientImpl& operator=(
  26. const BluetoothProfileManagerClientImpl&) = delete;
  27. ~BluetoothProfileManagerClientImpl() override = default;
  28. // BluetoothProfileManagerClient override.
  29. void RegisterProfile(const dbus::ObjectPath& profile_path,
  30. const std::string& uuid,
  31. const Options& options,
  32. base::OnceClosure callback,
  33. ErrorCallback error_callback) override {
  34. dbus::MethodCall method_call(
  35. bluetooth_profile_manager::kBluetoothProfileManagerInterface,
  36. bluetooth_profile_manager::kRegisterProfile);
  37. dbus::MessageWriter writer(&method_call);
  38. writer.AppendObjectPath(profile_path);
  39. writer.AppendString(uuid);
  40. dbus::MessageWriter array_writer(nullptr);
  41. writer.OpenArray("{sv}", &array_writer);
  42. dbus::MessageWriter main_dict_writer(nullptr);
  43. // Send Name if provided.
  44. if (options.name.get() != nullptr) {
  45. array_writer.OpenDictEntry(&main_dict_writer);
  46. main_dict_writer.AppendString(bluetooth_profile_manager::kNameOption);
  47. main_dict_writer.AppendVariantOfString(*(options.name));
  48. array_writer.CloseContainer(&main_dict_writer);
  49. }
  50. // Send Service if provided.
  51. if (options.service.get() != nullptr) {
  52. dbus::MessageWriter dict_writer(nullptr);
  53. array_writer.OpenDictEntry(&dict_writer);
  54. dict_writer.AppendString(bluetooth_profile_manager::kServiceOption);
  55. dict_writer.AppendVariantOfString(*(options.service));
  56. array_writer.CloseContainer(&dict_writer);
  57. }
  58. // Send Role if not the default value.
  59. if (options.role != SYMMETRIC) {
  60. dbus::MessageWriter dict_writer(nullptr);
  61. array_writer.OpenDictEntry(&dict_writer);
  62. dict_writer.AppendString(bluetooth_profile_manager::kRoleOption);
  63. if (options.role == CLIENT)
  64. dict_writer.AppendVariantOfString(
  65. bluetooth_profile_manager::kClientRoleOption);
  66. else if (options.role == SERVER)
  67. dict_writer.AppendVariantOfString(
  68. bluetooth_profile_manager::kServerRoleOption);
  69. else
  70. dict_writer.AppendVariantOfString("");
  71. array_writer.CloseContainer(&dict_writer);
  72. }
  73. // Send Channel if provided.
  74. if (options.channel.get() != nullptr) {
  75. dbus::MessageWriter dict_writer(nullptr);
  76. array_writer.OpenDictEntry(&dict_writer);
  77. dict_writer.AppendString(bluetooth_profile_manager::kChannelOption);
  78. dict_writer.AppendVariantOfUint16(*(options.channel));
  79. array_writer.CloseContainer(&dict_writer);
  80. }
  81. // Send PSM if provided.
  82. if (options.psm.get() != nullptr) {
  83. dbus::MessageWriter dict_writer(nullptr);
  84. array_writer.OpenDictEntry(&dict_writer);
  85. dict_writer.AppendString(bluetooth_profile_manager::kPSMOption);
  86. dict_writer.AppendVariantOfUint16(*(options.psm));
  87. array_writer.CloseContainer(&dict_writer);
  88. }
  89. // Send RequireAuthentication if provided.
  90. if (options.require_authentication.get() != nullptr) {
  91. array_writer.OpenDictEntry(&main_dict_writer);
  92. main_dict_writer.AppendString(
  93. bluetooth_profile_manager::kRequireAuthenticationOption);
  94. main_dict_writer.AppendVariantOfBool(*(options.require_authentication));
  95. array_writer.CloseContainer(&main_dict_writer);
  96. }
  97. // Send RequireAuthorization if provided.
  98. if (options.require_authorization.get() != nullptr) {
  99. array_writer.OpenDictEntry(&main_dict_writer);
  100. main_dict_writer.AppendString(
  101. bluetooth_profile_manager::kRequireAuthorizationOption);
  102. main_dict_writer.AppendVariantOfBool(*(options.require_authorization));
  103. array_writer.CloseContainer(&main_dict_writer);
  104. }
  105. // Send AutoConnect if provided.
  106. if (options.auto_connect.get() != nullptr) {
  107. array_writer.OpenDictEntry(&main_dict_writer);
  108. main_dict_writer.AppendString(
  109. bluetooth_profile_manager::kAutoConnectOption);
  110. main_dict_writer.AppendVariantOfBool(*(options.auto_connect));
  111. array_writer.CloseContainer(&main_dict_writer);
  112. }
  113. // Send ServiceRecord if provided.
  114. if (options.service_record.get() != nullptr) {
  115. dbus::MessageWriter dict_writer(nullptr);
  116. array_writer.OpenDictEntry(&dict_writer);
  117. dict_writer.AppendString(bluetooth_profile_manager::kServiceRecordOption);
  118. dict_writer.AppendVariantOfString(*(options.service_record));
  119. array_writer.CloseContainer(&dict_writer);
  120. }
  121. // Send Version if provided.
  122. if (options.version.get() != nullptr) {
  123. dbus::MessageWriter dict_writer(nullptr);
  124. array_writer.OpenDictEntry(&dict_writer);
  125. dict_writer.AppendString(bluetooth_profile_manager::kVersionOption);
  126. dict_writer.AppendVariantOfUint16(*(options.version));
  127. array_writer.CloseContainer(&dict_writer);
  128. }
  129. // Send Features if provided.
  130. if (options.features.get() != nullptr) {
  131. dbus::MessageWriter dict_writer(nullptr);
  132. array_writer.OpenDictEntry(&dict_writer);
  133. dict_writer.AppendString(bluetooth_profile_manager::kFeaturesOption);
  134. dict_writer.AppendVariantOfUint16(*(options.features));
  135. array_writer.CloseContainer(&dict_writer);
  136. }
  137. writer.CloseContainer(&array_writer);
  138. object_proxy_->CallMethodWithErrorCallback(
  139. &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
  140. base::BindOnce(
  141. &BluetoothProfileManagerClientImpl::OnRegisterProfileSuccess,
  142. weak_ptr_factory_.GetWeakPtr(), std::move(callback),
  143. /*start_time=*/base::Time::Now()),
  144. base::BindOnce(
  145. &BluetoothProfileManagerClientImpl::OnRegisterProfileError,
  146. weak_ptr_factory_.GetWeakPtr(), std::move(error_callback)));
  147. }
  148. // BluetoothProfileManagerClient override.
  149. void UnregisterProfile(const dbus::ObjectPath& profile_path,
  150. base::OnceClosure callback,
  151. ErrorCallback error_callback) override {
  152. dbus::MethodCall method_call(
  153. bluetooth_profile_manager::kBluetoothProfileManagerInterface,
  154. bluetooth_profile_manager::kUnregisterProfile);
  155. dbus::MessageWriter writer(&method_call);
  156. writer.AppendObjectPath(profile_path);
  157. object_proxy_->CallMethodWithErrorCallback(
  158. &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
  159. base::BindOnce(
  160. &BluetoothProfileManagerClientImpl::OnUnregisterProfileSuccess,
  161. weak_ptr_factory_.GetWeakPtr(), std::move(callback),
  162. /*start_time=*/base::Time::Now()),
  163. base::BindOnce(
  164. &BluetoothProfileManagerClientImpl::OnUnregisterProfileError,
  165. weak_ptr_factory_.GetWeakPtr(), std::move(error_callback)));
  166. }
  167. protected:
  168. void Init(dbus::Bus* bus,
  169. const std::string& bluetooth_service_name) override {
  170. DCHECK(bus);
  171. object_proxy_ = bus->GetObjectProxy(
  172. bluetooth_service_name,
  173. dbus::ObjectPath(
  174. bluetooth_profile_manager::kBluetoothProfileManagerServicePath));
  175. }
  176. private:
  177. void OnRegisterProfileSuccess(base::OnceClosure callback,
  178. base::Time start_time,
  179. dbus::Response* response) {
  180. DCHECK(response);
  181. RecordSuccess(kRegisterProfileMethod, start_time);
  182. std::move(callback).Run();
  183. }
  184. void OnRegisterProfileError(ErrorCallback error_callback,
  185. dbus::ErrorResponse* response) {
  186. RecordFailure(kRegisterProfileMethod, response);
  187. OnError(std::move(error_callback), response);
  188. }
  189. void OnUnregisterProfileSuccess(base::OnceClosure callback,
  190. base::Time start_time,
  191. dbus::Response* response) {
  192. DCHECK(response);
  193. RecordSuccess(kUnregisterProfileMethod, start_time);
  194. std::move(callback).Run();
  195. }
  196. void OnUnregisterProfileError(ErrorCallback error_callback,
  197. dbus::ErrorResponse* response) {
  198. RecordFailure(kUnregisterProfileMethod, response);
  199. OnError(std::move(error_callback), response);
  200. }
  201. // Called when a response for a failed method call is received.
  202. void OnError(ErrorCallback error_callback, dbus::ErrorResponse* response) {
  203. // Error response has optional error message argument.
  204. std::string error_name;
  205. std::string error_message;
  206. if (response) {
  207. dbus::MessageReader reader(response);
  208. error_name = response->GetErrorName();
  209. reader.PopString(&error_message);
  210. } else {
  211. error_name = kNoResponseError;
  212. error_message = "";
  213. }
  214. std::move(error_callback).Run(error_name, error_message);
  215. }
  216. raw_ptr<dbus::ObjectProxy> object_proxy_;
  217. // Weak pointer factory for generating 'this' pointers that might live longer
  218. // than we do.
  219. // Note: This should remain the last member so it'll be destroyed and
  220. // invalidate its weak pointers before any other members are destroyed.
  221. base::WeakPtrFactory<BluetoothProfileManagerClientImpl> weak_ptr_factory_{
  222. this};
  223. };
  224. BluetoothProfileManagerClient::BluetoothProfileManagerClient() = default;
  225. BluetoothProfileManagerClient::~BluetoothProfileManagerClient() = default;
  226. BluetoothProfileManagerClient* BluetoothProfileManagerClient::Create() {
  227. return new BluetoothProfileManagerClientImpl();
  228. }
  229. } // namespace bluez