Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / device / bluetooth / bluetooth_adapter_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_ADAPTER_WIN_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_WIN_H_
7
8 #include <string>
9 #include <utility>
10 #include <vector>
11
12 #include "base/containers/hash_tables.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/threading/thread_checker.h"
17 #include "device/bluetooth/bluetooth_adapter.h"
18 #include "device/bluetooth/bluetooth_task_manager_win.h"
19
20 namespace base {
21 class SequencedTaskRunner;
22 class Thread;
23 }  // namespace base
24
25 namespace device {
26
27 class BluetoothAdapterWinTest;
28 class BluetoothDevice;
29 class BluetoothSocketThread;
30
31 class BluetoothAdapterWin : public BluetoothAdapter,
32                             public BluetoothTaskManagerWin::Observer {
33  public:
34   static base::WeakPtr<BluetoothAdapter> CreateAdapter(
35       const InitCallback& init_callback);
36
37   // BluetoothAdapter:
38   virtual void AddObserver(BluetoothAdapter::Observer* observer) override;
39   virtual void RemoveObserver(BluetoothAdapter::Observer* observer) override;
40   virtual std::string GetAddress() const override;
41   virtual std::string GetName() const override;
42   virtual void SetName(const std::string& name,
43                        const base::Closure& callback,
44                        const ErrorCallback& error_callback) override;
45   virtual bool IsInitialized() const override;
46   virtual bool IsPresent() const override;
47   virtual bool IsPowered() const override;
48   virtual void SetPowered(
49       bool discoverable,
50       const base::Closure& callback,
51       const ErrorCallback& error_callback) override;
52   virtual bool IsDiscoverable() const override;
53   virtual void SetDiscoverable(
54       bool discoverable,
55       const base::Closure& callback,
56       const ErrorCallback& error_callback) override;
57   virtual bool IsDiscovering() const override;
58   virtual void CreateRfcommService(
59       const BluetoothUUID& uuid,
60       const ServiceOptions& options,
61       const CreateServiceCallback& callback,
62       const CreateServiceErrorCallback& error_callback) override;
63   virtual void CreateL2capService(
64       const BluetoothUUID& uuid,
65       const ServiceOptions& options,
66       const CreateServiceCallback& callback,
67       const CreateServiceErrorCallback& error_callback) override;
68
69   // BluetoothTaskManagerWin::Observer override
70   virtual void AdapterStateChanged(
71       const BluetoothTaskManagerWin::AdapterState& state) override;
72   virtual void DiscoveryStarted(bool success) override;
73   virtual void DiscoveryStopped() override;
74   virtual void DevicesPolled(const ScopedVector<
75       BluetoothTaskManagerWin::DeviceState>& devices) override;
76
77   const scoped_refptr<base::SequencedTaskRunner>& ui_task_runner() const {
78     return ui_task_runner_;
79   }
80   const scoped_refptr<BluetoothSocketThread>& socket_thread() const {
81     return socket_thread_;
82   }
83
84  protected:
85   // BluetoothAdapter:
86   virtual void RemovePairingDelegateInternal(
87       device::BluetoothDevice::PairingDelegate* pairing_delegate) override;
88
89  private:
90   friend class BluetoothAdapterWinTest;
91
92   enum DiscoveryStatus {
93     NOT_DISCOVERING,
94     DISCOVERY_STARTING,
95     DISCOVERING,
96     DISCOVERY_STOPPING
97   };
98
99   explicit BluetoothAdapterWin(const InitCallback& init_callback);
100   virtual ~BluetoothAdapterWin();
101
102   // BluetoothAdapter:
103   virtual void AddDiscoverySession(
104       const base::Closure& callback,
105       const ErrorCallback& error_callback) override;
106   virtual void RemoveDiscoverySession(
107       const base::Closure& callback,
108       const ErrorCallback& error_callback) override;
109
110   void Init();
111   void InitForTest(
112       scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
113       scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner);
114
115   void MaybePostStartDiscoveryTask();
116   void MaybePostStopDiscoveryTask();
117
118   InitCallback init_callback_;
119   std::string address_;
120   std::string name_;
121   bool initialized_;
122   bool powered_;
123   DiscoveryStatus discovery_status_;
124   base::hash_set<std::string> discovered_devices_;
125
126   std::vector<std::pair<base::Closure, ErrorCallback> >
127       on_start_discovery_callbacks_;
128   std::vector<base::Closure> on_stop_discovery_callbacks_;
129   size_t num_discovery_listeners_;
130
131   scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
132   scoped_refptr<BluetoothSocketThread> socket_thread_;
133   scoped_refptr<BluetoothTaskManagerWin> task_manager_;
134
135   base::ThreadChecker thread_checker_;
136
137   // List of observers interested in event notifications from us.
138   ObserverList<BluetoothAdapter::Observer> observers_;
139
140   // NOTE: This should remain the last member so it'll be destroyed and
141   // invalidate its weak pointers before any other members are destroyed.
142   base::WeakPtrFactory<BluetoothAdapterWin> weak_ptr_factory_;
143
144   DISALLOW_COPY_AND_ASSIGN(BluetoothAdapterWin);
145 };
146
147 }  // namespace device
148
149 #endif  // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_WIN_H_