| Index: content/child/bluetooth/bluetooth_dispatcher.cc
|
| diff --git a/content/child/bluetooth/bluetooth_dispatcher.cc b/content/child/bluetooth/bluetooth_dispatcher.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..0b11a2f4843040cd17f971e0d7e2adddcd351db4
|
| --- /dev/null
|
| +++ b/content/child/bluetooth/bluetooth_dispatcher.cc
|
| @@ -0,0 +1,142 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "content/child/bluetooth/bluetooth_dispatcher.h"
|
| +
|
| +#include "base/lazy_instance.h"
|
| +#include "base/memory/scoped_ptr.h"
|
| +#include "base/message_loop/message_loop.h"
|
| +#include "base/thread_task_runner_handle.h"
|
| +#include "content/child/thread_safe_sender.h"
|
| +#include "content/child/worker_thread_task_runner.h"
|
| +#include "content/common/bluetooth/bluetooth_messages.h"
|
| +#include "third_party/WebKit/public/platform/WebBluetoothDevice.h"
|
| +#include "third_party/WebKit/public/platform/WebBluetoothError.h"
|
| +
|
| +using blink::WebBluetoothDevice;
|
| +using blink::WebBluetoothError;
|
| +using blink::WebBluetoothRequestDeviceCallbacks;
|
| +using blink::WebString;
|
| +
|
| +namespace content {
|
| +
|
| +namespace {
|
| +
|
| +base::LazyInstance<base::ThreadLocalPointer<BluetoothDispatcher>>::Leaky
|
| + g_dispatcher_tls = LAZY_INSTANCE_INITIALIZER;
|
| +
|
| +BluetoothDispatcher* const kHasBeenDeleted =
|
| + reinterpret_cast<BluetoothDispatcher*>(0x1);
|
| +
|
| +int CurrentWorkerId() {
|
| + return WorkerTaskRunner::Instance()->CurrentWorkerId();
|
| +}
|
| +
|
| +WebBluetoothError::ErrorType WebBluetoothErrorFromBluetoothError(
|
| + BluetoothError error_type) {
|
| + switch (error_type) {
|
| + case BluetoothError::NOT_FOUND:
|
| + return WebBluetoothError::NotFoundError;
|
| + case BluetoothError::SECURITY:
|
| + return WebBluetoothError::SecurityError;
|
| + }
|
| + NOTIMPLEMENTED();
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +BluetoothDispatcher::BluetoothDispatcher(ThreadSafeSender* sender)
|
| + : thread_safe_sender_(sender) {
|
| + g_dispatcher_tls.Pointer()->Set(this);
|
| +}
|
| +
|
| +BluetoothDispatcher::~BluetoothDispatcher() {
|
| + g_dispatcher_tls.Pointer()->Set(kHasBeenDeleted);
|
| +}
|
| +
|
| +BluetoothDispatcher* BluetoothDispatcher::GetOrCreateThreadSpecificInstance(
|
| + ThreadSafeSender* thread_safe_sender) {
|
| + if (g_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted) {
|
| + NOTREACHED() << "Re-instantiating TLS BluetoothDispatcher.";
|
| + g_dispatcher_tls.Pointer()->Set(NULL);
|
| + }
|
| + if (g_dispatcher_tls.Pointer()->Get())
|
| + return g_dispatcher_tls.Pointer()->Get();
|
| +
|
| + BluetoothDispatcher* dispatcher = new BluetoothDispatcher(thread_safe_sender);
|
| + if (WorkerTaskRunner::Instance()->CurrentWorkerId())
|
| + WorkerTaskRunner::Instance()->AddStopObserver(dispatcher);
|
| + return dispatcher;
|
| +}
|
| +
|
| +bool BluetoothDispatcher::Send(IPC::Message* msg) {
|
| + return thread_safe_sender_->Send(msg);
|
| +}
|
| +
|
| +void BluetoothDispatcher::OnMessageReceived(const IPC::Message& msg) {
|
| + bool handled = true;
|
| + IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcher, msg)
|
| + IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceSuccess,
|
| + OnRequestDeviceSuccess);
|
| + IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceError, OnRequestDeviceError);
|
| + IPC_MESSAGE_UNHANDLED(handled = false)
|
| + IPC_END_MESSAGE_MAP()
|
| + DCHECK(handled) << "Unhandled message:" << msg.type();
|
| +}
|
| +
|
| +// TODO: Remove old void version after crrev.com/715613005 lands.
|
| +void BluetoothDispatcher::requestDevice(
|
| + blink::WebCallbacks<void, blink::WebBluetoothError>* callbacks) {
|
| + int request_id = pending_requests_old_.Add(callbacks);
|
| + Send(new BluetoothHostMsg_RequestDevice(CurrentWorkerId(), request_id));
|
| +}
|
| +
|
| +void BluetoothDispatcher::requestDevice(
|
| + blink::WebCallbacks<blink::WebBluetoothDevice, blink::WebBluetoothError>*
|
| + callbacks) {
|
| + int request_id = pending_requests_.Add(callbacks);
|
| + Send(new BluetoothHostMsg_RequestDevice(CurrentWorkerId(), request_id));
|
| +}
|
| +
|
| +void BluetoothDispatcher::SetBluetoothMockDataSetForTesting(
|
| + const std::string& name) {
|
| + Send(new BluetoothHostMsg_SetBluetoothMockDataSetForTesting(name));
|
| +}
|
| +
|
| +void BluetoothDispatcher::OnWorkerRunLoopStopped() {
|
| + delete this;
|
| +}
|
| +
|
| +void BluetoothDispatcher::OnRequestDeviceSuccess(int thread_id,
|
| + int request_id,
|
| + const std::string& device_id) {
|
| + // TODO: Remove old void version after crrev.com/715613005 lands.
|
| + if (pending_requests_old_.Lookup(request_id)) {
|
| + pending_requests_old_.Lookup(request_id)->onSuccess();
|
| + pending_requests_old_.Remove(request_id);
|
| + } else {
|
| + pending_requests_.Lookup(request_id)
|
| + ->onSuccess(new WebBluetoothDevice(WebString::fromUTF8(device_id)));
|
| + pending_requests_.Remove(request_id);
|
| + }
|
| +}
|
| +
|
| +void BluetoothDispatcher::OnRequestDeviceError(int thread_id,
|
| + int request_id,
|
| + BluetoothError error_type) {
|
| + // TODO: Remove old void version after crrev.com/715613005 lands.
|
| + if (pending_requests_old_.Lookup(request_id)) {
|
| + pending_requests_old_.Lookup(request_id)
|
| + ->onError(new WebBluetoothError(
|
| + WebBluetoothErrorFromBluetoothError(error_type), ""));
|
| + pending_requests_old_.Remove(request_id);
|
| + } else {
|
| + pending_requests_.Lookup(request_id)
|
| + ->onError(new WebBluetoothError(
|
| + WebBluetoothErrorFromBluetoothError(error_type), ""));
|
| + pending_requests_.Remove(request_id);
|
| + }
|
| +}
|
| +
|
| +} // namespace content
|
|
|