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

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

Issue 2506813003: Use new wrapper types for web_bluetooth.mojom (Closed)
Patch Set: updated web_bluetooth_impl.cc Created 4 years, 1 month 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 2014 The Chromium Authors. All rights reserved. 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 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/renderer/bluetooth/web_bluetooth_impl.h" 5 #include "content/renderer/bluetooth/web_bluetooth_impl.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 const blink::WebString& characteristic_instance_id, 163 const blink::WebString& characteristic_instance_id,
164 blink::WebBluetoothRemoteGATTCharacteristic* characteristic) { 164 blink::WebBluetoothRemoteGATTCharacteristic* characteristic) {
165 // TODO(ortuno): After the Bluetooth Tree is implemented, there will 165 // TODO(ortuno): After the Bluetooth Tree is implemented, there will
166 // only be one object per characteristic. But for now we replace 166 // only be one object per characteristic. But for now we replace
167 // the previous object. 167 // the previous object.
168 // https://crbug.com/495270 168 // https://crbug.com/495270
169 active_characteristics_[characteristic_instance_id.utf8()] = characteristic; 169 active_characteristics_[characteristic_instance_id.utf8()] = characteristic;
170 } 170 }
171 171
172 void WebBluetoothImpl::RemoteCharacteristicValueChanged( 172 void WebBluetoothImpl::RemoteCharacteristicValueChanged(
173 const mojo::String& characteristic_instance_id, 173 const std::string& characteristic_instance_id,
174 mojo::Array<uint8_t> value) { 174 const std::vector<uint8_t>& value) {
175 // We post a task so that the event is fired after any pending promises have 175 // We post a task so that the event is fired after any pending promises have
176 // resolved. 176 // resolved.
177 base::ThreadTaskRunnerHandle::Get()->PostTask( 177 base::ThreadTaskRunnerHandle::Get()->PostTask(
178 FROM_HERE, 178 FROM_HERE,
179 base::Bind(&WebBluetoothImpl::DispatchCharacteristicValueChanged, 179 base::Bind(&WebBluetoothImpl::DispatchCharacteristicValueChanged,
180 base::Unretained(this), characteristic_instance_id, 180 base::Unretained(this), characteristic_instance_id, value));
181 value.PassStorage()));
182 } 181 }
183 182
184 void WebBluetoothImpl::OnRequestDeviceComplete( 183 void WebBluetoothImpl::OnRequestDeviceComplete(
185 std::unique_ptr<blink::WebBluetoothRequestDeviceCallbacks> callbacks, 184 std::unique_ptr<blink::WebBluetoothRequestDeviceCallbacks> callbacks,
186 const blink::mojom::WebBluetoothResult result, 185 const blink::mojom::WebBluetoothResult result,
187 blink::mojom::WebBluetoothDevicePtr device) { 186 blink::mojom::WebBluetoothDevicePtr device) {
188 if (result == blink::mojom::WebBluetoothResult::SUCCESS) { 187 if (result == blink::mojom::WebBluetoothResult::SUCCESS) {
189 callbacks->onSuccess(base::MakeUnique<blink::WebBluetoothDeviceInit>( 188 callbacks->onSuccess(base::MakeUnique<blink::WebBluetoothDeviceInit>(
190 blink::WebString::fromUTF8(device->id.str()), 189 blink::WebString::fromUTF8(device->id.str()),
191 device->name.is_null() ? blink::WebString() 190 device->name ? blink::WebString::fromUTF8(device->name.value().c_str())
192 : blink::WebString::fromUTF8(device->name))); 191 : blink::WebString()));
193 } else { 192 } else {
194 callbacks->onError(ToInt32(result)); 193 callbacks->onError(ToInt32(result));
195 } 194 }
196 } 195 }
197 196
198 void WebBluetoothImpl::GattServerDisconnected( 197 void WebBluetoothImpl::GattServerDisconnected(
199 const WebBluetoothDeviceId& device_id) { 198 const WebBluetoothDeviceId& device_id) {
200 auto device_iter = connected_devices_.find(device_id); 199 auto device_iter = connected_devices_.find(device_id);
201 if (device_iter != connected_devices_.end()) { 200 if (device_iter != connected_devices_.end()) {
202 // Remove device from the map before calling dispatchGattServerDisconnected 201 // Remove device from the map before calling dispatchGattServerDisconnected
(...skipping 13 matching lines...) Expand all
216 callbacks->onSuccess(); 215 callbacks->onSuccess();
217 } else { 216 } else {
218 callbacks->onError(ToInt32(result)); 217 callbacks->onError(ToInt32(result));
219 } 218 }
220 } 219 }
221 220
222 void WebBluetoothImpl::OnGetPrimaryServicesComplete( 221 void WebBluetoothImpl::OnGetPrimaryServicesComplete(
223 const blink::WebString& device_id, 222 const blink::WebString& device_id,
224 std::unique_ptr<blink::WebBluetoothGetPrimaryServicesCallbacks> callbacks, 223 std::unique_ptr<blink::WebBluetoothGetPrimaryServicesCallbacks> callbacks,
225 blink::mojom::WebBluetoothResult result, 224 blink::mojom::WebBluetoothResult result,
226 mojo::Array<blink::mojom::WebBluetoothRemoteGATTServicePtr> services) { 225 base::Optional<std::vector<blink::mojom::WebBluetoothRemoteGATTServicePtr>>
226 services) {
227 if (result == blink::mojom::WebBluetoothResult::SUCCESS) { 227 if (result == blink::mojom::WebBluetoothResult::SUCCESS) {
228 // TODO(dcheng): This WebVector should use smart pointers. 228 // TODO(dcheng): This WebVector should use smart pointers.
229 blink::WebVector<blink::WebBluetoothRemoteGATTService*> promise_services( 229 blink::WebVector<blink::WebBluetoothRemoteGATTService*> promise_services;
230 services.size()); 230 if (services) {
ortuno 2016/11/21 04:42:31 Just DCHECK. If result == SUCCESS then we are guar
juncai 2016/11/21 21:27:05 Done.
231 231 blink::WebVector<blink::WebBluetoothRemoteGATTService*> tmp(
232 for (size_t i = 0; i < services.size(); i++) { 232 services->size());
233 promise_services[i] = new blink::WebBluetoothRemoteGATTService( 233 promise_services.swap(tmp);
234 blink::WebString::fromUTF8(services[i]->instance_id), 234 for (size_t i = 0; i < services->size(); i++) {
235 blink::WebString::fromUTF8(services[i]->uuid), true /* isPrimary */, 235 promise_services[i] = new blink::WebBluetoothRemoteGATTService(
236 device_id); 236 blink::WebString::fromUTF8(services.value()[i]->instance_id),
237 blink::WebString::fromUTF8(services.value()[i]->uuid),
238 true /* isPrimary */, device_id);
239 }
237 } 240 }
238 callbacks->onSuccess(promise_services); 241 callbacks->onSuccess(promise_services);
239 } else { 242 } else {
240 callbacks->onError(ToInt32(result)); 243 callbacks->onError(ToInt32(result));
241 } 244 }
242 } 245 }
243 246
244 void WebBluetoothImpl::OnGetCharacteristicsComplete( 247 void WebBluetoothImpl::OnGetCharacteristicsComplete(
245 const blink::WebString& service_instance_id, 248 const blink::WebString& service_instance_id,
246 std::unique_ptr<blink::WebBluetoothGetCharacteristicsCallbacks> callbacks, 249 std::unique_ptr<blink::WebBluetoothGetCharacteristicsCallbacks> callbacks,
247 blink::mojom::WebBluetoothResult result, 250 blink::mojom::WebBluetoothResult result,
248 mojo::Array<blink::mojom::WebBluetoothRemoteGATTCharacteristicPtr> 251 base::Optional<
252 std::vector<blink::mojom::WebBluetoothRemoteGATTCharacteristicPtr>>
249 characteristics) { 253 characteristics) {
250 if (result == blink::mojom::WebBluetoothResult::SUCCESS) { 254 if (result == blink::mojom::WebBluetoothResult::SUCCESS) {
251 // TODO(dcheng): This WebVector should use smart pointers. 255 // TODO(dcheng): This WebVector should use smart pointers.
252 blink::WebVector<blink::WebBluetoothRemoteGATTCharacteristicInit*> 256 blink::WebVector<blink::WebBluetoothRemoteGATTCharacteristicInit*>
253 promise_characteristics(characteristics.size()); 257 promise_characteristics;
254 258 if (characteristics) {
ortuno 2016/11/21 04:42:31 Same here. Just DCHECK.
juncai 2016/11/21 21:27:05 Done.
255 for (size_t i = 0; i < characteristics.size(); i++) { 259 blink::WebVector<blink::WebBluetoothRemoteGATTCharacteristicInit*> tmp(
256 promise_characteristics[i] = 260 characteristics->size());
257 new blink::WebBluetoothRemoteGATTCharacteristicInit( 261 promise_characteristics.swap(tmp);
258 service_instance_id, 262 for (size_t i = 0; i < characteristics->size(); i++) {
259 blink::WebString::fromUTF8(characteristics[i]->instance_id), 263 promise_characteristics[i] =
260 blink::WebString::fromUTF8(characteristics[i]->uuid), 264 new blink::WebBluetoothRemoteGATTCharacteristicInit(
261 characteristics[i]->properties); 265 service_instance_id,
266 blink::WebString::fromUTF8(
267 characteristics.value()[i]->instance_id),
268 blink::WebString::fromUTF8(characteristics.value()[i]->uuid),
269 characteristics.value()[i]->properties);
270 }
262 } 271 }
263 callbacks->onSuccess(promise_characteristics); 272 callbacks->onSuccess(promise_characteristics);
264 } else { 273 } else {
265 callbacks->onError(ToInt32(result)); 274 callbacks->onError(ToInt32(result));
266 } 275 }
267 } 276 }
268 277
269 void WebBluetoothImpl::OnReadValueComplete( 278 void WebBluetoothImpl::OnReadValueComplete(
270 std::unique_ptr<blink::WebBluetoothReadValueCallbacks> callbacks, 279 std::unique_ptr<blink::WebBluetoothReadValueCallbacks> callbacks,
271 blink::mojom::WebBluetoothResult result, 280 blink::mojom::WebBluetoothResult result,
272 mojo::Array<uint8_t> value) { 281 const base::Optional<std::vector<uint8_t>>& value) {
273 if (result == blink::mojom::WebBluetoothResult::SUCCESS) { 282 if (result == blink::mojom::WebBluetoothResult::SUCCESS) {
274 callbacks->onSuccess(value.PassStorage()); 283 callbacks->onSuccess(value ? value.value() : std::vector<uint8_t>());
ortuno 2016/11/21 04:42:31 And here as well. Just DCHECK. Also does it make s
juncai 2016/11/21 21:27:05 Done.
275 } else { 284 } else {
276 callbacks->onError(ToInt32(result)); 285 callbacks->onError(ToInt32(result));
277 } 286 }
278 } 287 }
279 288
280 void WebBluetoothImpl::OnWriteValueComplete( 289 void WebBluetoothImpl::OnWriteValueComplete(
281 const blink::WebVector<uint8_t>& value, 290 const blink::WebVector<uint8_t>& value,
282 std::unique_ptr<blink::WebBluetoothWriteValueCallbacks> callbacks, 291 std::unique_ptr<blink::WebBluetoothWriteValueCallbacks> callbacks,
283 blink::mojom::WebBluetoothResult result) { 292 blink::mojom::WebBluetoothResult result) {
284 if (result == blink::mojom::WebBluetoothResult::SUCCESS) { 293 if (result == blink::mojom::WebBluetoothResult::SUCCESS) {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 // Create an associated interface ptr and pass it to the WebBluetoothService 327 // Create an associated interface ptr and pass it to the WebBluetoothService
319 // so that it can send us events without us prompting. 328 // so that it can send us events without us prompting.
320 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo ptr_info; 329 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo ptr_info;
321 binding_.Bind(&ptr_info, web_bluetooth_service_.associated_group()); 330 binding_.Bind(&ptr_info, web_bluetooth_service_.associated_group());
322 web_bluetooth_service_->SetClient(std::move(ptr_info)); 331 web_bluetooth_service_->SetClient(std::move(ptr_info));
323 } 332 }
324 return *web_bluetooth_service_; 333 return *web_bluetooth_service_;
325 } 334 }
326 335
327 } // namespace content 336 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698