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