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 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_PROFILE_CHROMEOS_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_PROFILE_CHROMEOS_H_
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/sequenced_task_runner.h"
15 #include "chromeos/chromeos_export.h"
16 #include "chromeos/dbus/bluetooth_profile_manager_client.h"
17 #include "chromeos/dbus/bluetooth_profile_service_provider.h"
18 #include "dbus/object_path.h"
19 #include "device/bluetooth/bluetooth_adapter.h"
20 #include "device/bluetooth/bluetooth_profile.h"
21 #include "device/bluetooth/bluetooth_uuid.h"
30 class BluetoothSocketThread;
35 class BluetoothSocketChromeOS;
37 // The BluetoothProfileChromeOS class implements BluetoothProfile for the
38 // Chrome OS platform.
39 class CHROMEOS_EXPORT BluetoothProfileChromeOS
40 : public device::BluetoothProfile,
41 public device::BluetoothAdapter::Observer,
42 public BluetoothProfileServiceProvider::Delegate {
44 // BluetoothProfile override.
45 virtual void Unregister() OVERRIDE;
46 virtual void SetConnectionCallback(
47 const ConnectionCallback& callback) OVERRIDE;
49 // Return the UUID of the profile.
50 const device::BluetoothUUID& uuid() const { return uuid_; }
53 friend class BluetoothProfile;
55 BluetoothProfileChromeOS(
56 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
57 scoped_refptr<device::BluetoothSocketThread> socket_thread);
58 virtual ~BluetoothProfileChromeOS();
60 // Called by BluetoothProfile::Register to initialize the profile object
61 // asynchronously. |uuid|, |options| and |callback| are the arguments to
62 // BluetoothProfile::Register.
63 void Init(const device::BluetoothUUID& uuid,
64 const device::BluetoothProfile::Options& options,
65 const ProfileCallback& callback);
67 // BluetoothProfileServiceProvider::Delegate override.
68 virtual void Released() OVERRIDE;
69 virtual void NewConnection(
70 const dbus::ObjectPath& device_path,
71 scoped_ptr<dbus::FileDescriptor> fd,
72 const BluetoothProfileServiceProvider::Delegate::Options& options,
73 const ConfirmationCallback& callback) OVERRIDE;
74 virtual void RequestDisconnection(
75 const dbus::ObjectPath& device_path,
76 const ConfirmationCallback& callback) OVERRIDE;
77 virtual void Cancel() OVERRIDE;
79 // device::BluetoothAdapter::Observer override.
80 virtual void AdapterPresentChanged(device::BluetoothAdapter* adapter,
81 bool present) OVERRIDE;
83 // Called by dbus:: on completion of the D-Bus method call to register the
84 // profile object as a result of the adapter becoming present.
85 void OnInternalRegisterProfile();
86 void OnInternalRegisterProfileError(const std::string& error_name,
87 const std::string& error_message);
89 // Internal method run to get the adapter object during initialization.
90 void OnGetAdapter(const ProfileCallback& callback,
91 scoped_refptr<device::BluetoothAdapter> adapter);
93 // Called by dbus:: on completion of the D-Bus method call to register the
94 // profile object during initialization.
95 void OnRegisterProfile(const ProfileCallback& callback);
96 void OnRegisterProfileError(const ProfileCallback& callback,
97 const std::string& error_name,
98 const std::string& error_message);
100 // Called by dbus:: on completion of the D-Bus method call to unregister
101 // the profile object.
102 void OnUnregisterProfile();
103 void OnUnregisterProfileError(const std::string& error_name,
104 const std::string& error_message);
106 // Method run once the file descriptor has been validated in order to get
107 // the device object to be passed to the connection callback.
109 // The |fd| argument is moved compared to the NewConnection() call since it
110 // becomes the result of a PostTaskAndReplyWithResult() call.
111 void OnCheckValidity(
112 const dbus::ObjectPath& device_path,
113 const BluetoothProfileServiceProvider::Delegate::Options& options,
114 const ConfirmationCallback& callback,
115 scoped_ptr<dbus::FileDescriptor> fd);
117 // Methods run after the socket has been connected to a
118 // BluetoothSocketChromeOS instance on the I/O thread, these methods complete
119 // the incoming connection calling both the Bluetooth daemon callback
120 // |callback| to indicate success or failure and calling this object's
121 // new connection callback method.
122 void OnConnect(const dbus::ObjectPath& device_path,
123 scoped_refptr<BluetoothSocketChromeOS> socket,
124 const ConfirmationCallback& callback);
125 void OnConnectError(const ConfirmationCallback& callback,
126 const std::string& error_message);
128 // UUID of the profile passed during initialization.
129 device::BluetoothUUID uuid_;
131 // Copy of the profile options passed during initialization.
132 BluetoothProfileManagerClient::Options options_;
134 // Object path of the local profile D-Bus object.
135 dbus::ObjectPath object_path_;
137 // Local profile D-Bus object used for receiving profile delegate methods
139 scoped_ptr<BluetoothProfileServiceProvider> profile_;
141 // Reference to the adapter object, the profile is re-registered when the
143 scoped_refptr<device::BluetoothAdapter> adapter_;
145 // Callback used on both outgoing and incoming connections to pass the
146 // connected socket to profile object owner.
147 ConnectionCallback connection_callback_;
149 // UI thread task runner and socket thread object used to create sockets.
150 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
151 scoped_refptr<device::BluetoothSocketThread> socket_thread_;
153 // Note: This should remain the last member so it'll be destroyed and
154 // invalidate its weak pointers before any other members are destroyed.
155 base::WeakPtrFactory<BluetoothProfileChromeOS> weak_ptr_factory_;
157 DISALLOW_COPY_AND_ASSIGN(BluetoothProfileChromeOS);
160 } // namespace chromeos
162 #endif // DEVICE_BLUETOOTH_BLUETOOTH_PROFILE_CHROMEOS_H_