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/thread_safe_sender.h" | 11 #include "content/child/thread_safe_sender.h" |
12 #include "content/common/bluetooth/bluetooth_messages.h" | 12 #include "content/common/bluetooth/bluetooth_messages.h" |
13 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevic
e.h" | 13 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevic
e.h" |
14 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothError
.h" | 14 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothError
.h" |
15 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothGATTR
emoteServer.h" | 15 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothGATTR
emoteServer.h" |
| 16 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothGATTS
ervice.h" |
16 | 17 |
17 using blink::WebBluetoothConnectGATTCallbacks; | 18 using blink::WebBluetoothConnectGATTCallbacks; |
18 using blink::WebBluetoothDevice; | 19 using blink::WebBluetoothDevice; |
19 using blink::WebBluetoothError; | 20 using blink::WebBluetoothError; |
20 using blink::WebBluetoothGATTRemoteServer; | 21 using blink::WebBluetoothGATTRemoteServer; |
| 22 using blink::WebBluetoothGATTService; |
21 using blink::WebBluetoothRequestDeviceCallbacks; | 23 using blink::WebBluetoothRequestDeviceCallbacks; |
22 using blink::WebString; | 24 using blink::WebString; |
23 using blink::WebVector; | 25 using blink::WebVector; |
24 | 26 |
| 27 namespace { |
| 28 |
| 29 struct BluetoothPrimaryServiceRequest { |
| 30 BluetoothPrimaryServiceRequest( |
| 31 blink::WebString device_instance_id, |
| 32 blink::WebString service_uuid, |
| 33 blink::WebBluetoothGetPrimaryServiceCallbacks* callbacks) |
| 34 : device_instance_id(device_instance_id), |
| 35 service_uuid(service_uuid), |
| 36 callbacks(callbacks) {} |
| 37 ~BluetoothPrimaryServiceRequest() {} |
| 38 |
| 39 blink::WebString device_instance_id; |
| 40 blink::WebString service_uuid; |
| 41 scoped_ptr<blink::WebBluetoothGetPrimaryServiceCallbacks> callbacks; |
| 42 }; |
| 43 |
| 44 } // namespace |
| 45 |
25 namespace content { | 46 namespace content { |
26 | 47 |
27 namespace { | 48 namespace { |
28 | 49 |
29 base::LazyInstance<base::ThreadLocalPointer<BluetoothDispatcher>>::Leaky | 50 base::LazyInstance<base::ThreadLocalPointer<BluetoothDispatcher>>::Leaky |
30 g_dispatcher_tls = LAZY_INSTANCE_INITIALIZER; | 51 g_dispatcher_tls = LAZY_INSTANCE_INITIALIZER; |
31 | 52 |
32 BluetoothDispatcher* const kHasBeenDeleted = | 53 BluetoothDispatcher* const kHasBeenDeleted = |
33 reinterpret_cast<BluetoothDispatcher*>(0x1); | 54 reinterpret_cast<BluetoothDispatcher*>(0x1); |
34 | 55 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 } | 116 } |
96 | 117 |
97 void BluetoothDispatcher::OnMessageReceived(const IPC::Message& msg) { | 118 void BluetoothDispatcher::OnMessageReceived(const IPC::Message& msg) { |
98 bool handled = true; | 119 bool handled = true; |
99 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcher, msg) | 120 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcher, msg) |
100 IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceSuccess, | 121 IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceSuccess, |
101 OnRequestDeviceSuccess); | 122 OnRequestDeviceSuccess); |
102 IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceError, OnRequestDeviceError); | 123 IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceError, OnRequestDeviceError); |
103 IPC_MESSAGE_HANDLER(BluetoothMsg_ConnectGATTSuccess, OnConnectGATTSuccess); | 124 IPC_MESSAGE_HANDLER(BluetoothMsg_ConnectGATTSuccess, OnConnectGATTSuccess); |
104 IPC_MESSAGE_HANDLER(BluetoothMsg_ConnectGATTError, OnConnectGATTError); | 125 IPC_MESSAGE_HANDLER(BluetoothMsg_ConnectGATTError, OnConnectGATTError); |
| 126 IPC_MESSAGE_HANDLER(BluetoothMsg_GetPrimaryServiceSuccess, |
| 127 OnGetPrimaryServiceSuccess); |
| 128 IPC_MESSAGE_HANDLER(BluetoothMsg_GetPrimaryServiceError, |
| 129 OnGetPrimaryServiceError); |
105 IPC_MESSAGE_UNHANDLED(handled = false) | 130 IPC_MESSAGE_UNHANDLED(handled = false) |
106 IPC_END_MESSAGE_MAP() | 131 IPC_END_MESSAGE_MAP() |
107 DCHECK(handled) << "Unhandled message:" << msg.type(); | 132 DCHECK(handled) << "Unhandled message:" << msg.type(); |
108 } | 133 } |
109 | 134 |
110 void BluetoothDispatcher::requestDevice( | 135 void BluetoothDispatcher::requestDevice( |
111 blink::WebBluetoothRequestDeviceCallbacks* callbacks) { | 136 blink::WebBluetoothRequestDeviceCallbacks* callbacks) { |
112 int request_id = pending_requests_.Add(callbacks); | 137 int request_id = pending_requests_.Add(callbacks); |
113 Send(new BluetoothHostMsg_RequestDevice(CurrentWorkerId(), request_id)); | 138 Send(new BluetoothHostMsg_RequestDevice(CurrentWorkerId(), request_id)); |
114 } | 139 } |
115 | 140 |
116 void BluetoothDispatcher::connectGATT( | 141 void BluetoothDispatcher::connectGATT( |
117 const blink::WebString& device_instance_id, | 142 const blink::WebString& device_instance_id, |
118 blink::WebBluetoothConnectGATTCallbacks* callbacks) { | 143 blink::WebBluetoothConnectGATTCallbacks* callbacks) { |
119 int request_id = pending_connect_requests_.Add(callbacks); | 144 int request_id = pending_connect_requests_.Add(callbacks); |
120 Send(new BluetoothHostMsg_ConnectGATT(CurrentWorkerId(), request_id, | 145 Send(new BluetoothHostMsg_ConnectGATT(CurrentWorkerId(), request_id, |
121 device_instance_id.utf8())); | 146 device_instance_id.utf8())); |
122 } | 147 } |
123 | 148 |
| 149 void BluetoothDispatcher::getPrimaryService( |
| 150 const blink::WebString& device_instance_id, |
| 151 const blink::WebString& service_uuid, |
| 152 blink::WebBluetoothGetPrimaryServiceCallbacks* callbacks) { |
| 153 int request_id = |
| 154 pending_primary_service_requests_.Add(new BluetoothPrimaryServiceRequest( |
| 155 device_instance_id, service_uuid, callbacks)); |
| 156 Send(new BluetoothHostMsg_GetPrimaryService(CurrentWorkerId(), request_id, |
| 157 device_instance_id.utf8(), |
| 158 service_uuid.utf8())); |
| 159 } |
| 160 |
124 void BluetoothDispatcher::OnWorkerRunLoopStopped() { | 161 void BluetoothDispatcher::OnWorkerRunLoopStopped() { |
125 delete this; | 162 delete this; |
126 } | 163 } |
127 | 164 |
128 void BluetoothDispatcher::OnRequestDeviceSuccess( | 165 void BluetoothDispatcher::OnRequestDeviceSuccess( |
129 int thread_id, | 166 int thread_id, |
130 int request_id, | 167 int request_id, |
131 const BluetoothDevice& device) { | 168 const BluetoothDevice& device) { |
132 DCHECK(pending_requests_.Lookup(request_id)) << request_id; | 169 DCHECK(pending_requests_.Lookup(request_id)) << request_id; |
133 | 170 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 BluetoothError error_type) { | 209 BluetoothError error_type) { |
173 DCHECK(pending_connect_requests_.Lookup(request_id)) << request_id; | 210 DCHECK(pending_connect_requests_.Lookup(request_id)) << request_id; |
174 pending_connect_requests_.Lookup(request_id) | 211 pending_connect_requests_.Lookup(request_id) |
175 ->onError(new WebBluetoothError( | 212 ->onError(new WebBluetoothError( |
176 // TODO(ortuno): Return more descriptive error messages. | 213 // TODO(ortuno): Return more descriptive error messages. |
177 // http://crbug.com/490419 | 214 // http://crbug.com/490419 |
178 WebBluetoothErrorFromBluetoothError(error_type), "")); | 215 WebBluetoothErrorFromBluetoothError(error_type), "")); |
179 pending_connect_requests_.Remove(request_id); | 216 pending_connect_requests_.Remove(request_id); |
180 } | 217 } |
181 | 218 |
| 219 void BluetoothDispatcher::OnGetPrimaryServiceSuccess( |
| 220 int thread_id, |
| 221 int request_id, |
| 222 const std::string& service_instance_id) { |
| 223 DCHECK(pending_primary_service_requests_.Lookup(request_id)) << request_id; |
| 224 BluetoothPrimaryServiceRequest* request = |
| 225 pending_primary_service_requests_.Lookup(request_id); |
| 226 request->callbacks->onSuccess(new WebBluetoothGATTService( |
| 227 WebString::fromUTF8(service_instance_id), request->service_uuid, |
| 228 true /* isPrimary */, request->device_instance_id)); |
| 229 pending_primary_service_requests_.Remove(request_id); |
| 230 } |
| 231 |
| 232 void BluetoothDispatcher::OnGetPrimaryServiceError(int thread_id, |
| 233 int request_id, |
| 234 BluetoothError error_type) { |
| 235 DCHECK(pending_primary_service_requests_.Lookup(request_id)) << request_id; |
| 236 |
| 237 // Since we couldn't find the service return null. See Step 3 of |
| 238 // getPrimaryService algorithm: |
| 239 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothgattremoteserv
er-getprimaryservice |
| 240 if (error_type == BluetoothError::NOT_FOUND) { |
| 241 pending_primary_service_requests_.Lookup(request_id) |
| 242 ->callbacks->onSuccess(nullptr); |
| 243 pending_primary_service_requests_.Remove(request_id); |
| 244 return; |
| 245 } |
| 246 |
| 247 pending_primary_service_requests_.Lookup(request_id) |
| 248 ->callbacks->onError(new WebBluetoothError( |
| 249 // TODO(ortuno): Return more descriptive error messages. |
| 250 // http://crbug.com/490419 |
| 251 WebBluetoothErrorFromBluetoothError(error_type), "")); |
| 252 pending_primary_service_requests_.Remove(request_id); |
| 253 } |
| 254 |
182 } // namespace content | 255 } // namespace content |
OLD | NEW |