Files.app: Provide detailed change information on onDirectoryChanged event
[chromium-blink-merge.git] / device / bluetooth / bluetooth_adapter_win.cc
blob8ef54705f188deb3f65ab7c914741178f6ebd648
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.
5 #include "device/bluetooth/bluetooth_adapter_win.h"
7 #include <hash_set>
8 #include <string>
9 #include <utility>
11 #include "base/logging.h"
12 #include "base/sequenced_task_runner.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/stl_util.h"
15 #include "base/thread_task_runner_handle.h"
16 #include "device/bluetooth/bluetooth_device_win.h"
17 #include "device/bluetooth/bluetooth_socket_thread.h"
18 #include "device/bluetooth/bluetooth_socket_win.h"
19 #include "device/bluetooth/bluetooth_task_manager_win.h"
20 #include "device/bluetooth/bluetooth_uuid.h"
22 namespace device {
24 // static
25 base::WeakPtr<BluetoothAdapter> BluetoothAdapter::CreateAdapter(
26 const InitCallback& init_callback) {
27 return BluetoothAdapterWin::CreateAdapter(init_callback);
30 // static
31 base::WeakPtr<BluetoothAdapter> BluetoothAdapterWin::CreateAdapter(
32 const InitCallback& init_callback) {
33 BluetoothAdapterWin* adapter = new BluetoothAdapterWin(init_callback);
34 adapter->Init();
35 return adapter->weak_ptr_factory_.GetWeakPtr();
38 BluetoothAdapterWin::BluetoothAdapterWin(const InitCallback& init_callback)
39 : BluetoothAdapter(),
40 init_callback_(init_callback),
41 initialized_(false),
42 powered_(false),
43 discovery_status_(NOT_DISCOVERING),
44 num_discovery_listeners_(0),
45 weak_ptr_factory_(this) {
48 BluetoothAdapterWin::~BluetoothAdapterWin() {
49 if (task_manager_) {
50 task_manager_->RemoveObserver(this);
51 task_manager_->Shutdown();
55 void BluetoothAdapterWin::AddObserver(BluetoothAdapter::Observer* observer) {
56 DCHECK(observer);
57 observers_.AddObserver(observer);
60 void BluetoothAdapterWin::RemoveObserver(BluetoothAdapter::Observer* observer) {
61 DCHECK(observer);
62 observers_.RemoveObserver(observer);
65 std::string BluetoothAdapterWin::GetAddress() const {
66 return address_;
69 std::string BluetoothAdapterWin::GetName() const {
70 return name_;
73 void BluetoothAdapterWin::SetName(const std::string& name,
74 const base::Closure& callback,
75 const ErrorCallback& error_callback) {
76 NOTIMPLEMENTED();
79 // TODO(youngki): Return true when |task_manager_| initializes the adapter
80 // state.
81 bool BluetoothAdapterWin::IsInitialized() const {
82 return initialized_;
85 bool BluetoothAdapterWin::IsPresent() const {
86 return !address_.empty();
89 bool BluetoothAdapterWin::IsPowered() const {
90 return powered_;
93 void BluetoothAdapterWin::SetPowered(
94 bool powered,
95 const base::Closure& callback,
96 const ErrorCallback& error_callback) {
97 task_manager_->PostSetPoweredBluetoothTask(powered, callback, error_callback);
100 bool BluetoothAdapterWin::IsDiscoverable() const {
101 NOTIMPLEMENTED();
102 return false;
105 void BluetoothAdapterWin::SetDiscoverable(
106 bool discoverable,
107 const base::Closure& callback,
108 const ErrorCallback& error_callback) {
109 NOTIMPLEMENTED();
112 bool BluetoothAdapterWin::IsDiscovering() const {
113 return discovery_status_ == DISCOVERING ||
114 discovery_status_ == DISCOVERY_STOPPING;
117 void BluetoothAdapterWin::DiscoveryStarted(bool success) {
118 discovery_status_ = success ? DISCOVERING : NOT_DISCOVERING;
119 for (std::vector<std::pair<base::Closure, ErrorCallback> >::const_iterator
120 iter = on_start_discovery_callbacks_.begin();
121 iter != on_start_discovery_callbacks_.end();
122 ++iter) {
123 if (success)
124 ui_task_runner_->PostTask(FROM_HERE, iter->first);
125 else
126 ui_task_runner_->PostTask(FROM_HERE, iter->second);
128 num_discovery_listeners_ = on_start_discovery_callbacks_.size();
129 on_start_discovery_callbacks_.clear();
131 if (success) {
132 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
133 AdapterDiscoveringChanged(this, true));
135 // If there are stop discovery requests, post the stop discovery again.
136 MaybePostStopDiscoveryTask();
137 } else if (!on_stop_discovery_callbacks_.empty()) {
138 // If there are stop discovery requests but start discovery has failed,
139 // notify that stop discovery has been complete.
140 DiscoveryStopped();
144 void BluetoothAdapterWin::DiscoveryStopped() {
145 discovered_devices_.clear();
146 bool was_discovering = IsDiscovering();
147 discovery_status_ = NOT_DISCOVERING;
148 for (std::vector<base::Closure>::const_iterator iter =
149 on_stop_discovery_callbacks_.begin();
150 iter != on_stop_discovery_callbacks_.end();
151 ++iter) {
152 ui_task_runner_->PostTask(FROM_HERE, *iter);
154 num_discovery_listeners_ = 0;
155 on_stop_discovery_callbacks_.clear();
156 if (was_discovering)
157 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
158 AdapterDiscoveringChanged(this, false));
160 // If there are start discovery requests, post the start discovery again.
161 MaybePostStartDiscoveryTask();
164 void BluetoothAdapterWin::CreateRfcommService(
165 const BluetoothUUID& uuid,
166 int channel,
167 const CreateServiceCallback& callback,
168 const CreateServiceErrorCallback& error_callback) {
169 scoped_refptr<BluetoothSocketWin> socket =
170 BluetoothSocketWin::CreateBluetoothSocket(
171 ui_task_runner_,
172 socket_thread_,
173 NULL,
174 net::NetLog::Source());
175 socket->Listen(this, uuid, channel,
176 base::Bind(callback, socket),
177 error_callback);
180 void BluetoothAdapterWin::CreateL2capService(
181 const BluetoothUUID& uuid,
182 int psm,
183 const CreateServiceCallback& callback,
184 const CreateServiceErrorCallback& error_callback) {
185 // TODO(keybuk): implement.
186 NOTIMPLEMENTED();
189 void BluetoothAdapterWin::RemovePairingDelegateInternal(
190 BluetoothDevice::PairingDelegate* pairing_delegate) {
193 void BluetoothAdapterWin::AdapterStateChanged(
194 const BluetoothTaskManagerWin::AdapterState& state) {
195 DCHECK(thread_checker_.CalledOnValidThread());
196 name_ = state.name;
197 bool was_present = IsPresent();
198 bool is_present = !state.address.empty();
199 address_ = BluetoothDevice::CanonicalizeAddress(state.address);
200 if (was_present != is_present) {
201 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
202 AdapterPresentChanged(this, is_present));
204 if (powered_ != state.powered) {
205 powered_ = state.powered;
206 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
207 AdapterPoweredChanged(this, powered_));
209 if (!initialized_) {
210 initialized_ = true;
211 init_callback_.Run();
215 void BluetoothAdapterWin::DevicesDiscovered(
216 const ScopedVector<BluetoothTaskManagerWin::DeviceState>& devices) {
217 DCHECK(thread_checker_.CalledOnValidThread());
218 for (ScopedVector<BluetoothTaskManagerWin::DeviceState>::const_iterator iter =
219 devices.begin();
220 iter != devices.end();
221 ++iter) {
222 if (discovered_devices_.find((*iter)->address) ==
223 discovered_devices_.end()) {
224 BluetoothDeviceWin device_win(
225 **iter, ui_task_runner_, socket_thread_, NULL, net::NetLog::Source());
226 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
227 DeviceAdded(this, &device_win));
228 discovered_devices_.insert((*iter)->address);
233 void BluetoothAdapterWin::DevicesUpdated(
234 const ScopedVector<BluetoothTaskManagerWin::DeviceState>& devices) {
235 STLDeleteValues(&devices_);
236 for (ScopedVector<BluetoothTaskManagerWin::DeviceState>::const_iterator iter =
237 devices.begin();
238 iter != devices.end();
239 ++iter) {
240 devices_[(*iter)->address] = new BluetoothDeviceWin(
241 **iter, ui_task_runner_, socket_thread_, NULL, net::NetLog::Source());
245 // If the method is called when |discovery_status_| is DISCOVERY_STOPPING,
246 // starting again is handled by BluetoothAdapterWin::DiscoveryStopped().
247 void BluetoothAdapterWin::AddDiscoverySession(
248 const base::Closure& callback,
249 const ErrorCallback& error_callback) {
250 if (discovery_status_ == DISCOVERING) {
251 num_discovery_listeners_++;
252 callback.Run();
253 return;
255 on_start_discovery_callbacks_.push_back(
256 std::make_pair(callback, error_callback));
257 MaybePostStartDiscoveryTask();
260 void BluetoothAdapterWin::RemoveDiscoverySession(
261 const base::Closure& callback,
262 const ErrorCallback& error_callback) {
263 if (discovery_status_ == NOT_DISCOVERING) {
264 error_callback.Run();
265 return;
267 on_stop_discovery_callbacks_.push_back(callback);
268 MaybePostStopDiscoveryTask();
271 void BluetoothAdapterWin::Init() {
272 ui_task_runner_ = base::ThreadTaskRunnerHandle::Get();
273 socket_thread_ = BluetoothSocketThread::Get();
274 task_manager_ =
275 new BluetoothTaskManagerWin(ui_task_runner_);
276 task_manager_->AddObserver(this);
277 task_manager_->Initialize();
280 void BluetoothAdapterWin::InitForTest(
281 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
282 scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner) {
283 ui_task_runner_ = ui_task_runner;
284 task_manager_ =
285 new BluetoothTaskManagerWin(ui_task_runner_);
286 task_manager_->AddObserver(this);
287 task_manager_->InitializeWithBluetoothTaskRunner(bluetooth_task_runner);
290 void BluetoothAdapterWin::MaybePostStartDiscoveryTask() {
291 if (discovery_status_ == NOT_DISCOVERING &&
292 !on_start_discovery_callbacks_.empty()) {
293 discovery_status_ = DISCOVERY_STARTING;
294 task_manager_->PostStartDiscoveryTask();
298 void BluetoothAdapterWin::MaybePostStopDiscoveryTask() {
299 if (discovery_status_ != DISCOVERING)
300 return;
302 if (on_stop_discovery_callbacks_.size() < num_discovery_listeners_) {
303 for (std::vector<base::Closure>::const_iterator iter =
304 on_stop_discovery_callbacks_.begin();
305 iter != on_stop_discovery_callbacks_.end();
306 ++iter) {
307 ui_task_runner_->PostTask(FROM_HERE, *iter);
309 num_discovery_listeners_ -= on_stop_discovery_callbacks_.size();
310 on_stop_discovery_callbacks_.clear();
311 return;
314 discovery_status_ = DISCOVERY_STOPPING;
315 task_manager_->PostStopDiscoveryTask();
318 } // namespace device