RCTInspector.mm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #import <React/RCTInspector.h>
  8. #if RCT_DEV
  9. #include <jsinspector/InspectorInterfaces.h>
  10. #import <React/RCTDefines.h>
  11. #import <React/RCTInspectorPackagerConnection.h>
  12. #import <React/RCTLog.h>
  13. #import <React/RCTSRWebSocket.h>
  14. #import <React/RCTUtils.h>
  15. using namespace facebook::react;
  16. // This is a port of the Android impl, at
  17. // react-native-github/ReactAndroid/src/main/java/com/facebook/react/bridge/Inspector.java
  18. // react-native-github/ReactAndroid/src/main/jni/react/jni/JInspector.cpp
  19. // please keep consistent :)
  20. class RemoteConnection : public IRemoteConnection {
  21. public:
  22. RemoteConnection(RCTInspectorRemoteConnection *connection) : _connection(connection) {}
  23. virtual void onMessage(std::string message) override
  24. {
  25. [_connection onMessage:@(message.c_str())];
  26. }
  27. virtual void onDisconnect() override
  28. {
  29. [_connection onDisconnect];
  30. }
  31. private:
  32. const RCTInspectorRemoteConnection *_connection;
  33. };
  34. @interface RCTInspectorPage () {
  35. NSInteger _id;
  36. NSString *_title;
  37. NSString *_vm;
  38. }
  39. - (instancetype)initWithId:(NSInteger)id title:(NSString *)title vm:(NSString *)vm;
  40. @end
  41. @interface RCTInspectorLocalConnection () {
  42. std::unique_ptr<ILocalConnection> _connection;
  43. }
  44. - (instancetype)initWithConnection:(std::unique_ptr<ILocalConnection>)connection;
  45. @end
  46. static IInspector *getInstance()
  47. {
  48. return &facebook::react::getInspectorInstance();
  49. }
  50. @implementation RCTInspector
  51. RCT_NOT_IMPLEMENTED(-(instancetype)init)
  52. + (NSArray<RCTInspectorPage *> *)pages
  53. {
  54. std::vector<InspectorPage> pages = getInstance()->getPages();
  55. NSMutableArray<RCTInspectorPage *> *array = [NSMutableArray arrayWithCapacity:pages.size()];
  56. for (size_t i = 0; i < pages.size(); i++) {
  57. RCTInspectorPage *pageWrapper = [[RCTInspectorPage alloc] initWithId:pages[i].id
  58. title:@(pages[i].title.c_str())
  59. vm:@(pages[i].vm.c_str())];
  60. [array addObject:pageWrapper];
  61. }
  62. return array;
  63. }
  64. + (RCTInspectorLocalConnection *)connectPage:(NSInteger)pageId
  65. forRemoteConnection:(RCTInspectorRemoteConnection *)remote
  66. {
  67. auto localConnection = getInstance()->connect((int)pageId, std::make_unique<RemoteConnection>(remote));
  68. return [[RCTInspectorLocalConnection alloc] initWithConnection:std::move(localConnection)];
  69. }
  70. @end
  71. @implementation RCTInspectorPage
  72. RCT_NOT_IMPLEMENTED(-(instancetype)init)
  73. - (instancetype)initWithId:(NSInteger)id title:(NSString *)title vm:(NSString *)vm
  74. {
  75. if (self = [super init]) {
  76. _id = id;
  77. _title = title;
  78. _vm = vm;
  79. }
  80. return self;
  81. }
  82. @end
  83. @implementation RCTInspectorLocalConnection
  84. RCT_NOT_IMPLEMENTED(-(instancetype)init)
  85. - (instancetype)initWithConnection:(std::unique_ptr<ILocalConnection>)connection
  86. {
  87. if (self = [super init]) {
  88. _connection = std::move(connection);
  89. }
  90. return self;
  91. }
  92. - (void)sendMessage:(NSString *)message
  93. {
  94. _connection->sendMessage([message UTF8String]);
  95. }
  96. - (void)disconnect
  97. {
  98. _connection->disconnect();
  99. }
  100. @end
  101. #endif