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 #ifndef CONTENT_CHILD_BLUETOOTH_BLUETOOTH_DISPATCHER_H_ | 5 #ifndef CONTENT_CHILD_BLUETOOTH_BLUETOOTH_DISPATCHER_H_ |
6 #define CONTENT_CHILD_BLUETOOTH_BLUETOOTH_DISPATCHER_H_ | 6 #define CONTENT_CHILD_BLUETOOTH_BLUETOOTH_DISPATCHER_H_ |
7 | 7 |
| 8 #include <map> |
| 9 #include <queue> |
| 10 |
8 #include "base/id_map.h" | 11 #include "base/id_map.h" |
9 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
10 #include "content/common/bluetooth/bluetooth_device.h" | 13 #include "content/common/bluetooth/bluetooth_device.h" |
11 #include "content/public/child/worker_thread.h" | 14 #include "content/public/child/worker_thread.h" |
12 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetooth.h" | 15 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetooth.h" |
13 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothError
.h" | 16 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothError
.h" |
14 | 17 |
15 namespace base { | 18 namespace base { |
16 class MessageLoop; | 19 class MessageLoop; |
17 class TaskRunner; | 20 class TaskRunner; |
18 } | 21 } |
19 | 22 |
| 23 namespace blink { |
| 24 class WebBluetoothGATTCharacteristic; |
| 25 } |
| 26 |
20 namespace IPC { | 27 namespace IPC { |
21 class Message; | 28 class Message; |
22 } | 29 } |
23 | 30 |
24 struct BluetoothCharacteristicRequest; | 31 struct BluetoothCharacteristicRequest; |
25 struct BluetoothPrimaryServiceRequest; | 32 struct BluetoothPrimaryServiceRequest; |
| 33 struct BluetoothNotificationsRequest; |
26 | 34 |
27 namespace content { | 35 namespace content { |
28 class ThreadSafeSender; | 36 class ThreadSafeSender; |
29 | 37 |
30 // Dispatcher for child process threads which communicates to the browser's | 38 // Dispatcher for child process threads which communicates to the browser's |
31 // BluetoothDispatcherHost. | 39 // BluetoothDispatcherHost. |
32 // | 40 // |
33 // Instances are created for each thread as necessary by WebBluetoothImpl. | 41 // Instances are created for each thread as necessary by WebBluetoothImpl. |
34 // | 42 // |
35 // Incoming IPC messages are received by the BluetoothMessageFilter and | 43 // Incoming IPC messages are received by the BluetoothMessageFilter and |
(...skipping 26 matching lines...) Expand all Loading... |
62 | 70 |
63 void getCharacteristic( | 71 void getCharacteristic( |
64 const blink::WebString& service_instance_id, | 72 const blink::WebString& service_instance_id, |
65 const blink::WebString& characteristic_uuid, | 73 const blink::WebString& characteristic_uuid, |
66 blink::WebBluetoothGetCharacteristicCallbacks* callbacks); | 74 blink::WebBluetoothGetCharacteristicCallbacks* callbacks); |
67 void readValue(const blink::WebString& characteristic_instance_id, | 75 void readValue(const blink::WebString& characteristic_instance_id, |
68 blink::WebBluetoothReadValueCallbacks* callbacks); | 76 blink::WebBluetoothReadValueCallbacks* callbacks); |
69 void writeValue(const blink::WebString& characteristic_instance_id, | 77 void writeValue(const blink::WebString& characteristic_instance_id, |
70 const std::vector<uint8_t>& value, | 78 const std::vector<uint8_t>& value, |
71 blink::WebBluetoothWriteValueCallbacks*); | 79 blink::WebBluetoothWriteValueCallbacks*); |
| 80 void startNotifications(const blink::WebString& characteristic_instance_id, |
| 81 blink::WebBluetoothGATTCharacteristic* delegate, |
| 82 blink::WebBluetoothNotificationsCallbacks*); |
| 83 void stopNotifications(const blink::WebString& characteristic_instance_id, |
| 84 blink::WebBluetoothGATTCharacteristic* delegate, |
| 85 blink::WebBluetoothNotificationsCallbacks*); |
| 86 void characteristicObjectRemoved( |
| 87 const blink::WebString& characteristic_instance_id, |
| 88 blink::WebBluetoothGATTCharacteristic* delegate); |
72 | 89 |
73 // WorkerThread::Observer implementation. | 90 // WorkerThread::Observer implementation. |
74 void WillStopCurrentWorkerThread() override; | 91 void WillStopCurrentWorkerThread() override; |
75 | 92 |
| 93 enum class NotificationsRequestType { START = 0, STOP = 1 }; |
| 94 |
76 private: | 95 private: |
| 96 // Notifications Queueing Notes: |
| 97 // To avoid races and sending unnecessary IPC messages we implement |
| 98 // a queueing system for notification requests. When receiving |
| 99 // a notification request, the request is immediately queued. If |
| 100 // there are no other pending requests then the request is processed. |
| 101 // When a characteristic object gets destroyed BluetoothDispatcher |
| 102 // gets notified by characteristicObjectRemoved. When this happens |
| 103 // a stop request should be queued if the characteristic was subscribed |
| 104 // to notifications. |
| 105 |
| 106 // Helper functions for notification requests queue: |
| 107 |
| 108 // Creates a notification request and queues it. |
| 109 int QueueNotificationRequest( |
| 110 const std::string& characteristic_instance_id, |
| 111 blink::WebBluetoothGATTCharacteristic* characteristic, |
| 112 blink::WebBluetoothNotificationsCallbacks* callbacks, |
| 113 NotificationsRequestType type); |
| 114 // Pops the last requests and runs the next request in the queue. |
| 115 void PopNotificationRequestQueueAndProcessNext(int request_id); |
| 116 // Checks if there is more than one request in the queue i.e. if there |
| 117 // are other requests besides the one being processed. |
| 118 bool HasNotificationRequestResponsePending( |
| 119 const std::string& characteristic_instance_id); |
| 120 // Checks if there are any objects subscribed to the characteristic's |
| 121 // notifications. |
| 122 bool HasActiveNotificationSubscription( |
| 123 const std::string& characteristic_instance_id); |
| 124 // Adds the object to the set of subscribed objects to a characteristic's |
| 125 // notifications. |
| 126 void AddToActiveNotificationSubscriptions( |
| 127 const std::string& characteristic_instance_id, |
| 128 blink::WebBluetoothGATTCharacteristic* characteristic); |
| 129 // Removes the object from the set of subscribed object to the |
| 130 // characteristic's notifications. Returns true if the subscription |
| 131 // becomes inactive. |
| 132 bool RemoveFromActiveNotificationSubscriptions( |
| 133 const std::string& characteristic_instance_id, |
| 134 blink::WebBluetoothGATTCharacteristic* characteristic); |
| 135 |
| 136 // The following functions decide whether to resolve the request immediately |
| 137 // or send an IPC to change the subscription state. |
| 138 // You should never call these functions if PendingNotificationRequest |
| 139 // is true since there is currently another request being processed. |
| 140 void ResolveOrSendStartNotificationRequest(int request_id); |
| 141 void ResolveOrSendStopNotificationsRequest(int request_id); |
| 142 |
77 // IPC Handlers, see definitions in bluetooth_messages.h. | 143 // IPC Handlers, see definitions in bluetooth_messages.h. |
78 void OnRequestDeviceSuccess(int thread_id, | 144 void OnRequestDeviceSuccess(int thread_id, |
79 int request_id, | 145 int request_id, |
80 const BluetoothDevice& device); | 146 const BluetoothDevice& device); |
81 void OnRequestDeviceError(int thread_id, | 147 void OnRequestDeviceError(int thread_id, |
82 int request_id, | 148 int request_id, |
83 blink::WebBluetoothError error); | 149 blink::WebBluetoothError error); |
84 | |
85 void OnConnectGATTSuccess(int thread_id, | 150 void OnConnectGATTSuccess(int thread_id, |
86 int request_id, | 151 int request_id, |
87 const std::string& message); | 152 const std::string& message); |
88 | |
89 void OnConnectGATTError(int thread_id, | 153 void OnConnectGATTError(int thread_id, |
90 int request_id, | 154 int request_id, |
91 blink::WebBluetoothError error); | 155 blink::WebBluetoothError error); |
92 void OnGetPrimaryServiceSuccess(int thread_id, | 156 void OnGetPrimaryServiceSuccess(int thread_id, |
93 int request_id, | 157 int request_id, |
94 const std::string& service_instance_id); | 158 const std::string& service_instance_id); |
95 void OnGetPrimaryServiceError(int thread_id, | 159 void OnGetPrimaryServiceError(int thread_id, |
96 int request_id, | 160 int request_id, |
97 blink::WebBluetoothError error); | 161 blink::WebBluetoothError error); |
98 void OnGetCharacteristicSuccess( | 162 void OnGetCharacteristicSuccess( |
99 int thread_id, | 163 int thread_id, |
100 int request_id, | 164 int request_id, |
101 const std::string& characteristic_instance_id); | 165 const std::string& characteristic_instance_id); |
102 void OnGetCharacteristicError(int thread_id, | 166 void OnGetCharacteristicError(int thread_id, |
103 int request_id, | 167 int request_id, |
104 blink::WebBluetoothError error); | 168 blink::WebBluetoothError error); |
105 void OnReadValueSuccess(int thread_id, | 169 void OnReadValueSuccess(int thread_id, |
106 int request_id, | 170 int request_id, |
107 const std::vector<uint8_t>& value); | 171 const std::vector<uint8_t>& value); |
108 void OnReadValueError(int thread_id, | 172 void OnReadValueError(int thread_id, |
109 int request_id, | 173 int request_id, |
110 blink::WebBluetoothError error); | 174 blink::WebBluetoothError error); |
111 void OnWriteValueSuccess(int thread_id, int request_id); | 175 void OnWriteValueSuccess(int thread_id, int request_id); |
112 void OnWriteValueError(int thread_id, | 176 void OnWriteValueError(int thread_id, |
113 int request_id, | 177 int request_id, |
114 blink::WebBluetoothError error); | 178 blink::WebBluetoothError error); |
| 179 void OnStartNotificationsSuccess(int thread_id, int request_id); |
| 180 void OnStartNotificationsError(int thread_id, |
| 181 int request_id, |
| 182 blink::WebBluetoothError error); |
| 183 void OnStopNotificationsSuccess(int thread_id, int request_id); |
115 | 184 |
116 scoped_refptr<ThreadSafeSender> thread_safe_sender_; | 185 scoped_refptr<ThreadSafeSender> thread_safe_sender_; |
117 | 186 |
| 187 // Map of characteristic_instance_id to a queue of Notification Requests' IDs. |
| 188 // See "Notifications Queueing Note" above. |
| 189 std::map<std::string, std::queue<int>> notification_requests_queues_; |
| 190 |
118 // Tracks device requests sent to browser to match replies with callbacks. | 191 // Tracks device requests sent to browser to match replies with callbacks. |
119 // Owns callback objects. | 192 // Owns callback objects. |
120 IDMap<blink::WebBluetoothRequestDeviceCallbacks, IDMapOwnPointer> | 193 IDMap<blink::WebBluetoothRequestDeviceCallbacks, IDMapOwnPointer> |
121 pending_requests_; | 194 pending_requests_; |
122 // Tracks requests to connect to a device. | 195 // Tracks requests to connect to a device. |
123 // Owns callback objects. | 196 // Owns callback objects. |
124 IDMap<blink::WebBluetoothConnectGATTCallbacks, IDMapOwnPointer> | 197 IDMap<blink::WebBluetoothConnectGATTCallbacks, IDMapOwnPointer> |
125 pending_connect_requests_; | 198 pending_connect_requests_; |
126 // Tracks requests to get a primary service from a device. | 199 // Tracks requests to get a primary service from a device. |
127 // Owns request objects. | 200 // Owns request objects. |
128 IDMap<BluetoothPrimaryServiceRequest, IDMapOwnPointer> | 201 IDMap<BluetoothPrimaryServiceRequest, IDMapOwnPointer> |
129 pending_primary_service_requests_; | 202 pending_primary_service_requests_; |
130 // Tracks requests to get a characteristic from a service. | 203 // Tracks requests to get a characteristic from a service. |
131 IDMap<BluetoothCharacteristicRequest, IDMapOwnPointer> | 204 IDMap<BluetoothCharacteristicRequest, IDMapOwnPointer> |
132 pending_characteristic_requests_; | 205 pending_characteristic_requests_; |
133 // Tracks requests to read from a characteristics. | 206 // Tracks requests to read from a characteristics. |
134 IDMap<blink::WebBluetoothReadValueCallbacks, IDMapOwnPointer> | 207 IDMap<blink::WebBluetoothReadValueCallbacks, IDMapOwnPointer> |
135 pending_read_value_requests_; | 208 pending_read_value_requests_; |
136 IDMap<blink::WebBluetoothWriteValueCallbacks, IDMapOwnPointer> | 209 IDMap<blink::WebBluetoothWriteValueCallbacks, IDMapOwnPointer> |
137 pending_write_value_requests_; | 210 pending_write_value_requests_; |
| 211 IDMap<BluetoothNotificationsRequest, IDMapOwnPointer> |
| 212 pending_notifications_requests_; |
| 213 |
| 214 // Map of characteristic_instance_id to a set of |
| 215 // WebBluetoothGATTCharacteristic pointers. Keeps track of which |
| 216 // objects are subscribed to notifications. |
| 217 std::map<std::string, std::set<blink::WebBluetoothGATTCharacteristic*>> |
| 218 active_notification_subscriptions_; |
138 | 219 |
139 DISALLOW_COPY_AND_ASSIGN(BluetoothDispatcher); | 220 DISALLOW_COPY_AND_ASSIGN(BluetoothDispatcher); |
140 }; | 221 }; |
141 | 222 |
142 } // namespace content | 223 } // namespace content |
143 | 224 |
144 #endif // CONTENT_CHILD_BLUETOOTH_BLUETOOTH_DISPATCHER_H_ | 225 #endif // CONTENT_CHILD_BLUETOOTH_BLUETOOTH_DISPATCHER_H_ |
OLD | NEW |