1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 28bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 38bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// found in the LICENSE file. 48bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef EXTENSIONS_BROWSER_API_SOCKETS_TCP_SERVER_TCP_SERVER_SOCKET_EVENT_DISPATCHER_H_ 6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define EXTENSIONS_BROWSER_API_SOCKETS_TCP_SERVER_TCP_SERVER_SOCKET_EVENT_DISPATCHER_H_ 78bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "extensions/browser/api/api_resource_manager.h" 9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "extensions/browser/api/sockets_tcp/sockets_tcp_api.h" 10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "extensions/browser/api/sockets_tcp_server/sockets_tcp_server_api.h" 11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace content { 13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class BrowserContext; 14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)namespace extensions { 178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)struct Event; 188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)class ResumableTCPSocket; 198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)} 208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)namespace extensions { 22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace core_api { 238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Dispatch events related to "sockets.tcp" sockets from callback on native 258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// socket instances. There is one instance per profile. 268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)class TCPServerSocketEventDispatcher 27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) : public BrowserContextKeyedAPI, 288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) public base::SupportsWeakPtr<TCPServerSocketEventDispatcher> { 298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) public: 30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) explicit TCPServerSocketEventDispatcher(content::BrowserContext* context); 318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) virtual ~TCPServerSocketEventDispatcher(); 328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // Server socket is active, start accepting connections from it. 348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) void OnServerSocketListen(const std::string& extension_id, int socket_id); 358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // Server socket is active again, start accepting connections from it. 378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) void OnServerSocketResume(const std::string& extension_id, int socket_id); 388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // BrowserContextKeyedAPI implementation. 40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) static BrowserContextKeyedAPIFactory<TCPServerSocketEventDispatcher>* 418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) GetFactoryInstance(); 428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // Convenience method to get the SocketEventDispatcher for a profile. 44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) static TCPServerSocketEventDispatcher* Get(content::BrowserContext* context); 458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) private: 478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) typedef ApiResourceManager<ResumableTCPServerSocket>::ApiResourceData 488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) ServerSocketData; 498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) typedef ApiResourceManager<ResumableTCPSocket>::ApiResourceData 508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) ClientSocketData; 51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) friend class BrowserContextKeyedAPIFactory<TCPServerSocketEventDispatcher>; 52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // BrowserContextKeyedAPI implementation. 53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) static const char* service_name() { return "TCPServerSocketEventDispatcher"; } 548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) static const bool kServiceHasOwnInstanceInIncognito = true; 558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) static const bool kServiceIsNULLWhileTesting = true; 568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // base::Bind supports methods with up to 6 parameters. AcceptParams is used 588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // as a workaround that limitation for invoking StartAccept. 598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) struct AcceptParams { 608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) AcceptParams(); 618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) ~AcceptParams(); 628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) content::BrowserThread::ID thread_id; 64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) void* browser_context_id; 658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) std::string extension_id; 668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) scoped_refptr<ServerSocketData> server_sockets; 678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) scoped_refptr<ClientSocketData> client_sockets; 688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) int socket_id; 698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) }; 708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // Start an accept and register a callback. 728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) void StartSocketAccept(const std::string& extension_id, int socket_id); 738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // Start an accept and register a callback. 758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) static void StartAccept(const AcceptParams& params); 768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // Called when socket accepts a new connection. 788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) static void AcceptCallback(const AcceptParams& params, 798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) int result_code, 80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) net::TCPClientSocket* socket); 818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // Post an extension event from |thread_id| to UI thread 83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) static void PostEvent(const AcceptParams& params, scoped_ptr<Event> event); 848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // Dispatch an extension event on to EventRouter instance on UI thread. 86a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) static void DispatchEvent(void* browser_context_id, 878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) const std::string& extension_id, 888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) scoped_ptr<Event> event); 898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // Usually IO thread (except for unit testing). 918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) content::BrowserThread::ID thread_id_; 92a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) content::BrowserContext* const browser_context_; 938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) scoped_refptr<ServerSocketData> server_sockets_; 948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) scoped_refptr<ClientSocketData> client_sockets_; 958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}; 968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 97a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} // namespace core_api 988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)} // namespace extensions 998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 100a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif // EXTENSIONS_BROWSER_API_SOCKETS_TCP_SERVER_TCP_SERVER_SOCKET_EVENT_DISPATCHER_H_ 101