Simple Cache: transactional index in subdirectory
[chromium-blink-merge.git] / chromeos / dbus / bluetooth_profile_manager_client.cc
blobaebd0d8929ba8fc122d375f92ec9a2098a4c897e
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_profile_manager_client.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "chromeos/dbus/fake_bluetooth_profile_manager_client.h"
10 #include "dbus/bus.h"
11 #include "dbus/message.h"
12 #include "dbus/object_path.h"
13 #include "dbus/object_proxy.h"
14 #include "third_party/cros_system_api/dbus/service_constants.h"
16 namespace chromeos {
18 const char BluetoothProfileManagerClient::kNoResponseError[] =
19 "org.chromium.Error.NoResponse";
22 BluetoothProfileManagerClient::Options::Options()
23 : role(SYMMETRIC),
24 require_authentication(false),
25 require_authorization(false),
26 auto_connect(true) {
29 BluetoothProfileManagerClient::Options::~Options() {
32 // The BluetoothProfileManagerClient implementation used in production.
33 class BluetoothProfileManagerClientImpl
34 : public BluetoothProfileManagerClient {
35 public:
36 BluetoothProfileManagerClientImpl() : weak_ptr_factory_(this) {}
38 virtual ~BluetoothProfileManagerClientImpl() {
41 // BluetoothProfileManagerClient override.
42 virtual void RegisterProfile(const dbus::ObjectPath& profile_path,
43 const std::string& uuid,
44 const Options& options,
45 const base::Closure& callback,
46 const ErrorCallback& error_callback) OVERRIDE {
47 dbus::MethodCall method_call(
48 bluetooth_profile_manager::kBluetoothProfileManagerInterface,
49 bluetooth_profile_manager::kRegisterProfile);
51 dbus::MessageWriter writer(&method_call);
52 writer.AppendObjectPath(profile_path);
53 writer.AppendString(uuid);
55 dbus::MessageWriter array_writer(NULL);
56 writer.OpenArray("{sv}", &array_writer);
58 dbus::MessageWriter dict_writer(NULL);
60 // Always send Name, even if empty string.
61 array_writer.OpenDictEntry(&dict_writer);
62 dict_writer.AppendString(bluetooth_profile_manager::kNameOption);
63 dict_writer.AppendVariantOfString(options.name);
64 array_writer.CloseContainer(&dict_writer);
66 // Don't send Service if not provided.
67 if (options.service.length()) {
68 dbus::MessageWriter dict_writer(NULL);
69 array_writer.OpenDictEntry(&dict_writer);
70 dict_writer.AppendString(bluetooth_profile_manager::kServiceOption);
71 dict_writer.AppendVariantOfString(options.service);
72 array_writer.CloseContainer(&dict_writer);
75 // Don't send the default Role since there's no value for it.
76 if (options.role != SYMMETRIC) {
77 dbus::MessageWriter dict_writer(NULL);
78 array_writer.OpenDictEntry(&dict_writer);
79 dict_writer.AppendString(bluetooth_profile_manager::kRoleOption);
80 if (options.role == CLIENT)
81 dict_writer.AppendVariantOfString(
82 bluetooth_profile_manager::kClientRoleOption);
83 else if (options.role == SERVER)
84 dict_writer.AppendVariantOfString(
85 bluetooth_profile_manager::kServerRoleOption);
86 else
87 dict_writer.AppendVariantOfString("");
88 array_writer.CloseContainer(&dict_writer);
91 // Don't send Channel unless given.
92 if (options.channel) {
93 dbus::MessageWriter dict_writer(NULL);
94 array_writer.OpenDictEntry(&dict_writer);
95 dict_writer.AppendString(bluetooth_profile_manager::kChannelOption);
96 dict_writer.AppendVariantOfUint16(options.channel);
97 array_writer.CloseContainer(&dict_writer);
100 // Don't send PSM unless given.
101 if (options.psm) {
102 dbus::MessageWriter dict_writer(NULL);
103 array_writer.OpenDictEntry(&dict_writer);
104 dict_writer.AppendString(bluetooth_profile_manager::kPSMOption);
105 dict_writer.AppendVariantOfUint16(options.psm);
106 array_writer.CloseContainer(&dict_writer);
109 // Always send RequireAuthentication, RequireAuthorization and AutoConnect.
110 array_writer.OpenDictEntry(&dict_writer);
111 dict_writer.AppendString(
112 bluetooth_profile_manager::kRequireAuthenticationOption);
113 dict_writer.AppendVariantOfBool(options.require_authentication);
114 array_writer.CloseContainer(&dict_writer);
116 array_writer.OpenDictEntry(&dict_writer);
117 dict_writer.AppendString(
118 bluetooth_profile_manager::kRequireAuthorizationOption);
119 dict_writer.AppendVariantOfBool(options.require_authorization);
120 array_writer.CloseContainer(&dict_writer);
122 array_writer.OpenDictEntry(&dict_writer);
123 dict_writer.AppendString(
124 bluetooth_profile_manager::kAutoConnectOption);
125 dict_writer.AppendVariantOfBool(options.auto_connect);
126 array_writer.CloseContainer(&dict_writer);
128 // Don't send ServiceRecord if not provided.
129 if (options.service_record.length()) {
130 dbus::MessageWriter dict_writer(NULL);
131 array_writer.OpenDictEntry(&dict_writer);
132 dict_writer.AppendString(bluetooth_profile_manager::kServiceRecordOption);
133 dict_writer.AppendVariantOfString(options.service_record);
134 array_writer.CloseContainer(&dict_writer);
137 // Don't send Version if not provided.
138 if (options.version) {
139 dbus::MessageWriter dict_writer(NULL);
140 array_writer.OpenDictEntry(&dict_writer);
141 dict_writer.AppendString(bluetooth_profile_manager::kVersionOption);
142 dict_writer.AppendVariantOfUint16(options.version);
143 array_writer.CloseContainer(&dict_writer);
146 // Don't send Features if not provided.
147 if (options.features) {
148 dbus::MessageWriter dict_writer(NULL);
149 array_writer.OpenDictEntry(&dict_writer);
150 dict_writer.AppendString(bluetooth_profile_manager::kFeaturesOption);
151 dict_writer.AppendVariantOfUint16(options.features);
152 array_writer.CloseContainer(&dict_writer);
155 writer.CloseContainer(&array_writer);
157 object_proxy_->CallMethodWithErrorCallback(
158 &method_call,
159 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
160 base::Bind(&BluetoothProfileManagerClientImpl::OnSuccess,
161 weak_ptr_factory_.GetWeakPtr(), callback),
162 base::Bind(&BluetoothProfileManagerClientImpl::OnError,
163 weak_ptr_factory_.GetWeakPtr(), error_callback));
166 // BluetoothProfileManagerClient override.
167 virtual void UnregisterProfile(const dbus::ObjectPath& profile_path,
168 const base::Closure& callback,
169 const ErrorCallback& error_callback) OVERRIDE {
170 dbus::MethodCall method_call(
171 bluetooth_profile_manager::kBluetoothProfileManagerInterface,
172 bluetooth_profile_manager::kUnregisterProfile);
174 dbus::MessageWriter writer(&method_call);
175 writer.AppendObjectPath(profile_path);
177 object_proxy_->CallMethodWithErrorCallback(
178 &method_call,
179 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
180 base::Bind(&BluetoothProfileManagerClientImpl::OnSuccess,
181 weak_ptr_factory_.GetWeakPtr(), callback),
182 base::Bind(&BluetoothProfileManagerClientImpl::OnError,
183 weak_ptr_factory_.GetWeakPtr(), error_callback));
186 protected:
187 virtual void Init(dbus::Bus* bus) OVERRIDE {
188 DCHECK(bus);
189 object_proxy_ = bus->GetObjectProxy(
190 bluetooth_profile_manager::kBluetoothProfileManagerServiceName,
191 dbus::ObjectPath(
192 bluetooth_profile_manager::kBluetoothProfileManagerServicePath));
195 private:
196 // Called when a response for successful method call is received.
197 void OnSuccess(const base::Closure& callback,
198 dbus::Response* response) {
199 DCHECK(response);
200 callback.Run();
203 // Called when a response for a failed method call is received.
204 void OnError(const ErrorCallback& error_callback,
205 dbus::ErrorResponse* response) {
206 // Error response has optional error message argument.
207 std::string error_name;
208 std::string error_message;
209 if (response) {
210 dbus::MessageReader reader(response);
211 error_name = response->GetErrorName();
212 reader.PopString(&error_message);
213 } else {
214 error_name = kNoResponseError;
215 error_message = "";
217 error_callback.Run(error_name, error_message);
220 dbus::ObjectProxy* object_proxy_;
222 // Weak pointer factory for generating 'this' pointers that might live longer
223 // than we do.
224 // Note: This should remain the last member so it'll be destroyed and
225 // invalidate its weak pointers before any other members are destroyed.
226 base::WeakPtrFactory<BluetoothProfileManagerClientImpl> weak_ptr_factory_;
228 DISALLOW_COPY_AND_ASSIGN(BluetoothProfileManagerClientImpl);
231 BluetoothProfileManagerClient::BluetoothProfileManagerClient() {
234 BluetoothProfileManagerClient::~BluetoothProfileManagerClient() {
237 BluetoothProfileManagerClient* BluetoothProfileManagerClient::Create(
238 DBusClientImplementationType type) {
239 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
240 return new BluetoothProfileManagerClientImpl();
241 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
242 return new FakeBluetoothProfileManagerClient();
245 } // namespace chromeos