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 // To avoid races and sending innecessary IPC messages we implement | |
Jeffrey Yasskin
2015/09/25 23:03:20
s/innecessary/unnecessary/
ortuno
2015/09/29 22:47:36
Done.
| |
96 // a queueing system for notification requests. When receiving | |
97 // a notification request, the request is immediately queued. If | |
98 // there are no other pending requests then the request is processed. | |
99 // When an characteristic object gets destroyed BluetoothDispatcher | |
Jeffrey Yasskin
2015/09/25 23:03:20
s/an/a/
ortuno
2015/09/29 22:47:36
Done.
| |
100 // gets notified by characteristicObjectRemoved. When this happens | |
101 // a stop request should be issued if the characterisic was susbscribed | |
Jeffrey Yasskin
2015/09/25 23:03:20
sp: characterisic, susbscribed
ortuno
2015/09/29 22:47:36
Done.
| |
102 // to notifications. | |
103 std::map<std::string, std::queue<int>> queued_notification_requests_; | |
Jeffrey Yasskin
2015/09/25 23:03:20
Move this down to the section of data members. We
Jeffrey Yasskin
2015/09/25 23:03:20
Comment what the std::string and int mean.
ortuno
2015/09/29 22:47:36
Done.
| |
104 // Helper functions for notification requests queue. | |
105 // Creates a notification request and queues it. | |
106 int QueueNotificationRequest( | |
107 const std::string& characteristic_instance_id, | |
108 blink::WebBluetoothGATTCharacteristic* characteristic, | |
109 blink::WebBluetoothNotificationsCallbacks* callbacks, | |
110 NotificationsRequestType type); | |
111 // Pops the last requests and runs the next request in the queue. | |
112 void ProcessQueuedNotificationRequests(int request_id); | |
113 // Check if there is more than one request in the queue i.e. if there | |
114 // are other requests besides the one being processed. | |
115 bool PendingNotificationRequest( | |
116 const std::string& characteristic_instance_id); | |
117 // Check if there are any objects subscribed to the characteristic's | |
118 // notifications. | |
119 bool HasActiveNotificationSubscription( | |
120 const std::string& characteristic_instance_id); | |
121 // Add the object to the set of subscribed objects to a characteristic's | |
122 // notifications. | |
123 void AddToActiveNotificationSubscriptions( | |
124 const std::string& characteristic_instance_id, | |
125 blink::WebBluetoothGATTCharacteristic* characteristic); | |
126 // Remove the object from the set of subscribed object to the | |
127 // characteristic's notifications. Returns if the subscription | |
128 // becomes inactive. | |
129 bool RemoveFromActiveNotificationSubscriptions( | |
130 const std::string& characteristic_instance_id, | |
131 blink::WebBluetoothGATTCharacteristic* characteristic); | |
132 | |
133 // The following functions decide whether to resolve the request immediately | |
134 // or send an IPC to change the subscription state. | |
135 // You should never call this functions if PendingNotificationRequest | |
136 // is true since there is currently another request being processed. | |
137 void processStartNotificationsRequest(int request_id); | |
138 void processStopNotificationsRequest(int request_id); | |
139 | |
77 // IPC Handlers, see definitions in bluetooth_messages.h. | 140 // IPC Handlers, see definitions in bluetooth_messages.h. |
78 void OnRequestDeviceSuccess(int thread_id, | 141 void OnRequestDeviceSuccess(int thread_id, |
79 int request_id, | 142 int request_id, |
80 const BluetoothDevice& device); | 143 const BluetoothDevice& device); |
81 void OnRequestDeviceError(int thread_id, | 144 void OnRequestDeviceError(int thread_id, |
82 int request_id, | 145 int request_id, |
83 blink::WebBluetoothError error); | 146 blink::WebBluetoothError error); |
84 | |
85 void OnConnectGATTSuccess(int thread_id, | 147 void OnConnectGATTSuccess(int thread_id, |
86 int request_id, | 148 int request_id, |
87 const std::string& message); | 149 const std::string& message); |
88 | |
89 void OnConnectGATTError(int thread_id, | 150 void OnConnectGATTError(int thread_id, |
90 int request_id, | 151 int request_id, |
91 blink::WebBluetoothError error); | 152 blink::WebBluetoothError error); |
92 void OnGetPrimaryServiceSuccess(int thread_id, | 153 void OnGetPrimaryServiceSuccess(int thread_id, |
93 int request_id, | 154 int request_id, |
94 const std::string& service_instance_id); | 155 const std::string& service_instance_id); |
95 void OnGetPrimaryServiceError(int thread_id, | 156 void OnGetPrimaryServiceError(int thread_id, |
96 int request_id, | 157 int request_id, |
97 blink::WebBluetoothError error); | 158 blink::WebBluetoothError error); |
98 void OnGetCharacteristicSuccess( | 159 void OnGetCharacteristicSuccess( |
99 int thread_id, | 160 int thread_id, |
100 int request_id, | 161 int request_id, |
101 const std::string& characteristic_instance_id); | 162 const std::string& characteristic_instance_id); |
102 void OnGetCharacteristicError(int thread_id, | 163 void OnGetCharacteristicError(int thread_id, |
103 int request_id, | 164 int request_id, |
104 blink::WebBluetoothError error); | 165 blink::WebBluetoothError error); |
105 void OnReadValueSuccess(int thread_id, | 166 void OnReadValueSuccess(int thread_id, |
106 int request_id, | 167 int request_id, |
107 const std::vector<uint8_t>& value); | 168 const std::vector<uint8_t>& value); |
108 void OnReadValueError(int thread_id, | 169 void OnReadValueError(int thread_id, |
109 int request_id, | 170 int request_id, |
110 blink::WebBluetoothError error); | 171 blink::WebBluetoothError error); |
111 void OnWriteValueSuccess(int thread_id, int request_id); | 172 void OnWriteValueSuccess(int thread_id, int request_id); |
112 void OnWriteValueError(int thread_id, | 173 void OnWriteValueError(int thread_id, |
113 int request_id, | 174 int request_id, |
114 blink::WebBluetoothError error); | 175 blink::WebBluetoothError error); |
176 void OnStartNotificationsSuccess(int thread_id, int request_id); | |
177 void OnStartNotificationsError(int thread_id, | |
178 int request_id, | |
179 blink::WebBluetoothError error); | |
180 void OnStopNotificationsSuccess(int thread_id, int request_id); | |
115 | 181 |
116 scoped_refptr<ThreadSafeSender> thread_safe_sender_; | 182 scoped_refptr<ThreadSafeSender> thread_safe_sender_; |
117 | 183 |
118 // Tracks device requests sent to browser to match replies with callbacks. | 184 // Tracks device requests sent to browser to match replies with callbacks. |
119 // Owns callback objects. | 185 // Owns callback objects. |
120 IDMap<blink::WebBluetoothRequestDeviceCallbacks, IDMapOwnPointer> | 186 IDMap<blink::WebBluetoothRequestDeviceCallbacks, IDMapOwnPointer> |
121 pending_requests_; | 187 pending_requests_; |
122 // Tracks requests to connect to a device. | 188 // Tracks requests to connect to a device. |
123 // Owns callback objects. | 189 // Owns callback objects. |
124 IDMap<blink::WebBluetoothConnectGATTCallbacks, IDMapOwnPointer> | 190 IDMap<blink::WebBluetoothConnectGATTCallbacks, IDMapOwnPointer> |
125 pending_connect_requests_; | 191 pending_connect_requests_; |
126 // Tracks requests to get a primary service from a device. | 192 // Tracks requests to get a primary service from a device. |
127 // Owns request objects. | 193 // Owns request objects. |
128 IDMap<BluetoothPrimaryServiceRequest, IDMapOwnPointer> | 194 IDMap<BluetoothPrimaryServiceRequest, IDMapOwnPointer> |
129 pending_primary_service_requests_; | 195 pending_primary_service_requests_; |
130 // Tracks requests to get a characteristic from a service. | 196 // Tracks requests to get a characteristic from a service. |
131 IDMap<BluetoothCharacteristicRequest, IDMapOwnPointer> | 197 IDMap<BluetoothCharacteristicRequest, IDMapOwnPointer> |
132 pending_characteristic_requests_; | 198 pending_characteristic_requests_; |
133 // Tracks requests to read from a characteristics. | 199 // Tracks requests to read from a characteristics. |
134 IDMap<blink::WebBluetoothReadValueCallbacks, IDMapOwnPointer> | 200 IDMap<blink::WebBluetoothReadValueCallbacks, IDMapOwnPointer> |
135 pending_read_value_requests_; | 201 pending_read_value_requests_; |
136 IDMap<blink::WebBluetoothWriteValueCallbacks, IDMapOwnPointer> | 202 IDMap<blink::WebBluetoothWriteValueCallbacks, IDMapOwnPointer> |
137 pending_write_value_requests_; | 203 pending_write_value_requests_; |
204 IDMap<BluetoothNotificationsRequest, IDMapOwnPointer> | |
205 pending_notifications_requests_; | |
206 | |
207 std::map<std::string, std::set<blink::WebBluetoothGATTCharacteristic*>> | |
Jeffrey Yasskin
2015/09/25 23:03:20
You lost the comment saying what these strings mea
ortuno
2015/09/29 22:47:36
Done.
| |
208 active_notification_subscriptions_; | |
138 | 209 |
139 DISALLOW_COPY_AND_ASSIGN(BluetoothDispatcher); | 210 DISALLOW_COPY_AND_ASSIGN(BluetoothDispatcher); |
140 }; | 211 }; |
141 | 212 |
142 } // namespace content | 213 } // namespace content |
143 | 214 |
144 #endif // CONTENT_CHILD_BLUETOOTH_BLUETOOTH_DISPATCHER_H_ | 215 #endif // CONTENT_CHILD_BLUETOOTH_BLUETOOTH_DISPATCHER_H_ |
OLD | NEW |