| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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/bluetooth_gatt_manager_client.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/memory/weak_ptr.h" | |
| 9 #include "dbus/bus.h" | |
| 10 #include "dbus/message.h" | |
| 11 #include "dbus/object_proxy.h" | |
| 12 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 13 | |
| 14 namespace chromeos { | |
| 15 | |
| 16 const char BluetoothGattManagerClient::kNoResponseError[] = | |
| 17 "org.chromium.Error.NoResponse"; | |
| 18 | |
| 19 // The BluetoothGattManagerClient implementation used in production. | |
| 20 class BluetoothGattManagerClientImpl : public BluetoothGattManagerClient { | |
| 21 public: | |
| 22 BluetoothGattManagerClientImpl() | |
| 23 : object_proxy_(NULL), | |
| 24 weak_ptr_factory_(this) { | |
| 25 } | |
| 26 | |
| 27 ~BluetoothGattManagerClientImpl() override {} | |
| 28 | |
| 29 // BluetoothGattManagerClient override. | |
| 30 void RegisterService(const dbus::ObjectPath& service_path, | |
| 31 const Options& options, | |
| 32 const base::Closure& callback, | |
| 33 const ErrorCallback& error_callback) override { | |
| 34 dbus::MethodCall method_call( | |
| 35 bluetooth_gatt_manager::kBluetoothGattManagerInterface, | |
| 36 bluetooth_gatt_manager::kRegisterService); | |
| 37 | |
| 38 dbus::MessageWriter writer(&method_call); | |
| 39 writer.AppendObjectPath(service_path); | |
| 40 | |
| 41 // TODO(armansito): The parameters of the Options dictionary are undefined | |
| 42 // but the method signature still requires a value dictionary. Pass an | |
| 43 // empty dictionary and fill in the contents later once this is defined. | |
| 44 dbus::MessageWriter array_writer(NULL); | |
| 45 writer.OpenArray("{sv}", &array_writer); | |
| 46 writer.CloseContainer(&array_writer); | |
| 47 | |
| 48 DCHECK(object_proxy_); | |
| 49 object_proxy_->CallMethodWithErrorCallback( | |
| 50 &method_call, | |
| 51 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 52 base::Bind(&BluetoothGattManagerClientImpl::OnSuccess, | |
| 53 weak_ptr_factory_.GetWeakPtr(), callback), | |
| 54 base::Bind(&BluetoothGattManagerClientImpl::OnError, | |
| 55 weak_ptr_factory_.GetWeakPtr(), error_callback)); | |
| 56 } | |
| 57 | |
| 58 // BluetoothGattManagerClient override. | |
| 59 void UnregisterService(const dbus::ObjectPath& service_path, | |
| 60 const base::Closure& callback, | |
| 61 const ErrorCallback& error_callback) override { | |
| 62 dbus::MethodCall method_call( | |
| 63 bluetooth_gatt_manager::kBluetoothGattManagerInterface, | |
| 64 bluetooth_gatt_manager::kUnregisterService); | |
| 65 | |
| 66 dbus::MessageWriter writer(&method_call); | |
| 67 writer.AppendObjectPath(service_path); | |
| 68 | |
| 69 DCHECK(object_proxy_); | |
| 70 object_proxy_->CallMethodWithErrorCallback( | |
| 71 &method_call, | |
| 72 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 73 base::Bind(&BluetoothGattManagerClientImpl::OnSuccess, | |
| 74 weak_ptr_factory_.GetWeakPtr(), callback), | |
| 75 base::Bind(&BluetoothGattManagerClientImpl::OnError, | |
| 76 weak_ptr_factory_.GetWeakPtr(), error_callback)); | |
| 77 } | |
| 78 | |
| 79 protected: | |
| 80 // chromeos::DBusClient override. | |
| 81 void Init(dbus::Bus* bus) override { | |
| 82 DCHECK(bus); | |
| 83 object_proxy_ = bus->GetObjectProxy( | |
| 84 bluetooth_gatt_manager::kBluetoothGattManagerServiceName, | |
| 85 dbus::ObjectPath( | |
| 86 bluetooth_gatt_manager::kBluetoothGattManagerInterface)); | |
| 87 } | |
| 88 | |
| 89 private: | |
| 90 // Called when a response for a successful method call is received. | |
| 91 void OnSuccess(const base::Closure& callback, dbus::Response* response) { | |
| 92 DCHECK(response); | |
| 93 callback.Run(); | |
| 94 } | |
| 95 | |
| 96 // Called when a response for a failed method call is received. | |
| 97 void OnError(const ErrorCallback& error_callback, | |
| 98 dbus::ErrorResponse* response) { | |
| 99 // Error response has optional error message argument. | |
| 100 std::string error_name; | |
| 101 std::string error_message; | |
| 102 if (response) { | |
| 103 dbus::MessageReader reader(response); | |
| 104 error_name = response->GetErrorName(); | |
| 105 reader.PopString(&error_message); | |
| 106 } else { | |
| 107 error_name = kNoResponseError; | |
| 108 } | |
| 109 error_callback.Run(error_name, error_message); | |
| 110 } | |
| 111 | |
| 112 // The proxy to the remote GATT manager object. | |
| 113 dbus::ObjectProxy* object_proxy_; | |
| 114 | |
| 115 // Weak pointer factory for generating 'this' pointers that might live longer | |
| 116 // than we do. | |
| 117 // Note: This should remain the last member so it'll be destroyed and | |
| 118 // invalidate its weak pointers before any other members are destroyed. | |
| 119 base::WeakPtrFactory<BluetoothGattManagerClientImpl> weak_ptr_factory_; | |
| 120 | |
| 121 DISALLOW_COPY_AND_ASSIGN(BluetoothGattManagerClientImpl); | |
| 122 }; | |
| 123 | |
| 124 BluetoothGattManagerClient::BluetoothGattManagerClient() { | |
| 125 } | |
| 126 | |
| 127 BluetoothGattManagerClient::~BluetoothGattManagerClient() { | |
| 128 } | |
| 129 | |
| 130 // static | |
| 131 BluetoothGattManagerClient* BluetoothGattManagerClient::Create() { | |
| 132 return new BluetoothGattManagerClientImpl(); | |
| 133 } | |
| 134 | |
| 135 } // namespace chromeos | |
| OLD | NEW |