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_
10 #include "extensions/browser/api/api_resource_manager.h"
11 #include "extensions/browser/api/sockets_udp/sockets_udp_api.h"
17 namespace extensions
{
19 class ResumableUDPSocket
;
22 namespace extensions
{
25 // Dispatch events related to "sockets.udp" sockets from callback on native
26 // socket instances. There is one instance per profile.
27 class UDPSocketEventDispatcher
28 : public BrowserContextKeyedAPI
,
29 public base::SupportsWeakPtr
<UDPSocketEventDispatcher
> {
31 explicit UDPSocketEventDispatcher(content::BrowserContext
* context
);
32 ~UDPSocketEventDispatcher() override
;
34 // Socket is active, start receving from it.
35 void OnSocketBind(const std::string
& extension_id
, int socket_id
);
37 // Socket is active again, start receiving data from it.
38 void OnSocketResume(const std::string
& extension_id
, int socket_id
);
40 // BrowserContextKeyedAPI implementation.
41 static BrowserContextKeyedAPIFactory
<UDPSocketEventDispatcher
>*
44 // Convenience method to get the SocketEventDispatcher for a profile.
45 static UDPSocketEventDispatcher
* Get(content::BrowserContext
* context
);
48 typedef ApiResourceManager
<ResumableUDPSocket
>::ApiResourceData SocketData
;
49 friend class BrowserContextKeyedAPIFactory
<UDPSocketEventDispatcher
>;
50 // BrowserContextKeyedAPI implementation.
51 static const char* service_name() { return "UDPSocketEventDispatcher"; }
52 static const bool kServiceHasOwnInstanceInIncognito
= true;
53 static const bool kServiceIsNULLWhileTesting
= true;
55 // base::Bind supports methods with up to 6 parameters. ReceiveParams is used
56 // as a workaround that limitation for invoking StartReceive.
57 struct ReceiveParams
{
61 content::BrowserThread::ID thread_id
;
62 void* browser_context_id
;
63 std::string extension_id
;
64 scoped_refptr
<SocketData
> sockets
;
68 // Start a receive and register a callback.
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
,
75 const std::string
& address
,
78 // Post an extension event from IO to UI thread
79 static void PostEvent(const ReceiveParams
& params
, scoped_ptr
<Event
> event
);
81 // Dispatch an extension event on to EventRouter instance on UI thread.
82 static void DispatchEvent(void* browser_context_id
,
83 const std::string
& extension_id
,
84 scoped_ptr
<Event
> event
);
86 // Usually IO thread (except for unit testing).
87 content::BrowserThread::ID thread_id_
;
88 content::BrowserContext
* const browser_context_
;
89 scoped_refptr
<SocketData
> sockets_
;
93 } // namespace extensions
95 #endif // EXTENSIONS_BROWSER_API_SOCKETS_UDP_UDP_SOCKET_EVENT_DISPATCHER_H_