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