summaryrefslogtreecommitdiffstats
path: root/device/bluetooth/bluetooth_profile_chromeos.h
blob: fd3ad1984ea0e1a997dd8ecd8acbb000a685433d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 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.

#ifndef DEVICE_BLUETOOTH_BLUETOOTH_PROFILE_CHROMEOS_H_
#define DEVICE_BLUETOOTH_BLUETOOTH_PROFILE_CHROMEOS_H_

#include <string>

#include "base/basictypes.h"
#include "base/callback.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "chromeos/chromeos_export.h"
#include "chromeos/dbus/bluetooth_profile_manager_client.h"
#include "chromeos/dbus/bluetooth_profile_service_provider.h"
#include "dbus/object_path.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_profile.h"
#include "device/bluetooth/bluetooth_uuid.h"

namespace dbus {

class FileDescriptor;

}  // namespace dbus

namespace chromeos {

// The BluetoothProfileChromeOS class implements BluetoothProfile for the
// Chrome OS platform.
class CHROMEOS_EXPORT BluetoothProfileChromeOS
    : public device::BluetoothProfile,
      public device::BluetoothAdapter::Observer,
      public BluetoothProfileServiceProvider::Delegate {
 public:
  // BluetoothProfile override.
  virtual void Unregister() OVERRIDE;
  virtual void SetConnectionCallback(
      const ConnectionCallback& callback) OVERRIDE;

  // Return the UUID of the profile.
  const device::BluetoothUUID& uuid() const { return uuid_; }

 private:
  friend class BluetoothProfile;

  BluetoothProfileChromeOS();
  virtual ~BluetoothProfileChromeOS();

  // Called by BluetoothProfile::Register to initialize the profile object
  // asynchronously. |uuid|, |options| and |callback| are the arguments to
  // BluetoothProfile::Register.
  void Init(const device::BluetoothUUID& uuid,
            const device::BluetoothProfile::Options& options,
            const ProfileCallback& callback);

  // BluetoothProfileServiceProvider::Delegate override.
  virtual void Released() OVERRIDE;
  virtual void NewConnection(
      const dbus::ObjectPath& device_path,
      scoped_ptr<dbus::FileDescriptor> fd,
      const BluetoothProfileServiceProvider::Delegate::Options& options,
      const ConfirmationCallback& callback) OVERRIDE;
  virtual void RequestDisconnection(
      const dbus::ObjectPath& device_path,
      const ConfirmationCallback& callback) OVERRIDE;
  virtual void Cancel() OVERRIDE;

  // device::BluetoothAdapter::Observer override.
  virtual void AdapterPresentChanged(device::BluetoothAdapter* adapter,
                                     bool present) OVERRIDE;

  // Called by dbus:: on completion of the D-Bus method call to register the
  // profile object as a result of the adapter becoming present.
  void OnInternalRegisterProfile();
  void OnInternalRegisterProfileError(const std::string& error_name,
                                      const std::string& error_message);

  // Internal method run to get the adapter object during initialization.
  void OnGetAdapter(const ProfileCallback& callback,
                    scoped_refptr<device::BluetoothAdapter> adapter);

  // Called by dbus:: on completion of the D-Bus method call to register the
  // profile object during initialization.
  void OnRegisterProfile(const ProfileCallback& callback);
  void OnRegisterProfileError(const ProfileCallback& callback,
                              const std::string& error_name,
                              const std::string& error_message);

  // Called by dbus:: on completion of the D-Bus method call to unregister
  // the profile object.
  void OnUnregisterProfile();
  void OnUnregisterProfileError(const std::string& error_name,
                                const std::string& error_message);

  // Method run once the file descriptor has been validated in order to get
  // the device object to be passed to the connection callback.
  //
  // The |fd| argument is moved compared to the NewConnection() call since it
  // becomes the result of a PostTaskAndReplyWithResult() call.
  void OnCheckValidity(
      const dbus::ObjectPath& device_path,
      const BluetoothProfileServiceProvider::Delegate::Options& options,
      const ConfirmationCallback& callback,
      scoped_ptr<dbus::FileDescriptor> fd);

  // UUID of the profile passed during initialization.
  device::BluetoothUUID uuid_;

  // Copy of the profile options passed during initialization.
  BluetoothProfileManagerClient::Options options_;

  // Object path of the local profile D-Bus object.
  dbus::ObjectPath object_path_;

  // Local profile D-Bus object used for receiving profile delegate methods
  // from BlueZ.
  scoped_ptr<BluetoothProfileServiceProvider> profile_;

  // Reference to the adapter object, the profile is re-registered when the
  // adapter changes.
  scoped_refptr<device::BluetoothAdapter> adapter_;

  // Callback used on both outgoing and incoming connections to pass the
  // connected socket to profile object owner.
  ConnectionCallback connection_callback_;

  // Note: This should remain the last member so it'll be destroyed and
  // invalidate its weak pointers before any other members are destroyed.
  base::WeakPtrFactory<BluetoothProfileChromeOS> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(BluetoothProfileChromeOS);
};

}  // namespace chromeos

#endif  // DEVICE_BLUETOOTH_BLUETOOTH_PROFILE_CHROMEOS_H_