Chromium Code Reviews| 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 WebContents* web_contents) | |
| 16 : web_contents_impl_(static_cast<WebContentsImpl*>(web_contents)) {} | |
| 17 | |
| 18 FrameConnectedBluetoothDevices::~FrameConnectedBluetoothDevices() { | |
| 19 for (size_t i = 0; i < device_id_to_connection_map_.size(); i++) { | |
| 20 DecrementDevicesConnectedCount(); | |
| 21 } | |
| 22 } | |
| 23 | |
| 24 bool FrameConnectedBluetoothDevices::IsConnectedToDeviceWithId( | |
| 25 const std::string& device_id) { | |
| 26 auto connection_iter = device_id_to_connection_map_.find(device_id); | |
| 27 if (connection_iter == device_id_to_connection_map_.end()) { | |
| 28 return false; | |
| 29 } | |
| 30 // Owners of FrameConnectedBluetoothDevices should notify it when a device | |
| 31 // disconnects but currently Android and Mac don't notify of disconnection, | |
| 32 // so the map could get into a state where it's holding a stale connection. | |
| 33 // For this reason we return the value of IsConnected for the connection. | |
| 34 // TODO(ortuno): Always return true once Android and Mac notify of | |
| 35 // disconnection. | |
| 36 // http://crbug.com/607273 | |
| 37 return connection_iter->second->IsConnected(); | |
| 38 } | |
| 39 | |
| 40 void FrameConnectedBluetoothDevices::Insert( | |
| 41 const std::string& device_id, | |
| 42 std::unique_ptr<device::BluetoothGattConnection> connection) { | |
| 43 auto connection_iter = device_id_to_connection_map_.find(device_id); | |
| 44 if (connection_iter != device_id_to_connection_map_.end()) { | |
| 45 // Owners of FrameConnectedBluetoothDevices should notify it when a device | |
| 46 // disconnects but currently Android and Mac don't notify of disconnection, | |
| 47 // so the map could get into a state where it's holding a stale connection. | |
| 48 // For this reason we check if the current connection is active and if | |
| 49 // not we remove it. | |
| 50 // TODO(ortuno): Remove once Android and Mac notify of disconnection. | |
| 51 // http://crbug.com/607273 | |
| 52 if (!connection_iter->second->IsConnected()) { | |
| 53 device_address_to_id_map_.erase( | |
| 54 connection_iter->second->GetDeviceAddress()); | |
| 55 device_id_to_connection_map_.erase(connection_iter); | |
| 56 DecrementDevicesConnectedCount(); | |
| 57 } else { | |
| 58 // It's possible for WebBluetoothServiceImpl to issue two successive | |
| 59 // connection requests for which it would get two successive responses | |
| 60 // and consequently try to insert two BluetoothGattConnections for the | |
| 61 // same device. WebBluetoothServiceImpl should reject or queue connection | |
| 62 // requests if there is a pending connection already, but the platform | |
| 63 // abstraction doesn't currently support checking for pending connections. | |
| 64 // TODO(ortuno): CHECK that this never happens once the platform | |
| 65 // abstraction allows to check for pending connections. | |
| 66 // http://crbug.com/583544 | |
| 67 return; | |
| 68 } | |
| 69 } | |
| 70 device_address_to_id_map_[connection->GetDeviceAddress()] = device_id; | |
| 71 device_id_to_connection_map_[device_id] = std::move(connection); | |
| 72 IncrementDevicesConnectedCount(); | |
| 73 } | |
| 74 | |
| 75 void FrameConnectedBluetoothDevices::CloseConnectionToDeviceWithId( | |
| 76 const std::string& device_id) { | |
| 77 auto connection_iter = device_id_to_connection_map_.find(device_id); | |
| 78 if (connection_iter == device_id_to_connection_map_.end()) { | |
| 79 return; | |
| 80 } | |
| 81 CHECK(device_address_to_id_map_.erase( | |
| 82 connection_iter->second->GetDeviceAddress())); | |
| 83 CHECK(device_id_to_connection_map_.erase(device_id)); | |
|
Jeffrey Yasskin
2016/05/03 16:31:32
erase(iter) is only valid for non-end arguments, a
ortuno
2016/05/03 16:54:39
Done.
| |
| 84 DecrementDevicesConnectedCount(); | |
| 85 } | |
| 86 | |
| 87 std::string FrameConnectedBluetoothDevices::CloseConnectionToDeviceWithAddress( | |
| 88 const std::string& device_address) { | |
| 89 auto device_address_iter = device_address_to_id_map_.find(device_address); | |
| 90 if (device_address_iter == device_address_to_id_map_.end()) { | |
| 91 return std::string(); | |
| 92 } | |
| 93 std::string device_id = device_address_iter->second; | |
| 94 CHECK(device_address_to_id_map_.erase(device_address)); | |
| 95 CHECK(device_id_to_connection_map_.erase(device_id)); | |
| 96 DecrementDevicesConnectedCount(); | |
| 97 return device_id; | |
| 98 } | |
| 99 | |
| 100 void FrameConnectedBluetoothDevices::IncrementDevicesConnectedCount() { | |
| 101 web_contents_impl_->IncrementBluetoothConnectedDeviceCount(); | |
| 102 } | |
| 103 | |
| 104 void FrameConnectedBluetoothDevices::DecrementDevicesConnectedCount() { | |
| 105 web_contents_impl_->DecrementBluetoothConnectedDeviceCount(); | |
| 106 } | |
| 107 | |
| 108 } // namespace content | |
| OLD | NEW |