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

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

Issue 1922923002: bluetooth: Move requestDevice to mojo (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bluetooth-separate-tests-request-device
Patch Set: Change ref to pointer Created 4 years, 6 months 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/renderer/bluetooth/bluetooth_dispatcher.h"
6
7 #include <stddef.h>
8
9 #include <memory>
10 #include <utility>
11
12 #include "base/lazy_instance.h"
13 #include "base/memory/ptr_util.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/threading/thread_task_runner_handle.h"
16 #include "content/child/thread_safe_sender.h"
17 #include "content/common/bluetooth/bluetooth_messages.h"
18 #include "device/bluetooth/bluetooth_uuid.h"
19 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevic eInit.h"
20 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothError .h"
21 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot eGATTService.h"
22 #include "third_party/WebKit/public/platform/modules/bluetooth/WebRequestDeviceO ptions.h"
23
24 using blink::WebBluetoothDeviceInit;
25 using blink::WebBluetoothError;
26 using blink::WebBluetoothRemoteGATTService;
27 using blink::WebBluetoothReadValueCallbacks;
28 using blink::WebBluetoothRequestDeviceCallbacks;
29 using blink::WebBluetoothScanFilter;
30 using blink::WebRequestDeviceOptions;
31 using blink::WebString;
32 using blink::WebVector;
33
34 namespace content {
35
36 namespace {
37
38 base::LazyInstance<base::ThreadLocalPointer<void>>::Leaky g_dispatcher_tls =
39 LAZY_INSTANCE_INITIALIZER;
40
41 void* const kHasBeenDeleted = reinterpret_cast<void*>(0x1);
42
43 int CurrentWorkerId() {
44 return WorkerThread::GetCurrentId();
45 }
46
47 } // namespace
48
49 BluetoothDispatcher::BluetoothDispatcher(ThreadSafeSender* sender)
50 : thread_safe_sender_(sender) {
51 g_dispatcher_tls.Pointer()->Set(static_cast<void*>(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 static_cast<BluetoothDispatcher*>(g_dispatcher_tls.Pointer()->Get());
66
67 BluetoothDispatcher* dispatcher = new BluetoothDispatcher(thread_safe_sender);
68 if (CurrentWorkerId())
69 WorkerThread::AddObserver(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 void BluetoothDispatcher::requestDevice(
89 int frame_routing_id,
90 const WebRequestDeviceOptions& options,
91 blink::WebBluetoothRequestDeviceCallbacks* callbacks) {
92 int request_id = pending_requests_.Add(callbacks);
93
94 // Convert |options| to its IPC form.
95 std::vector<content::BluetoothScanFilter> filters(options.filters.size());
96 for (size_t i = 0; i < options.filters.size(); ++i) {
97 const WebBluetoothScanFilter& web_filter = options.filters[i];
98 BluetoothScanFilter& filter = filters[i];
99 filter.services.reserve(web_filter.services.size());
100 for (const WebString& service : web_filter.services) {
101 filter.services.push_back(device::BluetoothUUID(service.utf8()));
102 }
103 filter.name = web_filter.name.utf8();
104 filter.namePrefix = web_filter.namePrefix.utf8();
105 }
106 std::vector<device::BluetoothUUID> optional_services;
107 optional_services.reserve(options.optionalServices.size());
108 for (const WebString& optional_service : options.optionalServices) {
109 optional_services.push_back(device::BluetoothUUID(optional_service.utf8()));
110 }
111
112 Send(new BluetoothHostMsg_RequestDevice(CurrentWorkerId(), request_id,
113 frame_routing_id, filters,
114 optional_services));
115 }
116
117 void BluetoothDispatcher::WillStopCurrentWorkerThread() {
118 delete this;
119 }
120
121 void BluetoothDispatcher::OnRequestDeviceSuccess(
122 int thread_id,
123 int request_id,
124 const BluetoothDevice& device) {
125 DCHECK(pending_requests_.Lookup(request_id)) << request_id;
126
127 WebVector<WebString> uuids(device.uuids.size());
128 for (size_t i = 0; i < device.uuids.size(); ++i)
129 uuids[i] = WebString::fromUTF8(device.uuids[i].c_str());
130
131 pending_requests_.Lookup(request_id)
132 ->onSuccess(base::WrapUnique(new WebBluetoothDeviceInit(
133 WebString::fromUTF8(device.id), WebString(device.name), uuids)));
134 pending_requests_.Remove(request_id);
135 }
136
137 void BluetoothDispatcher::OnRequestDeviceError(int thread_id,
138 int request_id,
139 WebBluetoothError error) {
140 DCHECK(pending_requests_.Lookup(request_id)) << request_id;
141 pending_requests_.Lookup(request_id)->onError(WebBluetoothError(error));
142 pending_requests_.Remove(request_id);
143 }
144
145 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/bluetooth/bluetooth_dispatcher.h ('k') | content/renderer/bluetooth/bluetooth_message_filter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698