| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/child/bluetooth/bluetooth_dispatcher.h" |
| 6 |
| 7 #include "base/lazy_instance.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/thread_task_runner_handle.h" |
| 11 #include "content/child/thread_safe_sender.h" |
| 12 #include "content/child/worker_thread_task_runner.h" |
| 13 #include "content/common/bluetooth/bluetooth_messages.h" |
| 14 #include "third_party/WebKit/public/platform/WebBluetoothDevice.h" |
| 15 #include "third_party/WebKit/public/platform/WebBluetoothError.h" |
| 16 |
| 17 using blink::WebBluetoothDevice; |
| 18 using blink::WebBluetoothError; |
| 19 using blink::WebBluetoothRequestDeviceCallbacks; |
| 20 using blink::WebString; |
| 21 |
| 22 namespace content { |
| 23 |
| 24 namespace { |
| 25 |
| 26 base::LazyInstance<base::ThreadLocalPointer<BluetoothDispatcher>>::Leaky |
| 27 g_dispatcher_tls = LAZY_INSTANCE_INITIALIZER; |
| 28 |
| 29 BluetoothDispatcher* const kHasBeenDeleted = |
| 30 reinterpret_cast<BluetoothDispatcher*>(0x1); |
| 31 |
| 32 int CurrentWorkerId() { |
| 33 return WorkerTaskRunner::Instance()->CurrentWorkerId(); |
| 34 } |
| 35 |
| 36 WebBluetoothError::ErrorType WebBluetoothErrorFromBluetoothError( |
| 37 BluetoothError error_type) { |
| 38 switch (error_type) { |
| 39 case BluetoothError::NOT_FOUND: |
| 40 return WebBluetoothError::NotFoundError; |
| 41 case BluetoothError::SECURITY: |
| 42 return WebBluetoothError::SecurityError; |
| 43 } |
| 44 NOTIMPLEMENTED(); |
| 45 } |
| 46 |
| 47 } // namespace |
| 48 |
| 49 BluetoothDispatcher::BluetoothDispatcher(ThreadSafeSender* sender) |
| 50 : thread_safe_sender_(sender) { |
| 51 g_dispatcher_tls.Pointer()->Set(this); |
| 52 } |
| 53 |
| 54 BluetoothDispatcher::~BluetoothDispatcher() { |
| 55 g_dispatcher_tls.Pointer()->Set(kHasBeenDeleted); |
| 56 } |
| 57 |
| 58 BluetoothDispatcher* BluetoothDispatcher::GetOrCreateThreadSpecificInstance( |
| 59 ThreadSafeSender* thread_safe_sender) { |
| 60 if (g_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted) { |
| 61 NOTREACHED() << "Re-instantiating TLS BluetoothDispatcher."; |
| 62 g_dispatcher_tls.Pointer()->Set(NULL); |
| 63 } |
| 64 if (g_dispatcher_tls.Pointer()->Get()) |
| 65 return g_dispatcher_tls.Pointer()->Get(); |
| 66 |
| 67 BluetoothDispatcher* dispatcher = new BluetoothDispatcher(thread_safe_sender); |
| 68 if (WorkerTaskRunner::Instance()->CurrentWorkerId()) |
| 69 WorkerTaskRunner::Instance()->AddStopObserver(dispatcher); |
| 70 return dispatcher; |
| 71 } |
| 72 |
| 73 bool BluetoothDispatcher::Send(IPC::Message* msg) { |
| 74 return thread_safe_sender_->Send(msg); |
| 75 } |
| 76 |
| 77 void BluetoothDispatcher::OnMessageReceived(const IPC::Message& msg) { |
| 78 bool handled = true; |
| 79 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcher, msg) |
| 80 IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceSuccess, |
| 81 OnRequestDeviceSuccess); |
| 82 IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceError, OnRequestDeviceError); |
| 83 IPC_MESSAGE_UNHANDLED(handled = false) |
| 84 IPC_END_MESSAGE_MAP() |
| 85 DCHECK(handled) << "Unhandled message:" << msg.type(); |
| 86 } |
| 87 |
| 88 // TODO: Remove old void version after crrev.com/715613005 lands. |
| 89 void BluetoothDispatcher::requestDevice( |
| 90 blink::WebCallbacks<void, blink::WebBluetoothError>* callbacks) { |
| 91 int request_id = pending_requests_old_.Add(callbacks); |
| 92 Send(new BluetoothHostMsg_RequestDevice(CurrentWorkerId(), request_id)); |
| 93 } |
| 94 |
| 95 void BluetoothDispatcher::requestDevice( |
| 96 blink::WebCallbacks<blink::WebBluetoothDevice, blink::WebBluetoothError>* |
| 97 callbacks) { |
| 98 int request_id = pending_requests_.Add(callbacks); |
| 99 Send(new BluetoothHostMsg_RequestDevice(CurrentWorkerId(), request_id)); |
| 100 } |
| 101 |
| 102 void BluetoothDispatcher::SetBluetoothMockDataSetForTesting( |
| 103 const std::string& name) { |
| 104 Send(new BluetoothHostMsg_SetBluetoothMockDataSetForTesting(name)); |
| 105 } |
| 106 |
| 107 void BluetoothDispatcher::OnWorkerRunLoopStopped() { |
| 108 delete this; |
| 109 } |
| 110 |
| 111 void BluetoothDispatcher::OnRequestDeviceSuccess(int thread_id, |
| 112 int request_id, |
| 113 const std::string& device_id) { |
| 114 // TODO: Remove old void version after crrev.com/715613005 lands. |
| 115 if (pending_requests_old_.Lookup(request_id)) { |
| 116 pending_requests_old_.Lookup(request_id)->onSuccess(); |
| 117 pending_requests_old_.Remove(request_id); |
| 118 } else { |
| 119 pending_requests_.Lookup(request_id) |
| 120 ->onSuccess(new WebBluetoothDevice(WebString::fromUTF8(device_id))); |
| 121 pending_requests_.Remove(request_id); |
| 122 } |
| 123 } |
| 124 |
| 125 void BluetoothDispatcher::OnRequestDeviceError(int thread_id, |
| 126 int request_id, |
| 127 BluetoothError error_type) { |
| 128 // TODO: Remove old void version after crrev.com/715613005 lands. |
| 129 if (pending_requests_old_.Lookup(request_id)) { |
| 130 pending_requests_old_.Lookup(request_id) |
| 131 ->onError(new WebBluetoothError( |
| 132 WebBluetoothErrorFromBluetoothError(error_type), "")); |
| 133 pending_requests_old_.Remove(request_id); |
| 134 } else { |
| 135 pending_requests_.Lookup(request_id) |
| 136 ->onError(new WebBluetoothError( |
| 137 WebBluetoothErrorFromBluetoothError(error_type), "")); |
| 138 pending_requests_.Remove(request_id); |
| 139 } |
| 140 } |
| 141 |
| 142 } // namespace content |
| OLD | NEW |