Home | History | Annotate | Download | only in dbus
      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 CHROMEOS_DBUS_BLUETOOTH_PROFILE_SERVICE_PROVIDER_H_
      6 #define CHROMEOS_DBUS_BLUETOOTH_PROFILE_SERVICE_PROVIDER_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 "chromeos/chromeos_export.h"
     14 #include "dbus/bus.h"
     15 #include "dbus/file_descriptor.h"
     16 #include "dbus/object_path.h"
     17 
     18 namespace chromeos {
     19 
     20 // BluetoothProfileServiceProvider is used to provide a D-Bus object that the
     21 // Bluetooth daemon can communicate with to connect application profiles.
     22 //
     23 // Instantiate with a chosen D-Bus object path and delegate object, and pass
     24 // the D-Bus object path as the |agent_path| argument to the
     25 // chromeos::BluetoothProfileManagerClient::RegisterProfile() method.
     26 //
     27 // When an incoming profile connection occurs, or after initiating a connection
     28 // using the chromeos::BluetoothDeviceClient::ConnectProfile() method, the
     29 // Bluetooth daemon will make calls to this profile object and they will be
     30 // passed on to your Delegate object for handling. Responses should be returned
     31 // using the callbacks supplied to those methods.
     32 class CHROMEOS_EXPORT BluetoothProfileServiceProvider {
     33  public:
     34   // Interface for reacting to profile requests.
     35   class Delegate {
     36    public:
     37     virtual ~Delegate() {}
     38 
     39     // Possible status values that may be returned to callbacks on a new
     40     // connection or a requested disconnection. Success indicates acceptance,
     41     // reject indicates the user rejected or denied the request; cancelled
     42     // means the user cancelled the request without confirming either way.
     43     enum Status {
     44       SUCCESS,
     45       REJECTED,
     46       CANCELLED
     47     };
     48 
     49     // Connection-specific options.
     50     struct CHROMEOS_EXPORT Options {
     51       Options() {}
     52       ~Options() {}
     53 
     54       // Profile version.
     55       uint16 version;
     56 
     57       // Profile features.
     58       uint16 features;
     59     };
     60 
     61     // The ConfirmationCallback is used for methods which require confirmation;
     62     // it should be called with one argument, the |status| of the request
     63     // (success, rejected or cancelled).
     64     typedef base::Callback<void(Status)> ConfirmationCallback;
     65 
     66     // This method will be called when the profile is unregistered from the
     67     // Bluetooth daemon, generally at shutdown or at the applications' request.
     68     // It may be used to perform cleanup tasks.
     69     virtual void Release() = 0;
     70 
     71     // This method will be called when a profile connection to the device
     72     // with object path |device_path| is established. |callback| must be called
     73     // to confirm the connection, or indicate rejection or cancellation.
     74     //
     75     // A file descriptor for the connection socket is provided in |fd|, and
     76     // details about the specific implementation of the profile in |options|.
     77     //
     78     // IMPORTANT: Ownership of the file descriptor object |fd| is passed to
     79     // the delegate by this call. The delegate is responsible for checking the
     80     // validity of |fd| on a thread where I/O is permitted before taking the
     81     // value. If the value is not taken, the file descriptor is closed.
     82     //
     83     // Ownership of |options| is NOT passed so information out of it must be
     84     // copied if required.
     85     virtual void NewConnection(const dbus::ObjectPath& device_path,
     86                                scoped_ptr<dbus::FileDescriptor> fd,
     87                                const Options& options,
     88                                const ConfirmationCallback& callback) = 0;
     89 
     90     // This method will be called when a profile connection to the device
     91     // with object path |device_path| is disconnected. Any file descriptors
     92     // owned by the service should be cleaned up and |callback| called to
     93     // confirm, or indicate rejection or cancellation of the disconnection.
     94     virtual void RequestDisconnection(const dbus::ObjectPath& device_path,
     95                                       const ConfirmationCallback& callback) = 0;
     96 
     97     // This method will be called by the Bluetooth daemon to indicate that
     98     // a profile request failed before a reply was returned from the device.
     99     virtual void Cancel() = 0;
    100   };
    101 
    102   virtual ~BluetoothProfileServiceProvider();
    103 
    104   // Creates the instance where |bus| is the D-Bus bus connection to export
    105   // the object onto, |object_path| is the object path that it should have
    106   // and |delegate| is the object to which all method calls will be passed
    107   // and responses generated from.
    108   static BluetoothProfileServiceProvider* Create(
    109       dbus::Bus* bus,
    110       const dbus::ObjectPath& object_path,
    111       Delegate* delegate);
    112 
    113  protected:
    114   BluetoothProfileServiceProvider();
    115 
    116  private:
    117   DISALLOW_COPY_AND_ASSIGN(BluetoothProfileServiceProvider);
    118 };
    119 
    120 }  // namespace chromeos
    121 
    122 #endif  // CHROMEOS_DBUS_BLUETOOTH_PROFILE_SERVICE_PROVIDER_H_
    123