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_TCP_TCP_SOCKET_EVENT_DISPATCHER_H_ 6#define CHROME_BROWSER_EXTENSIONS_API_SOCKETS_TCP_TCP_SOCKET_EVENT_DISPATCHER_H_ 7 8#include "chrome/browser/extensions/api/api_resource_manager.h" 9#include "chrome/browser/extensions/api/sockets_tcp/sockets_tcp_api.h" 10 11namespace extensions { 12struct Event; 13class ResumableTCPSocket; 14} 15 16namespace extensions { 17namespace api { 18 19// Dispatch events related to "sockets.tcp" sockets from callback on native 20// socket instances. There is one instance per profile. 21class TCPSocketEventDispatcher 22 : public ProfileKeyedAPI, 23 public base::SupportsWeakPtr<TCPSocketEventDispatcher> { 24 public: 25 explicit TCPSocketEventDispatcher(Profile* profile); 26 virtual ~TCPSocketEventDispatcher(); 27 28 // Socket is active, start receving from it. 29 void OnSocketConnect(const std::string& extension_id, int socket_id); 30 31 // Socket is active again, start receiving data from it. 32 void OnSocketResume(const std::string& extension_id, int socket_id); 33 34 // ProfileKeyedAPI implementation. 35 static ProfileKeyedAPIFactory<TCPSocketEventDispatcher>* GetFactoryInstance(); 36 37 // Convenience method to get the SocketEventDispatcher for a profile. 38 static TCPSocketEventDispatcher* Get(Profile* profile); 39 40 private: 41 typedef ApiResourceManager<ResumableTCPSocket>::ApiResourceData SocketData; 42 friend class ProfileKeyedAPIFactory<TCPSocketEventDispatcher>; 43 // ProfileKeyedAPI implementation. 44 static const char* service_name() { 45 return "TCPSocketEventDispatcher"; 46 } 47 static const bool kServiceHasOwnInstanceInIncognito = true; 48 static const bool kServiceIsNULLWhileTesting = true; 49 50 // base::Bind supports methods with up to 6 parameters. ReadParams is used 51 // as a workaround that limitation for invoking StartReceive. 52 struct ReadParams { 53 ReadParams(); 54 ~ReadParams(); 55 56 content::BrowserThread::ID thread_id; 57 void* profile_id; 58 std::string extension_id; 59 scoped_refptr<SocketData> sockets; 60 int socket_id; 61 }; 62 63 // Start a receive and register a callback. 64 void StartSocketRead(const std::string& extension_id, int socket_id); 65 66 // Start a receive and register a callback. 67 static void StartRead(const ReadParams& params); 68 69 // Called when socket receive data. 70 static void ReadCallback(const ReadParams& params, 71 int bytes_read, 72 scoped_refptr<net::IOBuffer> io_buffer); 73 74 // Post an extension event from IO to UI thread 75 static void PostEvent(const ReadParams& params, scoped_ptr<Event> event); 76 77 // Dispatch an extension event on to EventRouter instance on UI thread. 78 static void DispatchEvent(void* profile_id, 79 const std::string& extension_id, 80 scoped_ptr<Event> event); 81 82 // Usually IO thread (except for unit testing). 83 content::BrowserThread::ID thread_id_; 84 Profile* const profile_; 85 scoped_refptr<SocketData> sockets_; 86}; 87 88} // namespace api 89} // namespace extensions 90 91#endif // CHROME_BROWSER_EXTENSIONS_API_SOCKETS_TCP_TCP_SOCKET_EVENT_DISPATCHER_H_ 92