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