ModuleRegistry.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 <memory>
  9. #include <unordered_set>
  10. #include <vector>
  11. #include <cxxreact/JSExecutor.h>
  12. #include <folly/Optional.h>
  13. #include <folly/dynamic.h>
  14. #ifndef RN_EXPORT
  15. #define RN_EXPORT __attribute__((visibility("default")))
  16. #endif
  17. namespace facebook {
  18. namespace react {
  19. class NativeModule;
  20. struct ModuleConfig {
  21. size_t index;
  22. folly::dynamic config;
  23. };
  24. class RN_EXPORT ModuleRegistry {
  25. public:
  26. // not implemented:
  27. // onBatchComplete: see
  28. // https://our.intern.facebook.com/intern/tasks/?t=5279396 getModule: only
  29. // used by views getAllModules: only used for cleanup; use RAII instead
  30. // notifyCatalystInstanceInitialized: this is really only used by view-related
  31. // code notifyCatalystInstanceDestroy: use RAII instead
  32. using ModuleNotFoundCallback = std::function<bool(const std::string &name)>;
  33. ModuleRegistry(
  34. std::vector<std::unique_ptr<NativeModule>> modules,
  35. ModuleNotFoundCallback callback = nullptr);
  36. void registerModules(std::vector<std::unique_ptr<NativeModule>> modules);
  37. std::vector<std::string> moduleNames();
  38. folly::Optional<ModuleConfig> getConfig(const std::string &name);
  39. void callNativeMethod(
  40. unsigned int moduleId,
  41. unsigned int methodId,
  42. folly::dynamic &&params,
  43. int callId);
  44. MethodCallResult callSerializableNativeHook(
  45. unsigned int moduleId,
  46. unsigned int methodId,
  47. folly::dynamic &&args);
  48. private:
  49. // This is always populated
  50. std::vector<std::unique_ptr<NativeModule>> modules_;
  51. // This is used to extend the population of modulesByName_ if registerModules
  52. // is called after moduleNames
  53. void updateModuleNamesFromIndex(size_t size);
  54. // This is only populated if moduleNames() is called. Values are indices into
  55. // modules_.
  56. std::unordered_map<std::string, size_t> modulesByName_;
  57. // This is populated with modules that are requested via getConfig but are
  58. // unknown. An error will be thrown if they are subsequently added to the
  59. // registry.
  60. std::unordered_set<std::string> unknownModules_;
  61. // Function will be called if a module was requested but was not found.
  62. // If the function returns true, ModuleRegistry will try to find the module
  63. // again (assuming it's registered) If the functon returns false,
  64. // ModuleRegistry will not try to find the module and return nullptr instead.
  65. ModuleNotFoundCallback moduleNotFoundCallback_;
  66. };
  67. } // namespace react
  68. } // namespace facebook