OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/bluetooth/bluetooth_dispatcher_host.h" | 5 #include "content/browser/bluetooth/bluetooth_dispatcher_host.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "content/common/bluetooth/bluetooth_messages.h" | 8 #include "content/common/bluetooth/bluetooth_messages.h" |
9 #include "device/bluetooth/bluetooth_adapter.h" | 9 #include "device/bluetooth/bluetooth_adapter.h" |
10 #include "device/bluetooth/bluetooth_adapter_factory.h" | 10 #include "device/bluetooth/bluetooth_adapter_factory.h" |
11 #include "device/bluetooth/bluetooth_device.h" | 11 #include "device/bluetooth/bluetooth_device.h" |
12 #include "device/bluetooth/bluetooth_discovery_session.h" | 12 #include "device/bluetooth/bluetooth_discovery_session.h" |
13 | 13 |
14 using device::BluetoothAdapter; | 14 using device::BluetoothAdapter; |
15 using device::BluetoothAdapterFactory; | 15 using device::BluetoothAdapterFactory; |
16 | 16 |
17 namespace content { | 17 namespace content { |
18 | 18 |
19 const uint32 kUnspecifiedDeviceClass = | |
20 0x1F00; // bluetooth.org/en-us/specification/assigned-numbers/baseband | |
21 const int kScanTime = 5; // 5 seconds of scan time | 19 const int kScanTime = 5; // 5 seconds of scan time |
20 const int kTestingScanTime = 0; // No need to scan for tests | |
22 | 21 |
23 BluetoothDispatcherHost::BluetoothDispatcherHost() | 22 BluetoothDispatcherHost::BluetoothDispatcherHost() |
24 : BrowserMessageFilter(BluetoothMsgStart), | 23 : BrowserMessageFilter(BluetoothMsgStart), |
25 bluetooth_mock_data_set_(MockData::NOT_MOCKING), | |
26 bluetooth_request_device_reject_type_(BluetoothError::NOT_FOUND), | |
27 weak_ptr_factory_(this) { | 24 weak_ptr_factory_(this) { |
28 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 25 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
26 current_scan_time_ = kScanTime; | |
29 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) | 27 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) |
30 BluetoothAdapterFactory::GetAdapter( | 28 BluetoothAdapterFactory::GetAdapter( |
31 base::Bind(&BluetoothDispatcherHost::set_adapter, | 29 base::Bind(&BluetoothDispatcherHost::set_adapter, |
32 weak_ptr_factory_.GetWeakPtr())); | 30 weak_ptr_factory_.GetWeakPtr())); |
33 } | 31 } |
34 | 32 |
35 void BluetoothDispatcherHost::OnDestruct() const { | 33 void BluetoothDispatcherHost::OnDestruct() const { |
36 // See class comment: UI Thread Note. | 34 // See class comment: UI Thread Note. |
37 BrowserThread::DeleteOnUIThread::Destruct(this); | 35 BrowserThread::DeleteOnUIThread::Destruct(this); |
38 } | 36 } |
(...skipping 27 matching lines...) Expand all Loading... | |
66 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 64 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
67 if (adapter_.get()) | 65 if (adapter_.get()) |
68 adapter_->RemoveObserver(this); | 66 adapter_->RemoveObserver(this); |
69 adapter_ = adapter; | 67 adapter_ = adapter; |
70 if (adapter_.get()) | 68 if (adapter_.get()) |
71 adapter_->AddObserver(this); | 69 adapter_->AddObserver(this); |
72 } | 70 } |
73 | 71 |
74 void BluetoothDispatcherHost::OnRequestDevice(int thread_id, int request_id) { | 72 void BluetoothDispatcherHost::OnRequestDevice(int thread_id, int request_id) { |
75 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 73 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
76 // TODO(scheib) Extend this very simple mock implementation by using | 74 // TODO(scheib): Filter devices by services: crbug.com/440594 |
77 // device/bluetooth/test mock adapter and related classes. | 75 // TODO(scheib): Device selection UI: crbug.com/436280 |
78 switch (bluetooth_mock_data_set_) { | 76 // TODO(scheib): Utilize BluetoothAdapter::Observer::DeviceAdded/Removed. |
79 case MockData::NOT_MOCKING: { | 77 if (adapter_.get()) { |
80 // TODO(scheib): Filter devices by services: crbug.com/440594 | 78 adapter_->StartDiscoverySession( |
81 // TODO(scheib): Device selection UI: crbug.com/436280 | 79 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStarted, |
82 // TODO(scheib): Utilize BluetoothAdapter::Observer::DeviceAdded/Removed. | 80 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id), |
83 BluetoothAdapter::DeviceList devices; | 81 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStartedError, |
84 | 82 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); |
85 if (adapter_.get()) { | 83 } else { |
86 adapter_->StartDiscoverySession( | 84 DLOG(WARNING) << "No BluetoothAdapter. Can't serve requestDevice."; |
87 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStarted, | 85 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, |
88 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id), | 86 BluetoothError::NOT_FOUND)); |
89 base::Bind(&BluetoothDispatcherHost::OnDiscoverySessionStartedError, | |
90 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id)); | |
91 } else { | |
92 DLOG(WARNING) << "No BluetoothAdapter. Can't serve requestDevice."; | |
93 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, | |
94 BluetoothError::NOT_FOUND)); | |
95 } | |
96 return; | |
97 } | |
98 case MockData::REJECT: { | |
99 Send(new BluetoothMsg_RequestDeviceError( | |
100 thread_id, request_id, bluetooth_request_device_reject_type_)); | |
101 return; | |
102 } | |
103 case MockData::RESOLVE: { | |
104 std::vector<std::string> uuids; | |
105 uuids.push_back("00001800-0000-1000-8000-00805f9b34fb"); | |
106 uuids.push_back("00001801-0000-1000-8000-00805f9b34fb"); | |
107 content::BluetoothDevice device_ipc( | |
108 "Empty Mock Device instanceID", // instance_id | |
109 base::UTF8ToUTF16("Empty Mock Device name"), // name | |
110 kUnspecifiedDeviceClass, // device_class | |
111 device::BluetoothDevice::VENDOR_ID_BLUETOOTH, // vendor_id_source | |
112 0xFFFF, // vendor_id | |
113 1, // product_id | |
114 2, // product_version | |
115 true, // paired | |
116 uuids); // uuids | |
117 Send(new BluetoothMsg_RequestDeviceSuccess(thread_id, request_id, | |
118 device_ipc)); | |
119 return; | |
120 } | |
121 } | 87 } |
122 NOTREACHED(); | 88 return; |
123 } | 89 } |
124 | 90 |
125 void BluetoothDispatcherHost::OnConnectGATT( | 91 void BluetoothDispatcherHost::OnConnectGATT( |
126 int thread_id, | 92 int thread_id, |
127 int request_id, | 93 int request_id, |
128 const std::string& device_instance_id) { | 94 const std::string& device_instance_id) { |
129 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 95 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
130 // TODO(ortuno): Add actual implementation of connectGATT. This needs to be | 96 // TODO(ortuno): Add actual implementation of connectGATT. This needs to be |
131 // done after the "allowed devices map" is implemented. | 97 // done after the "allowed devices map" is implemented. |
132 Send(new BluetoothMsg_ConnectGATTSuccess(thread_id, request_id, | 98 Send(new BluetoothMsg_ConnectGATTSuccess(thread_id, request_id, |
133 device_instance_id)); | 99 device_instance_id)); |
134 } | 100 } |
135 | 101 |
136 void BluetoothDispatcherHost::SetBluetoothAdapterForTesting( | 102 void BluetoothDispatcherHost::SetBluetoothAdapterForTesting( |
scheib
2015/05/16 02:39:14
Move to match declaration order.
ortuno
2015/05/18 17:41:55
Done.
| |
137 const std::string& name) { | 103 scoped_refptr<device::BluetoothAdapter> mock_adapter) { |
138 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 104 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
139 if (name == "RejectRequestDevice_NotFoundError") { | 105 current_scan_time_ = kTestingScanTime; |
140 bluetooth_mock_data_set_ = MockData::REJECT; | 106 set_adapter(mock_adapter.Pass()); |
141 bluetooth_request_device_reject_type_ = BluetoothError::NOT_FOUND; | |
142 } else if (name == "RejectRequestDevice_SecurityError") { | |
143 bluetooth_mock_data_set_ = MockData::REJECT; | |
144 bluetooth_request_device_reject_type_ = BluetoothError::SECURITY; | |
145 } else if (name == "ResolveRequestDevice_Empty" || // TODO(scheib): Remove. | |
146 name == "Single Empty Device") { | |
147 bluetooth_mock_data_set_ = MockData::RESOLVE; | |
148 } else { | |
149 bluetooth_mock_data_set_ = MockData::NOT_MOCKING; | |
150 } | |
151 } | 107 } |
152 | 108 |
153 void BluetoothDispatcherHost::OnDiscoverySessionStarted( | 109 void BluetoothDispatcherHost::OnDiscoverySessionStarted( |
154 int thread_id, | 110 int thread_id, |
155 int request_id, | 111 int request_id, |
156 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { | 112 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { |
157 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 113 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
158 BrowserThread::PostDelayedTask( | 114 BrowserThread::PostDelayedTask( |
159 BrowserThread::UI, FROM_HERE, | 115 BrowserThread::UI, FROM_HERE, |
160 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession, | 116 base::Bind(&BluetoothDispatcherHost::StopDiscoverySession, |
161 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, | 117 weak_ptr_factory_.GetWeakPtr(), thread_id, request_id, |
162 base::Passed(&discovery_session)), | 118 base::Passed(&discovery_session)), |
163 base::TimeDelta::FromSeconds(kScanTime)); | 119 base::TimeDelta::FromSeconds(current_scan_time_)); |
164 } | 120 } |
165 | 121 |
166 void BluetoothDispatcherHost::OnDiscoverySessionStartedError(int thread_id, | 122 void BluetoothDispatcherHost::OnDiscoverySessionStartedError(int thread_id, |
167 int request_id) { | 123 int request_id) { |
168 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 124 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
169 DLOG(WARNING) << "BluetoothDispatcherHost::OnDiscoverySessionStartedError"; | 125 DLOG(WARNING) << "BluetoothDispatcherHost::OnDiscoverySessionStartedError"; |
170 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, | 126 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, |
171 BluetoothError::NOT_FOUND)); | 127 BluetoothError::NOT_FOUND)); |
172 } | 128 } |
173 | 129 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
210 | 166 |
211 void BluetoothDispatcherHost::OnDiscoverySessionStoppedError(int thread_id, | 167 void BluetoothDispatcherHost::OnDiscoverySessionStoppedError(int thread_id, |
212 int request_id) { | 168 int request_id) { |
213 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 169 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
214 DLOG(WARNING) << "BluetoothDispatcherHost::OnDiscoverySessionStoppedError"; | 170 DLOG(WARNING) << "BluetoothDispatcherHost::OnDiscoverySessionStoppedError"; |
215 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, | 171 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, |
216 BluetoothError::NOT_FOUND)); | 172 BluetoothError::NOT_FOUND)); |
217 } | 173 } |
218 | 174 |
219 } // namespace content | 175 } // namespace content |
OLD | NEW |