- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / sockets_udp / udp_socket_event_dispatcher.h
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.
4
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_
7
8 #include "chrome/browser/extensions/api/api_resource_manager.h"
9 #include "chrome/browser/extensions/api/sockets_udp/sockets_udp_api.h"
10
11 namespace extensions {
12 struct Event;
13 class ResumableUDPSocket;
14 }
15
16 namespace extensions {
17 namespace api {
18
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> {
24  public:
25   explicit UDPSocketEventDispatcher(Profile* profile);
26   virtual ~UDPSocketEventDispatcher();
27
28   // Socket is active, start receving from it.
29   void OnSocketBind(const std::string& extension_id, int socket_id);
30
31   // ProfileKeyedAPI implementation.
32   static ProfileKeyedAPIFactory<UDPSocketEventDispatcher>* GetFactoryInstance();
33
34   // Convenience method to get the SocketEventDispatcher for a profile.
35   static UDPSocketEventDispatcher* Get(Profile* profile);
36
37  private:
38   typedef ApiResourceManager<ResumableUDPSocket>::ApiResourceData SocketData;
39   friend class ProfileKeyedAPIFactory<UDPSocketEventDispatcher>;
40   // ProfileKeyedAPI implementation.
41   static const char* service_name() {
42     return "UDPSocketEventDispatcher";
43   }
44   static const bool kServiceHasOwnInstanceInIncognito = true;
45   static const bool kServiceIsNULLWhileTesting = true;
46
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 {
50     ReceiveParams();
51     ~ReceiveParams();
52
53     content::BrowserThread::ID thread_id;
54     void* profile_id;
55     std::string extension_id;
56     scoped_refptr<SocketData> sockets;
57     int socket_id;
58   };
59
60   // Start a receive and register a callback.
61   static void StartReceive(const ReceiveParams& params);
62
63   // Called when socket receive data.
64   static void ReceiveCallback(const ReceiveParams& params,
65                               int bytes_read,
66                               scoped_refptr<net::IOBuffer> io_buffer,
67                               const std::string& address,
68                               int port);
69
70   // Post an extension event from IO to UI thread
71   static void PostEvent(const ReceiveParams& params, scoped_ptr<Event> event);
72
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);
77
78   // Usually IO thread (except for unit testing).
79   content::BrowserThread::ID thread_id_;
80   Profile* const profile_;
81   scoped_refptr<SocketData> sockets_;
82 };
83
84 }  // namespace api
85 }  // namespace extensions
86
87 #endif  // CHROME_BROWSER_EXTENSIONS_API_SOCKETS_UDP_UDP_SOCKET_EVENT_DISPATCHER_H_