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