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
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DISPATCHER_HOST_H_
6 #define CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DISPATCHER_HOST_H_
8 #include "base/basictypes.h"
9 #include "base/id_map.h"
10 #include "base/memory/weak_ptr.h"
11 #include "content/public/browser/bluetooth_chooser.h"
12 #include "content/public/browser/browser_message_filter.h"
13 #include "device/bluetooth/bluetooth_adapter.h"
14 #include "device/bluetooth/bluetooth_gatt_connection.h"
15 #include "device/bluetooth/bluetooth_gatt_service.h"
23 struct BluetoothScanFilter
;
25 // Dispatches and sends bluetooth related messages sent to/from a child
26 // process BluetoothDispatcher from/to the main browser process.
28 // Intended to be instantiated by the RenderProcessHost and installed as
29 // a filter on the channel. BrowserMessageFilter is refcounted and typically
30 // lives as long as it is installed on a channel.
33 // BluetoothDispatcherHost is constructed, operates, and destroyed on the UI
34 // thread because BluetoothAdapter and related objects live there.
35 class CONTENT_EXPORT BluetoothDispatcherHost final
36 : public BrowserMessageFilter
,
37 public device::BluetoothAdapter::Observer
{
39 BluetoothDispatcherHost(int render_process_id
);
40 // BrowserMessageFilter:
41 void OnDestruct() const override
;
42 void OverrideThreadForMessage(const IPC::Message
& message
,
43 BrowserThread::ID
* thread
) override
;
44 bool OnMessageReceived(const IPC::Message
& message
) override
;
46 void SetBluetoothAdapterForTesting(
47 scoped_refptr
<device::BluetoothAdapter
> mock_adapter
);
50 ~BluetoothDispatcherHost() override
;
53 friend class base::DeleteHelper
<BluetoothDispatcherHost
>;
54 friend struct BrowserThread::DeleteOnThread
<BrowserThread::UI
>;
56 struct RequestDeviceSession
;
58 // Set |adapter_| to a BluetoothAdapter instance and register observers,
59 // releasing references to previous |adapter_|.
60 void set_adapter(scoped_refptr
<device::BluetoothAdapter
> adapter
);
62 // Stops all BluetoothDiscoverySessions being run for requestDevice()
64 void StopDeviceDiscovery();
66 // BluetoothAdapter::Observer:
67 void AdapterPoweredChanged(device::BluetoothAdapter
* adapter
,
68 bool powered
) override
;
69 void DeviceAdded(device::BluetoothAdapter
* adapter
,
70 device::BluetoothDevice
* device
) override
;
71 void DeviceRemoved(device::BluetoothAdapter
* adapter
,
72 device::BluetoothDevice
* device
) override
;
74 // IPC Handlers, see definitions in bluetooth_messages.h.
79 const std::vector
<content::BluetoothScanFilter
>& filters
,
80 const std::vector
<device::BluetoothUUID
>& optional_services
);
81 void OnConnectGATT(int thread_id
, int request_id
,
82 const std::string
& device_instance_id
);
83 void OnGetPrimaryService(int thread_id
,
85 const std::string
& device_instance_id
,
86 const std::string
& service_uuid
);
87 void OnGetCharacteristic(int thread_id
,
89 const std::string
& service_instance_id
,
90 const std::string
& characteristic_uuid
);
91 void OnReadValue(int thread_id
,
93 const std::string
& characteristic_instance_id
);
94 void OnWriteValue(int thread_id
,
96 const std::string
& characteristic_instance_id
,
97 const std::vector
<uint8_t>& value
);
99 // Callbacks for BluetoothAdapter::StartDiscoverySession.
100 void OnDiscoverySessionStarted(
102 scoped_ptr
<device::BluetoothDiscoverySession
> discovery_session
);
103 void OnDiscoverySessionStartedError(int chooser_id
);
105 // BluetoothChooser::EventHandler:
106 void OnBluetoothChooserEvent(int chooser_id
,
107 BluetoothChooser::Event event
,
108 const std::string
& device_id
);
110 // The chooser implementation yields to the event loop to avoid re-entering
111 // code that's still using the RequestDeviceSession, and continues with this
113 void FinishClosingChooser(int chooser_id
,
114 BluetoothChooser::Event event
,
115 const std::string
& device_id
);
117 // Callbacks for BluetoothDevice::CreateGattConnection.
118 void OnGATTConnectionCreated(
121 const std::string
& device_instance_id
,
122 base::TimeTicks start_time
,
123 scoped_ptr
<device::BluetoothGattConnection
> connection
);
124 void OnCreateGATTConnectionError(
127 const std::string
& device_instance_id
,
128 base::TimeTicks start_time
,
129 device::BluetoothDevice::ConnectErrorCode error_code
);
131 // Callback for future BluetoothAdapter::ServicesDiscovered callback:
132 // For now we just post a delayed task.
133 // See: https://crbug.com/484504
134 void OnServicesDiscovered(int thread_id
,
136 const std::string
& device_instance_id
,
137 const std::string
& service_uuid
);
139 // Callbacks for BluetoothGattCharacteristic::ReadRemoteCharacteristic.
140 void OnCharacteristicValueRead(int thread_id
,
142 const std::vector
<uint8
>& value
);
143 void OnCharacteristicReadValueError(
146 device::BluetoothGattService::GattErrorCode
);
148 // Callbacks for BluetoothGattCharacteristic::WriteRemoteCharacteristic.
149 void OnWriteValueSuccess(int thread_id
, int request_id
);
150 void OnWriteValueFailed(int thread_id
,
152 device::BluetoothGattService::GattErrorCode
);
154 // Show help pages from the chooser dialog.
155 void ShowBluetoothOverviewLink();
156 void ShowBluetoothPairingLink();
157 void ShowBluetoothAdapterOffLink();
159 int render_process_id_
;
161 // Maps a (thread_id,request_id) to information about its requestDevice call,
162 // including the chooser dialog.
163 // An entry is added to this map in OnRequestDevice, and should be removed
164 // again everywhere a requestDevice() reply is sent.
165 IDMap
<RequestDeviceSession
, IDMapOwnPointer
> request_device_sessions_
;
167 // Maps to get the object's parent based on it's instanceID
168 // Map of service_instance_id to device_instance_id.
169 std::map
<std::string
, std::string
> service_to_device_
;
170 // Map of characteristic_instance_id to service_instance_id.
171 std::map
<std::string
, std::string
> characteristic_to_service_
;
173 // Defines how long to scan for and how long to discover services for.
174 int current_delay_time_
;
176 // A BluetoothAdapter instance representing an adapter of the system.
177 scoped_refptr
<device::BluetoothAdapter
> adapter_
;
179 // Automatically stops Bluetooth discovery a set amount of time after it was
180 // started. We have a single timer for all of Web Bluetooth because it's
181 // simpler than tracking timeouts for each RequestDeviceSession individually,
182 // and because there's no harm in extending the length of a few discovery
183 // sessions when other sessions are active.
184 base::Timer discovery_session_timer_
;
186 // Must be last member, see base/memory/weak_ptr.h documentation
187 base::WeakPtrFactory
<BluetoothDispatcherHost
> weak_ptr_factory_
;
189 DISALLOW_COPY_AND_ASSIGN(BluetoothDispatcherHost
);
192 } // namespace content
194 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DISPATCHER_HOST_H_