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

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

Issue 1922923002: bluetooth: Move requestDevice to mojo (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bluetooth-separate-tests-request-device
Patch Set: Address moar comments! 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
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
11 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "content/child/mojo/type_converters.h" 12 #include "content/child/mojo/type_converters.h"
13 #include "content/child/thread_safe_sender.h" 13 #include "content/child/thread_safe_sender.h"
14 #include "content/public/common/service_registry.h" 14 #include "content/public/common/service_registry.h"
15 #include "content/renderer/bluetooth/bluetooth_dispatcher.h" 15 #include "content/renderer/bluetooth/bluetooth_type_converters.h"
16 #include "ipc/ipc_message.h" 16 #include "ipc/ipc_message.h"
17 #include "mojo/public/cpp/bindings/array.h" 17 #include "mojo/public/cpp/bindings/array.h"
18 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevic e.h"
18 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot eGATTCharacteristic.h" 19 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot eGATTCharacteristic.h"
19 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot eGATTCharacteristicInit.h" 20 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot eGATTCharacteristicInit.h"
20 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot eGATTService.h" 21 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot eGATTService.h"
22 #include "third_party/WebKit/public/platform/modules/bluetooth/WebRequestDeviceO ptions.h"
21 23
22 namespace content { 24 namespace content {
23 25
24 WebBluetoothImpl::WebBluetoothImpl(ServiceRegistry* service_registry, 26 WebBluetoothImpl::WebBluetoothImpl(ServiceRegistry* service_registry)
25 ThreadSafeSender* thread_safe_sender, 27 : service_registry_(service_registry), binding_(this) {}
26 int frame_routing_id)
27 : service_registry_(service_registry),
28 binding_(this),
29 thread_safe_sender_(thread_safe_sender),
30 frame_routing_id_(frame_routing_id) {}
31 28
32 WebBluetoothImpl::~WebBluetoothImpl() { 29 WebBluetoothImpl::~WebBluetoothImpl() {
33 } 30 }
34 31
35 void WebBluetoothImpl::requestDevice( 32 void WebBluetoothImpl::requestDevice(
36 const blink::WebRequestDeviceOptions& options, 33 const blink::WebRequestDeviceOptions& options,
37 blink::WebBluetoothRequestDeviceCallbacks* callbacks) { 34 blink::WebBluetoothRequestDeviceCallbacks* callbacks) {
38 GetDispatcher()->requestDevice(frame_routing_id_, options, callbacks); 35 GetWebBluetoothService().RequestDevice(
36 blink::mojom::WebBluetoothRequestDeviceOptions::From(options),
37 base::Bind(&WebBluetoothImpl::OnRequestDeviceComplete,
38 base::Unretained(this),
39 base::Passed(base::WrapUnique(callbacks))));
39 } 40 }
40 41
41 void WebBluetoothImpl::connect( 42 void WebBluetoothImpl::connect(
42 const blink::WebString& device_id, 43 const blink::WebString& device_id,
43 blink::WebBluetoothRemoteGATTServerConnectCallbacks* callbacks) { 44 blink::WebBluetoothRemoteGATTServerConnectCallbacks* callbacks) {
44 GetWebBluetoothService().RemoteServerConnect( 45 GetWebBluetoothService().RemoteServerConnect(
45 mojo::String::From(device_id), 46 mojo::String::From(device_id),
46 base::Bind(&WebBluetoothImpl::OnConnectComplete, base::Unretained(this), 47 base::Bind(&WebBluetoothImpl::OnConnectComplete, base::Unretained(this),
47 base::Passed(base::WrapUnique(callbacks)))); 48 base::Passed(base::WrapUnique(callbacks))));
48 } 49 }
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 mojo::Array<uint8_t> value) { 140 mojo::Array<uint8_t> value) {
140 // We post a task so that the event is fired after any pending promises have 141 // We post a task so that the event is fired after any pending promises have
141 // resolved. 142 // resolved.
142 base::ThreadTaskRunnerHandle::Get()->PostTask( 143 base::ThreadTaskRunnerHandle::Get()->PostTask(
143 FROM_HERE, 144 FROM_HERE,
144 base::Bind(&WebBluetoothImpl::DispatchCharacteristicValueChanged, 145 base::Bind(&WebBluetoothImpl::DispatchCharacteristicValueChanged,
145 base::Unretained(this), characteristic_instance_id, 146 base::Unretained(this), characteristic_instance_id,
146 value.PassStorage())); 147 value.PassStorage()));
147 } 148 }
148 149
150 void WebBluetoothImpl::OnRequestDeviceComplete(
151 std::unique_ptr<blink::WebBluetoothRequestDeviceCallbacks> callbacks,
152 const blink::mojom::WebBluetoothError error,
153 blink::mojom::WebBluetoothDevicePtr device) {
154 if (error == blink::mojom::WebBluetoothError::SUCCESS) {
155 blink::WebVector<blink::WebString> uuids(device->uuids.size());
156 for (size_t i = 0; i < device->uuids.size(); ++i)
157 uuids[i] = blink::WebString::fromUTF8(device->uuids[i]);
158
159 callbacks->onSuccess(base::WrapUnique(new blink::WebBluetoothDevice(
160 blink::WebString::fromUTF8(device->id),
161 blink::WebString::fromUTF8(device->name), uuids)));
162 } else {
163 callbacks->onError(error);
164 }
165 }
166
149 void WebBluetoothImpl::OnConnectComplete( 167 void WebBluetoothImpl::OnConnectComplete(
150 std::unique_ptr<blink::WebBluetoothRemoteGATTServerConnectCallbacks> 168 std::unique_ptr<blink::WebBluetoothRemoteGATTServerConnectCallbacks>
151 callbacks, 169 callbacks,
152 blink::mojom::WebBluetoothError error) { 170 blink::mojom::WebBluetoothError error) {
153 if (error == blink::mojom::WebBluetoothError::SUCCESS) { 171 if (error == blink::mojom::WebBluetoothError::SUCCESS) {
154 callbacks->onSuccess(); 172 callbacks->onSuccess();
155 } else { 173 } else {
156 callbacks->onError(error); 174 callbacks->onError(error);
157 } 175 }
158 } 176 }
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 255
238 void WebBluetoothImpl::DispatchCharacteristicValueChanged( 256 void WebBluetoothImpl::DispatchCharacteristicValueChanged(
239 const std::string& characteristic_instance_id, 257 const std::string& characteristic_instance_id,
240 const std::vector<uint8_t>& value) { 258 const std::vector<uint8_t>& value) {
241 auto active_iter = active_characteristics_.find(characteristic_instance_id); 259 auto active_iter = active_characteristics_.find(characteristic_instance_id);
242 if (active_iter != active_characteristics_.end()) { 260 if (active_iter != active_characteristics_.end()) {
243 active_iter->second->dispatchCharacteristicValueChanged(value); 261 active_iter->second->dispatchCharacteristicValueChanged(value);
244 } 262 }
245 } 263 }
246 264
247 BluetoothDispatcher* WebBluetoothImpl::GetDispatcher() {
248 return BluetoothDispatcher::GetOrCreateThreadSpecificInstance(
249 thread_safe_sender_.get());
250 }
251
252 blink::mojom::WebBluetoothService& WebBluetoothImpl::GetWebBluetoothService() { 265 blink::mojom::WebBluetoothService& WebBluetoothImpl::GetWebBluetoothService() {
253 if (!web_bluetooth_service_) { 266 if (!web_bluetooth_service_) {
254 service_registry_->ConnectToRemoteService( 267 service_registry_->ConnectToRemoteService(
255 mojo::GetProxy(&web_bluetooth_service_)); 268 mojo::GetProxy(&web_bluetooth_service_));
256 // Create an associated interface ptr and pass it to the WebBluetoothService 269 // Create an associated interface ptr and pass it to the WebBluetoothService
257 // so that it can send us events without us prompting. 270 // so that it can send us events without us prompting.
258 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo ptr_info; 271 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo ptr_info;
259 binding_.Bind(&ptr_info, web_bluetooth_service_.associated_group()); 272 binding_.Bind(&ptr_info, web_bluetooth_service_.associated_group());
260 web_bluetooth_service_->SetClient(std::move(ptr_info)); 273 web_bluetooth_service_->SetClient(std::move(ptr_info));
261 } 274 }
262 return *web_bluetooth_service_; 275 return *web_bluetooth_service_;
263 } 276 }
264 277
265 } // namespace content 278 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698