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" |
(...skipping 11 matching lines...) Expand all Loading... |
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 auto connection_iter = device_id_to_connection_map_.find(device_id); |
29 if (connection_iter == device_id_to_connection_map_.end()) { | 29 if (connection_iter == device_id_to_connection_map_.end()) { |
30 return false; | 30 return false; |
31 } | 31 } |
32 DCHECK(connection_iter->second->IsConnected()); | 32 DCHECK(connection_iter->second.first->IsConnected()); |
33 return true; | 33 return true; |
34 } | 34 } |
35 | 35 |
36 void FrameConnectedBluetoothDevices::Insert( | 36 void FrameConnectedBluetoothDevices::Insert( |
37 const WebBluetoothDeviceId& device_id, | 37 const WebBluetoothDeviceId& device_id, |
38 std::unique_ptr<device::BluetoothGattConnection> connection) { | 38 std::unique_ptr<device::BluetoothGattConnection> connection, |
| 39 blink::mojom::WebBluetoothServerClientAssociatedPtr client) { |
39 if (device_id_to_connection_map_.find(device_id) != | 40 if (device_id_to_connection_map_.find(device_id) != |
40 device_id_to_connection_map_.end()) { | 41 device_id_to_connection_map_.end()) { |
41 // It's possible for WebBluetoothServiceImpl to issue two successive | 42 // It's possible for WebBluetoothServiceImpl to issue two successive |
42 // connection requests for which it would get two successive responses | 43 // connection requests for which it would get two successive responses |
43 // and consequently try to insert two BluetoothGattConnections for the | 44 // and consequently try to insert two BluetoothGattConnections for the |
44 // same device. WebBluetoothServiceImpl should reject or queue connection | 45 // same device. WebBluetoothServiceImpl should reject or queue connection |
45 // requests if there is a pending connection already, but the platform | 46 // requests if there is a pending connection already, but the platform |
46 // abstraction doesn't currently support checking for pending connections. | 47 // abstraction doesn't currently support checking for pending connections. |
47 // TODO(ortuno): CHECK that this never happens once the platform | 48 // TODO(ortuno): CHECK that this never happens once the platform |
48 // abstraction allows to check for pending connections. | 49 // abstraction allows to check for pending connections. |
49 // http://crbug.com/583544 | 50 // http://crbug.com/583544 |
50 return; | 51 return; |
51 } | 52 } |
52 device_address_to_id_map_[connection->GetDeviceAddress()] = device_id; | 53 device_address_to_id_map_[connection->GetDeviceAddress()] = device_id; |
53 device_id_to_connection_map_[device_id] = std::move(connection); | 54 device_id_to_connection_map_[device_id] = |
| 55 std::make_pair(std::move(connection), std::move(client)); |
54 IncrementDevicesConnectedCount(); | 56 IncrementDevicesConnectedCount(); |
55 } | 57 } |
56 | 58 |
57 void FrameConnectedBluetoothDevices::CloseConnectionToDeviceWithId( | 59 void FrameConnectedBluetoothDevices::CloseConnectionToDeviceWithId( |
58 const WebBluetoothDeviceId& device_id) { | 60 const WebBluetoothDeviceId& device_id) { |
59 auto connection_iter = device_id_to_connection_map_.find(device_id); | 61 auto connection_iter = device_id_to_connection_map_.find(device_id); |
60 if (connection_iter == device_id_to_connection_map_.end()) { | 62 if (connection_iter == device_id_to_connection_map_.end()) { |
61 return; | 63 return; |
62 } | 64 } |
63 CHECK(device_address_to_id_map_.erase( | 65 CHECK(device_address_to_id_map_.erase( |
64 connection_iter->second->GetDeviceAddress())); | 66 connection_iter->second.first->GetDeviceAddress())); |
65 device_id_to_connection_map_.erase(connection_iter); | 67 device_id_to_connection_map_.erase(connection_iter); |
66 DecrementDevicesConnectedCount(); | 68 DecrementDevicesConnectedCount(); |
67 } | 69 } |
68 | 70 |
69 base::Optional<WebBluetoothDeviceId> | 71 base::Optional<WebBluetoothDeviceId> |
70 FrameConnectedBluetoothDevices::CloseConnectionToDeviceWithAddress( | 72 FrameConnectedBluetoothDevices::CloseConnectionToDeviceWithAddress( |
71 const std::string& device_address) { | 73 const std::string& device_address) { |
72 auto device_address_iter = device_address_to_id_map_.find(device_address); | 74 auto device_address_iter = device_address_to_id_map_.find(device_address); |
73 if (device_address_iter == device_address_to_id_map_.end()) { | 75 if (device_address_iter == device_address_to_id_map_.end()) { |
74 return base::nullopt; | 76 return base::nullopt; |
75 } | 77 } |
76 WebBluetoothDeviceId device_id = device_address_iter->second; | 78 WebBluetoothDeviceId device_id = device_address_iter->second; |
| 79 auto device_id_iter = device_id_to_connection_map_.find(device_id); |
| 80 CHECK(device_id_iter != device_id_to_connection_map_.end()); |
| 81 device_id_iter->second.second->GATTServerDisconnected(); |
77 CHECK(device_address_to_id_map_.erase(device_address)); | 82 CHECK(device_address_to_id_map_.erase(device_address)); |
78 CHECK(device_id_to_connection_map_.erase(device_id)); | 83 device_id_to_connection_map_.erase(device_id); |
79 DecrementDevicesConnectedCount(); | 84 DecrementDevicesConnectedCount(); |
80 return base::make_optional(device_id); | 85 return base::make_optional(device_id); |
81 } | 86 } |
82 | 87 |
83 void FrameConnectedBluetoothDevices::IncrementDevicesConnectedCount() { | 88 void FrameConnectedBluetoothDevices::IncrementDevicesConnectedCount() { |
84 web_contents_impl_->IncrementBluetoothConnectedDeviceCount(); | 89 web_contents_impl_->IncrementBluetoothConnectedDeviceCount(); |
85 } | 90 } |
86 | 91 |
87 void FrameConnectedBluetoothDevices::DecrementDevicesConnectedCount() { | 92 void FrameConnectedBluetoothDevices::DecrementDevicesConnectedCount() { |
88 web_contents_impl_->DecrementBluetoothConnectedDeviceCount(); | 93 web_contents_impl_->DecrementBluetoothConnectedDeviceCount(); |
89 } | 94 } |
90 | 95 |
91 } // namespace content | 96 } // namespace content |
OLD | NEW |