/extensions/browser/api/sockets_tcp/tcp_socket_event_dispatcher.h

https://gitlab.com/jonnialva90/iridium-browser · C Header · 96 lines · 56 code · 22 blank · 18 comment · 0 complexity · 08a7a7fdf6fad03d765daa0de6660b6e MD5 · raw file

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