Index: content/renderer/bluetooth/bluetooth_dispatcher.h |
diff --git a/content/renderer/bluetooth/bluetooth_dispatcher.h b/content/renderer/bluetooth/bluetooth_dispatcher.h |
index 4109414400c2bd31b62e9309db61f11cbd7e9227..534383d1d4cf849fdbcdf7fa95c1dd67b3c77512 100644 |
--- a/content/renderer/bluetooth/bluetooth_dispatcher.h |
+++ b/content/renderer/bluetooth/bluetooth_dispatcher.h |
@@ -5,6 +5,8 @@ |
#ifndef CONTENT_CHILD_BLUETOOTH_BLUETOOTH_DISPATCHER_H_ |
#define CONTENT_CHILD_BLUETOOTH_BLUETOOTH_DISPATCHER_H_ |
+#include <queue> |
+ |
#include "base/id_map.h" |
#include "base/memory/ref_counted.h" |
#include "content/common/bluetooth/bluetooth_device.h" |
@@ -17,12 +19,17 @@ class MessageLoop; |
class TaskRunner; |
} |
+namespace blink { |
+class WebBluetoothGATTCharacteristic; |
+} |
+ |
namespace IPC { |
class Message; |
} |
struct BluetoothCharacteristicRequest; |
struct BluetoothPrimaryServiceRequest; |
+struct BluetoothNotificationsRequest; |
namespace content { |
class ThreadSafeSender; |
@@ -69,11 +76,67 @@ class BluetoothDispatcher : public WorkerThread::Observer { |
void writeValue(const blink::WebString& characteristic_instance_id, |
const std::vector<uint8_t>& value, |
blink::WebBluetoothWriteValueCallbacks*); |
+ void startNotifications(const blink::WebString& characteristic_instance_id, |
+ blink::WebBluetoothGATTCharacteristic* delegate, |
+ blink::WebBluetoothNotificationsCallbacks*); |
+ void stopNotifications(const blink::WebString& characteristic_instance_id, |
+ blink::WebBluetoothGATTCharacteristic* delegate, |
+ blink::WebBluetoothNotificationsCallbacks*); |
+ void characteristicObjectRemoved( |
+ const blink::WebString& characteristic_instance_id, |
+ blink::WebBluetoothGATTCharacteristic* delegate); |
// WorkerThread::Observer implementation. |
void WillStopCurrentWorkerThread() override; |
+ enum class NotificationsRequestType { START = 0, STOP = 1 }; |
+ |
private: |
+ // 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.
|
+ // a queueing system for notification requests. When receiving |
+ // a notification request, the request is immediately queued. If |
+ // there are no other pending requests then the request is processed. |
+ // When a characteristic object gets destroyed BluetoothDispatcher |
+ // gets notified by characteristicObjectRemoved. When this happens |
+ // a stop request should be queued if the characteristic was subscribed |
+ // to notifications. |
+ |
+ // 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.
|
+ // Creates a notification request and queues it. |
+ int QueueNotificationRequest( |
+ const std::string& characteristic_instance_id, |
+ blink::WebBluetoothGATTCharacteristic* characteristic, |
+ blink::WebBluetoothNotificationsCallbacks* callbacks, |
+ NotificationsRequestType type); |
+ // Pops the last requests and runs the next request in the queue. |
+ void ProcessQueuedNotificationRequests(int request_id); |
scheib
2015/10/01 22:05:23
ProcessQueuedNotificationRequests -> PopNotificati
ortuno
2015/10/03 04:03:04
Done.
|
+ // 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.
|
+ // are other requests besides the one being processed. |
+ 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
|
+ const std::string& characteristic_instance_id); |
+ // Check if there are any objects subscribed to the characteristic's |
+ // notifications. |
+ bool HasActiveNotificationSubscription( |
+ const std::string& characteristic_instance_id); |
+ // Add the object to the set of subscribed objects to a characteristic's |
+ // notifications. |
+ void AddToActiveNotificationSubscriptions( |
+ const std::string& characteristic_instance_id, |
+ blink::WebBluetoothGATTCharacteristic* characteristic); |
+ // Remove the object from the set of subscribed object to the |
+ // 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.
|
+ // becomes inactive. |
+ bool RemoveFromActiveNotificationSubscriptions( |
+ const std::string& characteristic_instance_id, |
+ blink::WebBluetoothGATTCharacteristic* characteristic); |
+ |
+ // The following functions decide whether to resolve the request immediately |
+ // or send an IPC to change the subscription state. |
+ // 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.
|
+ // is true since there is currently another request being processed. |
+ 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
|
+ void processStopNotificationsRequest(int request_id); |
+ |
// IPC Handlers, see definitions in bluetooth_messages.h. |
void OnRequestDeviceSuccess(int thread_id, |
int request_id, |
@@ -81,11 +144,9 @@ class BluetoothDispatcher : public WorkerThread::Observer { |
void OnRequestDeviceError(int thread_id, |
int request_id, |
blink::WebBluetoothError error); |
- |
void OnConnectGATTSuccess(int thread_id, |
int request_id, |
const std::string& message); |
- |
void OnConnectGATTError(int thread_id, |
int request_id, |
blink::WebBluetoothError error); |
@@ -112,9 +173,18 @@ class BluetoothDispatcher : public WorkerThread::Observer { |
void OnWriteValueError(int thread_id, |
int request_id, |
blink::WebBluetoothError error); |
+ void OnStartNotificationsSuccess(int thread_id, int request_id); |
+ void OnStartNotificationsError(int thread_id, |
+ int request_id, |
+ blink::WebBluetoothError error); |
+ void OnStopNotificationsSuccess(int thread_id, int request_id); |
scoped_refptr<ThreadSafeSender> thread_safe_sender_; |
+ // Map of characteristic_instance_id to a queue of Notification Requests' IDs. |
+ // 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.
|
+ std::map<std::string, std::queue<int>> queued_notification_requests_; |
+ |
// Tracks device requests sent to browser to match replies with callbacks. |
// Owns callback objects. |
IDMap<blink::WebBluetoothRequestDeviceCallbacks, IDMapOwnPointer> |
@@ -135,6 +205,14 @@ class BluetoothDispatcher : public WorkerThread::Observer { |
pending_read_value_requests_; |
IDMap<blink::WebBluetoothWriteValueCallbacks, IDMapOwnPointer> |
pending_write_value_requests_; |
+ IDMap<BluetoothNotificationsRequest, IDMapOwnPointer> |
+ pending_notifications_requests_; |
+ |
+ // Map of characteristic_instance_id to a set of |
+ // WebBluetoothGATTCharacteristic pointers. Keeps track of which |
+ // objects are subscribed to notifications. |
+ std::map<std::string, std::set<blink::WebBluetoothGATTCharacteristic*>> |
+ active_notification_subscriptions_; |
DISALLOW_COPY_AND_ASSIGN(BluetoothDispatcher); |
}; |