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_profile_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 ExperimentalBluetoothProfileManagerClient::kNoResponseError[] = |
| 18 "org.chromium.Error.NoResponse"; |
| 19 |
| 20 |
| 21 ExperimentalBluetoothProfileManagerClient::Options::Options() |
| 22 : role(SYMMETRIC), |
| 23 require_authentication(false), |
| 24 require_authorization(true), |
| 25 auto_connect(true) { |
| 26 } |
| 27 |
| 28 ExperimentalBluetoothProfileManagerClient::Options::~Options() { |
| 29 } |
| 30 |
| 31 // The ExperimentalBluetoothProfileManagerClient implementation used in |
| 32 // production. |
| 33 class ExperimentalBluetoothProfileManagerClientImpl |
| 34 : public ExperimentalBluetoothProfileManagerClient { |
| 35 public: |
| 36 explicit ExperimentalBluetoothProfileManagerClientImpl(dbus::Bus* bus) |
| 37 : bus_(bus), |
| 38 weak_ptr_factory_(this) { |
| 39 DCHECK(bus_); |
| 40 object_proxy_ = bus_->GetObjectProxy( |
| 41 bluetooth_profile_manager::kBluetoothProfileManagerServiceName, |
| 42 dbus::ObjectPath( |
| 43 bluetooth_profile_manager::kExperimentalBluetoothProfileManagerInterface)); |
| 44 } |
| 45 |
| 46 virtual ~ExperimentalBluetoothProfileManagerClientImpl() { |
| 47 } |
| 48 |
| 49 // ExperimentalBluetoothProfileManagerClient override. |
| 50 virtual void RegisterProfile(const dbus::ObjectPath& profile_path, |
| 51 const std::string& uuid, |
| 52 const Options& options, |
| 53 const base::Closure& callback, |
| 54 const ErrorCallback& error_callback) OVERRIDE { |
| 55 dbus::MethodCall method_call( |
| 56 bluetooth_profile_manager::kExperimentalBluetoothProfileManagerInterface, |
| 57 bluetooth_profile_manager::kRegisterProfile); |
| 58 |
| 59 dbus::MessageWriter writer(&method_call); |
| 60 writer.AppendObjectPath(profile_path); |
| 61 writer.AppendString(uuid); |
| 62 |
| 63 dbus::MessageWriter array_writer(NULL); |
| 64 writer.OpenArray("{sv}", &array_writer); |
| 65 |
| 66 dbus::MessageWriter dict_writer(NULL); |
| 67 |
| 68 // Always send Name, even if empty string. |
| 69 array_writer.OpenDictEntry(&dict_writer); |
| 70 dict_writer.AppendString(bluetooth_profile_manager::kNameOption); |
| 71 dict_writer.AppendVariantOfString(options.name); |
| 72 array_writer.CloseContainer(&dict_writer); |
| 73 |
| 74 // Don't send Service if not provided. |
| 75 if (options.service.length()) { |
| 76 dbus::MessageWriter dict_writer(NULL); |
| 77 array_writer.OpenDictEntry(&dict_writer); |
| 78 dict_writer.AppendString(bluetooth_profile_manager::kServiceOption); |
| 79 dict_writer.AppendVariantOfString(options.service); |
| 80 array_writer.CloseContainer(&dict_writer); |
| 81 } |
| 82 |
| 83 // Don't send the default Role since there's no value for it. |
| 84 if (options.role != SYMMETRIC) { |
| 85 dbus::MessageWriter dict_writer(NULL); |
| 86 array_writer.OpenDictEntry(&dict_writer); |
| 87 dict_writer.AppendString(bluetooth_profile_manager::kRoleOption); |
| 88 if (options.role == CLIENT) |
| 89 dict_writer.AppendVariantOfString( |
| 90 bluetooth_profile_manager::kClientRoleOption); |
| 91 else if (options.role == SERVER) |
| 92 dict_writer.AppendVariantOfString( |
| 93 bluetooth_profile_manager::kServerRoleOption); |
| 94 else |
| 95 dict_writer.AppendVariantOfString(""); |
| 96 array_writer.CloseContainer(&dict_writer); |
| 97 } |
| 98 |
| 99 // Don't send Channel unless given. |
| 100 if (options.channel) { |
| 101 dbus::MessageWriter dict_writer(NULL); |
| 102 array_writer.OpenDictEntry(&dict_writer); |
| 103 dict_writer.AppendString(bluetooth_profile_manager::kChannelOption); |
| 104 dict_writer.AppendVariantOfUint16(options.channel); |
| 105 array_writer.CloseContainer(&dict_writer); |
| 106 } |
| 107 |
| 108 // Don't send PSM unless given. |
| 109 if (options.psm) { |
| 110 dbus::MessageWriter dict_writer(NULL); |
| 111 array_writer.OpenDictEntry(&dict_writer); |
| 112 dict_writer.AppendString(bluetooth_profile_manager::kPSMOption); |
| 113 dict_writer.AppendVariantOfUint16(options.psm); |
| 114 array_writer.CloseContainer(&dict_writer); |
| 115 } |
| 116 |
| 117 // Always send RequireAuthentication, RequireAuthorization and AutoConnect. |
| 118 array_writer.OpenDictEntry(&dict_writer); |
| 119 dict_writer.AppendString( |
| 120 bluetooth_profile_manager::kRequireAuthenticationOption); |
| 121 dict_writer.AppendVariantOfBool(options.require_authentication); |
| 122 array_writer.CloseContainer(&dict_writer); |
| 123 |
| 124 array_writer.OpenDictEntry(&dict_writer); |
| 125 dict_writer.AppendString( |
| 126 bluetooth_profile_manager::kRequireAuthorizationOption); |
| 127 dict_writer.AppendVariantOfBool(options.require_authorization); |
| 128 array_writer.CloseContainer(&dict_writer); |
| 129 |
| 130 array_writer.OpenDictEntry(&dict_writer); |
| 131 dict_writer.AppendString( |
| 132 bluetooth_profile_manager::kAutoConnectOption); |
| 133 dict_writer.AppendVariantOfBool(options.auto_connect); |
| 134 array_writer.CloseContainer(&dict_writer); |
| 135 |
| 136 // Don't send ServiceRecord if not provided. |
| 137 if (options.service_record.length()) { |
| 138 dbus::MessageWriter dict_writer(NULL); |
| 139 array_writer.OpenDictEntry(&dict_writer); |
| 140 dict_writer.AppendString(bluetooth_profile_manager::kServiceRecordOption); |
| 141 dict_writer.AppendVariantOfString(options.service_record); |
| 142 array_writer.CloseContainer(&dict_writer); |
| 143 } |
| 144 |
| 145 // Don't send Version if not provided. |
| 146 if (options.version) { |
| 147 dbus::MessageWriter dict_writer(NULL); |
| 148 array_writer.OpenDictEntry(&dict_writer); |
| 149 dict_writer.AppendString(bluetooth_profile_manager::kVersionOption); |
| 150 dict_writer.AppendVariantOfUint16(options.version); |
| 151 array_writer.CloseContainer(&dict_writer); |
| 152 } |
| 153 |
| 154 // Don't send Features if not provided. |
| 155 if (options.features) { |
| 156 dbus::MessageWriter dict_writer(NULL); |
| 157 array_writer.OpenDictEntry(&dict_writer); |
| 158 dict_writer.AppendString(bluetooth_profile_manager::kFeaturesOption); |
| 159 dict_writer.AppendVariantOfUint16(options.features); |
| 160 array_writer.CloseContainer(&dict_writer); |
| 161 } |
| 162 |
| 163 writer.CloseContainer(&array_writer); |
| 164 |
| 165 object_proxy_->CallMethodWithErrorCallback( |
| 166 &method_call, |
| 167 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 168 base::Bind(&ExperimentalBluetoothProfileManagerClientImpl::OnSuccess, |
| 169 weak_ptr_factory_.GetWeakPtr(), callback), |
| 170 base::Bind(&ExperimentalBluetoothProfileManagerClientImpl::OnError, |
| 171 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
| 172 } |
| 173 |
| 174 // ExperimentalBluetoothProfileManagerClient override. |
| 175 virtual void UnregisterProfile(const dbus::ObjectPath& profile_path, |
| 176 const base::Closure& callback, |
| 177 const ErrorCallback& error_callback) OVERRIDE { |
| 178 dbus::MethodCall method_call( |
| 179 bluetooth_profile_manager::kExperimentalBluetoothProfileManagerInterface, |
| 180 bluetooth_profile_manager::kUnregisterProfile); |
| 181 |
| 182 dbus::MessageWriter writer(&method_call); |
| 183 writer.AppendObjectPath(profile_path); |
| 184 |
| 185 object_proxy_->CallMethodWithErrorCallback( |
| 186 &method_call, |
| 187 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 188 base::Bind(&ExperimentalBluetoothProfileManagerClientImpl::OnSuccess, |
| 189 weak_ptr_factory_.GetWeakPtr(), callback), |
| 190 base::Bind(&ExperimentalBluetoothProfileManagerClientImpl::OnError, |
| 191 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
| 192 } |
| 193 |
| 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(); |
| 200 } |
| 201 |
| 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 = ""; |
| 215 } |
| 216 error_callback.Run(error_name, error_message); |
| 217 } |
| 218 |
| 219 dbus::Bus* bus_; |
| 220 dbus::ObjectProxy* object_proxy_; |
| 221 |
| 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<ExperimentalBluetoothProfileManagerClientImpl> |
| 227 weak_ptr_factory_; |
| 228 |
| 229 DISALLOW_COPY_AND_ASSIGN(ExperimentalBluetoothProfileManagerClientImpl); |
| 230 }; |
| 231 |
| 232 // The ExperimentalBluetoothProfileManagerClient implementation used on Linux |
| 233 // desktop, which does nothing. |
| 234 class ExperimentalBluetoothProfileManagerClientStubImpl |
| 235 : public ExperimentalBluetoothProfileManagerClient { |
| 236 public: |
| 237 ExperimentalBluetoothProfileManagerClientStubImpl() { |
| 238 } |
| 239 |
| 240 // ExperimentalBluetoothProfileManagerClient override. |
| 241 virtual void RegisterProfile(const dbus::ObjectPath& profile_path, |
| 242 const std::string& uuid, |
| 243 const Options& options, |
| 244 const base::Closure& callback, |
| 245 const ErrorCallback& error_callback) OVERRIDE { |
| 246 VLOG(1) << "RegisterProfile: " << profile_path.value() << ": " << uuid; |
| 247 error_callback.Run(kNoResponseError, ""); |
| 248 } |
| 249 |
| 250 // ExperimentalBluetoothProfileManagerClient override. |
| 251 virtual void UnregisterProfile(const dbus::ObjectPath& profile_path, |
| 252 const base::Closure& callback, |
| 253 const ErrorCallback& error_callback) OVERRIDE { |
| 254 VLOG(1) << "UnregisterAgent: " << profile_path.value(); |
| 255 error_callback.Run(kNoResponseError, ""); |
| 256 } |
| 257 }; |
| 258 |
| 259 ExperimentalBluetoothProfileManagerClient:: |
| 260 ExperimentalBluetoothProfileManagerClient() { |
| 261 } |
| 262 |
| 263 ExperimentalBluetoothProfileManagerClient:: |
| 264 ~ExperimentalBluetoothProfileManagerClient() { |
| 265 } |
| 266 |
| 267 ExperimentalBluetoothProfileManagerClient* |
| 268 ExperimentalBluetoothProfileManagerClient::Create( |
| 269 DBusClientImplementationType type, |
| 270 dbus::Bus* bus) { |
| 271 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
| 272 return new ExperimentalBluetoothProfileManagerClientImpl(bus); |
| 273 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
| 274 return new ExperimentalBluetoothProfileManagerClientStubImpl(); |
| 275 } |
| 276 |
| 277 } // namespace chromeos |
OLD | NEW |