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_CHILD_BLUETOOTH_BLUETOOTH_DISPATCHER_H_
6 #define CONTENT_CHILD_BLUETOOTH_BLUETOOTH_DISPATCHER_H_
8 #include "base/id_map.h"
9 #include "base/memory/ref_counted.h"
10 #include "content/common/bluetooth/bluetooth_device.h"
11 #include "content/public/child/worker_thread.h"
12 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetooth.h"
13 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothError.h"
24 struct BluetoothCharacteristicRequest
;
25 struct BluetoothPrimaryServiceRequest
;
28 class ThreadSafeSender
;
30 // Dispatcher for child process threads which communicates to the browser's
31 // BluetoothDispatcherHost.
33 // Instances are created for each thread as necessary by WebBluetoothImpl.
35 // Incoming IPC messages are received by the BluetoothMessageFilter and
36 // directed to the thread specific instance of this class.
37 // Outgoing messages come from WebBluetoothImpl.
38 class BluetoothDispatcher
: public WorkerThread::Observer
{
40 explicit BluetoothDispatcher(ThreadSafeSender
* sender
);
41 ~BluetoothDispatcher() override
;
43 // Gets or Creates a BluetoothDispatcher for the current thread.
44 // |thread_safe_sender| is required when constructing a BluetoothDispatcher.
45 static BluetoothDispatcher
* GetOrCreateThreadSpecificInstance(
46 ThreadSafeSender
* thread_safe_sender
);
48 // IPC Send and Receiving interface, see IPC::Sender and IPC::Listener.
49 bool Send(IPC::Message
* msg
);
50 void OnMessageReceived(const IPC::Message
& msg
);
52 // Corresponding to WebBluetoothImpl methods.
53 void requestDevice(int frame_routing_id
,
54 const blink::WebRequestDeviceOptions
& options
,
55 blink::WebBluetoothRequestDeviceCallbacks
* callbacks
);
56 void connectGATT(const blink::WebString
& device_instance_id
,
57 blink::WebBluetoothConnectGATTCallbacks
* callbacks
);
58 void getPrimaryService(
59 const blink::WebString
& device_instance_id
,
60 const blink::WebString
& service_uuid
,
61 blink::WebBluetoothGetPrimaryServiceCallbacks
* callbacks
);
63 void getCharacteristic(
64 const blink::WebString
& service_instance_id
,
65 const blink::WebString
& characteristic_uuid
,
66 blink::WebBluetoothGetCharacteristicCallbacks
* callbacks
);
67 void readValue(const blink::WebString
& characteristic_instance_id
,
68 blink::WebBluetoothReadValueCallbacks
* callbacks
);
69 void writeValue(const blink::WebString
& characteristic_instance_id
,
70 const std::vector
<uint8_t>& value
,
71 blink::WebBluetoothWriteValueCallbacks
*);
73 // WorkerThread::Observer implementation.
74 void WillStopCurrentWorkerThread() override
;
77 // IPC Handlers, see definitions in bluetooth_messages.h.
78 void OnRequestDeviceSuccess(int thread_id
,
80 const BluetoothDevice
& device
);
81 void OnRequestDeviceError(int thread_id
,
83 blink::WebBluetoothError error
);
85 void OnConnectGATTSuccess(int thread_id
,
87 const std::string
& message
);
89 void OnConnectGATTError(int thread_id
,
91 blink::WebBluetoothError error
);
92 void OnGetPrimaryServiceSuccess(int thread_id
,
94 const std::string
& service_instance_id
);
95 void OnGetPrimaryServiceError(int thread_id
,
97 blink::WebBluetoothError error
);
98 void OnGetCharacteristicSuccess(
101 const std::string
& characteristic_instance_id
);
102 void OnGetCharacteristicError(int thread_id
,
104 blink::WebBluetoothError error
);
105 void OnReadValueSuccess(int thread_id
,
107 const std::vector
<uint8_t>& value
);
108 void OnReadValueError(int thread_id
,
110 blink::WebBluetoothError error
);
111 void OnWriteValueSuccess(int thread_id
, int request_id
);
112 void OnWriteValueError(int thread_id
,
114 blink::WebBluetoothError error
);
116 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
118 // Tracks device requests sent to browser to match replies with callbacks.
119 // Owns callback objects.
120 IDMap
<blink::WebBluetoothRequestDeviceCallbacks
, IDMapOwnPointer
>
122 // Tracks requests to connect to a device.
123 // Owns callback objects.
124 IDMap
<blink::WebBluetoothConnectGATTCallbacks
, IDMapOwnPointer
>
125 pending_connect_requests_
;
126 // Tracks requests to get a primary service from a device.
127 // Owns request objects.
128 IDMap
<BluetoothPrimaryServiceRequest
, IDMapOwnPointer
>
129 pending_primary_service_requests_
;
130 // Tracks requests to get a characteristic from a service.
131 IDMap
<BluetoothCharacteristicRequest
, IDMapOwnPointer
>
132 pending_characteristic_requests_
;
133 // Tracks requests to read from a characteristics.
134 IDMap
<blink::WebBluetoothReadValueCallbacks
, IDMapOwnPointer
>
135 pending_read_value_requests_
;
136 IDMap
<blink::WebBluetoothWriteValueCallbacks
, IDMapOwnPointer
>
137 pending_write_value_requests_
;
139 DISALLOW_COPY_AND_ASSIGN(BluetoothDispatcher
);
142 } // namespace content
144 #endif // CONTENT_CHILD_BLUETOOTH_BLUETOOTH_DISPATCHER_H_