1 // Copyright 2013 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_SOCKETS_UDP_UDP_SOCKET_EVENT_DISPATCHER_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKETS_UDP_UDP_SOCKET_EVENT_DISPATCHER_H_
8 #include "chrome/browser/extensions/api/api_resource_manager.h"
9 #include "chrome/browser/extensions/api/sockets_udp/sockets_udp_api.h"
11 namespace extensions {
13 class ResumableUDPSocket;
16 namespace extensions {
19 // Dispatch events related to "sockets.udp" sockets from callback on native
20 // socket instances. There is one instance per profile.
21 class UDPSocketEventDispatcher
22 : public ProfileKeyedAPI,
23 public base::SupportsWeakPtr<UDPSocketEventDispatcher> {
25 explicit UDPSocketEventDispatcher(Profile* profile);
26 virtual ~UDPSocketEventDispatcher();
28 // Socket is active, start receving from it.
29 void OnSocketBind(const std::string& extension_id, int socket_id);
31 // ProfileKeyedAPI implementation.
32 static ProfileKeyedAPIFactory<UDPSocketEventDispatcher>* GetFactoryInstance();
34 // Convenience method to get the SocketEventDispatcher for a profile.
35 static UDPSocketEventDispatcher* Get(Profile* profile);
38 typedef ApiResourceManager<ResumableUDPSocket>::ApiResourceData SocketData;
39 friend class ProfileKeyedAPIFactory<UDPSocketEventDispatcher>;
40 // ProfileKeyedAPI implementation.
41 static const char* service_name() {
42 return "UDPSocketEventDispatcher";
44 static const bool kServiceHasOwnInstanceInIncognito = true;
45 static const bool kServiceIsNULLWhileTesting = true;
47 // base::Bind supports methods with up to 6 parameters. ReceiveParams is used
48 // as a workaround that limitation for invoking StartReceive.
49 struct ReceiveParams {
53 content::BrowserThread::ID thread_id;
55 std::string extension_id;
56 scoped_refptr<SocketData> sockets;
60 // Start a receive and register a callback.
61 static void StartReceive(const ReceiveParams& params);
63 // Called when socket receive data.
64 static void ReceiveCallback(const ReceiveParams& params,
66 scoped_refptr<net::IOBuffer> io_buffer,
67 const std::string& address,
70 // Post an extension event from IO to UI thread
71 static void PostEvent(const ReceiveParams& params, scoped_ptr<Event> event);
73 // Dispatch an extension event on to EventRouter instance on UI thread.
74 static void DispatchEvent(void* profile_id,
75 const std::string& extension_id,
76 scoped_ptr<Event> event);
78 // Usually IO thread (except for unit testing).
79 content::BrowserThread::ID thread_id_;
80 Profile* const profile_;
81 scoped_refptr<SocketData> sockets_;
85 } // namespace extensions
87 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKETS_UDP_UDP_SOCKET_EVENT_DISPATCHER_H_