InspectorInterfaces.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #pragma once
  8. #include <functional>
  9. #include <memory>
  10. #include <string>
  11. #include <vector>
  12. namespace facebook {
  13. namespace react {
  14. class IDestructible {
  15. public:
  16. virtual ~IDestructible() = 0;
  17. };
  18. struct InspectorPage {
  19. const int id;
  20. const std::string title;
  21. const std::string vm;
  22. };
  23. /// IRemoteConnection allows the VM to send debugger messages to the client.
  24. class IRemoteConnection : public IDestructible {
  25. public:
  26. virtual ~IRemoteConnection() = 0;
  27. virtual void onMessage(std::string message) = 0;
  28. virtual void onDisconnect() = 0;
  29. };
  30. /// ILocalConnection allows the client to send debugger messages to the VM.
  31. class ILocalConnection : public IDestructible {
  32. public:
  33. virtual ~ILocalConnection() = 0;
  34. virtual void sendMessage(std::string message) = 0;
  35. virtual void disconnect() = 0;
  36. };
  37. /// IInspector tracks debuggable JavaScript targets (pages).
  38. class IInspector : public IDestructible {
  39. public:
  40. using ConnectFunc = std::function<std::unique_ptr<ILocalConnection>(
  41. std::unique_ptr<IRemoteConnection>)>;
  42. virtual ~IInspector() = 0;
  43. /// addPage is called by the VM to add a page to the list of debuggable pages.
  44. virtual int addPage(
  45. const std::string &title,
  46. const std::string &vm,
  47. ConnectFunc connectFunc) = 0;
  48. /// removePage is called by the VM to remove a page from the list of
  49. /// debuggable pages.
  50. virtual void removePage(int pageId) = 0;
  51. /// getPages is called by the client to list all debuggable pages.
  52. virtual std::vector<InspectorPage> getPages() const = 0;
  53. /// connect is called by the client to initiate a debugging session on the
  54. /// given page.
  55. virtual std::unique_ptr<ILocalConnection> connect(
  56. int pageId,
  57. std::unique_ptr<IRemoteConnection> remote) = 0;
  58. };
  59. /// getInspectorInstance retrieves the singleton inspector that tracks all
  60. /// debuggable pages in this process.
  61. extern IInspector &getInspectorInstance();
  62. /// makeTestInspectorInstance creates an independent inspector instance that
  63. /// should only be used in tests.
  64. extern std::unique_ptr<IInspector> makeTestInspectorInstance();
  65. } // namespace react
  66. } // namespace facebook