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/memory/weak_ptr.h"
10 #include "content/public/browser/browser_message_filter.h"
11 #include "device/bluetooth/bluetooth_adapter.h"
12 #include "device/bluetooth/bluetooth_gatt_connection.h"
13 #include "device/bluetooth/bluetooth_gatt_service.h"
21 struct BluetoothScanFilter
;
23 // Dispatches and sends bluetooth related messages sent to/from a child
24 // process BluetoothDispatcher from/to the main browser process.
26 // Intended to be instantiated by the RenderProcessHost and installed as
27 // a filter on the channel. BrowserMessageFilter is refcounted and typically
28 // lives as long as it is installed on a channel.
31 // BluetoothDispatcherHost is constructed, operates, and destroyed on the UI
32 // thread because BluetoothAdapter and related objects live there.
33 class CONTENT_EXPORT BluetoothDispatcherHost final
34 : public BrowserMessageFilter
,
35 public device::BluetoothAdapter::Observer
{
37 BluetoothDispatcherHost(int render_process_id
);
38 // BrowserMessageFilter:
39 void OnDestruct() const override
;
40 void OverrideThreadForMessage(const IPC::Message
& message
,
41 BrowserThread::ID
* thread
) override
;
42 bool OnMessageReceived(const IPC::Message
& message
) override
;
44 void SetBluetoothAdapterForTesting(
45 scoped_refptr
<device::BluetoothAdapter
> mock_adapter
);
48 ~BluetoothDispatcherHost() override
;
51 friend class base::DeleteHelper
<BluetoothDispatcherHost
>;
52 friend struct BrowserThread::DeleteOnThread
<BrowserThread::UI
>;
54 struct RequestDeviceSession
;
56 // Set |adapter_| to a BluetoothAdapter instance and register observers,
57 // releasing references to previous |adapter_|.
58 void set_adapter(scoped_refptr
<device::BluetoothAdapter
> adapter
);
60 // IPC Handlers, see definitions in bluetooth_messages.h.
65 const std::vector
<content::BluetoothScanFilter
>& filters
,
66 const std::vector
<device::BluetoothUUID
>& optional_services
);
67 void OnConnectGATT(int thread_id
, int request_id
,
68 const std::string
& device_instance_id
);
69 void OnGetPrimaryService(int thread_id
,
71 const std::string
& device_instance_id
,
72 const std::string
& service_uuid
);
73 void OnGetCharacteristic(int thread_id
,
75 const std::string
& service_instance_id
,
76 const std::string
& characteristic_uuid
);
77 void OnReadValue(int thread_id
,
79 const std::string
& characteristic_instance_id
);
80 void OnWriteValue(int thread_id
,
82 const std::string
& characteristic_instance_id
,
83 const std::vector
<uint8_t>& value
);
85 // Callbacks for BluetoothAdapter::StartDiscoverySession.
86 void OnDiscoverySessionStarted(
89 scoped_ptr
<device::BluetoothDiscoverySession
> discovery_session
);
90 void OnDiscoverySessionStartedError(int thread_id
, int request_id
);
92 // Stop in progress discovery session.
93 void StopDiscoverySession(
96 scoped_ptr
<device::BluetoothDiscoverySession
> discovery_session
);
98 // Callbacks for BluetoothDiscoverySession::Stop.
99 void OnDiscoverySessionStopped(int thread_id
, int request_id
);
100 void OnDiscoverySessionStoppedError(int thread_id
, int request_id
);
102 // Callbacks for BluetoothDevice::CreateGattConnection.
103 void OnGATTConnectionCreated(
106 const std::string
& device_instance_id
,
107 scoped_ptr
<device::BluetoothGattConnection
> connection
);
108 void OnCreateGATTConnectionError(
111 const std::string
& device_instance_id
,
112 device::BluetoothDevice::ConnectErrorCode error_code
);
114 // Callback for future BluetoothAdapter::ServicesDiscovered callback:
115 // For now we just post a delayed task.
116 // See: https://crbug.com/484504
117 void OnServicesDiscovered(int thread_id
,
119 const std::string
& device_instance_id
,
120 const std::string
& service_uuid
);
122 // Callbacks for BluetoothGattCharacteristic::ReadRemoteCharacteristic.
123 void OnCharacteristicValueRead(int thread_id
,
125 const std::vector
<uint8
>& value
);
126 void OnCharacteristicReadValueError(
129 device::BluetoothGattService::GattErrorCode
);
131 // Callbacks for BluetoothGattCharacteristic::WriteRemoteCharacteristic.
132 void OnWriteValueSuccess(int thread_id
, int request_id
);
133 void OnWriteValueFailed(int thread_id
,
135 device::BluetoothGattService::GattErrorCode
);
137 int render_process_id_
;
139 // Maps a (thread_id,request_id) to information about its requestDevice call,
140 // including the chooser dialog.
141 // An entry is added to this map in OnRequestDevice, and should be removed
142 // again everywhere a requestDevice() reply is sent.
143 std::map
<std::pair
<int, int>, RequestDeviceSession
> request_device_sessions_
;
145 // Maps to get the object's parent based on it's instanceID
146 // Map of service_instance_id to device_instance_id.
147 std::map
<std::string
, std::string
> service_to_device_
;
148 // Map of characteristic_instance_id to service_instance_id.
149 std::map
<std::string
, std::string
> characteristic_to_service_
;
151 // Defines how long to scan for and how long to discover services for.
152 int current_delay_time_
;
154 // A BluetoothAdapter instance representing an adapter of the system.
155 scoped_refptr
<device::BluetoothAdapter
> adapter_
;
157 // Must be last member, see base/memory/weak_ptr.h documentation
158 base::WeakPtrFactory
<BluetoothDispatcherHost
> weak_ptr_factory_
;
160 DISALLOW_COPY_AND_ASSIGN(BluetoothDispatcherHost
);
163 } // namespace content
165 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DISPATCHER_HOST_H_