| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_SOCKETS_TCP_SERVER_TCP_SERVER_SOCKET_EVENT
_DISPATCHER_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_SOCKETS_TCP_SERVER_TCP_SERVER_SOCKET_EVENT_DISPAT
CHER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKETS_TCP_SERVER_TCP_SERVER_SOCKET_EVENT
_DISPATCHER_H_ | 6 #define EXTENSIONS_BROWSER_API_SOCKETS_TCP_SERVER_TCP_SERVER_SOCKET_EVENT_DISPAT
CHER_H_ |
| 7 | 7 |
| 8 #include "chrome/browser/extensions/api/api_resource_manager.h" | 8 #include "chrome/browser/extensions/api/api_resource_manager.h" |
| 9 #include "chrome/browser/extensions/api/sockets_tcp/sockets_tcp_api.h" | 9 #include "extensions/browser/api/sockets_tcp/sockets_tcp_api.h" |
| 10 #include "chrome/browser/extensions/api/sockets_tcp_server/sockets_tcp_server_ap
i.h" | 10 #include "extensions/browser/api/sockets_tcp_server/sockets_tcp_server_api.h" |
| 11 | 11 |
| 12 namespace content { | 12 namespace content { |
| 13 class BrowserContext; | 13 class BrowserContext; |
| 14 } | 14 } |
| 15 | 15 |
| 16 namespace extensions { | 16 namespace extensions { |
| 17 struct Event; | 17 struct Event; |
| 18 class ResumableTCPSocket; | 18 class ResumableTCPSocket; |
| 19 } | 19 } |
| 20 | 20 |
| 21 namespace extensions { | 21 namespace extensions { |
| 22 namespace api { | 22 namespace core_api { |
| 23 | 23 |
| 24 // Dispatch events related to "sockets.tcp" sockets from callback on native | 24 // Dispatch events related to "sockets.tcp" sockets from callback on native |
| 25 // socket instances. There is one instance per profile. | 25 // socket instances. There is one instance per profile. |
| 26 class TCPServerSocketEventDispatcher | 26 class TCPServerSocketEventDispatcher |
| 27 : public BrowserContextKeyedAPI, | 27 : public BrowserContextKeyedAPI, |
| 28 public base::SupportsWeakPtr<TCPServerSocketEventDispatcher> { | 28 public base::SupportsWeakPtr<TCPServerSocketEventDispatcher> { |
| 29 public: | 29 public: |
| 30 explicit TCPServerSocketEventDispatcher(content::BrowserContext* context); | 30 explicit TCPServerSocketEventDispatcher(content::BrowserContext* context); |
| 31 virtual ~TCPServerSocketEventDispatcher(); | 31 virtual ~TCPServerSocketEventDispatcher(); |
| 32 | 32 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 43 // Convenience method to get the SocketEventDispatcher for a profile. | 43 // Convenience method to get the SocketEventDispatcher for a profile. |
| 44 static TCPServerSocketEventDispatcher* Get(content::BrowserContext* context); | 44 static TCPServerSocketEventDispatcher* Get(content::BrowserContext* context); |
| 45 | 45 |
| 46 private: | 46 private: |
| 47 typedef ApiResourceManager<ResumableTCPServerSocket>::ApiResourceData | 47 typedef ApiResourceManager<ResumableTCPServerSocket>::ApiResourceData |
| 48 ServerSocketData; | 48 ServerSocketData; |
| 49 typedef ApiResourceManager<ResumableTCPSocket>::ApiResourceData | 49 typedef ApiResourceManager<ResumableTCPSocket>::ApiResourceData |
| 50 ClientSocketData; | 50 ClientSocketData; |
| 51 friend class BrowserContextKeyedAPIFactory<TCPServerSocketEventDispatcher>; | 51 friend class BrowserContextKeyedAPIFactory<TCPServerSocketEventDispatcher>; |
| 52 // BrowserContextKeyedAPI implementation. | 52 // BrowserContextKeyedAPI implementation. |
| 53 static const char* service_name() { | 53 static const char* service_name() { return "TCPServerSocketEventDispatcher"; } |
| 54 return "TCPServerSocketEventDispatcher"; | |
| 55 } | |
| 56 static const bool kServiceHasOwnInstanceInIncognito = true; | 54 static const bool kServiceHasOwnInstanceInIncognito = true; |
| 57 static const bool kServiceIsNULLWhileTesting = true; | 55 static const bool kServiceIsNULLWhileTesting = true; |
| 58 | 56 |
| 59 // base::Bind supports methods with up to 6 parameters. AcceptParams is used | 57 // base::Bind supports methods with up to 6 parameters. AcceptParams is used |
| 60 // as a workaround that limitation for invoking StartAccept. | 58 // as a workaround that limitation for invoking StartAccept. |
| 61 struct AcceptParams { | 59 struct AcceptParams { |
| 62 AcceptParams(); | 60 AcceptParams(); |
| 63 ~AcceptParams(); | 61 ~AcceptParams(); |
| 64 | 62 |
| 65 content::BrowserThread::ID thread_id; | 63 content::BrowserThread::ID thread_id; |
| 66 void* browser_context_id; | 64 void* browser_context_id; |
| 67 std::string extension_id; | 65 std::string extension_id; |
| 68 scoped_refptr<ServerSocketData> server_sockets; | 66 scoped_refptr<ServerSocketData> server_sockets; |
| 69 scoped_refptr<ClientSocketData> client_sockets; | 67 scoped_refptr<ClientSocketData> client_sockets; |
| 70 int socket_id; | 68 int socket_id; |
| 71 }; | 69 }; |
| 72 | 70 |
| 73 // Start an accept and register a callback. | 71 // Start an accept and register a callback. |
| 74 void StartSocketAccept(const std::string& extension_id, int socket_id); | 72 void StartSocketAccept(const std::string& extension_id, int socket_id); |
| 75 | 73 |
| 76 // Start an accept and register a callback. | 74 // Start an accept and register a callback. |
| 77 static void StartAccept(const AcceptParams& params); | 75 static void StartAccept(const AcceptParams& params); |
| 78 | 76 |
| 79 // Called when socket accepts a new connection. | 77 // Called when socket accepts a new connection. |
| 80 static void AcceptCallback(const AcceptParams& params, | 78 static void AcceptCallback(const AcceptParams& params, |
| 81 int result_code, | 79 int result_code, |
| 82 net::TCPClientSocket *socket); | 80 net::TCPClientSocket* socket); |
| 83 | 81 |
| 84 // Post an extension event from |thread_id| to UI thread | 82 // Post an extension event from |thread_id| to UI thread |
| 85 static void PostEvent(const AcceptParams& params, | 83 static void PostEvent(const AcceptParams& params, scoped_ptr<Event> event); |
| 86 scoped_ptr<Event> event); | |
| 87 | 84 |
| 88 // Dispatch an extension event on to EventRouter instance on UI thread. | 85 // Dispatch an extension event on to EventRouter instance on UI thread. |
| 89 static void DispatchEvent(void* browser_context_id, | 86 static void DispatchEvent(void* browser_context_id, |
| 90 const std::string& extension_id, | 87 const std::string& extension_id, |
| 91 scoped_ptr<Event> event); | 88 scoped_ptr<Event> event); |
| 92 | 89 |
| 93 // Usually IO thread (except for unit testing). | 90 // Usually IO thread (except for unit testing). |
| 94 content::BrowserThread::ID thread_id_; | 91 content::BrowserThread::ID thread_id_; |
| 95 content::BrowserContext* const browser_context_; | 92 content::BrowserContext* const browser_context_; |
| 96 scoped_refptr<ServerSocketData> server_sockets_; | 93 scoped_refptr<ServerSocketData> server_sockets_; |
| 97 scoped_refptr<ClientSocketData> client_sockets_; | 94 scoped_refptr<ClientSocketData> client_sockets_; |
| 98 }; | 95 }; |
| 99 | 96 |
| 100 } // namespace api | 97 } // namespace core_api |
| 101 } // namespace extensions | 98 } // namespace extensions |
| 102 | 99 |
| 103 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKETS_TCP_SERVER_TCP_SERVER_SOCKET_EV
ENT_DISPATCHER_H_ | 100 #endif // EXTENSIONS_BROWSER_API_SOCKETS_TCP_SERVER_TCP_SERVER_SOCKET_EVENT_DIS
PATCHER_H_ |
| OLD | NEW |