Chromium Code Reviews| Index: device/bluetooth/bluetooth_profile_experimental_chromeos.cc |
| diff --git a/device/bluetooth/bluetooth_profile_experimental_chromeos.cc b/device/bluetooth/bluetooth_profile_experimental_chromeos.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..84f56c2ea232f0235314e90c014d0133481aa8b9 |
| --- /dev/null |
| +++ b/device/bluetooth/bluetooth_profile_experimental_chromeos.cc |
| @@ -0,0 +1,257 @@ |
| +// Copyright (c) 2013 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_profile_experimental_chromeos.h" |
| + |
| +#include <string> |
| + |
| +#include "base/basictypes.h" |
| +#include "base/bind.h" |
| +#include "base/callback.h" |
| +#include "base/logging.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/message_loop.h" |
| +#include "base/string_util.h" |
| +#include "base/threading/thread_restrictions.h" |
| +#include "base/threading/worker_pool.h" |
| +#include "chromeos/dbus/dbus_thread_manager.h" |
| +#include "chromeos/dbus/experimental_bluetooth_profile_manager_client.h" |
| +#include "chromeos/dbus/experimental_bluetooth_profile_service_provider.h" |
| +#include "dbus/bus.h" |
| +#include "dbus/file_descriptor.h" |
| +#include "dbus/object_path.h" |
| +#include "device/bluetooth/bluetooth_adapter_experimental_chromeos.h" |
| +#include "device/bluetooth/bluetooth_adapter_factory.h" |
| +#include "device/bluetooth/bluetooth_device.h" |
| +#include "device/bluetooth/bluetooth_device_experimental_chromeos.h" |
| +#include "device/bluetooth/bluetooth_profile.h" |
| +#include "device/bluetooth/bluetooth_socket.h" |
| +#include "device/bluetooth/bluetooth_socket_experimental_chromeos.h" |
| + |
| +using device::BluetoothAdapter; |
| +using device::BluetoothAdapterFactory; |
| +using device::BluetoothDevice; |
| +using device::BluetoothProfile; |
| +using device::BluetoothSocket; |
| + |
| +namespace chromeos { |
| + |
| +BluetoothProfileExperimentalChromeOS::BluetoothProfileExperimentalChromeOS() |
| + : weak_ptr_factory_(this) { |
| +} |
| + |
| +BluetoothProfileExperimentalChromeOS::~BluetoothProfileExperimentalChromeOS() { |
| + DCHECK(object_path_.value().empty()); |
| + DCHECK(profile_.get() == NULL); |
| +} |
| + |
| +void BluetoothProfileExperimentalChromeOS::Init( |
| + const std::string& uuid, |
| + const device::BluetoothProfile::Options& options, |
| + const ProfileCallback& callback) { |
| + DCHECK(object_path_.value().empty()); |
| + DCHECK(profile_.get() == NULL); |
| + |
| + if (!BluetoothDevice::IsUUIDValid(uuid)) { |
| + callback.Run(NULL); |
| + return; |
| + } |
| + |
| + uuid_ = uuid; |
| + |
| + ExperimentalBluetoothProfileManagerClient::Options bluetooth_options; |
| + bluetooth_options.name = options.name; |
| + bluetooth_options.service = uuid; |
| + bluetooth_options.channel = options.channel; |
| + bluetooth_options.psm = options.psm; |
| + bluetooth_options.require_authentication = options.require_authentication; |
| + bluetooth_options.require_authorization = options.require_authorization; |
| + bluetooth_options.auto_connect = options.auto_connect; |
| + bluetooth_options.version = options.version; |
| + bluetooth_options.features = options.features; |
| + |
| + // The object path is relatively meaningless, but has to be unique, so we |
| + // use the UUID of the profile. |
| + std::string uuid_path; |
| + ReplaceChars(uuid, ":-", "_", &uuid_path); |
| + |
| + object_path_ = dbus::ObjectPath("/org/chromium/bluetooth_profile/" + |
| + uuid_path); |
| + |
| + dbus::Bus* system_bus = DBusThreadManager::Get()->GetSystemBus(); |
| + profile_.reset(ExperimentalBluetoothProfileServiceProvider::Create( |
| + system_bus, object_path_, this)); |
| + DCHECK(profile_.get()); |
| + |
| + VLOG(1) << object_path_.value() << ": Register profile"; |
| + DBusThreadManager::Get()->GetExperimentalBluetoothProfileManagerClient()-> |
| + RegisterProfile( |
| + object_path_, |
| + uuid, |
| + bluetooth_options, |
| + base::Bind( |
| + &BluetoothProfileExperimentalChromeOS::OnRegisterProfile, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + callback), |
| + base::Bind( |
| + &BluetoothProfileExperimentalChromeOS::OnRegisterProfileError, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + callback)); |
| +} |
| + |
| +void BluetoothProfileExperimentalChromeOS::Unregister() { |
| + DCHECK(!object_path_.value().empty()); |
| + DCHECK(profile_.get()); |
| + |
| + profile_.reset(); |
| + |
| + VLOG(1) << object_path_.value() << ": Unregister profile"; |
| + DBusThreadManager::Get()->GetExperimentalBluetoothProfileManagerClient()-> |
| + UnregisterProfile( |
| + object_path_, |
| + base::Bind( |
| + &BluetoothProfileExperimentalChromeOS::OnUnregisterProfile, |
| + weak_ptr_factory_.GetWeakPtr()), |
| + base::Bind( |
| + &BluetoothProfileExperimentalChromeOS::OnUnregisterProfileError, |
| + weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +void BluetoothProfileExperimentalChromeOS::SetConnectionCallback( |
| + const ConnectionCallback& callback) { |
| + connection_callback_ = callback; |
| +} |
| + |
| +void BluetoothProfileExperimentalChromeOS::Release() { |
| + VLOG(1) << object_path_.value() << ": Release"; |
| +} |
| + |
| +void BluetoothProfileExperimentalChromeOS::NewConnection( |
| + const dbus::ObjectPath& device_path, |
| + dbus::FileDescriptor* fd, |
| + const ExperimentalBluetoothProfileServiceProvider::Delegate::Options& |
| + options, |
| + const ConfirmationCallback& callback) { |
| + VLOG(1) << object_path_.value() << ": New connection from device: " |
| + << device_path.value();; |
| + if (connection_callback_.is_null()) { |
| + callback.Run(REJECTED); |
| + delete fd; |
| + return; |
| + } |
| + |
| + // Punt descriptor validity check to a worker thread where i/o is permitted; |
| + // on return we'll fetch the adapter and then call the connection callback. |
| + // |
| + // base::Owned is used to take ownership of the file descriptor so it would |
| + // be freed after completion of the bottom half, or if the weak ptr is freed |
| + // in the meantime. |
| + base::WorkerPool::PostTaskAndReply( |
| + FROM_HERE, |
| + base::Bind(&BluetoothProfileExperimentalChromeOS::CheckValidity, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + fd), |
| + base::Bind(&BluetoothProfileExperimentalChromeOS::OnCheckValidity, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + device_path, |
| + base::Owned(fd), |
|
satorux1
2013/05/02 01:33:01
Instead of base::Owned(), you can probably make fd
keybuk
2013/05/02 20:17:14
Awesome! This was exactly the kind of advice I was
|
| + options, |
| + callback), |
| + false); |
| +} |
| + |
| +void BluetoothProfileExperimentalChromeOS::RequestDisconnection( |
| + const dbus::ObjectPath& device_path, |
| + const ConfirmationCallback& callback) { |
| + VLOG(1) << object_path_.value() << ": Request disconnection"; |
| + callback.Run(SUCCESS); |
| +} |
| + |
| +void BluetoothProfileExperimentalChromeOS::Cancel() { |
| + VLOG(1) << object_path_.value() << ": Cancel"; |
| +} |
| + |
| +void BluetoothProfileExperimentalChromeOS::OnRegisterProfile( |
| + const ProfileCallback& callback) { |
| + VLOG(1) << object_path_.value() << ": Profile registered"; |
| + callback.Run(this); |
| +} |
| + |
| +void BluetoothProfileExperimentalChromeOS::OnRegisterProfileError( |
| + const ProfileCallback& callback, |
| + const std::string& error_name, |
| + const std::string& error_message) { |
| + LOG(WARNING) << object_path_.value() << ": Failed to register profile: " |
| + << error_name << ": " << error_message; |
| + callback.Run(NULL); |
| + |
| + Unregister(); |
| +} |
| + |
| +void BluetoothProfileExperimentalChromeOS::OnUnregisterProfile() { |
| + VLOG(1) << object_path_.value() << ": Profile unregistered"; |
| + object_path_ = dbus::ObjectPath(""); |
| + delete this; |
| +} |
| + |
| +void BluetoothProfileExperimentalChromeOS::OnUnregisterProfileError( |
| + const std::string& error_name, |
| + const std::string& error_message) { |
| + LOG(WARNING) << object_path_.value() << ": Failed to unregister profile: " |
| + << error_name << ": " << error_message; |
| + object_path_ = dbus::ObjectPath(""); |
| + delete this; |
| +} |
| + |
| +void BluetoothProfileExperimentalChromeOS::CheckValidity( |
| + dbus::FileDescriptor* fd) { |
| + base::ThreadRestrictions::AssertIOAllowed(); |
| + fd->CheckValidity(); |
| +} |
|
satorux1
2013/05/02 01:33:01
you might want to make this a free function in ano
keybuk
2013/05/02 20:17:14
Done.
|
| + |
| +void BluetoothProfileExperimentalChromeOS::OnCheckValidity( |
| + const dbus::ObjectPath& device_path, |
| + dbus::FileDescriptor* fd, |
| + const ExperimentalBluetoothProfileServiceProvider::Delegate::Options& |
| + options, |
| + const ConfirmationCallback& callback) { |
| + VLOG(1) << object_path_.value() << ": Validity check complete"; |
| + if (!fd->is_valid()) { |
| + callback.Run(REJECTED); |
| + return; |
| + } |
| + |
| + // Obtain the adapter instance; despite the callback call style, this is |
| + // actually synchronous on Chrome OS - so we know the fd object isn't about |
| + // to get thrown away. |
| + BluetoothAdapterFactory::GetAdapter( |
| + base::Bind(&BluetoothProfileExperimentalChromeOS::OnGetAdapter, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + device_path, |
| + fd, |
| + options, |
| + callback)); |
| +} |
| + |
| +void BluetoothProfileExperimentalChromeOS::OnGetAdapter( |
| + const dbus::ObjectPath& device_path, |
| + dbus::FileDescriptor* fd, |
| + const ExperimentalBluetoothProfileServiceProvider::Delegate::Options& |
| + options, |
| + const ConfirmationCallback& callback, |
| + scoped_refptr<BluetoothAdapter> adapter) { |
| + VLOG(1) << object_path_.value() << ": Obtained adapter reference"; |
| + callback.Run(SUCCESS); |
| + |
| + BluetoothDeviceExperimentalChromeOS* device = |
| + static_cast<BluetoothAdapterExperimentalChromeOS*>(adapter.get())-> |
| + GetDeviceWithPath(device_path); |
| + DCHECK(device); |
| + |
| + scoped_refptr<BluetoothSocket> socket(( |
| + BluetoothSocketExperimentalChromeOS::Create(fd))); |
| + connection_callback_.Run(device, socket); |
| +} |
| + |
| +} // namespace chromeos |