| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/bluetooth/frame_connected_bluetooth_devices.h" |
| 6 |
| 7 #include "base/strings/string_util.h" |
| 8 #include "content/browser/web_contents/web_contents_impl.h" |
| 9 #include "content/public/browser/web_contents.h" |
| 10 #include "device/bluetooth/bluetooth_gatt_connection.h" |
| 11 |
| 12 namespace content { |
| 13 |
| 14 FrameConnectedBluetoothDevices::FrameConnectedBluetoothDevices( |
| 15 RenderFrameHost* rfh) |
| 16 : web_contents_impl_(static_cast<WebContentsImpl*>( |
| 17 WebContents::FromRenderFrameHost(rfh))) {} |
| 18 |
| 19 FrameConnectedBluetoothDevices::~FrameConnectedBluetoothDevices() { |
| 20 for (size_t i = 0; i < device_id_to_connection_map_.size(); i++) { |
| 21 DecrementDevicesConnectedCount(); |
| 22 } |
| 23 } |
| 24 |
| 25 bool FrameConnectedBluetoothDevices::IsConnectedToDeviceWithId( |
| 26 const std::string& device_id) { |
| 27 auto connection_iter = device_id_to_connection_map_.find(device_id); |
| 28 if (connection_iter == device_id_to_connection_map_.end()) { |
| 29 return false; |
| 30 } |
| 31 // Owners of FrameConnectedBluetoothDevices should notify it when a device |
| 32 // 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 // 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 // disconnection. |
| 37 // http://crbug.com/607273 |
| 38 return connection_iter->second->IsConnected(); |
| 39 } |
| 40 |
| 41 void FrameConnectedBluetoothDevices::Insert( |
| 42 const std::string& device_id, |
| 43 std::unique_ptr<device::BluetoothGattConnection> connection) { |
| 44 auto connection_iter = device_id_to_connection_map_.find(device_id); |
| 45 if (connection_iter != device_id_to_connection_map_.end()) { |
| 46 // Owners of FrameConnectedBluetoothDevices should notify it when a device |
| 47 // 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 // For this reason we check if the current connection is active and if |
| 50 // not we remove it. |
| 51 // TODO(ortuno): Remove once Android and Mac notify of disconnection. |
| 52 // http://crbug.com/607273 |
| 53 if (!connection_iter->second->IsConnected()) { |
| 54 device_address_to_id_map_.erase( |
| 55 connection_iter->second->GetDeviceAddress()); |
| 56 device_id_to_connection_map_.erase(connection_iter); |
| 57 DecrementDevicesConnectedCount(); |
| 58 } else { |
| 59 // It's possible for WebBluetoothServiceImpl to issue two successive |
| 60 // connection requests for which it would get two successive responses |
| 61 // and consequently try to insert two BluetoothGattConnections for the |
| 62 // same device. WebBluetoothServiceImpl should reject or queue connection |
| 63 // requests if there is a pending connection already, but the platform |
| 64 // abstraction doesn't currently support checking for pending connections. |
| 65 // TODO(ortuno): CHECK that this never happens once the platform |
| 66 // abstraction allows to check for pending connections. |
| 67 // http://crbug.com/583544 |
| 68 return; |
| 69 } |
| 70 } |
| 71 device_address_to_id_map_[connection->GetDeviceAddress()] = device_id; |
| 72 device_id_to_connection_map_[device_id] = std::move(connection); |
| 73 IncrementDevicesConnectedCount(); |
| 74 } |
| 75 |
| 76 void FrameConnectedBluetoothDevices::CloseConnectionToDeviceWithId( |
| 77 const std::string& device_id) { |
| 78 auto connection_iter = device_id_to_connection_map_.find(device_id); |
| 79 if (connection_iter == device_id_to_connection_map_.end()) { |
| 80 return; |
| 81 } |
| 82 CHECK(device_address_to_id_map_.erase( |
| 83 connection_iter->second->GetDeviceAddress())); |
| 84 device_id_to_connection_map_.erase(connection_iter); |
| 85 DecrementDevicesConnectedCount(); |
| 86 } |
| 87 |
| 88 std::string FrameConnectedBluetoothDevices::CloseConnectionToDeviceWithAddress( |
| 89 const std::string& device_address) { |
| 90 auto device_address_iter = device_address_to_id_map_.find(device_address); |
| 91 if (device_address_iter == device_address_to_id_map_.end()) { |
| 92 return std::string(); |
| 93 } |
| 94 std::string device_id = device_address_iter->second; |
| 95 CHECK(device_address_to_id_map_.erase(device_address)); |
| 96 CHECK(device_id_to_connection_map_.erase(device_id)); |
| 97 DecrementDevicesConnectedCount(); |
| 98 return device_id; |
| 99 } |
| 100 |
| 101 void FrameConnectedBluetoothDevices::IncrementDevicesConnectedCount() { |
| 102 web_contents_impl_->IncrementBluetoothConnectedDeviceCount(); |
| 103 } |
| 104 |
| 105 void FrameConnectedBluetoothDevices::DecrementDevicesConnectedCount() { |
| 106 web_contents_impl_->DecrementBluetoothConnectedDeviceCount(); |
| 107 } |
| 108 |
| 109 } // namespace content |
| OLD | NEW |