| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_BROWSER_BLUETOOTH_FRAME_CONNECTED_BLUETOOTH_DEVICES_H_ | 5 #ifndef CONTENT_BROWSER_BLUETOOTH_FRAME_CONNECTED_BLUETOOTH_DEVICES_H_ |
| 6 #define CONTENT_BROWSER_BLUETOOTH_FRAME_CONNECTED_BLUETOOTH_DEVICES_H_ | 6 #define CONTENT_BROWSER_BLUETOOTH_FRAME_CONNECTED_BLUETOOTH_DEVICES_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <unordered_map> | 10 #include <unordered_map> |
| 11 | 11 |
| 12 #include "base/optional.h" |
| 13 #include "components/web_bluetooth/web_bluetooth_device_id.h" |
| 12 #include "content/common/content_export.h" | 14 #include "content/common/content_export.h" |
| 13 #include "url/origin.h" | 15 #include "url/origin.h" |
| 14 | 16 |
| 15 namespace device { | 17 namespace device { |
| 16 class BluetoothGattConnection; | 18 class BluetoothGattConnection; |
| 17 } // namespace device | 19 } // namespace device |
| 18 | 20 |
| 19 namespace content { | 21 namespace content { |
| 20 | 22 |
| 21 class RenderFrameHost; | 23 class RenderFrameHost; |
| 22 class WebContentsImpl; | 24 class WebContentsImpl; |
| 23 | 25 |
| 24 // Holds information about connected devices and updates the WebContents | 26 // Holds information about connected devices and updates the WebContents |
| 25 // when new connections are made or connections closed. WebContents must | 27 // when new connections are made or connections closed. WebContents must |
| 26 // outlive this class. | 28 // outlive this class. |
| 27 // This class does not keep track of the status of the connections. Owners of | 29 // This class does not keep track of the status of the connections. Owners of |
| 28 // this class should inform it when a connection is terminated so that the | 30 // this class should inform it when a connection is terminated so that the |
| 29 // connection is removed from the appropriate maps. | 31 // connection is removed from the appropriate maps. |
| 30 class CONTENT_EXPORT FrameConnectedBluetoothDevices final { | 32 class CONTENT_EXPORT FrameConnectedBluetoothDevices final { |
| 31 public: | 33 public: |
| 32 // |rfh| should be the RenderFrameHost that owns the WebBluetoothServiceImpl | 34 // |rfh| should be the RenderFrameHost that owns the WebBluetoothServiceImpl |
| 33 // that owns this map. | 35 // that owns this map. |
| 34 explicit FrameConnectedBluetoothDevices(RenderFrameHost* rfh); | 36 explicit FrameConnectedBluetoothDevices(RenderFrameHost* rfh); |
| 35 ~FrameConnectedBluetoothDevices(); | 37 ~FrameConnectedBluetoothDevices(); |
| 36 | 38 |
| 37 // Returns true if the map holds a connection to |device_id|. | 39 // Returns true if the map holds a connection to |device_id|. |
| 38 bool IsConnectedToDeviceWithId(const std::string& device_id); | 40 bool IsConnectedToDeviceWithId( |
| 41 const web_bluetooth::WebBluetoothDeviceId& device_id); |
| 39 | 42 |
| 40 // If a connection doesn't exist already for |device_id|, adds a connection to | 43 // If a connection doesn't exist already for |device_id|, adds a connection to |
| 41 // the map and increases the WebContents count of connected devices. | 44 // the map and increases the WebContents count of connected devices. |
| 42 void Insert(const std::string& device_id, | 45 void Insert(const web_bluetooth::WebBluetoothDeviceId& device_id, |
| 43 std::unique_ptr<device::BluetoothGattConnection> connection); | 46 std::unique_ptr<device::BluetoothGattConnection> connection); |
| 44 | 47 |
| 45 // Deletes the BluetoothGattConnection for |device_id| and decrements the | 48 // Deletes the BluetoothGattConnection for |device_id| and decrements the |
| 46 // WebContents count of connected devices if |device_id| had a connection. | 49 // WebContents count of connected devices if |device_id| had a connection. |
| 47 void CloseConnectionToDeviceWithId(const std::string& device_id); | 50 void CloseConnectionToDeviceWithId( |
| 51 const web_bluetooth::WebBluetoothDeviceId& device_id); |
| 48 | 52 |
| 49 // Deletes the BluetoothGattConnection for |device_address| and decrements the | 53 // Deletes the BluetoothGattConnection for |device_address| and decrements the |
| 50 // WebContents count of connected devices if |device_address| had a | 54 // WebContents count of connected devices if |device_address| had a |
| 51 // connection. Returns the device_id of the device associated with the | 55 // connection. Returns the device_id of the device associated with the |
| 52 // connection. | 56 // connection. |
| 53 std::string CloseConnectionToDeviceWithAddress( | 57 base::Optional<web_bluetooth::WebBluetoothDeviceId> |
| 54 const std::string& device_address); | 58 CloseConnectionToDeviceWithAddress(const std::string& device_address); |
| 55 | 59 |
| 56 private: | 60 private: |
| 57 // Increments the Connected Devices count of the frame's WebContents. | 61 // Increments the Connected Devices count of the frame's WebContents. |
| 58 void IncrementDevicesConnectedCount(); | 62 void IncrementDevicesConnectedCount(); |
| 59 // Decrements the Connected Devices count of the frame's WebContents. | 63 // Decrements the Connected Devices count of the frame's WebContents. |
| 60 void DecrementDevicesConnectedCount(); | 64 void DecrementDevicesConnectedCount(); |
| 61 | 65 |
| 62 // WebContentsImpl that owns the WebBluetoothServiceImpl that owns this map. | 66 // WebContentsImpl that owns the WebBluetoothServiceImpl that owns this map. |
| 63 WebContentsImpl* web_contents_impl_; | 67 WebContentsImpl* web_contents_impl_; |
| 64 | 68 |
| 65 // Keeps the BluetoothGattConnection objects alive so that connections don't | 69 // Keeps the BluetoothGattConnection objects alive so that connections don't |
| 66 // get closed. | 70 // get closed. |
| 67 std::unordered_map<std::string, | 71 std::unordered_map<web_bluetooth::WebBluetoothDeviceId, |
| 68 std::unique_ptr<device::BluetoothGattConnection>> | 72 std::unique_ptr<device::BluetoothGattConnection>, |
| 73 web_bluetooth::WebBluetoothDeviceIdHash> |
| 69 device_id_to_connection_map_; | 74 device_id_to_connection_map_; |
| 70 | 75 |
| 71 // Keeps track of which device addresses correspond to which ids. | 76 // Keeps track of which device addresses correspond to which ids. |
| 72 std::unordered_map<std::string, std::string> device_address_to_id_map_; | 77 std::unordered_map<std::string, web_bluetooth::WebBluetoothDeviceId> |
| 78 device_address_to_id_map_; |
| 73 | 79 |
| 74 DISALLOW_COPY_AND_ASSIGN(FrameConnectedBluetoothDevices); | 80 DISALLOW_COPY_AND_ASSIGN(FrameConnectedBluetoothDevices); |
| 75 }; | 81 }; |
| 76 | 82 |
| 77 } // namespace content | 83 } // namespace content |
| 78 | 84 |
| 79 #endif // CONTENT_BROWSER_BLUETOOTH_FRAME_CONNECTED_BLUETOOTH_DEVICES_H_ | 85 #endif // CONTENT_BROWSER_BLUETOOTH_FRAME_CONNECTED_BLUETOOTH_DEVICES_H_ |
| OLD | NEW |