Chromium Code Reviews| 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/optional.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "content/browser/web_contents/web_contents_impl.h" | 9 #include "content/browser/web_contents/web_contents_impl.h" |
| 10 #include "content/public/browser/web_contents.h" | 10 #include "content/public/browser/web_contents.h" |
| 11 #include "device/bluetooth/bluetooth_gatt_connection.h" | 11 #include "device/bluetooth/bluetooth_gatt_connection.h" |
| 12 | 12 |
| 13 namespace content { | 13 namespace content { |
| 14 | 14 |
| 15 FrameConnectedBluetoothDevices::FrameConnectedBluetoothDevices( | 15 FrameConnectedBluetoothDevices::FrameConnectedBluetoothDevices( |
| 16 RenderFrameHost* rfh) | 16 RenderFrameHost* rfh) |
| 17 : web_contents_impl_(static_cast<WebContentsImpl*>( | 17 : web_contents_impl_(static_cast<WebContentsImpl*>( |
| 18 WebContents::FromRenderFrameHost(rfh))) {} | 18 WebContents::FromRenderFrameHost(rfh))) {} |
| 19 | 19 |
| 20 FrameConnectedBluetoothDevices::~FrameConnectedBluetoothDevices() { | 20 FrameConnectedBluetoothDevices::~FrameConnectedBluetoothDevices() { |
| 21 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++) { |
| 22 DecrementDevicesConnectedCount(); | 22 DecrementDevicesConnectedCount(); |
| 23 } | 23 } |
| 24 } | 24 } |
| 25 | 25 |
| 26 bool FrameConnectedBluetoothDevices::IsConnectedToDeviceWithId( | 26 bool FrameConnectedBluetoothDevices::IsConnectedToDeviceWithId( |
| 27 const WebBluetoothDeviceId& device_id) { | 27 const WebBluetoothDeviceId& device_id) { |
| 28 auto connection_iter = device_id_to_connection_map_.find(device_id); | 28 if (device_id_to_connection_map_.find(device_id) == |
| 29 if (connection_iter == device_id_to_connection_map_.end()) { | 29 device_id_to_connection_map_.end()) { |
| 30 return false; | 30 return false; |
| 31 } | 31 } |
| 32 // Owners of FrameConnectedBluetoothDevices should notify it when a device | 32 return true; |
|
ortuno
2017/02/23 02:27:03
nit: could we add a DCHECK?
DCHECK(connection_iter
juncai
2017/02/23 04:53:41
Done.
| |
| 33 // disconnects but currently Android and Mac don't notify of disconnection, | |
| 34 // so the map could get into a state where it's holding a stale connection. | |
| 35 // For this reason we return the value of IsConnected for the connection. | |
| 36 // TODO(ortuno): Always return true once Android and Mac notify of | |
| 37 // disconnection. | |
| 38 // http://crbug.com/607273 | |
| 39 return connection_iter->second->IsConnected(); | |
| 40 } | 33 } |
| 41 | 34 |
| 42 void FrameConnectedBluetoothDevices::Insert( | 35 void FrameConnectedBluetoothDevices::Insert( |
| 43 const WebBluetoothDeviceId& device_id, | 36 const WebBluetoothDeviceId& device_id, |
| 44 std::unique_ptr<device::BluetoothGattConnection> connection) { | 37 std::unique_ptr<device::BluetoothGattConnection> connection) { |
| 45 auto connection_iter = device_id_to_connection_map_.find(device_id); | 38 if (device_id_to_connection_map_.find(device_id) != |
| 46 if (connection_iter != device_id_to_connection_map_.end()) { | 39 device_id_to_connection_map_.end()) { |
| 47 // Owners of FrameConnectedBluetoothDevices should notify it when a device | 40 // It's possible for WebBluetoothServiceImpl to issue two successive |
| 48 // disconnects but currently Android and Mac don't notify of disconnection, | 41 // connection requests for which it would get two successive responses |
| 49 // so the map could get into a state where it's holding a stale connection. | 42 // and consequently try to insert two BluetoothGattConnections for the |
| 50 // For this reason we check if the current connection is active and if | 43 // same device. WebBluetoothServiceImpl should reject or queue connection |
| 51 // not we remove it. | 44 // requests if there is a pending connection already, but the platform |
| 52 // TODO(ortuno): Remove once Android and Mac notify of disconnection. | 45 // abstraction doesn't currently support checking for pending connections. |
| 53 // http://crbug.com/607273 | 46 // TODO(ortuno): CHECK that this never happens once the platform |
| 54 if (!connection_iter->second->IsConnected()) { | 47 // abstraction allows to check for pending connections. |
| 55 device_address_to_id_map_.erase( | 48 // http://crbug.com/583544 |
| 56 connection_iter->second->GetDeviceAddress()); | 49 return; |
| 57 device_id_to_connection_map_.erase(connection_iter); | |
| 58 DecrementDevicesConnectedCount(); | |
| 59 } else { | |
| 60 // It's possible for WebBluetoothServiceImpl to issue two successive | |
| 61 // connection requests for which it would get two successive responses | |
| 62 // and consequently try to insert two BluetoothGattConnections for the | |
| 63 // same device. WebBluetoothServiceImpl should reject or queue connection | |
| 64 // requests if there is a pending connection already, but the platform | |
| 65 // abstraction doesn't currently support checking for pending connections. | |
| 66 // TODO(ortuno): CHECK that this never happens once the platform | |
| 67 // abstraction allows to check for pending connections. | |
| 68 // http://crbug.com/583544 | |
| 69 return; | |
| 70 } | |
| 71 } | 50 } |
| 72 device_address_to_id_map_[connection->GetDeviceAddress()] = device_id; | 51 device_address_to_id_map_[connection->GetDeviceAddress()] = device_id; |
| 73 device_id_to_connection_map_[device_id] = std::move(connection); | 52 device_id_to_connection_map_[device_id] = std::move(connection); |
| 74 IncrementDevicesConnectedCount(); | 53 IncrementDevicesConnectedCount(); |
| 75 } | 54 } |
| 76 | 55 |
| 77 void FrameConnectedBluetoothDevices::CloseConnectionToDeviceWithId( | 56 void FrameConnectedBluetoothDevices::CloseConnectionToDeviceWithId( |
| 78 const WebBluetoothDeviceId& device_id) { | 57 const WebBluetoothDeviceId& device_id) { |
| 79 auto connection_iter = device_id_to_connection_map_.find(device_id); | 58 auto connection_iter = device_id_to_connection_map_.find(device_id); |
| 80 if (connection_iter == device_id_to_connection_map_.end()) { | 59 if (connection_iter == device_id_to_connection_map_.end()) { |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 102 | 81 |
| 103 void FrameConnectedBluetoothDevices::IncrementDevicesConnectedCount() { | 82 void FrameConnectedBluetoothDevices::IncrementDevicesConnectedCount() { |
| 104 web_contents_impl_->IncrementBluetoothConnectedDeviceCount(); | 83 web_contents_impl_->IncrementBluetoothConnectedDeviceCount(); |
| 105 } | 84 } |
| 106 | 85 |
| 107 void FrameConnectedBluetoothDevices::DecrementDevicesConnectedCount() { | 86 void FrameConnectedBluetoothDevices::DecrementDevicesConnectedCount() { |
| 108 web_contents_impl_->DecrementBluetoothConnectedDeviceCount(); | 87 web_contents_impl_->DecrementBluetoothConnectedDeviceCount(); |
| 109 } | 88 } |
| 110 | 89 |
| 111 } // namespace content | 90 } // namespace content |
| OLD | NEW |