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