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

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

Issue 2718583002: Refactor WebBluetoothServiceClient in the web_bluetooth.mojom (Closed)
Patch Set: rebase Created 3 years, 9 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
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/memory/ptr_util.h"
7 #include "base/optional.h" 8 #include "base/optional.h"
8 #include "base/strings/string_util.h" 9 #include "base/strings/string_util.h"
9 #include "content/browser/web_contents/web_contents_impl.h" 10 #include "content/browser/web_contents/web_contents_impl.h"
10 #include "content/public/browser/web_contents.h" 11 #include "content/public/browser/web_contents.h"
11 #include "device/bluetooth/bluetooth_gatt_connection.h" 12 #include "device/bluetooth/bluetooth_gatt_connection.h"
12 13
13 namespace content { 14 namespace content {
14 15
16 struct GATTConnectionAndServerClient {
17 GATTConnectionAndServerClient(
18 std::unique_ptr<device::BluetoothGattConnection> connection,
19 blink::mojom::WebBluetoothServerClientAssociatedPtr client)
20 : gatt_connection(std::move(connection)),
21 server_client(std::move(client)) {}
22
23 std::unique_ptr<device::BluetoothGattConnection> gatt_connection;
24 blink::mojom::WebBluetoothServerClientAssociatedPtr server_client;
25 };
26
15 FrameConnectedBluetoothDevices::FrameConnectedBluetoothDevices( 27 FrameConnectedBluetoothDevices::FrameConnectedBluetoothDevices(
16 RenderFrameHost* rfh) 28 RenderFrameHost* rfh)
17 : web_contents_impl_(static_cast<WebContentsImpl*>( 29 : web_contents_impl_(static_cast<WebContentsImpl*>(
18 WebContents::FromRenderFrameHost(rfh))) {} 30 WebContents::FromRenderFrameHost(rfh))) {}
19 31
20 FrameConnectedBluetoothDevices::~FrameConnectedBluetoothDevices() { 32 FrameConnectedBluetoothDevices::~FrameConnectedBluetoothDevices() {
21 for (size_t i = 0; i < device_id_to_connection_map_.size(); i++) { 33 for (size_t i = 0; i < device_id_to_connection_map_.size(); i++) {
22 DecrementDevicesConnectedCount(); 34 DecrementDevicesConnectedCount();
23 } 35 }
24 } 36 }
25 37
26 bool FrameConnectedBluetoothDevices::IsConnectedToDeviceWithId( 38 bool FrameConnectedBluetoothDevices::IsConnectedToDeviceWithId(
27 const WebBluetoothDeviceId& device_id) { 39 const WebBluetoothDeviceId& device_id) {
28 auto connection_iter = device_id_to_connection_map_.find(device_id); 40 auto connection_iter = device_id_to_connection_map_.find(device_id);
29 if (connection_iter == device_id_to_connection_map_.end()) { 41 if (connection_iter == device_id_to_connection_map_.end()) {
30 return false; 42 return false;
31 } 43 }
32 DCHECK(connection_iter->second->IsConnected()); 44 DCHECK(connection_iter->second->gatt_connection->IsConnected());
33 return true; 45 return true;
34 } 46 }
35 47
36 void FrameConnectedBluetoothDevices::Insert( 48 void FrameConnectedBluetoothDevices::Insert(
37 const WebBluetoothDeviceId& device_id, 49 const WebBluetoothDeviceId& device_id,
38 std::unique_ptr<device::BluetoothGattConnection> connection) { 50 std::unique_ptr<device::BluetoothGattConnection> connection,
51 blink::mojom::WebBluetoothServerClientAssociatedPtr client) {
39 if (device_id_to_connection_map_.find(device_id) != 52 if (device_id_to_connection_map_.find(device_id) !=
40 device_id_to_connection_map_.end()) { 53 device_id_to_connection_map_.end()) {
41 // It's possible for WebBluetoothServiceImpl to issue two successive 54 // It's possible for WebBluetoothServiceImpl to issue two successive
42 // connection requests for which it would get two successive responses 55 // connection requests for which it would get two successive responses
43 // and consequently try to insert two BluetoothGattConnections for the 56 // and consequently try to insert two BluetoothGattConnections for the
44 // same device. WebBluetoothServiceImpl should reject or queue connection 57 // same device. WebBluetoothServiceImpl should reject or queue connection
45 // requests if there is a pending connection already, but the platform 58 // requests if there is a pending connection already, but the platform
46 // abstraction doesn't currently support checking for pending connections. 59 // abstraction doesn't currently support checking for pending connections.
47 // TODO(ortuno): CHECK that this never happens once the platform 60 // TODO(ortuno): CHECK that this never happens once the platform
48 // abstraction allows to check for pending connections. 61 // abstraction allows to check for pending connections.
49 // http://crbug.com/583544 62 // http://crbug.com/583544
50 return; 63 return;
51 } 64 }
52 device_address_to_id_map_[connection->GetDeviceAddress()] = device_id; 65 device_address_to_id_map_[connection->GetDeviceAddress()] = device_id;
53 device_id_to_connection_map_[device_id] = std::move(connection); 66 auto gatt_connection_and_client =
67 base::MakeUnique<GATTConnectionAndServerClient>(std::move(connection),
68 std::move(client));
69 device_id_to_connection_map_[device_id] =
70 std::move(gatt_connection_and_client);
54 IncrementDevicesConnectedCount(); 71 IncrementDevicesConnectedCount();
55 } 72 }
56 73
57 void FrameConnectedBluetoothDevices::CloseConnectionToDeviceWithId( 74 void FrameConnectedBluetoothDevices::CloseConnectionToDeviceWithId(
58 const WebBluetoothDeviceId& device_id) { 75 const WebBluetoothDeviceId& device_id) {
59 auto connection_iter = device_id_to_connection_map_.find(device_id); 76 auto connection_iter = device_id_to_connection_map_.find(device_id);
60 if (connection_iter == device_id_to_connection_map_.end()) { 77 if (connection_iter == device_id_to_connection_map_.end()) {
61 return; 78 return;
62 } 79 }
63 CHECK(device_address_to_id_map_.erase( 80 CHECK(device_address_to_id_map_.erase(
64 connection_iter->second->GetDeviceAddress())); 81 connection_iter->second->gatt_connection->GetDeviceAddress()));
65 device_id_to_connection_map_.erase(connection_iter); 82 device_id_to_connection_map_.erase(connection_iter);
66 DecrementDevicesConnectedCount(); 83 DecrementDevicesConnectedCount();
67 } 84 }
68 85
69 base::Optional<WebBluetoothDeviceId> 86 base::Optional<WebBluetoothDeviceId>
70 FrameConnectedBluetoothDevices::CloseConnectionToDeviceWithAddress( 87 FrameConnectedBluetoothDevices::CloseConnectionToDeviceWithAddress(
71 const std::string& device_address) { 88 const std::string& device_address) {
72 auto device_address_iter = device_address_to_id_map_.find(device_address); 89 auto device_address_iter = device_address_to_id_map_.find(device_address);
73 if (device_address_iter == device_address_to_id_map_.end()) { 90 if (device_address_iter == device_address_to_id_map_.end()) {
74 return base::nullopt; 91 return base::nullopt;
75 } 92 }
76 WebBluetoothDeviceId device_id = device_address_iter->second; 93 WebBluetoothDeviceId device_id = device_address_iter->second;
94 auto device_id_iter = device_id_to_connection_map_.find(device_id);
95 CHECK(device_id_iter != device_id_to_connection_map_.end());
96 device_id_iter->second->server_client->GATTServerDisconnected();
77 CHECK(device_address_to_id_map_.erase(device_address)); 97 CHECK(device_address_to_id_map_.erase(device_address));
78 CHECK(device_id_to_connection_map_.erase(device_id)); 98 device_id_to_connection_map_.erase(device_id);
79 DecrementDevicesConnectedCount(); 99 DecrementDevicesConnectedCount();
80 return base::make_optional(device_id); 100 return base::make_optional(device_id);
81 } 101 }
82 102
83 void FrameConnectedBluetoothDevices::IncrementDevicesConnectedCount() { 103 void FrameConnectedBluetoothDevices::IncrementDevicesConnectedCount() {
84 web_contents_impl_->IncrementBluetoothConnectedDeviceCount(); 104 web_contents_impl_->IncrementBluetoothConnectedDeviceCount();
85 } 105 }
86 106
87 void FrameConnectedBluetoothDevices::DecrementDevicesConnectedCount() { 107 void FrameConnectedBluetoothDevices::DecrementDevicesConnectedCount() {
88 web_contents_impl_->DecrementBluetoothConnectedDeviceCount(); 108 web_contents_impl_->DecrementBluetoothConnectedDeviceCount();
89 } 109 }
90 110
91 } // namespace content 111 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698