[Password Generation] Enable new UI
[chromium-blink-merge.git] / chromeos / dbus / bluetooth_profile_manager_client.cc
blobee2fe2b5d1fd05e1afe24e19f30f196a7407add6
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 "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"
15 namespace chromeos {
17 const char BluetoothProfileManagerClient::kNoResponseError[] =
18 "org.chromium.Error.NoResponse";
21 BluetoothProfileManagerClient::Options::Options()
22 : role(SYMMETRIC),
23 require_authentication(false),
24 require_authorization(false),
25 auto_connect(true) {
28 BluetoothProfileManagerClient::Options::~Options() {
31 // The BluetoothProfileManagerClient implementation used in production.
32 class BluetoothProfileManagerClientImpl
33 : public BluetoothProfileManagerClient {
34 public:
35 BluetoothProfileManagerClientImpl() : weak_ptr_factory_(this) {}
37 virtual ~BluetoothProfileManagerClientImpl() {
40 // BluetoothProfileManagerClient override.
41 virtual void RegisterProfile(const dbus::ObjectPath& profile_path,
42 const std::string& uuid,
43 const Options& options,
44 const base::Closure& callback,
45 const ErrorCallback& error_callback) OVERRIDE {
46 dbus::MethodCall method_call(
47 bluetooth_profile_manager::kBluetoothProfileManagerInterface,
48 bluetooth_profile_manager::kRegisterProfile);
50 dbus::MessageWriter writer(&method_call);
51 writer.AppendObjectPath(profile_path);
52 writer.AppendString(uuid);
54 dbus::MessageWriter array_writer(NULL);
55 writer.OpenArray("{sv}", &array_writer);
57 dbus::MessageWriter dict_writer(NULL);
59 // Always send Name, even if empty string.
60 array_writer.OpenDictEntry(&dict_writer);
61 dict_writer.AppendString(bluetooth_profile_manager::kNameOption);
62 dict_writer.AppendVariantOfString(options.name);
63 array_writer.CloseContainer(&dict_writer);
65 // Don't send Service if not provided.
66 if (options.service.length()) {
67 dbus::MessageWriter dict_writer(NULL);
68 array_writer.OpenDictEntry(&dict_writer);
69 dict_writer.AppendString(bluetooth_profile_manager::kServiceOption);
70 dict_writer.AppendVariantOfString(options.service);
71 array_writer.CloseContainer(&dict_writer);
74 // Don't send the default Role since there's no value for it.
75 if (options.role != SYMMETRIC) {
76 dbus::MessageWriter dict_writer(NULL);
77 array_writer.OpenDictEntry(&dict_writer);
78 dict_writer.AppendString(bluetooth_profile_manager::kRoleOption);
79 if (options.role == CLIENT)
80 dict_writer.AppendVariantOfString(
81 bluetooth_profile_manager::kClientRoleOption);
82 else if (options.role == SERVER)
83 dict_writer.AppendVariantOfString(
84 bluetooth_profile_manager::kServerRoleOption);
85 else
86 dict_writer.AppendVariantOfString("");
87 array_writer.CloseContainer(&dict_writer);
90 // Don't send Channel unless given.
91 if (options.channel) {
92 dbus::MessageWriter dict_writer(NULL);
93 array_writer.OpenDictEntry(&dict_writer);
94 dict_writer.AppendString(bluetooth_profile_manager::kChannelOption);
95 dict_writer.AppendVariantOfUint16(options.channel);
96 array_writer.CloseContainer(&dict_writer);
99 // Don't send PSM unless given.
100 if (options.psm) {
101 dbus::MessageWriter dict_writer(NULL);
102 array_writer.OpenDictEntry(&dict_writer);
103 dict_writer.AppendString(bluetooth_profile_manager::kPSMOption);
104 dict_writer.AppendVariantOfUint16(options.psm);
105 array_writer.CloseContainer(&dict_writer);
108 // Always send RequireAuthentication, RequireAuthorization and AutoConnect.
109 array_writer.OpenDictEntry(&dict_writer);
110 dict_writer.AppendString(
111 bluetooth_profile_manager::kRequireAuthenticationOption);
112 dict_writer.AppendVariantOfBool(options.require_authentication);
113 array_writer.CloseContainer(&dict_writer);
115 array_writer.OpenDictEntry(&dict_writer);
116 dict_writer.AppendString(
117 bluetooth_profile_manager::kRequireAuthorizationOption);
118 dict_writer.AppendVariantOfBool(options.require_authorization);
119 array_writer.CloseContainer(&dict_writer);
121 array_writer.OpenDictEntry(&dict_writer);
122 dict_writer.AppendString(
123 bluetooth_profile_manager::kAutoConnectOption);
124 dict_writer.AppendVariantOfBool(options.auto_connect);
125 array_writer.CloseContainer(&dict_writer);
127 // Don't send ServiceRecord if not provided.
128 if (options.service_record.length()) {
129 dbus::MessageWriter dict_writer(NULL);
130 array_writer.OpenDictEntry(&dict_writer);
131 dict_writer.AppendString(bluetooth_profile_manager::kServiceRecordOption);
132 dict_writer.AppendVariantOfString(options.service_record);
133 array_writer.CloseContainer(&dict_writer);
136 // Don't send Version if not provided.
137 if (options.version) {
138 dbus::MessageWriter dict_writer(NULL);
139 array_writer.OpenDictEntry(&dict_writer);
140 dict_writer.AppendString(bluetooth_profile_manager::kVersionOption);
141 dict_writer.AppendVariantOfUint16(options.version);
142 array_writer.CloseContainer(&dict_writer);
145 // Don't send Features if not provided.
146 if (options.features) {
147 dbus::MessageWriter dict_writer(NULL);
148 array_writer.OpenDictEntry(&dict_writer);
149 dict_writer.AppendString(bluetooth_profile_manager::kFeaturesOption);
150 dict_writer.AppendVariantOfUint16(options.features);
151 array_writer.CloseContainer(&dict_writer);
154 writer.CloseContainer(&array_writer);
156 object_proxy_->CallMethodWithErrorCallback(
157 &method_call,
158 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
159 base::Bind(&BluetoothProfileManagerClientImpl::OnSuccess,
160 weak_ptr_factory_.GetWeakPtr(), callback),
161 base::Bind(&BluetoothProfileManagerClientImpl::OnError,
162 weak_ptr_factory_.GetWeakPtr(), error_callback));
165 // BluetoothProfileManagerClient override.
166 virtual void UnregisterProfile(const dbus::ObjectPath& profile_path,
167 const base::Closure& callback,
168 const ErrorCallback& error_callback) OVERRIDE {
169 dbus::MethodCall method_call(
170 bluetooth_profile_manager::kBluetoothProfileManagerInterface,
171 bluetooth_profile_manager::kUnregisterProfile);
173 dbus::MessageWriter writer(&method_call);
174 writer.AppendObjectPath(profile_path);
176 object_proxy_->CallMethodWithErrorCallback(
177 &method_call,
178 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
179 base::Bind(&BluetoothProfileManagerClientImpl::OnSuccess,
180 weak_ptr_factory_.GetWeakPtr(), callback),
181 base::Bind(&BluetoothProfileManagerClientImpl::OnError,
182 weak_ptr_factory_.GetWeakPtr(), error_callback));
185 protected:
186 virtual void Init(dbus::Bus* bus) OVERRIDE {
187 DCHECK(bus);
188 object_proxy_ = bus->GetObjectProxy(
189 bluetooth_profile_manager::kBluetoothProfileManagerServiceName,
190 dbus::ObjectPath(
191 bluetooth_profile_manager::kBluetoothProfileManagerServicePath));
194 private:
195 // Called when a response for successful method call is received.
196 void OnSuccess(const base::Closure& callback,
197 dbus::Response* response) {
198 DCHECK(response);
199 callback.Run();
202 // Called when a response for a failed method call is received.
203 void OnError(const ErrorCallback& error_callback,
204 dbus::ErrorResponse* response) {
205 // Error response has optional error message argument.
206 std::string error_name;
207 std::string error_message;
208 if (response) {
209 dbus::MessageReader reader(response);
210 error_name = response->GetErrorName();
211 reader.PopString(&error_message);
212 } else {
213 error_name = kNoResponseError;
214 error_message = "";
216 error_callback.Run(error_name, error_message);
219 dbus::ObjectProxy* object_proxy_;
221 // Weak pointer factory for generating 'this' pointers that might live longer
222 // than we do.
223 // Note: This should remain the last member so it'll be destroyed and
224 // invalidate its weak pointers before any other members are destroyed.
225 base::WeakPtrFactory<BluetoothProfileManagerClientImpl> weak_ptr_factory_;
227 DISALLOW_COPY_AND_ASSIGN(BluetoothProfileManagerClientImpl);
230 BluetoothProfileManagerClient::BluetoothProfileManagerClient() {
233 BluetoothProfileManagerClient::~BluetoothProfileManagerClient() {
236 BluetoothProfileManagerClient* BluetoothProfileManagerClient::Create() {
237 return new BluetoothProfileManagerClientImpl();
240 } // namespace chromeos