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 "content/common/bluetooth/bluetooth_messages.h" | 7 #include "content/common/bluetooth/bluetooth_messages.h" |
8 #include "device/bluetooth/bluetooth_adapter.h" | |
9 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
10 #include "device/bluetooth/bluetooth_device.h" | |
11 | |
12 using device::BluetoothAdapter; | |
13 using device::BluetoothAdapterFactory; | |
14 using device::BluetoothDevice; | |
8 | 15 |
9 namespace content { | 16 namespace content { |
10 | 17 |
11 BluetoothDispatcherHost::BluetoothDispatcherHost() | 18 scoped_refptr<BluetoothDispatcherHost> BluetoothDispatcherHost::Create() { |
12 : BrowserMessageFilter(BluetoothMsgStart), | |
13 bluetooth_mock_data_set_(MockData::NOT_MOCKING), | |
14 bluetooth_request_device_reject_type_(BluetoothError::NOT_FOUND) { | |
15 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 19 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
20 | |
21 // Hold a reference to the BluetoothDispatcherHost because the callback below | |
22 // may run and would otherwise release the BluetoothDispatcherHost | |
23 // prematurely. | |
24 scoped_refptr<BluetoothDispatcherHost> host(new BluetoothDispatcherHost()); | |
25 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) | |
26 BluetoothAdapterFactory::GetAdapter( | |
27 base::Bind(&BluetoothDispatcherHost::set_adapter, host)); | |
28 return host; | |
16 } | 29 } |
17 | 30 |
18 bool BluetoothDispatcherHost::OnMessageReceived(const IPC::Message& message) { | 31 bool BluetoothDispatcherHost::OnMessageReceived(const IPC::Message& message) { |
19 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 32 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
20 bool handled = true; | 33 bool handled = true; |
21 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcherHost, message) | 34 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcherHost, message) |
22 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RequestDevice, OnRequestDevice) | 35 IPC_MESSAGE_HANDLER(BluetoothHostMsg_RequestDevice, OnRequestDevice) |
23 IPC_MESSAGE_HANDLER(BluetoothHostMsg_SetBluetoothMockDataSetForTesting, | 36 IPC_MESSAGE_HANDLER(BluetoothHostMsg_SetBluetoothMockDataSetForTesting, |
24 OnSetBluetoothMockDataSetForTesting) | 37 OnSetBluetoothMockDataSetForTesting) |
25 IPC_MESSAGE_UNHANDLED(handled = false) | 38 IPC_MESSAGE_UNHANDLED(handled = false) |
26 IPC_END_MESSAGE_MAP() | 39 IPC_END_MESSAGE_MAP() |
27 return handled; | 40 return handled; |
28 } | 41 } |
29 | 42 |
43 BluetoothDispatcherHost::BluetoothDispatcherHost() | |
44 : BrowserMessageFilter(BluetoothMsgStart), | |
45 bluetooth_mock_data_set_(MockData::NOT_MOCKING), | |
46 bluetooth_request_device_reject_type_(BluetoothError::NOT_FOUND) { | |
47 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
48 } | |
49 | |
30 BluetoothDispatcherHost::~BluetoothDispatcherHost() { | 50 BluetoothDispatcherHost::~BluetoothDispatcherHost() { |
51 // Clear adapter, releasing observer references. | |
52 set_adapter(scoped_refptr<device::BluetoothAdapter>()); | |
53 } | |
54 | |
55 void BluetoothDispatcherHost::set_adapter( | |
56 scoped_refptr<device::BluetoothAdapter> adapter) { | |
57 if (adapter_.get()) | |
58 adapter_->RemoveObserver(this); | |
59 adapter_ = adapter; | |
60 if (adapter_.get()) | |
61 adapter_->AddObserver(this); | |
31 } | 62 } |
32 | 63 |
33 void BluetoothDispatcherHost::OnRequestDevice(int thread_id, int request_id) { | 64 void BluetoothDispatcherHost::OnRequestDevice(int thread_id, int request_id) { |
34 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 65 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
35 // Mock implementation util a more complete implementation is built out. | 66 // Mock implementation util a more complete implementation is built out. |
armansito
2014/12/10 00:40:04
nit: s/util/until/ ?
scheib
2014/12/10 17:54:02
Done.
| |
36 switch (bluetooth_mock_data_set_) { | 67 switch (bluetooth_mock_data_set_) { |
37 case MockData::NOT_MOCKING: { | 68 case MockData::NOT_MOCKING: { |
38 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, | 69 // TODO(scheib): Filter devices by services: crbug.com/440594 |
39 BluetoothError::NOT_FOUND)); | 70 // TODO(scheib): Device selection UI: crbug.com/436280 |
71 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); | |
armansito
2014/12/10 00:40:04
I don't know what the final behavior will look lik
scheib
2014/12/10 17:54:02
Done.
| |
72 if (devices.begin() == devices.end()) { | |
73 Send(new BluetoothMsg_RequestDeviceError(thread_id, request_id, | |
74 BluetoothError::NOT_FOUND)); | |
75 } else { | |
76 BluetoothDevice* device = *devices.begin(); | |
77 Send(new BluetoothMsg_RequestDeviceSuccess(thread_id, request_id, | |
78 device->GetAddress())); | |
79 } | |
40 return; | 80 return; |
41 } | 81 } |
42 case MockData::REJECT: { | 82 case MockData::REJECT: { |
43 Send(new BluetoothMsg_RequestDeviceError( | 83 Send(new BluetoothMsg_RequestDeviceError( |
44 thread_id, request_id, bluetooth_request_device_reject_type_)); | 84 thread_id, request_id, bluetooth_request_device_reject_type_)); |
45 return; | 85 return; |
46 } | 86 } |
47 case MockData::RESOLVE: { | 87 case MockData::RESOLVE: { |
48 Send(new BluetoothMsg_RequestDeviceSuccess(thread_id, request_id, | 88 Send(new BluetoothMsg_RequestDeviceSuccess(thread_id, request_id, |
49 "Empty Mock deviceId")); | 89 "Empty Mock deviceId")); |
(...skipping 14 matching lines...) Expand all Loading... | |
64 bluetooth_request_device_reject_type_ = BluetoothError::SECURITY; | 104 bluetooth_request_device_reject_type_ = BluetoothError::SECURITY; |
65 } else if (name == "ResolveRequestDevice_Empty" || // TODO(scheib): Remove. | 105 } else if (name == "ResolveRequestDevice_Empty" || // TODO(scheib): Remove. |
66 name == "Single Empty Device") { | 106 name == "Single Empty Device") { |
67 bluetooth_mock_data_set_ = MockData::RESOLVE; | 107 bluetooth_mock_data_set_ = MockData::RESOLVE; |
68 } else { | 108 } else { |
69 bluetooth_mock_data_set_ = MockData::NOT_MOCKING; | 109 bluetooth_mock_data_set_ = MockData::NOT_MOCKING; |
70 } | 110 } |
71 } | 111 } |
72 | 112 |
73 } // namespace content | 113 } // namespace content |
OLD | NEW |