Add OWNERS to content/browser/quota
[chromium-blink-merge.git] / device / bluetooth / bluetooth_device_win.cc
blobed51d4be2e74b2696b75eee5ff9ec7bfec019942
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_device_win.h"
7 #include <string>
9 #include "base/basictypes.h"
10 #include "base/containers/scoped_ptr_hash_map.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/sequenced_task_runner.h"
14 #include "base/strings/stringprintf.h"
15 #include "device/bluetooth/bluetooth_service_record_win.h"
16 #include "device/bluetooth/bluetooth_socket_thread.h"
17 #include "device/bluetooth/bluetooth_socket_win.h"
18 #include "device/bluetooth/bluetooth_task_manager_win.h"
19 #include "device/bluetooth/bluetooth_uuid.h"
21 namespace {
23 const char kApiUnavailable[] = "This API is not implemented on this platform.";
25 } // namespace
27 namespace device {
29 BluetoothDeviceWin::BluetoothDeviceWin(
30 const BluetoothTaskManagerWin::DeviceState& device_state,
31 const scoped_refptr<base::SequencedTaskRunner>& ui_task_runner,
32 const scoped_refptr<BluetoothSocketThread>& socket_thread,
33 net::NetLog* net_log,
34 const net::NetLog::Source& net_log_source)
35 : BluetoothDevice(),
36 ui_task_runner_(ui_task_runner),
37 socket_thread_(socket_thread),
38 net_log_(net_log),
39 net_log_source_(net_log_source) {
40 Update(device_state);
43 BluetoothDeviceWin::~BluetoothDeviceWin() {
46 void BluetoothDeviceWin::Update(
47 const BluetoothTaskManagerWin::DeviceState& device_state) {
48 address_ = device_state.address;
49 // Note: Callers are responsible for providing a canonicalized address.
50 DCHECK_EQ(address_, BluetoothDevice::CanonicalizeAddress(address_));
51 name_ = device_state.name;
52 bluetooth_class_ = device_state.bluetooth_class;
53 visible_ = device_state.visible;
54 connected_ = device_state.connected;
55 paired_ = device_state.authenticated;
56 UpdateServices(device_state);
59 void BluetoothDeviceWin::UpdateServices(
60 const BluetoothTaskManagerWin::DeviceState& device_state) {
61 uuids_.clear();
62 service_record_list_.clear();
64 for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator
65 iter = device_state.service_record_states.begin();
66 iter != device_state.service_record_states.end();
67 ++iter) {
68 BluetoothServiceRecordWin* service_record =
69 new BluetoothServiceRecordWin(device_state.address,
70 (*iter)->name,
71 (*iter)->sdp_bytes,
72 (*iter)->gatt_uuid);
73 service_record_list_.push_back(service_record);
74 uuids_.push_back(service_record->uuid());
78 bool BluetoothDeviceWin::IsEqual(
79 const BluetoothTaskManagerWin::DeviceState& device_state) {
80 if (address_ != device_state.address || name_ != device_state.name ||
81 bluetooth_class_ != device_state.bluetooth_class ||
82 visible_ != device_state.visible ||
83 connected_ != device_state.connected ||
84 paired_ != device_state.authenticated) {
85 return false;
88 // Checks service collection
89 typedef std::set<BluetoothUUID> UUIDSet;
90 typedef base::ScopedPtrHashMap<
91 std::string, scoped_ptr<BluetoothServiceRecordWin>> ServiceRecordMap;
93 UUIDSet known_services;
94 for (UUIDList::const_iterator iter = uuids_.begin(); iter != uuids_.end();
95 ++iter) {
96 known_services.insert((*iter));
99 UUIDSet new_services;
100 ServiceRecordMap new_service_records;
101 for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator
102 iter = device_state.service_record_states.begin();
103 iter != device_state.service_record_states.end();
104 ++iter) {
105 BluetoothServiceRecordWin* service_record = new BluetoothServiceRecordWin(
106 address_, (*iter)->name, (*iter)->sdp_bytes, (*iter)->gatt_uuid);
107 new_services.insert(service_record->uuid());
108 new_service_records.set(
109 service_record->uuid().canonical_value(),
110 scoped_ptr<BluetoothServiceRecordWin>(service_record));
113 UUIDSet removed_services =
114 base::STLSetDifference<UUIDSet>(known_services, new_services);
115 if (!removed_services.empty()) {
116 return false;
118 UUIDSet added_devices =
119 base::STLSetDifference<UUIDSet>(new_services, known_services);
120 if (!added_devices.empty()) {
121 return false;
124 for (ServiceRecordList::const_iterator iter = service_record_list_.begin();
125 iter != service_record_list_.end();
126 ++iter) {
127 BluetoothServiceRecordWin* service_record = (*iter);
128 BluetoothServiceRecordWin* new_service_record =
129 new_service_records.get((*iter)->uuid().canonical_value());
130 if (!service_record->IsEqual(*new_service_record))
131 return false;
133 return true;
136 void BluetoothDeviceWin::SetVisible(bool visible) {
137 visible_ = visible;
140 uint32 BluetoothDeviceWin::GetBluetoothClass() const {
141 return bluetooth_class_;
144 std::string BluetoothDeviceWin::GetDeviceName() const {
145 return name_;
148 std::string BluetoothDeviceWin::GetAddress() const {
149 return address_;
152 BluetoothDevice::VendorIDSource
153 BluetoothDeviceWin::GetVendorIDSource() const {
154 return VENDOR_ID_UNKNOWN;
157 uint16 BluetoothDeviceWin::GetVendorID() const {
158 return 0;
161 uint16 BluetoothDeviceWin::GetProductID() const {
162 return 0;
165 uint16 BluetoothDeviceWin::GetDeviceID() const {
166 return 0;
169 bool BluetoothDeviceWin::IsPaired() const {
170 return paired_;
173 bool BluetoothDeviceWin::IsConnected() const {
174 return connected_;
177 bool BluetoothDeviceWin::IsConnectable() const {
178 return false;
181 bool BluetoothDeviceWin::IsConnecting() const {
182 return false;
185 BluetoothDevice::UUIDList BluetoothDeviceWin::GetUUIDs() const {
186 return uuids_;
189 int16 BluetoothDeviceWin::GetInquiryRSSI() const {
190 return kUnknownPower;
193 int16 BluetoothDeviceWin::GetInquiryTxPower() const {
194 NOTIMPLEMENTED();
195 return kUnknownPower;
198 bool BluetoothDeviceWin::ExpectingPinCode() const {
199 NOTIMPLEMENTED();
200 return false;
203 bool BluetoothDeviceWin::ExpectingPasskey() const {
204 NOTIMPLEMENTED();
205 return false;
208 bool BluetoothDeviceWin::ExpectingConfirmation() const {
209 NOTIMPLEMENTED();
210 return false;
213 void BluetoothDeviceWin::GetConnectionInfo(
214 const ConnectionInfoCallback& callback) {
215 NOTIMPLEMENTED();
216 callback.Run(ConnectionInfo());
219 void BluetoothDeviceWin::Connect(
220 PairingDelegate* pairing_delegate,
221 const base::Closure& callback,
222 const ConnectErrorCallback& error_callback) {
223 NOTIMPLEMENTED();
226 void BluetoothDeviceWin::SetPinCode(const std::string& pincode) {
227 NOTIMPLEMENTED();
230 void BluetoothDeviceWin::SetPasskey(uint32 passkey) {
231 NOTIMPLEMENTED();
234 void BluetoothDeviceWin::ConfirmPairing() {
235 NOTIMPLEMENTED();
238 void BluetoothDeviceWin::RejectPairing() {
239 NOTIMPLEMENTED();
242 void BluetoothDeviceWin::CancelPairing() {
243 NOTIMPLEMENTED();
246 void BluetoothDeviceWin::Disconnect(
247 const base::Closure& callback,
248 const ErrorCallback& error_callback) {
249 NOTIMPLEMENTED();
252 void BluetoothDeviceWin::Forget(const ErrorCallback& error_callback) {
253 NOTIMPLEMENTED();
256 void BluetoothDeviceWin::ConnectToService(
257 const BluetoothUUID& uuid,
258 const ConnectToServiceCallback& callback,
259 const ConnectToServiceErrorCallback& error_callback) {
260 scoped_refptr<BluetoothSocketWin> socket(
261 BluetoothSocketWin::CreateBluetoothSocket(
262 ui_task_runner_, socket_thread_));
263 socket->Connect(this, uuid, base::Bind(callback, socket), error_callback);
266 void BluetoothDeviceWin::ConnectToServiceInsecurely(
267 const BluetoothUUID& uuid,
268 const ConnectToServiceCallback& callback,
269 const ConnectToServiceErrorCallback& error_callback) {
270 error_callback.Run(kApiUnavailable);
273 void BluetoothDeviceWin::CreateGattConnection(
274 const GattConnectionCallback& callback,
275 const ConnectErrorCallback& error_callback) {
276 // TODO(armansito): Implement.
277 error_callback.Run(ERROR_UNSUPPORTED_DEVICE);
280 const BluetoothServiceRecordWin* BluetoothDeviceWin::GetServiceRecord(
281 const device::BluetoothUUID& uuid) const {
282 for (ServiceRecordList::const_iterator iter = service_record_list_.begin();
283 iter != service_record_list_.end();
284 ++iter) {
285 if ((*iter)->uuid() == uuid)
286 return *iter;
288 return NULL;
291 } // namespace device