| 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 | 16 |
| 17 using blink::WebBluetoothConnectGATTCallbacks; |
| 16 using blink::WebBluetoothDevice; | 18 using blink::WebBluetoothDevice; |
| 17 using blink::WebBluetoothError; | 19 using blink::WebBluetoothError; |
| 20 using blink::WebBluetoothGATTRemoteServer; |
| 18 using blink::WebBluetoothRequestDeviceCallbacks; | 21 using blink::WebBluetoothRequestDeviceCallbacks; |
| 19 using blink::WebString; | 22 using blink::WebString; |
| 20 using blink::WebVector; | 23 using blink::WebVector; |
| 21 | 24 |
| 22 namespace content { | 25 namespace content { |
| 23 | 26 |
| 24 namespace { | 27 namespace { |
| 25 | 28 |
| 26 base::LazyInstance<base::ThreadLocalPointer<BluetoothDispatcher>>::Leaky | 29 base::LazyInstance<base::ThreadLocalPointer<BluetoothDispatcher>>::Leaky |
| 27 g_dispatcher_tls = LAZY_INSTANCE_INITIALIZER; | 30 g_dispatcher_tls = LAZY_INSTANCE_INITIALIZER; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 bool BluetoothDispatcher::Send(IPC::Message* msg) { | 91 bool BluetoothDispatcher::Send(IPC::Message* msg) { |
| 89 return thread_safe_sender_->Send(msg); | 92 return thread_safe_sender_->Send(msg); |
| 90 } | 93 } |
| 91 | 94 |
| 92 void BluetoothDispatcher::OnMessageReceived(const IPC::Message& msg) { | 95 void BluetoothDispatcher::OnMessageReceived(const IPC::Message& msg) { |
| 93 bool handled = true; | 96 bool handled = true; |
| 94 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcher, msg) | 97 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcher, msg) |
| 95 IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceSuccess, | 98 IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceSuccess, |
| 96 OnRequestDeviceSuccess); | 99 OnRequestDeviceSuccess); |
| 97 IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceError, OnRequestDeviceError); | 100 IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceError, OnRequestDeviceError); |
| 101 IPC_MESSAGE_HANDLER(BluetoothMsg_ConnectGATTSuccess, OnConnectGATTSuccess); |
| 98 IPC_MESSAGE_UNHANDLED(handled = false) | 102 IPC_MESSAGE_UNHANDLED(handled = false) |
| 99 IPC_END_MESSAGE_MAP() | 103 IPC_END_MESSAGE_MAP() |
| 100 DCHECK(handled) << "Unhandled message:" << msg.type(); | 104 DCHECK(handled) << "Unhandled message:" << msg.type(); |
| 101 } | 105 } |
| 102 | 106 |
| 103 void BluetoothDispatcher::requestDevice( | 107 void BluetoothDispatcher::requestDevice( |
| 104 blink::WebBluetoothRequestDeviceCallbacks* callbacks) { | 108 blink::WebBluetoothRequestDeviceCallbacks* callbacks) { |
| 105 int request_id = pending_requests_.Add(callbacks); | 109 int request_id = pending_requests_.Add(callbacks); |
| 106 Send(new BluetoothHostMsg_RequestDevice(CurrentWorkerId(), request_id)); | 110 Send(new BluetoothHostMsg_RequestDevice(CurrentWorkerId(), request_id)); |
| 107 } | 111 } |
| 108 | 112 |
| 113 void BluetoothDispatcher::connectGATT( |
| 114 const blink::WebString& device_instance_id, |
| 115 blink::WebBluetoothConnectGATTCallbacks* callbacks) { |
| 116 int request_id = pending_connect_requests_.Add(callbacks); |
| 117 Send(new BluetoothHostMsg_ConnectGATT(CurrentWorkerId(), request_id, |
| 118 device_instance_id.utf8())); |
| 119 } |
| 120 |
| 109 void BluetoothDispatcher::SetBluetoothMockDataSetForTesting( | 121 void BluetoothDispatcher::SetBluetoothMockDataSetForTesting( |
| 110 const std::string& name) { | 122 const std::string& name) { |
| 111 Send(new BluetoothHostMsg_SetBluetoothMockDataSetForTesting(name)); | 123 Send(new BluetoothHostMsg_SetBluetoothMockDataSetForTesting(name)); |
| 112 } | 124 } |
| 113 | 125 |
| 114 void BluetoothDispatcher::OnWorkerRunLoopStopped() { | 126 void BluetoothDispatcher::OnWorkerRunLoopStopped() { |
| 115 delete this; | 127 delete this; |
| 116 } | 128 } |
| 117 | 129 |
| 118 void BluetoothDispatcher::OnRequestDeviceSuccess( | 130 void BluetoothDispatcher::OnRequestDeviceSuccess( |
| 119 int thread_id, | 131 int thread_id, |
| 120 int request_id, | 132 int request_id, |
| 121 const BluetoothDevice& device) { | 133 const BluetoothDevice& device) { |
| 122 DCHECK(pending_requests_.Lookup(request_id)) << request_id; | 134 DCHECK(pending_requests_.Lookup(request_id)) << request_id; |
| 123 | 135 |
| 124 WebVector<WebString> uuids(device.uuids.size()); | 136 WebVector<WebString> uuids(device.uuids.size()); |
| 125 for (size_t i = 0; i < device.uuids.size(); ++i) | 137 for (size_t i = 0; i < device.uuids.size(); ++i) |
| 126 uuids[i] = WebString::fromUTF8(device.uuids[i].c_str()); | 138 uuids[i] = WebString::fromUTF8(device.uuids[i].c_str()); |
| 127 | 139 |
| 128 pending_requests_.Lookup(request_id) | 140 pending_requests_.Lookup(request_id) |
| 129 ->onSuccess(new WebBluetoothDevice( | 141 ->onSuccess(new WebBluetoothDevice( |
| 130 WebString::fromUTF8(device.instance_id), WebString(device.name), | 142 WebString::fromUTF8(device.instance_id), WebString(device.name), |
| 131 device.device_class, GetWebVendorIdSource(device.vendor_id_source), | 143 device.device_class, GetWebVendorIdSource(device.vendor_id_source), |
| 132 device.vendor_id, device.product_id, device.product_version, | 144 device.vendor_id, device.product_id, device.product_version, |
| 133 device.paired, device.connected, uuids)); | 145 device.paired, device.connected, uuids)); |
| 134 pending_requests_.Remove(request_id); | 146 pending_requests_.Remove(request_id); |
| 135 } | 147 } |
| 136 | 148 |
| 149 void BluetoothDispatcher::OnConnectGATTSuccess( |
| 150 int thread_id, |
| 151 int request_id, |
| 152 const std::string& device_instance_id) { |
| 153 DCHECK(pending_connect_requests_.Lookup(request_id)) << request_id; |
| 154 pending_connect_requests_.Lookup(request_id) |
| 155 ->onSuccess(new WebBluetoothGATTRemoteServer( |
| 156 WebString::fromUTF8(device_instance_id), true /* connected */)); |
| 157 pending_connect_requests_.Remove(request_id); |
| 158 } |
| 159 |
| 137 void BluetoothDispatcher::OnRequestDeviceError(int thread_id, | 160 void BluetoothDispatcher::OnRequestDeviceError(int thread_id, |
| 138 int request_id, | 161 int request_id, |
| 139 BluetoothError error_type) { | 162 BluetoothError error_type) { |
| 140 DCHECK(pending_requests_.Lookup(request_id)) << request_id; | 163 DCHECK(pending_requests_.Lookup(request_id)) << request_id; |
| 141 pending_requests_.Lookup(request_id) | 164 pending_requests_.Lookup(request_id) |
| 142 ->onError(new WebBluetoothError( | 165 ->onError(new WebBluetoothError( |
| 143 WebBluetoothErrorFromBluetoothError(error_type), "")); | 166 WebBluetoothErrorFromBluetoothError(error_type), "")); |
| 144 pending_requests_.Remove(request_id); | 167 pending_requests_.Remove(request_id); |
| 145 } | 168 } |
| 146 | 169 |
| 147 } // namespace content | 170 } // namespace content |
| OLD | NEW |