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

Side by Side Diff: content/renderer/bluetooth/web_bluetooth_impl.cc

Issue 2565913002: [Onion Soup] Move WebBluetoothImpl from //content/renderer/bluetooth to Blink's bluetooth module (Closed)
Patch Set: renamed BluetoothUUID.typemap to Bluetooth.typemap Created 3 years, 11 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
« no previous file with comments | « content/renderer/bluetooth/web_bluetooth_impl.h ('k') | content/renderer/render_frame_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 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/renderer/bluetooth/web_bluetooth_impl.h"
6
7 #include <memory>
8 #include <utility>
9 #include <vector>
10
11 #include "base/memory/ptr_util.h"
12 #include "base/optional.h"
13 #include "content/child/mojo/type_converters.h"
14 #include "content/child/thread_safe_sender.h"
15 #include "content/common/bluetooth/web_bluetooth_device_id.h"
16 #include "content/renderer/bluetooth/bluetooth_type_converters.h"
17 #include "ipc/ipc_message.h"
18 #include "mojo/public/cpp/bindings/array.h"
19 #include "services/service_manager/public/cpp/interface_provider.h"
20 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevic e.h"
21 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevic eInit.h"
22 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot eGATTCharacteristic.h"
23 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot eGATTCharacteristicInit.h"
24 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot eGATTService.h"
25 #include "third_party/WebKit/public/platform/modules/bluetooth/WebRequestDeviceO ptions.h"
26
27 namespace content {
28
29 namespace {
30
31 // Blink can't use non-blink mojo enums like blink::mojom::WebBluetoothResult,
32 // so we pass it as an int32 across the boundary.
33 int32_t ToInt32(blink::mojom::WebBluetoothResult result) {
34 return static_cast<int32_t>(result);
35 }
36
37 } // namespace
38
39 WebBluetoothImpl::WebBluetoothImpl(
40 service_manager::InterfaceProvider* remote_interfaces)
41 : remote_interfaces_(remote_interfaces), binding_(this) {}
42
43 WebBluetoothImpl::~WebBluetoothImpl() {
44 }
45
46 void WebBluetoothImpl::requestDevice(
47 const blink::WebRequestDeviceOptions& options,
48 blink::WebBluetoothRequestDeviceCallbacks* callbacks) {
49 GetWebBluetoothService().RequestDevice(
50 blink::mojom::WebBluetoothRequestDeviceOptions::From(options),
51 base::Bind(&WebBluetoothImpl::OnRequestDeviceComplete,
52 base::Unretained(this),
53 base::Passed(base::WrapUnique(callbacks))));
54 }
55
56 void WebBluetoothImpl::connect(
57 const blink::WebString& device_id,
58 blink::WebBluetoothDevice* device,
59 blink::WebBluetoothRemoteGATTServerConnectCallbacks* callbacks) {
60 // TODO(crbug.com/495270): After the Bluetooth Tree is implemented, there will
61 // only be one object per device. But for now we replace the previous object.
62 WebBluetoothDeviceId device_id_obj = WebBluetoothDeviceId(device_id.utf8());
63 connected_devices_[device_id_obj] = device;
64
65 GetWebBluetoothService().RemoteServerConnect(
66 std::move(device_id_obj),
67 base::Bind(&WebBluetoothImpl::OnConnectComplete, base::Unretained(this),
68 base::Passed(base::WrapUnique(callbacks))));
69 }
70
71 void WebBluetoothImpl::disconnect(const blink::WebString& device_id) {
72 WebBluetoothDeviceId device_id_obj = WebBluetoothDeviceId(device_id.utf8());
73 connected_devices_.erase(device_id_obj);
74
75 GetWebBluetoothService().RemoteServerDisconnect(std::move(device_id_obj));
76 }
77
78 void WebBluetoothImpl::getPrimaryServices(
79 const blink::WebString& device_id,
80 int32_t quantity,
81 const blink::WebString& services_uuid,
82 blink::WebBluetoothGetPrimaryServicesCallbacks* callbacks) {
83 DCHECK(blink::mojom::IsKnownEnumValue(
84 static_cast<blink::mojom::WebBluetoothGATTQueryQuantity>(quantity)));
85 GetWebBluetoothService().RemoteServerGetPrimaryServices(
86 WebBluetoothDeviceId(device_id.utf8()),
87 static_cast<blink::mojom::WebBluetoothGATTQueryQuantity>(quantity),
88 services_uuid.isEmpty()
89 ? base::nullopt
90 : base::make_optional(device::BluetoothUUID(services_uuid.utf8())),
91 base::Bind(&WebBluetoothImpl::OnGetPrimaryServicesComplete,
92 base::Unretained(this), device_id,
93 base::Passed(base::WrapUnique(callbacks))));
94 }
95
96 void WebBluetoothImpl::getCharacteristics(
97 const blink::WebString& service_instance_id,
98 int32_t quantity,
99 const blink::WebString& characteristics_uuid,
100 blink::WebBluetoothGetCharacteristicsCallbacks* callbacks) {
101 DCHECK(blink::mojom::IsKnownEnumValue(
102 static_cast<blink::mojom::WebBluetoothGATTQueryQuantity>(quantity)));
103 GetWebBluetoothService().RemoteServiceGetCharacteristics(
104 mojo::String::From(service_instance_id),
105 static_cast<blink::mojom::WebBluetoothGATTQueryQuantity>(quantity),
106 characteristics_uuid.isEmpty()
107 ? base::nullopt
108 : base::make_optional(
109 device::BluetoothUUID(characteristics_uuid.utf8())),
110 base::Bind(&WebBluetoothImpl::OnGetCharacteristicsComplete,
111 base::Unretained(this), service_instance_id,
112 base::Passed(base::WrapUnique(callbacks))));
113 }
114
115 void WebBluetoothImpl::readValue(
116 const blink::WebString& characteristic_instance_id,
117 blink::WebBluetoothReadValueCallbacks* callbacks) {
118 GetWebBluetoothService().RemoteCharacteristicReadValue(
119 mojo::String::From(characteristic_instance_id),
120 base::Bind(&WebBluetoothImpl::OnReadValueComplete, base::Unretained(this),
121 base::Passed(base::WrapUnique(callbacks))));
122 }
123
124 void WebBluetoothImpl::writeValue(
125 const blink::WebString& characteristic_instance_id,
126 const blink::WebVector<uint8_t>& value,
127 blink::WebBluetoothWriteValueCallbacks* callbacks) {
128 GetWebBluetoothService().RemoteCharacteristicWriteValue(
129 mojo::String::From(characteristic_instance_id),
130 mojo::Array<uint8_t>::From(value),
131 base::Bind(&WebBluetoothImpl::OnWriteValueComplete,
132 base::Unretained(this), value,
133 base::Passed(base::WrapUnique(callbacks))));
134 }
135
136 void WebBluetoothImpl::startNotifications(
137 const blink::WebString& characteristic_instance_id,
138 blink::WebBluetoothNotificationsCallbacks* callbacks) {
139 GetWebBluetoothService().RemoteCharacteristicStartNotifications(
140 mojo::String::From(characteristic_instance_id),
141 base::Bind(&WebBluetoothImpl::OnStartNotificationsComplete,
142 base::Unretained(this),
143 base::Passed(base::WrapUnique(callbacks))));
144 }
145
146 void WebBluetoothImpl::stopNotifications(
147 const blink::WebString& characteristic_instance_id,
148 blink::WebBluetoothNotificationsCallbacks* callbacks) {
149 GetWebBluetoothService().RemoteCharacteristicStopNotifications(
150 mojo::String::From(characteristic_instance_id),
151 base::Bind(&WebBluetoothImpl::OnStopNotificationsComplete,
152 base::Unretained(this),
153 base::Passed(base::WrapUnique(callbacks))));
154 }
155
156 void WebBluetoothImpl::characteristicObjectRemoved(
157 const blink::WebString& characteristic_instance_id,
158 blink::WebBluetoothRemoteGATTCharacteristic* characteristic) {
159 active_characteristics_.erase(characteristic_instance_id.utf8());
160 }
161
162 void WebBluetoothImpl::registerCharacteristicObject(
163 const blink::WebString& characteristic_instance_id,
164 blink::WebBluetoothRemoteGATTCharacteristic* characteristic) {
165 // TODO(ortuno): After the Bluetooth Tree is implemented, there will
166 // only be one object per characteristic. But for now we replace
167 // the previous object.
168 // https://crbug.com/495270
169 active_characteristics_[characteristic_instance_id.utf8()] = characteristic;
170 }
171
172 void WebBluetoothImpl::RemoteCharacteristicValueChanged(
173 const std::string& characteristic_instance_id,
174 const std::vector<uint8_t>& value) {
175 // We post a task so that the event is fired after any pending promises have
176 // resolved.
177 base::ThreadTaskRunnerHandle::Get()->PostTask(
178 FROM_HERE,
179 base::Bind(&WebBluetoothImpl::DispatchCharacteristicValueChanged,
180 base::Unretained(this), characteristic_instance_id, value));
181 }
182
183 void WebBluetoothImpl::OnRequestDeviceComplete(
184 std::unique_ptr<blink::WebBluetoothRequestDeviceCallbacks> callbacks,
185 const blink::mojom::WebBluetoothResult result,
186 blink::mojom::WebBluetoothDevicePtr device) {
187 if (result == blink::mojom::WebBluetoothResult::SUCCESS) {
188 callbacks->onSuccess(base::MakeUnique<blink::WebBluetoothDeviceInit>(
189 blink::WebString::fromUTF8(device->id.str()),
190 device->name ? blink::WebString::fromUTF8(device->name.value())
191 : blink::WebString()));
192 } else {
193 callbacks->onError(ToInt32(result));
194 }
195 }
196
197 void WebBluetoothImpl::GattServerDisconnected(
198 const WebBluetoothDeviceId& device_id) {
199 auto device_iter = connected_devices_.find(device_id);
200 if (device_iter != connected_devices_.end()) {
201 // Remove device from the map before calling dispatchGattServerDisconnected
202 // to avoid removing a device the gattserverdisconnected event handler might
203 // have re-connected.
204 blink::WebBluetoothDevice* device = device_iter->second;
205 connected_devices_.erase(device_iter);
206 device->dispatchGattServerDisconnected();
207 }
208 }
209
210 void WebBluetoothImpl::OnConnectComplete(
211 std::unique_ptr<blink::WebBluetoothRemoteGATTServerConnectCallbacks>
212 callbacks,
213 blink::mojom::WebBluetoothResult result) {
214 if (result == blink::mojom::WebBluetoothResult::SUCCESS) {
215 callbacks->onSuccess();
216 } else {
217 callbacks->onError(ToInt32(result));
218 }
219 }
220
221 void WebBluetoothImpl::OnGetPrimaryServicesComplete(
222 const blink::WebString& device_id,
223 std::unique_ptr<blink::WebBluetoothGetPrimaryServicesCallbacks> callbacks,
224 blink::mojom::WebBluetoothResult result,
225 base::Optional<std::vector<blink::mojom::WebBluetoothRemoteGATTServicePtr>>
226 services) {
227 if (result == blink::mojom::WebBluetoothResult::SUCCESS) {
228 DCHECK(services);
229 // TODO(dcheng): This WebVector should use smart pointers.
230 blink::WebVector<blink::WebBluetoothRemoteGATTService*> promise_services(
231 services->size());
232 for (size_t i = 0; i < services->size(); i++) {
233 promise_services[i] = new blink::WebBluetoothRemoteGATTService(
234 blink::WebString::fromUTF8(services.value()[i]->instance_id),
235 blink::WebString::fromUTF8(services.value()[i]->uuid),
236 true /* isPrimary */, device_id);
237 }
238 callbacks->onSuccess(promise_services);
239 } else {
240 callbacks->onError(ToInt32(result));
241 }
242 }
243
244 void WebBluetoothImpl::OnGetCharacteristicsComplete(
245 const blink::WebString& service_instance_id,
246 std::unique_ptr<blink::WebBluetoothGetCharacteristicsCallbacks> callbacks,
247 blink::mojom::WebBluetoothResult result,
248 base::Optional<
249 std::vector<blink::mojom::WebBluetoothRemoteGATTCharacteristicPtr>>
250 characteristics) {
251 if (result == blink::mojom::WebBluetoothResult::SUCCESS) {
252 DCHECK(characteristics);
253 // TODO(dcheng): This WebVector should use smart pointers.
254 blink::WebVector<blink::WebBluetoothRemoteGATTCharacteristicInit*>
255 promise_characteristics(characteristics->size());
256 for (size_t i = 0; i < characteristics->size(); i++) {
257 promise_characteristics[i] =
258 new blink::WebBluetoothRemoteGATTCharacteristicInit(
259 service_instance_id, blink::WebString::fromUTF8(
260 characteristics.value()[i]->instance_id),
261 blink::WebString::fromUTF8(characteristics.value()[i]->uuid),
262 characteristics.value()[i]->properties);
263 }
264 callbacks->onSuccess(promise_characteristics);
265 } else {
266 callbacks->onError(ToInt32(result));
267 }
268 }
269
270 void WebBluetoothImpl::OnReadValueComplete(
271 std::unique_ptr<blink::WebBluetoothReadValueCallbacks> callbacks,
272 blink::mojom::WebBluetoothResult result,
273 const base::Optional<std::vector<uint8_t>>& value) {
274 if (result == blink::mojom::WebBluetoothResult::SUCCESS) {
275 DCHECK(value);
276 callbacks->onSuccess(value.value());
277 } else {
278 callbacks->onError(ToInt32(result));
279 }
280 }
281
282 void WebBluetoothImpl::OnWriteValueComplete(
283 const blink::WebVector<uint8_t>& value,
284 std::unique_ptr<blink::WebBluetoothWriteValueCallbacks> callbacks,
285 blink::mojom::WebBluetoothResult result) {
286 if (result == blink::mojom::WebBluetoothResult::SUCCESS) {
287 callbacks->onSuccess(value);
288 } else {
289 callbacks->onError(ToInt32(result));
290 }
291 }
292
293 void WebBluetoothImpl::OnStartNotificationsComplete(
294 std::unique_ptr<blink::WebBluetoothNotificationsCallbacks> callbacks,
295 blink::mojom::WebBluetoothResult result) {
296 if (result == blink::mojom::WebBluetoothResult::SUCCESS) {
297 callbacks->onSuccess();
298 } else {
299 callbacks->onError(ToInt32(result));
300 }
301 }
302
303 void WebBluetoothImpl::OnStopNotificationsComplete(
304 std::unique_ptr<blink::WebBluetoothNotificationsCallbacks> callbacks) {
305 callbacks->onSuccess();
306 }
307
308 void WebBluetoothImpl::DispatchCharacteristicValueChanged(
309 const std::string& characteristic_instance_id,
310 const std::vector<uint8_t>& value) {
311 auto active_iter = active_characteristics_.find(characteristic_instance_id);
312 if (active_iter != active_characteristics_.end()) {
313 active_iter->second->dispatchCharacteristicValueChanged(value);
314 }
315 }
316
317 blink::mojom::WebBluetoothService& WebBluetoothImpl::GetWebBluetoothService() {
318 if (!web_bluetooth_service_) {
319 remote_interfaces_->GetInterface(
320 mojo::MakeRequest(&web_bluetooth_service_));
321 // Create an associated interface ptr and pass it to the WebBluetoothService
322 // so that it can send us events without us prompting.
323 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo ptr_info;
324 binding_.Bind(&ptr_info, web_bluetooth_service_.associated_group());
325 web_bluetooth_service_->SetClient(std::move(ptr_info));
326 }
327 return *web_bluetooth_service_;
328 }
329
330 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/bluetooth/web_bluetooth_impl.h ('k') | content/renderer/render_frame_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698