Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(208)

Side by Side Diff: content/child/bluetooth/bluetooth_dispatcher.cc

Issue 699843003: bluetooth: Impl uses new WebBluetoothDevice in content/browser. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkcr
Patch Set: Addressing comments from 12 - 14 Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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(
112 int thread_id,
113 int request_id,
114 const std::string& device_instance_id) {
115 // TODO: Remove old void version after crrev.com/715613005 lands.
116 if (pending_requests_old_.Lookup(request_id)) {
117 pending_requests_old_.Lookup(request_id)->onSuccess();
118 pending_requests_old_.Remove(request_id);
119 } else {
120 pending_requests_.Lookup(request_id)
121 ->onSuccess(
122 new WebBluetoothDevice(WebString::fromUTF8(device_instance_id)));
123 pending_requests_.Remove(request_id);
124 }
125 }
126
127 void BluetoothDispatcher::OnRequestDeviceError(int thread_id,
128 int request_id,
129 BluetoothError error_type) {
130 // TODO: Remove old void version after crrev.com/715613005 lands.
131 if (pending_requests_old_.Lookup(request_id)) {
132 pending_requests_old_.Lookup(request_id)
133 ->onError(new WebBluetoothError(
134 WebBluetoothErrorFromBluetoothError(error_type), ""));
135 pending_requests_old_.Remove(request_id);
136 } else {
137 pending_requests_.Lookup(request_id)
138 ->onError(new WebBluetoothError(
139 WebBluetoothErrorFromBluetoothError(error_type), ""));
140 pending_requests_.Remove(request_id);
141 }
142 }
143
144 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698