Index: chromeos/dbus/bluetooth_advertisement_manager_client.cc |
diff --git a/chromeos/dbus/bluetooth_advertisement_manager_client.cc b/chromeos/dbus/bluetooth_advertisement_manager_client.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..78ae2cecda4d727caf5b841efa647c31dfddfcba |
--- /dev/null |
+++ b/chromeos/dbus/bluetooth_advertisement_manager_client.cc |
@@ -0,0 +1,124 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chromeos/dbus/bluetooth_advertisement_manager_client.h" |
+ |
+#include "base/bind.h" |
+#include "base/logging.h" |
+#include "dbus/bus.h" |
+#include "dbus/message.h" |
+#include "dbus/object_proxy.h" |
+#include "third_party/cros_system_api/dbus/service_constants.h" |
+ |
+namespace chromeos { |
+ |
+// TODO(rkc): Remove these once the service constants header is updated. |
armansito
2015/04/09 22:08:32
Put these in an anonymous namespace.
rkc
2015/04/13 19:47:45
Removed.
|
+const char BluetoothAdvertisementManagerClient::kNoResponseError[] = ""; |
armansito
2015/04/09 22:08:32
You already have a TODO to move these later, why a
rkc
2015/04/13 19:47:45
Removed.
|
+const char kBluetoothAdvertisementManagerInterface[] = ""; |
+const char kRegisterAdvertisement[] = ""; |
+const char kUnregisterAdvertisement[] = ""; |
+const char kBluetoothAdvertisementManagerServiceName[] = ""; |
+const char kBluetoothAdvertisementManagerServicePath[] = ""; |
+ |
+// The BluetoothAdvertisementManagerClient implementation used in production. |
+class BluetoothAdvertisementManagerClientImpl |
+ : public BluetoothAdvertisementManagerClient { |
+ public: |
+ BluetoothAdvertisementManagerClientImpl() |
+ : object_proxy_(NULL), weak_ptr_factory_(this) {} |
+ |
+ ~BluetoothAdvertisementManagerClientImpl() override {} |
+ |
+ // BluetoothAdvertisementManagerClient override. |
+ void RegisterAdvertisement(const dbus::ObjectPath& obj_path, |
+ const base::Closure& callback, |
+ const ErrorCallback& error_callback) override { |
+ dbus::MethodCall method_call(kBluetoothAdvertisementManagerInterface, |
+ kRegisterAdvertisement); |
+ |
+ dbus::MessageWriter writer(&method_call); |
+ writer.AppendObjectPath(obj_path); |
armansito
2015/04/09 22:08:32
The RegisterAdvertisement method also expects an "
rkc
2015/04/13 19:47:45
Done.
|
+ |
+ object_proxy_->CallMethodWithErrorCallback( |
+ &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
+ base::Bind(&BluetoothAdvertisementManagerClientImpl::OnSuccess, |
+ weak_ptr_factory_.GetWeakPtr(), callback), |
+ base::Bind(&BluetoothAdvertisementManagerClientImpl::OnError, |
+ weak_ptr_factory_.GetWeakPtr(), error_callback)); |
+ } |
+ |
+ // BluetoothAdvertisementManagerClient override. |
+ void UnregisterAdvertisement(const dbus::ObjectPath& profile_path, |
+ const base::Closure& callback, |
+ const ErrorCallback& error_callback) override { |
+ dbus::MethodCall method_call(kBluetoothAdvertisementManagerInterface, |
+ kUnregisterAdvertisement); |
+ |
+ dbus::MessageWriter writer(&method_call); |
+ writer.AppendObjectPath(profile_path); |
armansito
2015/04/09 22:08:32
Fix the "profile_path" copy-pasta, here and elsewh
rkc
2015/04/13 19:47:45
Done.
|
+ |
+ object_proxy_->CallMethodWithErrorCallback( |
+ &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
+ base::Bind(&BluetoothAdvertisementManagerClientImpl::OnSuccess, |
+ weak_ptr_factory_.GetWeakPtr(), callback), |
+ base::Bind(&BluetoothAdvertisementManagerClientImpl::OnError, |
+ weak_ptr_factory_.GetWeakPtr(), error_callback)); |
+ } |
+ |
+ protected: |
+ void Init(dbus::Bus* bus) override { |
+ DCHECK(bus); |
+ object_proxy_ = bus->GetObjectProxy( |
+ kBluetoothAdvertisementManagerServiceName, |
+ dbus::ObjectPath(kBluetoothAdvertisementManagerServicePath)); |
armansito
2015/04/09 22:08:32
There's no hard-coded object path for the LEAdvert
rkc
2015/04/13 19:47:46
Done.
|
+ } |
+ |
+ private: |
+ // Called when a response for successful method call is received. |
+ void OnSuccess(const base::Closure& callback, dbus::Response* response) { |
+ DCHECK(response); |
+ callback.Run(); |
+ } |
+ |
+ // Called when a response for a failed method call is received. |
+ void OnError(const ErrorCallback& error_callback, |
+ dbus::ErrorResponse* response) { |
+ // Error response has optional error message argument. |
+ std::string error_name; |
+ std::string error_message; |
+ if (response) { |
+ dbus::MessageReader reader(response); |
+ error_name = response->GetErrorName(); |
+ reader.PopString(&error_message); |
+ } else { |
+ error_name = kNoResponseError; |
+ error_message = ""; |
+ } |
+ error_callback.Run(error_name, error_message); |
+ } |
+ |
+ dbus::ObjectProxy* object_proxy_; |
armansito
2015/04/09 22:08:32
You'll need to use a dbus::ObjectManager here inst
rkc
2015/04/13 19:47:45
Done.
|
+ |
+ // Weak pointer factory for generating 'this' pointers that might live longer |
+ // than we do. |
+ // Note: This should remain the last member so it'll be destroyed and |
+ // invalidate its weak pointers before any other members are destroyed. |
+ base::WeakPtrFactory<BluetoothAdvertisementManagerClientImpl> |
+ weak_ptr_factory_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(BluetoothAdvertisementManagerClientImpl); |
+}; |
+ |
+BluetoothAdvertisementManagerClient::BluetoothAdvertisementManagerClient() { |
+} |
+ |
+BluetoothAdvertisementManagerClient::~BluetoothAdvertisementManagerClient() { |
+} |
+ |
+BluetoothAdvertisementManagerClient* |
+BluetoothAdvertisementManagerClient::Create() { |
+ return new BluetoothAdvertisementManagerClientImpl(); |
+} |
+ |
+} // namespace chromeos |