1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 23551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 33551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// found in the LICENSE file. 43551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef EXTENSIONS_BROWSER_API_SOCKETS_UDP_UDP_SOCKET_EVENT_DISPATCHER_H_ 6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define EXTENSIONS_BROWSER_API_SOCKETS_UDP_UDP_SOCKET_EVENT_DISPATCHER_H_ 73551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "extensions/browser/api/api_resource_manager.h" 9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "extensions/browser/api/sockets_udp/sockets_udp_api.h" 10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace content { 12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class BrowserContext; 13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)namespace extensions { 1668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)struct Event; 173551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)class ResumableUDPSocket; 183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)namespace extensions { 21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace core_api { 223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Dispatch events related to "sockets.udp" sockets from callback on native 243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// socket instances. There is one instance per profile. 253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)class UDPSocketEventDispatcher 26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) : public BrowserContextKeyedAPI, 273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) public base::SupportsWeakPtr<UDPSocketEventDispatcher> { 283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) public: 29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) explicit UDPSocketEventDispatcher(content::BrowserContext* context); 303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) virtual ~UDPSocketEventDispatcher(); 313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 323551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // Socket is active, start receving from it. 333551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) void OnSocketBind(const std::string& extension_id, int socket_id); 343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 35a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // Socket is active again, start receiving data from it. 36a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) void OnSocketResume(const std::string& extension_id, int socket_id); 37a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // BrowserContextKeyedAPI implementation. 39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) static BrowserContextKeyedAPIFactory<UDPSocketEventDispatcher>* 40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) GetFactoryInstance(); 413551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 423551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // Convenience method to get the SocketEventDispatcher for a profile. 43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) static UDPSocketEventDispatcher* Get(content::BrowserContext* context); 443551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 453551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) private: 4668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) typedef ApiResourceManager<ResumableUDPSocket>::ApiResourceData SocketData; 47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) friend class BrowserContextKeyedAPIFactory<UDPSocketEventDispatcher>; 48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // BrowserContextKeyedAPI implementation. 49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) static const char* service_name() { return "UDPSocketEventDispatcher"; } 5068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) static const bool kServiceHasOwnInstanceInIncognito = true; 5168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) static const bool kServiceIsNULLWhileTesting = true; 5268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) 5368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) // base::Bind supports methods with up to 6 parameters. ReceiveParams is used 5468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) // as a workaround that limitation for invoking StartReceive. 5568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) struct ReceiveParams { 5668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) ReceiveParams(); 5768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) ~ReceiveParams(); 583551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 5968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) content::BrowserThread::ID thread_id; 60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) void* browser_context_id; 6168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) std::string extension_id; 6268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) scoped_refptr<SocketData> sockets; 6368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) int socket_id; 6468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) }; 653551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 663551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // Start a receive and register a callback. 6768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) static void StartReceive(const ReceiveParams& params); 683551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 693551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // Called when socket receive data. 7068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) static void ReceiveCallback(const ReceiveParams& params, 7168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) int bytes_read, 7268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) scoped_refptr<net::IOBuffer> io_buffer, 7368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) const std::string& address, 7468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) int port); 753551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 7668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) // Post an extension event from IO to UI thread 7768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) static void PostEvent(const ReceiveParams& params, scoped_ptr<Event> event); 7868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) 7968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) // Dispatch an extension event on to EventRouter instance on UI thread. 80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) static void DispatchEvent(void* browser_context_id, 8168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) const std::string& extension_id, 8268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) scoped_ptr<Event> event); 833551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 843551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // Usually IO thread (except for unit testing). 8568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) content::BrowserThread::ID thread_id_; 86a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) content::BrowserContext* const browser_context_; 8768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) scoped_refptr<SocketData> sockets_; 883551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}; 893551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 90a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} // namespace core_api 913551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} // namespace extensions 923551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 93a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif // EXTENSIONS_BROWSER_API_SOCKETS_UDP_UDP_SOCKET_EVENT_DISPATCHER_H_ 94