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