/extensions/browser/api/bluetooth_socket/bluetooth_socket_event_dispatcher.h

https://gitlab.com/jonnialva90/iridium-browser · C Header · 119 lines · 70 code · 26 blank · 23 comment · 0 complexity · 7646143164f619a7dc5fdc56f44420fe 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_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_EVENT_DISPATCHER_H_
  5. #define EXTENSIONS_BROWSER_API_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_EVENT_DISPATCHER_H_
  6. #include "extensions/browser/api/api_resource_manager.h"
  7. #include "extensions/browser/api/bluetooth_socket/bluetooth_api_socket.h"
  8. #include "extensions/browser/browser_context_keyed_api_factory.h"
  9. namespace content {
  10. class BrowserContext;
  11. }
  12. namespace device {
  13. class BluetoothDevice;
  14. class BluetoothSocket;
  15. }
  16. namespace extensions {
  17. struct Event;
  18. class BluetoothApiSocket;
  19. }
  20. namespace extensions {
  21. namespace api {
  22. // Dispatch events related to "bluetooth" sockets from callback on native socket
  23. // instances. There is one instance per browser context.
  24. class BluetoothSocketEventDispatcher
  25. : public BrowserContextKeyedAPI,
  26. public base::SupportsWeakPtr<BluetoothSocketEventDispatcher> {
  27. public:
  28. explicit BluetoothSocketEventDispatcher(content::BrowserContext* context);
  29. ~BluetoothSocketEventDispatcher() override;
  30. // Socket is active, start receiving data from it.
  31. void OnSocketConnect(const std::string& extension_id, int socket_id);
  32. // Socket is active again, start accepting connections from it.
  33. void OnSocketListen(const std::string& extension_id, int socket_id);
  34. // Socket is active again, start receiving data from it.
  35. void OnSocketResume(const std::string& extension_id, int socket_id);
  36. // BrowserContextKeyedAPI implementation.
  37. static BrowserContextKeyedAPIFactory<BluetoothSocketEventDispatcher>*
  38. GetFactoryInstance();
  39. // Convenience method to get the SocketEventDispatcher for a profile.
  40. static BluetoothSocketEventDispatcher* Get(content::BrowserContext* context);
  41. private:
  42. typedef ApiResourceManager<BluetoothApiSocket>::ApiResourceData SocketData;
  43. friend class BrowserContextKeyedAPIFactory<BluetoothSocketEventDispatcher>;
  44. // BrowserContextKeyedAPI implementation.
  45. static const char* service_name() { return "BluetoothSocketEventDispatcher"; }
  46. static const bool kServiceHasOwnInstanceInIncognito = true;
  47. static const bool kServiceIsNULLWhileTesting = true;
  48. // base::Bind supports methods with up to 6 parameters. SocketParams is used
  49. // as a workaround that limitation for invoking StartReceive() and
  50. // StartAccept().
  51. struct SocketParams {
  52. SocketParams();
  53. ~SocketParams();
  54. content::BrowserThread::ID thread_id;
  55. void* browser_context_id;
  56. std::string extension_id;
  57. scoped_refptr<SocketData> sockets;
  58. int socket_id;
  59. };
  60. // Start a receive and register a callback.
  61. static void StartReceive(const SocketParams& params);
  62. // Called when socket receive data.
  63. static void ReceiveCallback(const SocketParams& params,
  64. int bytes_read,
  65. scoped_refptr<net::IOBuffer> io_buffer);
  66. // Called when socket receive data.
  67. static void ReceiveErrorCallback(const SocketParams& params,
  68. BluetoothApiSocket::ErrorReason error_reason,
  69. const std::string& error);
  70. // Start an accept and register a callback.
  71. static void StartAccept(const SocketParams& params);
  72. // Called when socket accepts a client connection.
  73. static void AcceptCallback(const SocketParams& params,
  74. const device::BluetoothDevice* device,
  75. scoped_refptr<device::BluetoothSocket> socket);
  76. // Called when socket encounters an error while accepting a client connection.
  77. static void AcceptErrorCallback(const SocketParams& params,
  78. BluetoothApiSocket::ErrorReason error_reason,
  79. const std::string& error);
  80. // Post an extension event from IO to UI thread
  81. static void PostEvent(const SocketParams& params, scoped_ptr<Event> event);
  82. // Dispatch an extension event on to EventRouter instance on UI thread.
  83. static void DispatchEvent(void* browser_context_id,
  84. const std::string& extension_id,
  85. scoped_ptr<Event> event);
  86. // Usually FILE thread (except for unit testing).
  87. content::BrowserThread::ID thread_id_;
  88. content::BrowserContext* const browser_context_;
  89. scoped_refptr<SocketData> sockets_;
  90. };
  91. } // namespace api
  92. } // namespace extensions
  93. #endif // EXTENSIONS_BROWSER_API_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_EVENT_DISPATCHER_H_