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