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 CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_EVENT_DISPATCHER_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_EVENT_DISPATCHER_H_
8 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_socket.h"
9 #include "extensions/browser/api/api_resource_manager.h"
10 #include "extensions/browser/browser_context_keyed_api_factory.h"
16 namespace extensions {
18 class BluetoothApiSocket;
21 namespace extensions {
24 // Dispatch events related to "bluetooth" sockets from callback on native socket
25 // instances. There is one instance per browser context.
26 class BluetoothSocketEventDispatcher
27 : public BrowserContextKeyedAPI,
28 public base::SupportsWeakPtr<BluetoothSocketEventDispatcher> {
30 explicit BluetoothSocketEventDispatcher(content::BrowserContext* context);
31 virtual ~BluetoothSocketEventDispatcher();
33 // Socket is active, start receiving data from it.
34 void OnSocketConnect(const std::string& extension_id, int socket_id);
36 // Socket is active again, start receiving data from it.
37 void OnSocketResume(const std::string& extension_id, int socket_id);
39 // BrowserContextKeyedAPI implementation.
40 static BrowserContextKeyedAPIFactory<BluetoothSocketEventDispatcher>*
43 // Convenience method to get the SocketEventDispatcher for a profile.
44 static BluetoothSocketEventDispatcher* Get(content::BrowserContext* context);
47 typedef ApiResourceManager<BluetoothApiSocket>::ApiResourceData SocketData;
48 friend class BrowserContextKeyedAPIFactory<BluetoothSocketEventDispatcher>;
49 // BrowserContextKeyedAPI implementation.
50 static const char* service_name() { return "BluetoothSocketEventDispatcher"; }
51 static const bool kServiceHasOwnInstanceInIncognito = true;
52 static const bool kServiceIsNULLWhileTesting = true;
54 // base::Bind supports methods with up to 6 parameters. ReceiveParams is used
55 // as a workaround that limitation for invoking StartReceive.
56 struct ReceiveParams {
60 content::BrowserThread::ID thread_id;
61 void* browser_context_id;
62 std::string extension_id;
63 scoped_refptr<SocketData> sockets;
67 // Start a receive and register a callback.
68 void StartSocketReceive(const std::string& extension_id, int socket_id);
69 static void StartReceive(const ReceiveParams& params);
71 // Called when socket receive data.
72 static void ReceiveCallback(const ReceiveParams& params,
74 scoped_refptr<net::IOBuffer> io_buffer);
76 // Called when socket receive data.
77 static void ReceiveErrorCallback(const ReceiveParams& params,
78 BluetoothApiSocket::ErrorReason error_reason,
79 const std::string& error);
81 // Post an extension event from IO to UI thread
82 static void PostEvent(const ReceiveParams& params, scoped_ptr<Event> event);
84 // Dispatch an extension event on to EventRouter instance on UI thread.
85 static void DispatchEvent(void* browser_context_id,
86 const std::string& extension_id,
87 scoped_ptr<Event> event);
89 // Usually FILE thread (except for unit testing).
90 content::BrowserThread::ID thread_id_;
91 content::BrowserContext* const browser_context_;
92 scoped_refptr<SocketData> sockets_;
96 } // namespace extensions
98 #endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_EVENT_DISPATCHER_H_