Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / device / bluetooth / bluetooth_adapter_mac.h
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_ADAPTER_MAC_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_MAC_H_
7
8 #include <IOKit/IOReturn.h>
9
10 #include <string>
11 #include <vector>
12
13 #include "base/containers/hash_tables.h"
14 #include "base/mac/scoped_nsobject.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list.h"
18 #include "device/bluetooth/bluetooth_adapter.h"
19
20 @class BluetoothAdapterMacDelegate;
21 @class IOBluetoothDevice;
22 @class IOBluetoothDeviceInquiry;
23 @class NSArray;
24 @class NSDate;
25
26 namespace base {
27
28 class SequencedTaskRunner;
29
30 }  // namespace base
31
32 namespace device {
33
34 class BluetoothAdapterMacTest;
35
36 class BluetoothAdapterMac : public BluetoothAdapter {
37  public:
38   static base::WeakPtr<BluetoothAdapter> CreateAdapter();
39
40   // BluetoothAdapter:
41   virtual void AddObserver(BluetoothAdapter::Observer* observer) OVERRIDE;
42   virtual void RemoveObserver(BluetoothAdapter::Observer* observer) OVERRIDE;
43   virtual std::string GetAddress() const OVERRIDE;
44   virtual std::string GetName() const OVERRIDE;
45   virtual void SetName(const std::string& name,
46                        const base::Closure& callback,
47                        const ErrorCallback& error_callback) OVERRIDE;
48   virtual bool IsInitialized() const OVERRIDE;
49   virtual bool IsPresent() const OVERRIDE;
50   virtual bool IsPowered() const OVERRIDE;
51   virtual void SetPowered(
52       bool powered,
53       const base::Closure& callback,
54       const ErrorCallback& error_callback) OVERRIDE;
55   virtual bool IsDiscoverable() const OVERRIDE;
56   virtual void SetDiscoverable(
57       bool discoverable,
58       const base::Closure& callback,
59       const ErrorCallback& error_callback) OVERRIDE;
60   virtual bool IsDiscovering() const OVERRIDE;
61   virtual void ReadLocalOutOfBandPairingData(
62       const BluetoothOutOfBandPairingDataCallback& callback,
63       const ErrorCallback& error_callback) OVERRIDE;
64
65   // called by BluetoothAdapterMacDelegate.
66   void DeviceInquiryStarted(IOBluetoothDeviceInquiry* inquiry);
67   void DeviceFound(IOBluetoothDeviceInquiry* inquiry,
68                    IOBluetoothDevice* device);
69   void DeviceInquiryComplete(IOBluetoothDeviceInquiry* inquiry,
70                              IOReturn error,
71                              bool aborted);
72
73  protected:
74   // BluetoothAdapter:
75   virtual void RemovePairingDelegateInternal(
76       device::BluetoothDevice::PairingDelegate* pairing_delegate) OVERRIDE;
77
78  private:
79   friend class BluetoothAdapterMacTest;
80
81   enum DiscoveryStatus {
82     NOT_DISCOVERING,
83     DISCOVERY_STARTING,
84     DISCOVERING,
85     DISCOVERY_STOPPING
86   };
87
88   BluetoothAdapterMac();
89   virtual ~BluetoothAdapterMac();
90
91   // BluetoothAdapter:
92   virtual void AddDiscoverySession(
93       const base::Closure& callback,
94       const ErrorCallback& error_callback) OVERRIDE;
95   virtual void RemoveDiscoverySession(
96       const base::Closure& callback,
97       const ErrorCallback& error_callback) OVERRIDE;
98
99   void Init();
100   void InitForTest(scoped_refptr<base::SequencedTaskRunner> ui_task_runner);
101   void PollAdapter();
102
103   // Updates |devices_| to be consistent with |devices|.
104   void UpdateDevices(NSArray* devices);
105
106   void MaybeStartDeviceInquiry();
107   void MaybeStopDeviceInquiry();
108
109   typedef std::vector<std::pair<base::Closure, ErrorCallback> >
110       DiscoveryCallbackList;
111   void RunCallbacks(const DiscoveryCallbackList& callback_list,
112                     bool success) const;
113
114   std::string address_;
115   std::string name_;
116   bool powered_;
117   DiscoveryStatus discovery_status_;
118
119   DiscoveryCallbackList on_start_discovery_callbacks_;
120   DiscoveryCallbackList on_stop_discovery_callbacks_;
121   size_t num_discovery_listeners_;
122
123   base::scoped_nsobject<BluetoothAdapterMacDelegate> adapter_delegate_;
124   base::scoped_nsobject<IOBluetoothDeviceInquiry> device_inquiry_;
125
126   // A list of discovered device addresses.
127   // This list is used to check if the same device is discovered twice during
128   // the discovery between consecutive inquiries.
129   base::hash_set<std::string> discovered_devices_;
130
131   // Timestamp for the recently accessed device.
132   // Used to determine if |devices_| needs an update.
133   base::scoped_nsobject<NSDate> recently_accessed_device_timestamp_;
134
135   scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
136
137   // List of observers interested in event notifications from us.
138   ObserverList<BluetoothAdapter::Observer> observers_;
139
140   base::WeakPtrFactory<BluetoothAdapterMac> weak_ptr_factory_;
141
142   DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterMac);
143 };
144
145 }  // namespace device
146
147 #endif  // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_MAC_H_