| 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 #include "content/browser/bluetooth/frame_connected_bluetooth_devices.h" | 5 #include "content/browser/bluetooth/frame_connected_bluetooth_devices.h" |
| 6 | 6 |
| 7 #include "base/optional.h" |
| 7 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 8 #include "content/browser/web_contents/web_contents_impl.h" | 9 #include "content/browser/web_contents/web_contents_impl.h" |
| 9 #include "content/public/browser/web_contents.h" | 10 #include "content/public/browser/web_contents.h" |
| 10 #include "device/bluetooth/bluetooth_gatt_connection.h" | 11 #include "device/bluetooth/bluetooth_gatt_connection.h" |
| 11 | 12 |
| 13 using web_bluetooth::WebBluetoothDeviceId; |
| 14 |
| 12 namespace content { | 15 namespace content { |
| 13 | 16 |
| 14 FrameConnectedBluetoothDevices::FrameConnectedBluetoothDevices( | 17 FrameConnectedBluetoothDevices::FrameConnectedBluetoothDevices( |
| 15 RenderFrameHost* rfh) | 18 RenderFrameHost* rfh) |
| 16 : web_contents_impl_(static_cast<WebContentsImpl*>( | 19 : web_contents_impl_(static_cast<WebContentsImpl*>( |
| 17 WebContents::FromRenderFrameHost(rfh))) {} | 20 WebContents::FromRenderFrameHost(rfh))) {} |
| 18 | 21 |
| 19 FrameConnectedBluetoothDevices::~FrameConnectedBluetoothDevices() { | 22 FrameConnectedBluetoothDevices::~FrameConnectedBluetoothDevices() { |
| 20 for (size_t i = 0; i < device_id_to_connection_map_.size(); i++) { | 23 for (size_t i = 0; i < device_id_to_connection_map_.size(); i++) { |
| 21 DecrementDevicesConnectedCount(); | 24 DecrementDevicesConnectedCount(); |
| 22 } | 25 } |
| 23 } | 26 } |
| 24 | 27 |
| 25 bool FrameConnectedBluetoothDevices::IsConnectedToDeviceWithId( | 28 bool FrameConnectedBluetoothDevices::IsConnectedToDeviceWithId( |
| 26 const std::string& device_id) { | 29 const WebBluetoothDeviceId& device_id) { |
| 27 auto connection_iter = device_id_to_connection_map_.find(device_id); | 30 auto connection_iter = device_id_to_connection_map_.find(device_id); |
| 28 if (connection_iter == device_id_to_connection_map_.end()) { | 31 if (connection_iter == device_id_to_connection_map_.end()) { |
| 29 return false; | 32 return false; |
| 30 } | 33 } |
| 31 // Owners of FrameConnectedBluetoothDevices should notify it when a device | 34 // Owners of FrameConnectedBluetoothDevices should notify it when a device |
| 32 // disconnects but currently Android and Mac don't notify of disconnection, | 35 // disconnects but currently Android and Mac don't notify of disconnection, |
| 33 // so the map could get into a state where it's holding a stale connection. | 36 // so the map could get into a state where it's holding a stale connection. |
| 34 // For this reason we return the value of IsConnected for the connection. | 37 // For this reason we return the value of IsConnected for the connection. |
| 35 // TODO(ortuno): Always return true once Android and Mac notify of | 38 // TODO(ortuno): Always return true once Android and Mac notify of |
| 36 // disconnection. | 39 // disconnection. |
| 37 // http://crbug.com/607273 | 40 // http://crbug.com/607273 |
| 38 return connection_iter->second->IsConnected(); | 41 return connection_iter->second->IsConnected(); |
| 39 } | 42 } |
| 40 | 43 |
| 41 void FrameConnectedBluetoothDevices::Insert( | 44 void FrameConnectedBluetoothDevices::Insert( |
| 42 const std::string& device_id, | 45 const WebBluetoothDeviceId& device_id, |
| 43 std::unique_ptr<device::BluetoothGattConnection> connection) { | 46 std::unique_ptr<device::BluetoothGattConnection> connection) { |
| 44 auto connection_iter = device_id_to_connection_map_.find(device_id); | 47 auto connection_iter = device_id_to_connection_map_.find(device_id); |
| 45 if (connection_iter != device_id_to_connection_map_.end()) { | 48 if (connection_iter != device_id_to_connection_map_.end()) { |
| 46 // Owners of FrameConnectedBluetoothDevices should notify it when a device | 49 // Owners of FrameConnectedBluetoothDevices should notify it when a device |
| 47 // disconnects but currently Android and Mac don't notify of disconnection, | 50 // disconnects but currently Android and Mac don't notify of disconnection, |
| 48 // so the map could get into a state where it's holding a stale connection. | 51 // so the map could get into a state where it's holding a stale connection. |
| 49 // For this reason we check if the current connection is active and if | 52 // For this reason we check if the current connection is active and if |
| 50 // not we remove it. | 53 // not we remove it. |
| 51 // TODO(ortuno): Remove once Android and Mac notify of disconnection. | 54 // TODO(ortuno): Remove once Android and Mac notify of disconnection. |
| 52 // http://crbug.com/607273 | 55 // http://crbug.com/607273 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 67 // http://crbug.com/583544 | 70 // http://crbug.com/583544 |
| 68 return; | 71 return; |
| 69 } | 72 } |
| 70 } | 73 } |
| 71 device_address_to_id_map_[connection->GetDeviceAddress()] = device_id; | 74 device_address_to_id_map_[connection->GetDeviceAddress()] = device_id; |
| 72 device_id_to_connection_map_[device_id] = std::move(connection); | 75 device_id_to_connection_map_[device_id] = std::move(connection); |
| 73 IncrementDevicesConnectedCount(); | 76 IncrementDevicesConnectedCount(); |
| 74 } | 77 } |
| 75 | 78 |
| 76 void FrameConnectedBluetoothDevices::CloseConnectionToDeviceWithId( | 79 void FrameConnectedBluetoothDevices::CloseConnectionToDeviceWithId( |
| 77 const std::string& device_id) { | 80 const WebBluetoothDeviceId& device_id) { |
| 78 auto connection_iter = device_id_to_connection_map_.find(device_id); | 81 auto connection_iter = device_id_to_connection_map_.find(device_id); |
| 79 if (connection_iter == device_id_to_connection_map_.end()) { | 82 if (connection_iter == device_id_to_connection_map_.end()) { |
| 80 return; | 83 return; |
| 81 } | 84 } |
| 82 CHECK(device_address_to_id_map_.erase( | 85 CHECK(device_address_to_id_map_.erase( |
| 83 connection_iter->second->GetDeviceAddress())); | 86 connection_iter->second->GetDeviceAddress())); |
| 84 device_id_to_connection_map_.erase(connection_iter); | 87 device_id_to_connection_map_.erase(connection_iter); |
| 85 DecrementDevicesConnectedCount(); | 88 DecrementDevicesConnectedCount(); |
| 86 } | 89 } |
| 87 | 90 |
| 88 std::string FrameConnectedBluetoothDevices::CloseConnectionToDeviceWithAddress( | 91 base::Optional<WebBluetoothDeviceId> |
| 92 FrameConnectedBluetoothDevices::CloseConnectionToDeviceWithAddress( |
| 89 const std::string& device_address) { | 93 const std::string& device_address) { |
| 90 auto device_address_iter = device_address_to_id_map_.find(device_address); | 94 auto device_address_iter = device_address_to_id_map_.find(device_address); |
| 91 if (device_address_iter == device_address_to_id_map_.end()) { | 95 if (device_address_iter == device_address_to_id_map_.end()) { |
| 92 return std::string(); | 96 return base::nullopt; |
| 93 } | 97 } |
| 94 std::string device_id = device_address_iter->second; | 98 WebBluetoothDeviceId device_id = device_address_iter->second; |
| 95 CHECK(device_address_to_id_map_.erase(device_address)); | 99 CHECK(device_address_to_id_map_.erase(device_address)); |
| 96 CHECK(device_id_to_connection_map_.erase(device_id)); | 100 CHECK(device_id_to_connection_map_.erase(device_id)); |
| 97 DecrementDevicesConnectedCount(); | 101 DecrementDevicesConnectedCount(); |
| 98 return device_id; | 102 return base::make_optional(device_id); |
| 99 } | 103 } |
| 100 | 104 |
| 101 void FrameConnectedBluetoothDevices::IncrementDevicesConnectedCount() { | 105 void FrameConnectedBluetoothDevices::IncrementDevicesConnectedCount() { |
| 102 web_contents_impl_->IncrementBluetoothConnectedDeviceCount(); | 106 web_contents_impl_->IncrementBluetoothConnectedDeviceCount(); |
| 103 } | 107 } |
| 104 | 108 |
| 105 void FrameConnectedBluetoothDevices::DecrementDevicesConnectedCount() { | 109 void FrameConnectedBluetoothDevices::DecrementDevicesConnectedCount() { |
| 106 web_contents_impl_->DecrementBluetoothConnectedDeviceCount(); | 110 web_contents_impl_->DecrementBluetoothConnectedDeviceCount(); |
| 107 } | 111 } |
| 108 | 112 |
| 109 } // namespace content | 113 } // namespace content |
| OLD | NEW |