Chromium Code Reviews| Index: device/bluetooth/bluetooth_adapter_profile_chromeos.cc |
| diff --git a/device/bluetooth/bluetooth_adapter_profile_chromeos.cc b/device/bluetooth/bluetooth_adapter_profile_chromeos.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..79f88d9eca99a1de586a9ff8aca65d20d7f1b81e |
| --- /dev/null |
| +++ b/device/bluetooth/bluetooth_adapter_profile_chromeos.cc |
| @@ -0,0 +1,169 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "device/bluetooth/bluetooth_adapter_profile_chromeos.h" |
| + |
| +#include <string> |
| + |
| +#include "base/bind.h" |
| +#include "base/logging.h" |
| +#include "base/strings/string_util.h" |
| +#include "chromeos/dbus/bluetooth_profile_service_provider.h" |
| +#include "chromeos/dbus/dbus_thread_manager.h" |
| +#include "dbus/bus.h" |
| +#include "dbus/object_path.h" |
| +#include "device/bluetooth/bluetooth_adapter_chromeos.h" |
| +#include "device/bluetooth/bluetooth_uuid.h" |
| + |
| +namespace chromeos { |
| + |
| +// static |
| +BluetoothAdapterProfileChromeOS* BluetoothAdapterProfileChromeOS::Register( |
| + BluetoothAdapterChromeOS* adapter, |
| + const device::BluetoothUUID& uuid, |
| + const BluetoothProfileManagerClient::Options* options, |
| + const base::Closure& success_callback, |
| + const BluetoothProfileManagerClient::ErrorCallback& error_callback) { |
| + BluetoothAdapterProfileChromeOS* profile = |
| + new BluetoothAdapterProfileChromeOS(adapter, uuid); |
| + |
| + VLOG(1) << profile->object_path().value() << ": Registering profile."; |
|
armansito
2015/01/21 01:44:47
nit: I'd turn this log line around: "Registering p
Marie Janssen
2015/01/22 21:55:32
Done.
|
| + DBusThreadManager::Get()->GetBluetoothProfileManagerClient()->RegisterProfile( |
| + profile->object_path(), uuid.canonical_value(), *options, |
| + success_callback, error_callback); |
| + |
| + return profile; |
| +} |
| + |
| +BluetoothAdapterProfileChromeOS::BluetoothAdapterProfileChromeOS( |
| + BluetoothAdapterChromeOS* adapter, |
| + const device::BluetoothUUID& uuid) |
| + : uuid_(uuid), adapter_(adapter), weak_ptr_factory_(this) { |
| + std::string uuid_path; |
| + base::ReplaceChars(uuid.canonical_value(), ":-", "_", &uuid_path); |
| + object_path_ = |
| + dbus::ObjectPath("/org/chromium/bluetooth_profile/" + uuid_path); |
| + |
| + dbus::Bus* system_bus = DBusThreadManager::Get()->GetSystemBus(); |
| + profile_.reset( |
| + BluetoothProfileServiceProvider::Create(system_bus, object_path_, this)); |
| + DCHECK(profile_.get()); |
| +} |
| + |
| +BluetoothAdapterProfileChromeOS::~BluetoothAdapterProfileChromeOS() { |
| + profile_.reset(); |
| +} |
| + |
| +bool BluetoothAdapterProfileChromeOS::SetDelegate( |
| + const dbus::ObjectPath& device_path, |
| + BluetoothProfileServiceProvider::Delegate* delegate) { |
|
armansito
2015/01/21 01:44:47
DCHECK(delegate);
Marie Janssen
2015/01/22 21:55:32
Done.
|
| + VLOG(1) << object_path_.value() << " dev " << device_path.value() |
| + << ": SetDelegate"; |
| + |
| + if (delegates_.find(device_path.value()) != delegates_.end()) { |
| + return false; |
| + } |
| + |
| + delegates_[device_path.value()] = delegate; |
| + return true; |
| +} |
| + |
| +void BluetoothAdapterProfileChromeOS::RemoveDelegate( |
| + const dbus::ObjectPath& device_path) { |
| + DCHECK(adapter_); |
| + VLOG(1) << object_path_.value() << " dev " << device_path.value() |
| + << ": RemoveDelegate"; |
| + |
| + if (delegates_.find(device_path.value()) == delegates_.end()) |
| + return; |
| + |
| + VLOG(1) << device_path.value() << "Delegate found"; |
| + |
| + delegates_.erase(device_path.value()); |
| + |
| + if (delegates_.size() != 0) |
| + return; |
| + |
| + // No users left, release the profile. |
| + DBusThreadManager::Get() |
| + ->GetBluetoothProfileManagerClient() |
| + ->UnregisterProfile( |
| + object_path_, |
| + base::Bind(&BluetoothAdapterProfileChromeOS::OnUnregisterProfile, |
| + weak_ptr_factory_.GetWeakPtr()), |
| + base::Bind(&BluetoothAdapterProfileChromeOS::OnUnregisterProfileError, |
| + weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +void BluetoothAdapterProfileChromeOS::OnUnregisterProfile() { |
| + VLOG(1) << object_path().value() << ": Profile unregistered"; |
| + |
| + adapter_->ReleaseProfile(uuid_); |
| +} |
| + |
| +void BluetoothAdapterProfileChromeOS::OnUnregisterProfileError( |
| + const std::string& error_name, |
| + const std::string& error_message) { |
| + LOG(WARNING) << this->object_path().value() |
| + << ": Failed to unregister profile: " << error_name << ": " |
| + << error_message; |
| + |
| + adapter_->ReleaseProfile(uuid_); |
| +} |
| + |
| +// BluetoothProfileServiceProvider::Delegate: |
| +void BluetoothAdapterProfileChromeOS::Released() { |
| + VLOG(1) << object_path_.value() << ": Release"; |
| +} |
| + |
| +void BluetoothAdapterProfileChromeOS::NewConnection( |
| + const dbus::ObjectPath& device_path, |
| + scoped_ptr<dbus::FileDescriptor> fd, |
| + const BluetoothProfileServiceProvider::Delegate::Options& options, |
| + const ConfirmationCallback& callback) { |
| + dbus::ObjectPath delegate_path = device_path; |
| + |
| + if (delegates_.find(device_path.value()) == delegates_.end()) |
| + delegate_path = dbus::ObjectPath(""); |
| + |
| + if (delegates_.find(delegate_path.value()) == delegates_.end()) { |
| + VLOG(1) << object_path_.value() << ": New connection for device " |
| + << device_path.value() << " which has no delegates!"; |
| + callback.Run(REJECTED); |
| + return; |
| + } |
| + |
| + delegates_[delegate_path.value()]->NewConnection(device_path, fd.Pass(), |
| + options, callback); |
| +} |
| + |
| +void BluetoothAdapterProfileChromeOS::RequestDisconnection( |
| + const dbus::ObjectPath& device_path, |
| + const ConfirmationCallback& callback) { |
| + dbus::ObjectPath delegate_path = device_path; |
| + |
| + if (delegates_.find(device_path.value()) == delegates_.end()) |
| + delegate_path = dbus::ObjectPath(""); |
| + |
| + if (delegates_.find(delegate_path.value()) == delegates_.end()) { |
| + VLOG(1) << object_path_.value() << ": RequestDisconnection for device " |
| + << device_path.value() << " which has no delegates!"; |
| + return; |
| + } |
| + |
| + delegates_[delegate_path.value()]->RequestDisconnection(device_path, |
| + callback); |
| +} |
| + |
| +void BluetoothAdapterProfileChromeOS::Cancel() { |
| + // Cancel() should only go to a delegate accepting connections. |
| + if (delegates_.find("") == delegates_.end()) { |
| + VLOG(1) << object_path_.value() << ": Cancel with no delegate!"; |
| + return; |
| + } |
| + |
| + delegates_[""]->Cancel(); |
| +} |
| + |
| +} // namespace chromeos |