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 EXTENSIONS_BROWSER_API_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_EVENT_DISPATCHER_H_
6 #define EXTENSIONS_BROWSER_API_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_EVENT_DISPATCHER_H_
8 #include "extensions/browser/api/api_resource_manager.h"
9 #include "extensions/browser/api/bluetooth_socket/bluetooth_api_socket.h"
10 #include "extensions/browser/browser_context_keyed_api_factory.h"
17 class BluetoothDevice
;
18 class BluetoothSocket
;
21 namespace extensions
{
23 class BluetoothApiSocket
;
26 namespace extensions
{
29 // Dispatch events related to "bluetooth" sockets from callback on native socket
30 // instances. There is one instance per browser context.
31 class BluetoothSocketEventDispatcher
32 : public BrowserContextKeyedAPI
,
33 public base::SupportsWeakPtr
<BluetoothSocketEventDispatcher
> {
35 explicit BluetoothSocketEventDispatcher(content::BrowserContext
* context
);
36 ~BluetoothSocketEventDispatcher() override
;
38 // Socket is active, start receiving data from it.
39 void OnSocketConnect(const std::string
& extension_id
, int socket_id
);
41 // Socket is active again, start accepting connections from it.
42 void OnSocketListen(const std::string
& extension_id
, int socket_id
);
44 // Socket is active again, start receiving data from it.
45 void OnSocketResume(const std::string
& extension_id
, int socket_id
);
47 // BrowserContextKeyedAPI implementation.
48 static BrowserContextKeyedAPIFactory
<BluetoothSocketEventDispatcher
>*
51 // Convenience method to get the SocketEventDispatcher for a profile.
52 static BluetoothSocketEventDispatcher
* Get(content::BrowserContext
* context
);
55 typedef ApiResourceManager
<BluetoothApiSocket
>::ApiResourceData SocketData
;
56 friend class BrowserContextKeyedAPIFactory
<BluetoothSocketEventDispatcher
>;
57 // BrowserContextKeyedAPI implementation.
58 static const char* service_name() { return "BluetoothSocketEventDispatcher"; }
59 static const bool kServiceHasOwnInstanceInIncognito
= true;
60 static const bool kServiceIsNULLWhileTesting
= true;
62 // base::Bind supports methods with up to 6 parameters. SocketParams is used
63 // as a workaround that limitation for invoking StartReceive() and
69 content::BrowserThread::ID thread_id
;
70 void* browser_context_id
;
71 std::string extension_id
;
72 scoped_refptr
<SocketData
> sockets
;
76 // Start a receive and register a callback.
77 static void StartReceive(const SocketParams
& params
);
79 // Called when socket receive data.
80 static void ReceiveCallback(const SocketParams
& params
,
82 scoped_refptr
<net::IOBuffer
> io_buffer
);
84 // Called when socket receive data.
85 static void ReceiveErrorCallback(const SocketParams
& params
,
86 BluetoothApiSocket::ErrorReason error_reason
,
87 const std::string
& error
);
89 // Start an accept and register a callback.
90 static void StartAccept(const SocketParams
& params
);
92 // Called when socket accepts a client connection.
93 static void AcceptCallback(const SocketParams
& params
,
94 const device::BluetoothDevice
* device
,
95 scoped_refptr
<device::BluetoothSocket
> socket
);
97 // Called when socket encounters an error while accepting a client connection.
98 static void AcceptErrorCallback(const SocketParams
& params
,
99 BluetoothApiSocket::ErrorReason error_reason
,
100 const std::string
& error
);
102 // Post an extension event from IO to UI thread
103 static void PostEvent(const SocketParams
& params
, scoped_ptr
<Event
> event
);
105 // Dispatch an extension event on to EventRouter instance on UI thread.
106 static void DispatchEvent(void* browser_context_id
,
107 const std::string
& extension_id
,
108 scoped_ptr
<Event
> event
);
110 // Usually FILE thread (except for unit testing).
111 content::BrowserThread::ID thread_id_
;
112 content::BrowserContext
* const browser_context_
;
113 scoped_refptr
<SocketData
> sockets_
;
116 } // namespace core_api
117 } // namespace extensions
119 #endif // EXTENSIONS_BROWSER_API_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_EVENT_DISPATCHER_H_