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 base::TimeTicks start_time
,
108 scoped_ptr
<device::BluetoothGattConnection
> connection
);
109 void OnCreateGATTConnectionError(
112 const std::string
& device_instance_id
,
113 base::TimeTicks start_time
,
114 device::BluetoothDevice::ConnectErrorCode error_code
);
116 // Callback for future BluetoothAdapter::ServicesDiscovered callback:
117 // For now we just post a delayed task.
118 // See: https://crbug.com/484504
119 void OnServicesDiscovered(int thread_id
,
121 const std::string
& device_instance_id
,
122 const std::string
& service_uuid
);
124 // Callbacks for BluetoothGattCharacteristic::ReadRemoteCharacteristic.
125 void OnCharacteristicValueRead(int thread_id
,
127 const std::vector
<uint8
>& value
);
128 void OnCharacteristicReadValueError(
131 device::BluetoothGattService::GattErrorCode
);
133 // Callbacks for BluetoothGattCharacteristic::WriteRemoteCharacteristic.
134 void OnWriteValueSuccess(int thread_id
, int request_id
);
135 void OnWriteValueFailed(int thread_id
,
137 device::BluetoothGattService::GattErrorCode
);
139 int render_process_id_
;
141 // Maps a (thread_id,request_id) to information about its requestDevice call,
142 // including the chooser dialog.
143 // An entry is added to this map in OnRequestDevice, and should be removed
144 // again everywhere a requestDevice() reply is sent.
145 std::map
<std::pair
<int, int>, RequestDeviceSession
> request_device_sessions_
;
147 // Maps to get the object's parent based on it's instanceID
148 // Map of service_instance_id to device_instance_id.
149 std::map
<std::string
, std::string
> service_to_device_
;
150 // Map of characteristic_instance_id to service_instance_id.
151 std::map
<std::string
, std::string
> characteristic_to_service_
;
153 // Defines how long to scan for and how long to discover services for.
154 int current_delay_time_
;
156 // A BluetoothAdapter instance representing an adapter of the system.
157 scoped_refptr
<device::BluetoothAdapter
> adapter_
;
159 // Must be last member, see base/memory/weak_ptr.h documentation
160 base::WeakPtrFactory
<BluetoothDispatcherHost
> weak_ptr_factory_
;
162 DISALLOW_COPY_AND_ASSIGN(BluetoothDispatcherHost
);
165 } // namespace content
167 #endif // CONTENT_BROWSER_BLUETOOTH_BLUETOOTH_DISPATCHER_HOST_H_