Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(968)

Side by Side Diff: content/renderer/bluetooth/bluetooth_dispatcher.h

Issue 1334763002: bluetooth: Subscribe to notifications (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bluetooth-origin
Patch Set: Address jyasskin's comments Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 unnecessary IPC messages we implement
scheib 2015/10/01 22:05:23 Title this block so for readability and so it can
ortuno 2015/10/03 04:03:04 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 a characteristic object gets destroyed BluetoothDispatcher
100 // gets notified by characteristicObjectRemoved. When this happens
101 // a stop request should be queued if the characteristic was subscribed
102 // to notifications.
103
104 // Helper functions for notification requests queue.
scheib 2015/10/01 22:05:23 End in a colon and blank line to show that this ap
ortuno 2015/10/03 04:03:04 Done.
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);
scheib 2015/10/01 22:05:23 ProcessQueuedNotificationRequests -> PopNotificati
ortuno 2015/10/03 04:03:04 Done.
113 // Check if there is more than one request in the queue i.e. if there
scheib 2015/10/01 22:05:23 "Checks if ..." (from guide, see 'descriptive') ht
ortuno 2015/10/03 04:03:03 Done.
114 // are other requests besides the one being processed.
115 bool PendingNotificationRequest(
scheib 2015/10/01 22:05:23 PendingNotificationRequest -> HasUnsentNotificatio
ortuno 2015/10/03 04:03:04 Unset doesn't reflect the state of the notificatio
scheib 2015/10/04 01:35:58 SG
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
scheib 2015/10/01 22:05:23 Returns true if
ortuno 2015/10/03 04:03:03 Done.
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
scheib 2015/10/01 22:05:23 'call these functions'
ortuno 2015/10/03 04:03:04 Done.
136 // is true since there is currently another request being processed.
137 void processStartNotificationsRequest(int request_id);
scheib 2015/10/01 22:05:23 processStartNotificationsRequest -> SendStartNotif
ortuno 2015/10/03 04:03:04 bikeshedding: ResolveOrSendStartNotificationsReque
scheib 2015/10/04 01:35:58 SG
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
184 // Map of characteristic_instance_id to a queue of Notification Requests' IDs.
185 // See note about why we queue notifications above.
scheib 2015/10/01 22:05:23 Refer to note by name (so that it can be text sear
ortuno 2015/10/03 04:03:04 Done.
186 std::map<std::string, std::queue<int>> queued_notification_requests_;
187
118 // Tracks device requests sent to browser to match replies with callbacks. 188 // Tracks device requests sent to browser to match replies with callbacks.
119 // Owns callback objects. 189 // Owns callback objects.
120 IDMap<blink::WebBluetoothRequestDeviceCallbacks, IDMapOwnPointer> 190 IDMap<blink::WebBluetoothRequestDeviceCallbacks, IDMapOwnPointer>
121 pending_requests_; 191 pending_requests_;
122 // Tracks requests to connect to a device. 192 // Tracks requests to connect to a device.
123 // Owns callback objects. 193 // Owns callback objects.
124 IDMap<blink::WebBluetoothConnectGATTCallbacks, IDMapOwnPointer> 194 IDMap<blink::WebBluetoothConnectGATTCallbacks, IDMapOwnPointer>
125 pending_connect_requests_; 195 pending_connect_requests_;
126 // Tracks requests to get a primary service from a device. 196 // Tracks requests to get a primary service from a device.
127 // Owns request objects. 197 // Owns request objects.
128 IDMap<BluetoothPrimaryServiceRequest, IDMapOwnPointer> 198 IDMap<BluetoothPrimaryServiceRequest, IDMapOwnPointer>
129 pending_primary_service_requests_; 199 pending_primary_service_requests_;
130 // Tracks requests to get a characteristic from a service. 200 // Tracks requests to get a characteristic from a service.
131 IDMap<BluetoothCharacteristicRequest, IDMapOwnPointer> 201 IDMap<BluetoothCharacteristicRequest, IDMapOwnPointer>
132 pending_characteristic_requests_; 202 pending_characteristic_requests_;
133 // Tracks requests to read from a characteristics. 203 // Tracks requests to read from a characteristics.
134 IDMap<blink::WebBluetoothReadValueCallbacks, IDMapOwnPointer> 204 IDMap<blink::WebBluetoothReadValueCallbacks, IDMapOwnPointer>
135 pending_read_value_requests_; 205 pending_read_value_requests_;
136 IDMap<blink::WebBluetoothWriteValueCallbacks, IDMapOwnPointer> 206 IDMap<blink::WebBluetoothWriteValueCallbacks, IDMapOwnPointer>
137 pending_write_value_requests_; 207 pending_write_value_requests_;
208 IDMap<BluetoothNotificationsRequest, IDMapOwnPointer>
209 pending_notifications_requests_;
210
211 // Map of characteristic_instance_id to a set of
212 // WebBluetoothGATTCharacteristic pointers. Keeps track of which
213 // objects are subscribed to notifications.
214 std::map<std::string, std::set<blink::WebBluetoothGATTCharacteristic*>>
215 active_notification_subscriptions_;
138 216
139 DISALLOW_COPY_AND_ASSIGN(BluetoothDispatcher); 217 DISALLOW_COPY_AND_ASSIGN(BluetoothDispatcher);
140 }; 218 };
141 219
142 } // namespace content 220 } // namespace content
143 221
144 #endif // CONTENT_CHILD_BLUETOOTH_BLUETOOTH_DISPATCHER_H_ 222 #endif // CONTENT_CHILD_BLUETOOTH_BLUETOOTH_DISPATCHER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698