- add sources.
[platform/framework/web/crosswalk.git] / src / device / bluetooth / bluetooth_task_manager_win.h
1 // Copyright (c) 2012 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_TASK_MANAGER_WIN_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/observer_list.h"
14 #include "base/win/scoped_handle.h"
15 #include "device/bluetooth/bluetooth_adapter.h"
16
17 namespace base {
18
19 class SequencedTaskRunner;
20 class SequencedWorkerPool;
21
22 }  // namespace base
23
24 namespace device {
25
26 // Manages the blocking Bluetooth tasks using |SequencedWorkerPool|. It runs
27 // bluetooth tasks using |SequencedWorkerPool| and informs its observers of
28 // bluetooth adapter state changes and any other bluetooth device inquiry
29 // result.
30 //
31 // It delegates the blocking Windows API calls to |bluetooth_task_runner_|'s
32 // message loop, and receives responses via methods like OnAdapterStateChanged
33 // posted to UI thread.
34 class BluetoothTaskManagerWin
35     : public base::RefCountedThreadSafe<BluetoothTaskManagerWin> {
36  public:
37   struct AdapterState {
38     std::string name;
39     std::string address;
40     bool powered;
41   };
42
43   struct ServiceRecordState {
44     std::string name;
45     std::string address;
46     std::vector<uint8> sdp_bytes;
47   };
48
49   struct DeviceState {
50     std::string name;
51     std::string address;
52     uint32 bluetooth_class;
53     bool visible;
54     bool connected;
55     bool authenticated;
56     ScopedVector<ServiceRecordState> service_record_states;
57   };
58
59   class Observer {
60    public:
61      virtual ~Observer() {}
62
63      virtual void AdapterStateChanged(const AdapterState& state) {}
64      virtual void DiscoveryStarted(bool success) {}
65      virtual void DiscoveryStopped() {}
66      virtual void DevicesUpdated(const ScopedVector<DeviceState>& devices) {}
67      virtual void DevicesDiscovered(const ScopedVector<DeviceState>& devices) {}
68   };
69
70   explicit BluetoothTaskManagerWin(
71       scoped_refptr<base::SequencedTaskRunner> ui_task_runner);
72
73   void AddObserver(Observer* observer);
74   void RemoveObserver(Observer* observer);
75
76   void Initialize();
77   void InitializeWithBluetoothTaskRunner(
78       scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner);
79   void Shutdown();
80
81   void PostSetPoweredBluetoothTask(
82       bool powered,
83       const base::Closure& callback,
84       const BluetoothAdapter::ErrorCallback& error_callback);
85   void PostStartDiscoveryTask();
86   void PostStopDiscoveryTask();
87
88  private:
89   friend class base::RefCountedThreadSafe<BluetoothTaskManagerWin>;
90   friend class BluetoothTaskManagerWinTest;
91
92   static const int kPollIntervalMs;
93
94   virtual ~BluetoothTaskManagerWin();
95
96   // Notify all Observers of updated AdapterState. Should only be called on the
97   // UI thread.
98   void OnAdapterStateChanged(const AdapterState* state);
99   void OnDiscoveryStarted(bool success);
100   void OnDiscoveryStopped();
101   void OnDevicesUpdated(const ScopedVector<DeviceState>* devices);
102   void OnDevicesDiscovered(const ScopedVector<DeviceState>* devices);
103
104   // Called on BluetoothTaskRunner.
105   void StartPolling();
106   void PollAdapter();
107   void PostAdapterStateToUi();
108   void SetPowered(bool powered,
109                   const base::Closure& callback,
110                   const BluetoothAdapter::ErrorCallback& error_callback);
111
112   // Starts discovery. Once the discovery starts, it issues a discovery inquiry
113   // with a short timeout, then issues more inquiries with greater timeout
114   // values. The discovery finishes when StopDiscovery() is called or timeout
115   // has reached its maximum value.
116   void StartDiscovery();
117   void StopDiscovery();
118
119   // Issues a device inquiry that runs for |timeout| * 1.28 seconds.
120   // This posts itself again with |timeout| + 1 until |timeout| reaches the
121   // maximum value or stop discovery call is received.
122   void DiscoverDevices(int timeout);
123
124   // Fetch already known device information. Similar to |StartDiscovery|, except
125   // this function does not issue a discovery inquiry. Instead it gets the
126   // device info cached in the adapter.
127   void GetKnownDevices();
128
129   // Sends a device search API call to the adapter.
130   void SearchDevices(int timeout,
131                      bool search_cached_devices_only,
132                      ScopedVector<DeviceState>* device_list);
133
134   // Discover services for the devices in |device_list|.
135   void DiscoverServices(ScopedVector<DeviceState>* device_list);
136
137   // UI task runner reference.
138   scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
139
140   scoped_refptr<base::SequencedWorkerPool> worker_pool_;
141   scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner_;
142
143   // List of observers interested in event notifications.
144   ObserverList<Observer> observers_;
145
146   // Adapter handle owned by bluetooth task runner.
147   base::win::ScopedHandle adapter_handle_;
148
149   // indicates whether the adapter is in discovery mode or not.
150   bool discovering_;
151
152   DISALLOW_COPY_AND_ASSIGN(BluetoothTaskManagerWin);
153 };
154
155 }  // namespace device
156
157 #endif  // DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_