Chromium Code Reviews| 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 return WebBluetoothError::NotFoundError; | |
| 46 } | |
| 47 | |
| 48 } // namespace | |
| 49 | |
| 50 BluetoothDispatcher::BluetoothDispatcher(ThreadSafeSender* sender) | |
| 51 : thread_safe_sender_(sender) { | |
| 52 g_dispatcher_tls.Pointer()->Set(this); | |
| 53 } | |
| 54 | |
| 55 BluetoothDispatcher::~BluetoothDispatcher() { | |
| 56 g_dispatcher_tls.Pointer()->Set(kHasBeenDeleted); | |
| 57 } | |
| 58 | |
| 59 BluetoothDispatcher* BluetoothDispatcher::GetOrCreateThreadSpecificInstance( | |
| 60 ThreadSafeSender* thread_safe_sender) { | |
| 61 if (g_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted) { | |
| 62 NOTREACHED() << "Re-instantiating TLS BluetoothDispatcher."; | |
| 63 g_dispatcher_tls.Pointer()->Set(NULL); | |
| 64 } | |
| 65 if (g_dispatcher_tls.Pointer()->Get()) | |
| 66 return g_dispatcher_tls.Pointer()->Get(); | |
| 67 | |
| 68 BluetoothDispatcher* dispatcher = new BluetoothDispatcher(thread_safe_sender); | |
| 69 if (WorkerTaskRunner::Instance()->CurrentWorkerId()) | |
|
ortuno
2015/04/01 19:45:30
Why isn't the function "CurrentWorkerId()", define
scheib
2015/04/01 19:47:25
Oversight, this could be cleaned up.
| |
| 70 WorkerTaskRunner::Instance()->AddStopObserver(dispatcher); | |
| 71 return dispatcher; | |
| 72 } | |
| 73 | |
| 74 bool BluetoothDispatcher::Send(IPC::Message* msg) { | |
| 75 return thread_safe_sender_->Send(msg); | |
| 76 } | |
| 77 | |
| 78 void BluetoothDispatcher::OnMessageReceived(const IPC::Message& msg) { | |
| 79 bool handled = true; | |
| 80 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcher, msg) | |
| 81 IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceSuccess, | |
| 82 OnRequestDeviceSuccess); | |
| 83 IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceError, OnRequestDeviceError); | |
| 84 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 85 IPC_END_MESSAGE_MAP() | |
| 86 DCHECK(handled) << "Unhandled message:" << msg.type(); | |
| 87 } | |
| 88 | |
| 89 // TODO(scheib): Remove old void version after crrev.com/715613005 lands. | |
| 90 void BluetoothDispatcher::requestDevice( | |
| 91 blink::WebCallbacks<void, blink::WebBluetoothError>* callbacks) { | |
| 92 int request_id = pending_requests_old_.Add(callbacks); | |
| 93 Send(new BluetoothHostMsg_RequestDevice(CurrentWorkerId(), request_id)); | |
| 94 } | |
| 95 | |
| 96 void BluetoothDispatcher::requestDevice( | |
| 97 blink::WebCallbacks<blink::WebBluetoothDevice, blink::WebBluetoothError>* | |
| 98 callbacks) { | |
| 99 int request_id = pending_requests_.Add(callbacks); | |
| 100 Send(new BluetoothHostMsg_RequestDevice(CurrentWorkerId(), request_id)); | |
| 101 } | |
| 102 | |
| 103 void BluetoothDispatcher::SetBluetoothMockDataSetForTesting( | |
| 104 const std::string& name) { | |
| 105 Send(new BluetoothHostMsg_SetBluetoothMockDataSetForTesting(name)); | |
| 106 } | |
| 107 | |
| 108 void BluetoothDispatcher::OnWorkerRunLoopStopped() { | |
| 109 delete this; | |
| 110 } | |
| 111 | |
| 112 void BluetoothDispatcher::OnRequestDeviceSuccess( | |
| 113 int thread_id, | |
| 114 int request_id, | |
| 115 const std::string& device_instance_id) { | |
| 116 // TODO(scheib): Remove old void version after crrev.com/715613005 lands. | |
| 117 if (pending_requests_old_.Lookup(request_id)) { | |
| 118 pending_requests_old_.Lookup(request_id)->onSuccess(); | |
| 119 pending_requests_old_.Remove(request_id); | |
| 120 } else { | |
| 121 pending_requests_.Lookup(request_id) | |
| 122 ->onSuccess( | |
| 123 new WebBluetoothDevice(WebString::fromUTF8(device_instance_id))); | |
| 124 pending_requests_.Remove(request_id); | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 void BluetoothDispatcher::OnRequestDeviceError(int thread_id, | |
| 129 int request_id, | |
| 130 BluetoothError error_type) { | |
| 131 // TODO(scheib): Remove old void version after crrev.com/715613005 lands. | |
| 132 if (pending_requests_old_.Lookup(request_id)) { | |
| 133 pending_requests_old_.Lookup(request_id) | |
| 134 ->onError(new WebBluetoothError( | |
| 135 WebBluetoothErrorFromBluetoothError(error_type), "")); | |
| 136 pending_requests_old_.Remove(request_id); | |
| 137 } else { | |
| 138 pending_requests_.Lookup(request_id) | |
| 139 ->onError(new WebBluetoothError( | |
| 140 WebBluetoothErrorFromBluetoothError(error_type), "")); | |
| 141 pending_requests_.Remove(request_id); | |
| 142 } | |
| 143 } | |
| 144 | |
| 145 } // namespace content | |
| OLD | NEW |