Home | History | Annotate | Download | only in bluetooth
      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.
      4 
      5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_PROFILE_CHROMEOS_H_
      6 #define DEVICE_BLUETOOTH_BLUETOOTH_PROFILE_CHROMEOS_H_
      7 
      8 #include <string>
      9 
     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 "chromeos/chromeos_export.h"
     15 #include "chromeos/dbus/bluetooth_profile_service_provider.h"
     16 #include "dbus/object_path.h"
     17 #include "device/bluetooth/bluetooth_profile.h"
     18 
     19 namespace dbus {
     20 
     21 class FileDescriptor;
     22 
     23 }  // namespace dbus
     24 
     25 namespace device {
     26 
     27 class BluetoothAdapter;
     28 
     29 }  // namespace device
     30 
     31 namespace chromeos {
     32 
     33 // The BluetoothProfileChromeOS class implements BluetoothProfile for the
     34 // Chrome OS platform.
     35 class CHROMEOS_EXPORT BluetoothProfileChromeOS
     36     : public device::BluetoothProfile,
     37       private BluetoothProfileServiceProvider::Delegate {
     38  public:
     39   // BluetoothProfile override.
     40   virtual void Unregister() OVERRIDE;
     41   virtual void SetConnectionCallback(
     42       const ConnectionCallback& callback) OVERRIDE;
     43 
     44   // Return the UUID of the profile.
     45   const std::string& uuid() const { return uuid_; }
     46 
     47  private:
     48   friend class BluetoothProfile;
     49 
     50   BluetoothProfileChromeOS();
     51   virtual ~BluetoothProfileChromeOS();
     52 
     53   // Called by BluetoothProfile::Register to initialize the profile object
     54   // asynchronously. |uuid|, |options| and |callback| are the arguments to
     55   // BluetoothProfile::Register.
     56   void Init(const std::string& uuid,
     57             const device::BluetoothProfile::Options& options,
     58             const ProfileCallback& callback);
     59 
     60   // BluetoothProfileServiceProvider::Delegate override.
     61   virtual void Release() OVERRIDE;
     62   virtual void NewConnection(
     63       const dbus::ObjectPath& device_path,
     64       scoped_ptr<dbus::FileDescriptor> fd,
     65       const BluetoothProfileServiceProvider::Delegate::Options& options,
     66       const ConfirmationCallback& callback) OVERRIDE;
     67   virtual void RequestDisconnection(
     68       const dbus::ObjectPath& device_path,
     69       const ConfirmationCallback& callback) OVERRIDE;
     70   virtual void Cancel() OVERRIDE;
     71 
     72   // Called by dbus:: on completion of the D-Bus method call to register the
     73   // profile object.
     74   void OnRegisterProfile(const ProfileCallback& callback);
     75   void OnRegisterProfileError(const ProfileCallback& callback,
     76                               const std::string& error_name,
     77                               const std::string& error_message);
     78 
     79   // Called by dbus:: on completion of the D-Bus method call to unregister
     80   // the profile object.
     81   void OnUnregisterProfile();
     82   void OnUnregisterProfileError(const std::string& error_name,
     83                                 const std::string& error_message);
     84 
     85   // Method run once the file descriptor has been validated in order to get
     86   // the default adapter, and method run once the default adapter has been
     87   // obtained in order to get the device object to be passed to the connection
     88   // callback.
     89   //
     90   // The |fd| argument is moved compared to the NewConnection() call since it
     91   // becomes the result of a PostTaskAndReplyWithResult() call.
     92   void GetAdapter(
     93       const dbus::ObjectPath& device_path,
     94       const BluetoothProfileServiceProvider::Delegate::Options& options,
     95       const ConfirmationCallback& callback,
     96       scoped_ptr<dbus::FileDescriptor> fd);
     97   void OnGetAdapter(
     98       const dbus::ObjectPath& device_path,
     99       const BluetoothProfileServiceProvider::Delegate::Options& options,
    100       const ConfirmationCallback& callback,
    101       scoped_ptr<dbus::FileDescriptor> fd,
    102       scoped_refptr<device::BluetoothAdapter>);
    103 
    104   // UUID of the profile passed during initialization.
    105   std::string uuid_;
    106 
    107   // Object path of the local profile D-Bus object.
    108   dbus::ObjectPath object_path_;
    109 
    110   // Local profile D-Bus object used for receiving profile delegate methods
    111   // from BlueZ.
    112   scoped_ptr<BluetoothProfileServiceProvider> profile_;
    113 
    114   // Callback used on both outgoing and incoming connections to pass the
    115   // connected socket to profile object owner.
    116   ConnectionCallback connection_callback_;
    117 
    118   // Note: This should remain the last member so it'll be destroyed and
    119   // invalidate its weak pointers before any other members are destroyed.
    120   base::WeakPtrFactory<BluetoothProfileChromeOS> weak_ptr_factory_;
    121 
    122   DISALLOW_COPY_AND_ASSIGN(BluetoothProfileChromeOS);
    123 };
    124 
    125 }  // namespace chromeos
    126 
    127 #endif  // DEVICE_BLUETOOTH_BLUETOOTH_PROFILE_CHROMEOS_H_
    128