OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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 | |
5 #include "chromeos/dbus/experimental_bluetooth_agent_manager_client.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/logging.h" | |
9 #include "dbus/bus.h" | |
10 #include "dbus/message.h" | |
11 #include "dbus/object_path.h" | |
12 #include "dbus/object_proxy.h" | |
13 #include "third_party/cros_system_api/dbus/service_constants.h" | |
14 | |
15 namespace chromeos { | |
16 | |
17 const char ExperimentalBluetoothAgentManagerClient::kNoResponseError[] = | |
18 "org.chromium.Error.NoResponse"; | |
19 | |
20 // The ExperimentalBluetoothAgentManagerClient implementation used in | |
21 // production. | |
22 class ExperimentalBluetoothAgentManagerClientImpl | |
23 : public ExperimentalBluetoothAgentManagerClient { | |
24 public: | |
25 explicit ExperimentalBluetoothAgentManagerClientImpl(dbus::Bus* bus) | |
26 : bus_(bus), | |
27 weak_ptr_factory_(this) { | |
28 DCHECK(bus_); | |
29 object_proxy_ = bus_->GetObjectProxy( | |
30 bluetooth_agent_manager::kBluetoothAgentManagerServiceName, | |
31 dbus::ObjectPath( | |
32 bluetooth_agent_manager::kExperimentalBluetoothAgentManagerInterface)); | |
33 } | |
34 | |
35 virtual ~ExperimentalBluetoothAgentManagerClientImpl() { | |
36 } | |
37 | |
38 // ExperimentalBluetoothAgentManagerClient override. | |
39 virtual void RegisterAgent(const dbus::ObjectPath& agent_path, | |
40 const std::string& capability, | |
41 const base::Closure& callback, | |
42 const ErrorCallback& error_callback) OVERRIDE { | |
43 dbus::MethodCall method_call( | |
44 bluetooth_agent_manager::kExperimentalBluetoothAgentManagerInterface, | |
satorux1
2013/03/22 02:00:12
four spaces before the parameter
| |
45 bluetooth_agent_manager::kRegisterAgent); | |
46 | |
47 dbus::MessageWriter writer(&method_call); | |
48 writer.AppendObjectPath(agent_path); | |
49 writer.AppendString(capability); | |
50 | |
51 object_proxy_->CallMethodWithErrorCallback( | |
52 &method_call, | |
53 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
54 base::Bind(&ExperimentalBluetoothAgentManagerClientImpl::OnSuccess, | |
55 weak_ptr_factory_.GetWeakPtr(), callback), | |
56 base::Bind(&ExperimentalBluetoothAgentManagerClientImpl::OnError, | |
57 weak_ptr_factory_.GetWeakPtr(), error_callback)); | |
58 } | |
59 | |
60 // ExperimentalBluetoothAgentManagerClient override. | |
61 virtual void UnregisterAgent(const dbus::ObjectPath& agent_path, | |
62 const base::Closure& callback, | |
63 const ErrorCallback& error_callback) OVERRIDE { | |
64 dbus::MethodCall method_call( | |
65 bluetooth_agent_manager::kExperimentalBluetoothAgentManagerInterface, | |
66 bluetooth_agent_manager::kUnregisterAgent); | |
67 | |
68 dbus::MessageWriter writer(&method_call); | |
69 writer.AppendObjectPath(agent_path); | |
70 | |
71 object_proxy_->CallMethodWithErrorCallback( | |
72 &method_call, | |
73 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
74 base::Bind(&ExperimentalBluetoothAgentManagerClientImpl::OnSuccess, | |
75 weak_ptr_factory_.GetWeakPtr(), callback), | |
76 base::Bind(&ExperimentalBluetoothAgentManagerClientImpl::OnError, | |
77 weak_ptr_factory_.GetWeakPtr(), error_callback)); | |
78 } | |
79 | |
80 | |
81 // ExperimentalBluetoothAgentManagerClient override. | |
82 virtual void RequestDefaultAgent(const dbus::ObjectPath& agent_path, | |
83 const base::Closure& callback, | |
84 const ErrorCallback& error_callback) | |
85 OVERRIDE { | |
86 dbus::MethodCall method_call( | |
87 bluetooth_agent_manager::kExperimentalBluetoothAgentManagerInterface, | |
88 bluetooth_agent_manager::kRequestDefaultAgent); | |
89 | |
90 dbus::MessageWriter writer(&method_call); | |
91 writer.AppendObjectPath(agent_path); | |
92 | |
93 object_proxy_->CallMethodWithErrorCallback( | |
94 &method_call, | |
95 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
96 base::Bind(&ExperimentalBluetoothAgentManagerClientImpl::OnSuccess, | |
97 weak_ptr_factory_.GetWeakPtr(), callback), | |
98 base::Bind(&ExperimentalBluetoothAgentManagerClientImpl::OnError, | |
99 weak_ptr_factory_.GetWeakPtr(), error_callback)); | |
100 } | |
101 | |
102 private: | |
103 // Called when a response for successful method call is received. | |
104 void OnSuccess(const base::Closure& callback, | |
105 dbus::Response* response) { | |
106 DCHECK(response); | |
107 callback.Run(); | |
108 } | |
109 | |
110 // Called when a response for a failed method call is received. | |
111 void OnError(const ErrorCallback& error_callback, | |
112 dbus::ErrorResponse* response) { | |
113 // Error response has optional error message argument. | |
114 std::string error_name; | |
115 std::string error_message; | |
116 if (response) { | |
117 dbus::MessageReader reader(response); | |
118 error_name = response->GetErrorName(); | |
119 reader.PopString(&error_message); | |
120 } else { | |
121 error_name = kNoResponseError; | |
122 error_message = ""; | |
123 } | |
124 error_callback.Run(error_name, error_message); | |
125 } | |
126 | |
127 dbus::Bus* bus_; | |
128 dbus::ObjectProxy* object_proxy_; | |
129 | |
130 // Weak pointer factory for generating 'this' pointers that might live longer | |
131 // than we do. | |
132 // Note: This should remain the last member so it'll be destroyed and | |
133 // invalidate its weak pointers before any other members are destroyed. | |
134 base::WeakPtrFactory<ExperimentalBluetoothAgentManagerClientImpl> | |
135 weak_ptr_factory_; | |
136 | |
137 DISALLOW_COPY_AND_ASSIGN(ExperimentalBluetoothAgentManagerClientImpl); | |
138 }; | |
139 | |
140 // The ExperimentalBluetoothAgentManagerClient implementation used on Linux | |
141 // desktop, which does nothing. | |
142 class ExperimentalBluetoothAgentManagerClientStubImpl | |
143 : public ExperimentalBluetoothAgentManagerClient { | |
144 public: | |
145 ExperimentalBluetoothAgentManagerClientStubImpl() { | |
146 } | |
147 | |
148 // ExperimentalBluetoothAgentManagerClient override. | |
149 virtual void RegisterAgent(const dbus::ObjectPath& agent_path, | |
150 const std::string& capability, | |
151 const base::Closure& callback, | |
152 const ErrorCallback& error_callback) OVERRIDE { | |
153 VLOG(1) << "RegisterAgent: " << agent_path.value(); | |
154 error_callback.Run(kNoResponseError, ""); | |
155 } | |
156 | |
157 // ExperimentalBluetoothAgentManagerClient override. | |
158 virtual void UnregisterAgent(const dbus::ObjectPath& agent_path, | |
159 const base::Closure& callback, | |
160 const ErrorCallback& error_callback) OVERRIDE { | |
161 VLOG(1) << "UnregisterAgent: " << agent_path.value(); | |
162 error_callback.Run(kNoResponseError, ""); | |
163 } | |
164 | |
165 // ExperimentalBluetoothAgentManagerClient override. | |
166 virtual void RequestDefaultAgent(const dbus::ObjectPath& agent_path, | |
167 const base::Closure& callback, | |
168 const ErrorCallback& error_callback) | |
169 OVERRIDE { | |
170 VLOG(1) << "RequestDefaultAgent: " << agent_path.value(); | |
171 error_callback.Run(kNoResponseError, ""); | |
172 } | |
173 }; | |
174 | |
175 ExperimentalBluetoothAgentManagerClient:: | |
176 ExperimentalBluetoothAgentManagerClient() { | |
177 } | |
178 | |
179 ExperimentalBluetoothAgentManagerClient:: | |
180 ~ExperimentalBluetoothAgentManagerClient() { | |
181 } | |
182 | |
183 ExperimentalBluetoothAgentManagerClient* | |
184 ExperimentalBluetoothAgentManagerClient::Create( | |
185 DBusClientImplementationType type, | |
186 dbus::Bus* bus) { | |
187 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | |
188 return new ExperimentalBluetoothAgentManagerClientImpl(bus); | |
189 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | |
190 return new ExperimentalBluetoothAgentManagerClientStubImpl(); | |
191 } | |
192 | |
193 } // namespace chromeos | |
OLD | NEW |