OLD | NEW |
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 <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "content/child/mojo/type_converters.h" | 10 #include "content/child/mojo/type_converters.h" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 const blink::WebString& service_instance_id, | 64 const blink::WebString& service_instance_id, |
65 const blink::WebString& characteristic_uuid, | 65 const blink::WebString& characteristic_uuid, |
66 blink::WebBluetoothGetCharacteristicsCallbacks* callbacks) { | 66 blink::WebBluetoothGetCharacteristicsCallbacks* callbacks) { |
67 GetDispatcher()->getCharacteristics(frame_routing_id_, service_instance_id, | 67 GetDispatcher()->getCharacteristics(frame_routing_id_, service_instance_id, |
68 characteristic_uuid, callbacks); | 68 characteristic_uuid, callbacks); |
69 } | 69 } |
70 | 70 |
71 void WebBluetoothImpl::readValue( | 71 void WebBluetoothImpl::readValue( |
72 const blink::WebString& characteristic_instance_id, | 72 const blink::WebString& characteristic_instance_id, |
73 blink::WebBluetoothReadValueCallbacks* callbacks) { | 73 blink::WebBluetoothReadValueCallbacks* callbacks) { |
74 GetDispatcher()->readValue(frame_routing_id_, characteristic_instance_id, | 74 GetWebBluetoothService().RemoteCharacteristicReadValue( |
75 callbacks); | 75 mojo::String::From(characteristic_instance_id), |
| 76 base::Bind(&WebBluetoothImpl::OnReadValueComplete, base::Unretained(this), |
| 77 base::Passed(base::WrapUnique(callbacks)))); |
76 } | 78 } |
77 | 79 |
78 void WebBluetoothImpl::writeValue( | 80 void WebBluetoothImpl::writeValue( |
79 const blink::WebString& characteristic_instance_id, | 81 const blink::WebString& characteristic_instance_id, |
80 const blink::WebVector<uint8_t>& value, | 82 const blink::WebVector<uint8_t>& value, |
81 blink::WebBluetoothWriteValueCallbacks* callbacks) { | 83 blink::WebBluetoothWriteValueCallbacks* callbacks) { |
82 GetWebBluetoothService().RemoteCharacteristicWriteValue( | 84 GetWebBluetoothService().RemoteCharacteristicWriteValue( |
83 mojo::String::From(characteristic_instance_id), | 85 mojo::String::From(characteristic_instance_id), |
84 mojo::Array<uint8_t>::From(value), | 86 mojo::Array<uint8_t>::From(value), |
85 base::Bind(&WebBluetoothImpl::OnWriteValueComplete, | 87 base::Bind(&WebBluetoothImpl::OnWriteValueComplete, |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 // TODO(ortuno): After the Bluetooth Tree is implemented, there will | 121 // TODO(ortuno): After the Bluetooth Tree is implemented, there will |
120 // only be one object per characteristic. But for now we replace | 122 // only be one object per characteristic. But for now we replace |
121 // the previous object. | 123 // the previous object. |
122 // https://crbug.com/495270 | 124 // https://crbug.com/495270 |
123 active_characteristics_[characteristic_instance_id.utf8()] = characteristic; | 125 active_characteristics_[characteristic_instance_id.utf8()] = characteristic; |
124 } | 126 } |
125 | 127 |
126 void WebBluetoothImpl::RemoteCharacteristicValueChanged( | 128 void WebBluetoothImpl::RemoteCharacteristicValueChanged( |
127 const mojo::String& characteristic_instance_id, | 129 const mojo::String& characteristic_instance_id, |
128 mojo::Array<uint8_t> value) { | 130 mojo::Array<uint8_t> value) { |
129 auto active_iter = active_characteristics_.find(characteristic_instance_id); | 131 // We post a task so that the event is fired after any pending promises have |
130 if (active_iter != active_characteristics_.end()) { | 132 // resolved. |
131 active_iter->second->dispatchCharacteristicValueChanged( | 133 base::ThreadTaskRunnerHandle::Get()->PostTask( |
132 value.PassStorage()); | 134 FROM_HERE, |
| 135 base::Bind(&WebBluetoothImpl::DispatchCharacteristicValueChanged, |
| 136 base::Unretained(this), characteristic_instance_id, |
| 137 value.PassStorage())); |
| 138 } |
| 139 |
| 140 void WebBluetoothImpl::OnReadValueComplete( |
| 141 std::unique_ptr<blink::WebBluetoothReadValueCallbacks> callbacks, |
| 142 blink::mojom::WebBluetoothError error, |
| 143 mojo::Array<uint8_t> value) { |
| 144 if (error == blink::mojom::WebBluetoothError::SUCCESS) { |
| 145 callbacks->onSuccess(value.PassStorage()); |
| 146 } else { |
| 147 callbacks->onError(error); |
133 } | 148 } |
134 } | 149 } |
135 | 150 |
136 void WebBluetoothImpl::OnWriteValueComplete( | 151 void WebBluetoothImpl::OnWriteValueComplete( |
137 const blink::WebVector<uint8_t>& value, | 152 const blink::WebVector<uint8_t>& value, |
138 std::unique_ptr<blink::WebBluetoothWriteValueCallbacks> callbacks, | 153 std::unique_ptr<blink::WebBluetoothWriteValueCallbacks> callbacks, |
139 blink::mojom::WebBluetoothError error) { | 154 blink::mojom::WebBluetoothError error) { |
140 if (error == blink::mojom::WebBluetoothError::SUCCESS) { | 155 if (error == blink::mojom::WebBluetoothError::SUCCESS) { |
141 callbacks->onSuccess(value); | 156 callbacks->onSuccess(value); |
142 } else { | 157 } else { |
143 callbacks->onError(error); | 158 callbacks->onError(error); |
144 } | 159 } |
145 } | 160 } |
146 | 161 |
147 void WebBluetoothImpl::OnStartNotificationsComplete( | 162 void WebBluetoothImpl::OnStartNotificationsComplete( |
148 std::unique_ptr<blink::WebBluetoothNotificationsCallbacks> callbacks, | 163 std::unique_ptr<blink::WebBluetoothNotificationsCallbacks> callbacks, |
149 blink::mojom::WebBluetoothError error) { | 164 blink::mojom::WebBluetoothError error) { |
150 if (error == blink::mojom::WebBluetoothError::SUCCESS) { | 165 if (error == blink::mojom::WebBluetoothError::SUCCESS) { |
151 callbacks->onSuccess(); | 166 callbacks->onSuccess(); |
152 } else { | 167 } else { |
153 callbacks->onError(error); | 168 callbacks->onError(error); |
154 } | 169 } |
155 } | 170 } |
156 | 171 |
157 void WebBluetoothImpl::OnStopNotificationsComplete( | 172 void WebBluetoothImpl::OnStopNotificationsComplete( |
158 std::unique_ptr<blink::WebBluetoothNotificationsCallbacks> callbacks) { | 173 std::unique_ptr<blink::WebBluetoothNotificationsCallbacks> callbacks) { |
159 callbacks->onSuccess(); | 174 callbacks->onSuccess(); |
160 } | 175 } |
161 | 176 |
| 177 void WebBluetoothImpl::DispatchCharacteristicValueChanged( |
| 178 const std::string& characteristic_instance_id, |
| 179 const std::vector<uint8_t>& value) { |
| 180 auto active_iter = active_characteristics_.find(characteristic_instance_id); |
| 181 if (active_iter != active_characteristics_.end()) { |
| 182 active_iter->second->dispatchCharacteristicValueChanged(value); |
| 183 } |
| 184 } |
| 185 |
162 BluetoothDispatcher* WebBluetoothImpl::GetDispatcher() { | 186 BluetoothDispatcher* WebBluetoothImpl::GetDispatcher() { |
163 return BluetoothDispatcher::GetOrCreateThreadSpecificInstance( | 187 return BluetoothDispatcher::GetOrCreateThreadSpecificInstance( |
164 thread_safe_sender_.get()); | 188 thread_safe_sender_.get()); |
165 } | 189 } |
166 | 190 |
167 blink::mojom::WebBluetoothService& WebBluetoothImpl::GetWebBluetoothService() { | 191 blink::mojom::WebBluetoothService& WebBluetoothImpl::GetWebBluetoothService() { |
168 if (!web_bluetooth_service_) { | 192 if (!web_bluetooth_service_) { |
169 service_registry_->ConnectToRemoteService( | 193 service_registry_->ConnectToRemoteService( |
170 mojo::GetProxy(&web_bluetooth_service_)); | 194 mojo::GetProxy(&web_bluetooth_service_)); |
171 // Create an associated interface ptr and pass it to the WebBluetoothService | 195 // Create an associated interface ptr and pass it to the WebBluetoothService |
172 // so that it can send us events without us prompting. | 196 // so that it can send us events without us prompting. |
173 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo ptr_info; | 197 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo ptr_info; |
174 binding_.Bind(&ptr_info, web_bluetooth_service_.associated_group()); | 198 binding_.Bind(&ptr_info, web_bluetooth_service_.associated_group()); |
175 web_bluetooth_service_->SetClient(std::move(ptr_info)); | 199 web_bluetooth_service_->SetClient(std::move(ptr_info)); |
176 } | 200 } |
177 return *web_bluetooth_service_; | 201 return *web_bluetooth_service_; |
178 } | 202 } |
179 | 203 |
180 } // namespace content | 204 } // namespace content |
OLD | NEW |