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_SOCKETS_UDP_UDP_SOCKET_EVENT_DISPATCHER_H_
6 #define EXTENSIONS_BROWSER_API_SOCKETS_UDP_UDP_SOCKET_EVENT_DISPATCHER_H_
8 #include "extensions/browser/api/api_resource_manager.h"
9 #include "extensions/browser/api/sockets_udp/sockets_udp_api.h"
15 namespace extensions
{
17 class ResumableUDPSocket
;
20 namespace extensions
{
23 // Dispatch events related to "sockets.udp" sockets from callback on native
24 // socket instances. There is one instance per profile.
25 class UDPSocketEventDispatcher
26 : public BrowserContextKeyedAPI
,
27 public base::SupportsWeakPtr
<UDPSocketEventDispatcher
> {
29 explicit UDPSocketEventDispatcher(content::BrowserContext
* context
);
30 virtual ~UDPSocketEventDispatcher();
32 // Socket is active, start receving from it.
33 void OnSocketBind(const std::string
& extension_id
, int socket_id
);
35 // Socket is active again, start receiving data from it.
36 void OnSocketResume(const std::string
& extension_id
, int socket_id
);
38 // BrowserContextKeyedAPI implementation.
39 static BrowserContextKeyedAPIFactory
<UDPSocketEventDispatcher
>*
42 // Convenience method to get the SocketEventDispatcher for a profile.
43 static UDPSocketEventDispatcher
* Get(content::BrowserContext
* context
);
46 typedef ApiResourceManager
<ResumableUDPSocket
>::ApiResourceData SocketData
;
47 friend class BrowserContextKeyedAPIFactory
<UDPSocketEventDispatcher
>;
48 // BrowserContextKeyedAPI implementation.
49 static const char* service_name() { return "UDPSocketEventDispatcher"; }
50 static const bool kServiceHasOwnInstanceInIncognito
= true;
51 static const bool kServiceIsNULLWhileTesting
= true;
53 // base::Bind supports methods with up to 6 parameters. ReceiveParams is used
54 // as a workaround that limitation for invoking StartReceive.
55 struct ReceiveParams
{
59 content::BrowserThread::ID thread_id
;
60 void* browser_context_id
;
61 std::string extension_id
;
62 scoped_refptr
<SocketData
> sockets
;
66 // Start a receive and register a callback.
67 static void StartReceive(const ReceiveParams
& params
);
69 // Called when socket receive data.
70 static void ReceiveCallback(const ReceiveParams
& params
,
72 scoped_refptr
<net::IOBuffer
> io_buffer
,
73 const std::string
& address
,
76 // Post an extension event from IO to UI thread
77 static void PostEvent(const ReceiveParams
& params
, scoped_ptr
<Event
> event
);
79 // Dispatch an extension event on to EventRouter instance on UI thread.
80 static void DispatchEvent(void* browser_context_id
,
81 const std::string
& extension_id
,
82 scoped_ptr
<Event
> event
);
84 // Usually IO thread (except for unit testing).
85 content::BrowserThread::ID thread_id_
;
86 content::BrowserContext
* const browser_context_
;
87 scoped_refptr
<SocketData
> sockets_
;
90 } // namespace core_api
91 } // namespace extensions
93 #endif // EXTENSIONS_BROWSER_API_SOCKETS_UDP_UDP_SOCKET_EVENT_DISPATCHER_H_