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/child/bluetooth/bluetooth_dispatcher.h" | 5 #include "content/child/bluetooth/bluetooth_dispatcher.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
| 11 #include "content/child/bluetooth/bluetooth_primary_service_request.h" |
11 #include "content/child/thread_safe_sender.h" | 12 #include "content/child/thread_safe_sender.h" |
12 #include "content/common/bluetooth/bluetooth_messages.h" | 13 #include "content/common/bluetooth/bluetooth_messages.h" |
13 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevic
e.h" | 14 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevic
e.h" |
14 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothError
.h" | 15 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothError
.h" |
15 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothGATTR
emoteServer.h" | 16 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothGATTR
emoteServer.h" |
| 17 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothGATTS
ervice.h" |
16 | 18 |
17 using blink::WebBluetoothConnectGATTCallbacks; | 19 using blink::WebBluetoothConnectGATTCallbacks; |
18 using blink::WebBluetoothDevice; | 20 using blink::WebBluetoothDevice; |
19 using blink::WebBluetoothError; | 21 using blink::WebBluetoothError; |
20 using blink::WebBluetoothGATTRemoteServer; | 22 using blink::WebBluetoothGATTRemoteServer; |
| 23 using blink::WebBluetoothGATTService; |
21 using blink::WebBluetoothRequestDeviceCallbacks; | 24 using blink::WebBluetoothRequestDeviceCallbacks; |
22 using blink::WebString; | 25 using blink::WebString; |
23 using blink::WebVector; | 26 using blink::WebVector; |
24 | 27 |
25 namespace content { | 28 namespace content { |
26 | 29 |
27 namespace { | 30 namespace { |
28 | 31 |
29 base::LazyInstance<base::ThreadLocalPointer<BluetoothDispatcher>>::Leaky | 32 base::LazyInstance<base::ThreadLocalPointer<BluetoothDispatcher>>::Leaky |
30 g_dispatcher_tls = LAZY_INSTANCE_INITIALIZER; | 33 g_dispatcher_tls = LAZY_INSTANCE_INITIALIZER; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 } | 98 } |
96 | 99 |
97 void BluetoothDispatcher::OnMessageReceived(const IPC::Message& msg) { | 100 void BluetoothDispatcher::OnMessageReceived(const IPC::Message& msg) { |
98 bool handled = true; | 101 bool handled = true; |
99 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcher, msg) | 102 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcher, msg) |
100 IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceSuccess, | 103 IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceSuccess, |
101 OnRequestDeviceSuccess); | 104 OnRequestDeviceSuccess); |
102 IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceError, OnRequestDeviceError); | 105 IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceError, OnRequestDeviceError); |
103 IPC_MESSAGE_HANDLER(BluetoothMsg_ConnectGATTSuccess, OnConnectGATTSuccess); | 106 IPC_MESSAGE_HANDLER(BluetoothMsg_ConnectGATTSuccess, OnConnectGATTSuccess); |
104 IPC_MESSAGE_HANDLER(BluetoothMsg_ConnectGATTError, OnConnectGATTError); | 107 IPC_MESSAGE_HANDLER(BluetoothMsg_ConnectGATTError, OnConnectGATTError); |
| 108 IPC_MESSAGE_HANDLER(BluetoothMsg_GetPrimaryServiceSuccess, |
| 109 OnGetPrimaryServiceSuccess); |
| 110 IPC_MESSAGE_HANDLER(BluetoothMsg_GetPrimaryServiceError, |
| 111 OnGetPrimaryServiceError); |
105 IPC_MESSAGE_UNHANDLED(handled = false) | 112 IPC_MESSAGE_UNHANDLED(handled = false) |
106 IPC_END_MESSAGE_MAP() | 113 IPC_END_MESSAGE_MAP() |
107 DCHECK(handled) << "Unhandled message:" << msg.type(); | 114 DCHECK(handled) << "Unhandled message:" << msg.type(); |
108 } | 115 } |
109 | 116 |
110 void BluetoothDispatcher::requestDevice( | 117 void BluetoothDispatcher::requestDevice( |
111 blink::WebBluetoothRequestDeviceCallbacks* callbacks) { | 118 blink::WebBluetoothRequestDeviceCallbacks* callbacks) { |
112 int request_id = pending_requests_.Add(callbacks); | 119 int request_id = pending_requests_.Add(callbacks); |
113 Send(new BluetoothHostMsg_RequestDevice(CurrentWorkerId(), request_id)); | 120 Send(new BluetoothHostMsg_RequestDevice(CurrentWorkerId(), request_id)); |
114 } | 121 } |
115 | 122 |
116 void BluetoothDispatcher::connectGATT( | 123 void BluetoothDispatcher::connectGATT( |
117 const blink::WebString& device_instance_id, | 124 const blink::WebString& device_instance_id, |
118 blink::WebBluetoothConnectGATTCallbacks* callbacks) { | 125 blink::WebBluetoothConnectGATTCallbacks* callbacks) { |
119 int request_id = pending_connect_requests_.Add(callbacks); | 126 int request_id = pending_connect_requests_.Add(callbacks); |
120 Send(new BluetoothHostMsg_ConnectGATT(CurrentWorkerId(), request_id, | 127 Send(new BluetoothHostMsg_ConnectGATT(CurrentWorkerId(), request_id, |
121 device_instance_id.utf8())); | 128 device_instance_id.utf8())); |
122 } | 129 } |
123 | 130 |
| 131 void BluetoothDispatcher::getPrimaryService( |
| 132 const blink::WebString& device_instance_id, |
| 133 const blink::WebString& service_uuid, |
| 134 blink::WebBluetoothGetPrimaryServiceCallbacks* callbacks) { |
| 135 int request_id = |
| 136 pending_primary_service_requests_.Add(new BluetoothPrimaryServiceRequest( |
| 137 device_instance_id, service_uuid, callbacks)); |
| 138 Send(new BluetoothHostMsg_GetPrimaryService(CurrentWorkerId(), request_id, |
| 139 device_instance_id.utf8(), |
| 140 service_uuid.utf8())); |
| 141 } |
| 142 |
124 void BluetoothDispatcher::OnWorkerRunLoopStopped() { | 143 void BluetoothDispatcher::OnWorkerRunLoopStopped() { |
125 delete this; | 144 delete this; |
126 } | 145 } |
127 | 146 |
128 void BluetoothDispatcher::OnRequestDeviceSuccess( | 147 void BluetoothDispatcher::OnRequestDeviceSuccess( |
129 int thread_id, | 148 int thread_id, |
130 int request_id, | 149 int request_id, |
131 const BluetoothDevice& device) { | 150 const BluetoothDevice& device) { |
132 DCHECK(pending_requests_.Lookup(request_id)) << request_id; | 151 DCHECK(pending_requests_.Lookup(request_id)) << request_id; |
133 | 152 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 BluetoothError error_type) { | 191 BluetoothError error_type) { |
173 DCHECK(pending_connect_requests_.Lookup(request_id)) << request_id; | 192 DCHECK(pending_connect_requests_.Lookup(request_id)) << request_id; |
174 pending_connect_requests_.Lookup(request_id) | 193 pending_connect_requests_.Lookup(request_id) |
175 ->onError(new WebBluetoothError( | 194 ->onError(new WebBluetoothError( |
176 // TODO(ortuno): Return more descriptive error messages. | 195 // TODO(ortuno): Return more descriptive error messages. |
177 // http://crbug.com/490419 | 196 // http://crbug.com/490419 |
178 WebBluetoothErrorFromBluetoothError(error_type), "")); | 197 WebBluetoothErrorFromBluetoothError(error_type), "")); |
179 pending_connect_requests_.Remove(request_id); | 198 pending_connect_requests_.Remove(request_id); |
180 } | 199 } |
181 | 200 |
| 201 void BluetoothDispatcher::OnGetPrimaryServiceSuccess( |
| 202 int thread_id, |
| 203 int request_id, |
| 204 const std::string& service_instance_id) { |
| 205 DCHECK(pending_primary_service_requests_.Lookup(request_id)) << request_id; |
| 206 BluetoothPrimaryServiceRequest* request = |
| 207 pending_primary_service_requests_.Lookup(request_id); |
| 208 request->callbacks->onSuccess(new WebBluetoothGATTService( |
| 209 WebString::fromUTF8(service_instance_id), request->service_uuid, |
| 210 true /* isPrimary */, request->device_instance_id)); |
| 211 pending_primary_service_requests_.Remove(request_id); |
| 212 } |
| 213 |
| 214 void BluetoothDispatcher::OnGetPrimaryServiceError(int thread_id, |
| 215 int request_id, |
| 216 BluetoothError error_type) { |
| 217 DCHECK(pending_primary_service_requests_.Lookup(request_id)) << request_id; |
| 218 |
| 219 // Since we couldn't find the service return null. See Step 3 of |
| 220 // getPrimaryService algorithm: |
| 221 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothgattremoteserv
er-getprimaryservice |
| 222 if (error_type == BluetoothError::NOT_FOUND) { |
| 223 pending_primary_service_requests_.Lookup(request_id) |
| 224 ->callbacks->onSuccess(nullptr); |
| 225 pending_primary_service_requests_.Remove(request_id); |
| 226 return; |
| 227 } |
| 228 |
| 229 pending_primary_service_requests_.Lookup(request_id) |
| 230 ->callbacks->onError(new WebBluetoothError( |
| 231 // TODO(ortuno): Return more descriptive error messages. |
| 232 // http://crbug.com/490419 |
| 233 WebBluetoothErrorFromBluetoothError(error_type), "")); |
| 234 pending_primary_service_requests_.Remove(request_id); |
| 235 } |
| 236 |
182 } // namespace content | 237 } // namespace content |
OLD | NEW |