Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: content/browser/bluetooth/bluetooth_connected_devices_map.cc

Issue 1902153003: bluetooth: Move connect/disconnect to mojo (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bluetooth-separate-connection-tests
Patch Set: Moar clean up Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/bluetooth_connected_devices_map.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 BluetoothConnectedDevicesMap::BluetoothConnectedDevicesMap(
15 WebContents* web_contents)
16 : web_contents_impl_(static_cast<WebContentsImpl*>(web_contents)) {}
17
18 BluetoothConnectedDevicesMap::~BluetoothConnectedDevicesMap() {
19 for (size_t i = 0; i < device_id_to_connection_map_.size(); i++) {
20 DecrementDevicesConnectedCount();
21 }
22 }
23
24 bool BluetoothConnectedDevicesMap::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 BluetoothConnectedDevicesMap 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 BluetoothConnectedDevicesMap::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 BluetoothConnectedDevicesMap 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 // A frame should never try to connect to an already connected device.
Jeffrey Yasskin 2016/05/03 01:15:57 I'm not sure whether we should detect duplicate co
ortuno 2016/05/03 16:11:53 This comment shouldn't imply that. The case I had
Jeffrey Yasskin 2016/05/03 16:31:32 Looks good, thanks.
59 // Right now this happens because there is no way of knowing of pending
60 // connections.
61 // TODO(ortuno): CHECK that this never happens.
62 // http://crbug.com/583544
63 return;
64 }
65 }
66 device_address_to_id_map_[connection->GetDeviceAddress()] = device_id;
67 device_id_to_connection_map_[device_id] = std::move(connection);
68 IncrementDevicesConnectedCount();
69 }
70
71 void BluetoothConnectedDevicesMap::CloseConnectionToDeviceWithId(
72 const std::string& device_id) {
73 auto connection_iter = device_id_to_connection_map_.find(device_id);
74 if (connection_iter == device_id_to_connection_map_.end()) {
75 return;
76 }
77 DCHECK(device_address_to_id_map_.erase(
Jeffrey Yasskin 2016/05/03 01:15:57 Oops, the contents of DCHECK won't run in release
ortuno 2016/05/03 16:11:53 Ah, I thought we did the same in BluetoothAllowedD
78 connection_iter->second->GetDeviceAddress()));
79 DCHECK(device_id_to_connection_map_.erase(device_id));
Jeffrey Yasskin 2016/05/03 01:15:57 May as well erase(connection_iter) here, to avoid
ortuno 2016/05/03 16:11:53 I did it this way because otherwise I have to comp
80 DecrementDevicesConnectedCount();
81 }
82
83 std::string BluetoothConnectedDevicesMap::CloseConnectionToDeviceWithAddress(
84 const std::string& device_address) {
85 auto device_address_iter = device_address_to_id_map_.find(device_address);
86 if (device_address_iter == device_address_to_id_map_.end()) {
87 return base::EmptyString();
Jeffrey Yasskin 2016/05/03 01:15:57 The main reason to use EmptyString() is if you're
ortuno 2016/05/03 16:11:53 Done.
88 }
89 std::string device_id = device_address_iter->second;
90 DCHECK(device_address_to_id_map_.erase(device_address));
91 DCHECK(device_id_to_connection_map_.erase(device_id));
92 DecrementDevicesConnectedCount();
93 return device_id;
94 }
95
96 void BluetoothConnectedDevicesMap::IncrementDevicesConnectedCount() {
97 web_contents_impl_->IncrementBluetoothConnectedDeviceCount();
98 }
99
100 void BluetoothConnectedDevicesMap::DecrementDevicesConnectedCount() {
101 web_contents_impl_->DecrementBluetoothConnectedDeviceCount();
102 }
103
104 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698