Revert 273817 "Record RenderViewContextMenu.Used histogram befor..."
[chromium-blink-merge.git] / device / bluetooth / bluetooth_adapter_win.cc
blob6bbdb0573b9d1988ae4b86c862170dfd14823711
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 bool insecure,
168 const CreateServiceCallback& callback,
169 const CreateServiceErrorCallback& error_callback) {
170 // TODO(keybuk): implement.
171 NOTIMPLEMENTED();
174 void BluetoothAdapterWin::CreateL2capService(
175 const BluetoothUUID& uuid,
176 int psm,
177 const CreateServiceCallback& callback,
178 const CreateServiceErrorCallback& error_callback) {
179 // TODO(keybuk): implement.
180 NOTIMPLEMENTED();
183 void BluetoothAdapterWin::RemovePairingDelegateInternal(
184 BluetoothDevice::PairingDelegate* pairing_delegate) {
187 void BluetoothAdapterWin::AdapterStateChanged(
188 const BluetoothTaskManagerWin::AdapterState& state) {
189 DCHECK(thread_checker_.CalledOnValidThread());
190 name_ = state.name;
191 bool was_present = IsPresent();
192 bool is_present = !state.address.empty();
193 address_ = BluetoothDevice::CanonicalizeAddress(state.address);
194 if (was_present != is_present) {
195 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
196 AdapterPresentChanged(this, is_present));
198 if (powered_ != state.powered) {
199 powered_ = state.powered;
200 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
201 AdapterPoweredChanged(this, powered_));
203 if (!initialized_) {
204 initialized_ = true;
205 init_callback_.Run();
209 void BluetoothAdapterWin::DevicesDiscovered(
210 const ScopedVector<BluetoothTaskManagerWin::DeviceState>& devices) {
211 DCHECK(thread_checker_.CalledOnValidThread());
212 for (ScopedVector<BluetoothTaskManagerWin::DeviceState>::const_iterator iter =
213 devices.begin();
214 iter != devices.end();
215 ++iter) {
216 if (discovered_devices_.find((*iter)->address) ==
217 discovered_devices_.end()) {
218 BluetoothDeviceWin device_win(
219 **iter, ui_task_runner_, socket_thread_, NULL, net::NetLog::Source());
220 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
221 DeviceAdded(this, &device_win));
222 discovered_devices_.insert((*iter)->address);
227 void BluetoothAdapterWin::DevicesUpdated(
228 const ScopedVector<BluetoothTaskManagerWin::DeviceState>& devices) {
229 STLDeleteValues(&devices_);
230 for (ScopedVector<BluetoothTaskManagerWin::DeviceState>::const_iterator iter =
231 devices.begin();
232 iter != devices.end();
233 ++iter) {
234 devices_[(*iter)->address] = new BluetoothDeviceWin(
235 **iter, ui_task_runner_, socket_thread_, NULL, net::NetLog::Source());
239 // If the method is called when |discovery_status_| is DISCOVERY_STOPPING,
240 // starting again is handled by BluetoothAdapterWin::DiscoveryStopped().
241 void BluetoothAdapterWin::AddDiscoverySession(
242 const base::Closure& callback,
243 const ErrorCallback& error_callback) {
244 if (discovery_status_ == DISCOVERING) {
245 num_discovery_listeners_++;
246 callback.Run();
247 return;
249 on_start_discovery_callbacks_.push_back(
250 std::make_pair(callback, error_callback));
251 MaybePostStartDiscoveryTask();
254 void BluetoothAdapterWin::RemoveDiscoverySession(
255 const base::Closure& callback,
256 const ErrorCallback& error_callback) {
257 if (discovery_status_ == NOT_DISCOVERING) {
258 error_callback.Run();
259 return;
261 on_stop_discovery_callbacks_.push_back(callback);
262 MaybePostStopDiscoveryTask();
265 void BluetoothAdapterWin::Init() {
266 ui_task_runner_ = base::ThreadTaskRunnerHandle::Get();
267 socket_thread_ = BluetoothSocketThread::Get();
268 task_manager_ =
269 new BluetoothTaskManagerWin(ui_task_runner_);
270 task_manager_->AddObserver(this);
271 task_manager_->Initialize();
274 void BluetoothAdapterWin::InitForTest(
275 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
276 scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner) {
277 ui_task_runner_ = ui_task_runner;
278 task_manager_ =
279 new BluetoothTaskManagerWin(ui_task_runner_);
280 task_manager_->AddObserver(this);
281 task_manager_->InitializeWithBluetoothTaskRunner(bluetooth_task_runner);
284 void BluetoothAdapterWin::MaybePostStartDiscoveryTask() {
285 if (discovery_status_ == NOT_DISCOVERING &&
286 !on_start_discovery_callbacks_.empty()) {
287 discovery_status_ = DISCOVERY_STARTING;
288 task_manager_->PostStartDiscoveryTask();
292 void BluetoothAdapterWin::MaybePostStopDiscoveryTask() {
293 if (discovery_status_ != DISCOVERING)
294 return;
296 if (on_stop_discovery_callbacks_.size() < num_discovery_listeners_) {
297 for (std::vector<base::Closure>::const_iterator iter =
298 on_stop_discovery_callbacks_.begin();
299 iter != on_stop_discovery_callbacks_.end();
300 ++iter) {
301 ui_task_runner_->PostTask(FROM_HERE, *iter);
303 num_discovery_listeners_ -= on_stop_discovery_callbacks_.size();
304 on_stop_discovery_callbacks_.clear();
305 return;
308 discovery_status_ = DISCOVERY_STOPPING;
309 task_manager_->PostStopDiscoveryTask();
312 } // namespace device