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.
5 #ifndef CONTENT_CHILD_BLUETOOTH_BLUETOOTH_DISPATCHER_H_
6 #define CONTENT_CHILD_BLUETOOTH_BLUETOOTH_DISPATCHER_H_
8 #include "base/id_map.h"
9 #include "base/memory/ref_counted.h"
10 #include "content/child/worker_task_runner.h"
11 #include "content/common/bluetooth/bluetooth_device.h"
12 #include "content/common/bluetooth/bluetooth_error.h"
13 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetooth.h"
25 class ThreadSafeSender
;
27 // Dispatcher for child process threads which communicates to the browser's
28 // BluetoothDispatcherHost.
30 // Instances are created for each thread as necessary by WebBluetoothImpl.
32 // Incoming IPC messages are received by the BluetoothMessageFilter and
33 // directed to the thread specific instance of this class.
34 // Outgoing messages come from WebBluetoothImpl.
35 class BluetoothDispatcher
: public WorkerTaskRunner::Observer
{
37 explicit BluetoothDispatcher(ThreadSafeSender
* sender
);
38 ~BluetoothDispatcher() override
;
40 // Gets or Creates a BluetoothDispatcher for the current thread.
41 // |thread_safe_sender| is required when constructing a BluetoothDispatcher.
42 static BluetoothDispatcher
* GetOrCreateThreadSpecificInstance(
43 ThreadSafeSender
* thread_safe_sender
);
45 // IPC Send and Receiving interface, see IPC::Sender and IPC::Listener.
46 bool Send(IPC::Message
* msg
);
47 void OnMessageReceived(const IPC::Message
& msg
);
49 // Corresponding to WebBluetoothImpl methods.
50 void requestDevice(blink::WebBluetoothRequestDeviceCallbacks
* callbacks
);
51 void connectGATT(const blink::WebString
& device_instance_id
,
52 blink::WebBluetoothConnectGATTCallbacks
* callbacks
);
53 void SetBluetoothMockDataSetForTesting(const std::string
& name
);
55 // WorkerTaskRunner::Observer implementation.
56 void OnWorkerRunLoopStopped() override
;
59 // IPC Handlers, see definitions in bluetooth_messages.h.
60 void OnRequestDeviceSuccess(int thread_id
,
62 const BluetoothDevice
& device
);
63 void OnRequestDeviceError(int thread_id
,
65 BluetoothError error_type
);
67 void OnConnectGATTSuccess(int thread_id
,
69 const std::string
& message
);
71 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
73 // Tracks device requests sent to browser to match replies with callbacks.
74 // Owns callback objects.
75 IDMap
<blink::WebBluetoothRequestDeviceCallbacks
, IDMapOwnPointer
>
77 // Tracks requests to connect to a device.
78 // Owns callback objects.
79 IDMap
<blink::WebBluetoothConnectGATTCallbacks
, IDMapOwnPointer
>
80 pending_connect_requests_
;
82 DISALLOW_COPY_AND_ASSIGN(BluetoothDispatcher
);
85 } // namespace content
87 #endif // CONTENT_CHILD_BLUETOOTH_BLUETOOTH_DISPATCHER_H_