1 /* 2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_ACELITE_MODULE_MANAGER_H 17 #define OHOS_ACELITE_MODULE_MANAGER_H 18 19 #include "jsi.h" 20 #include "non_copyable.h" 21 #include "product_adapter.h" 22 23 namespace OHOS { 24 namespace ACELite { 25 /** 26 * Function pointer type used for getting current bundle name. 27 */ 28 typedef const char *(*BundleNameGetter)(); 29 30 /** 31 * @brief module manager for module load. 32 */ 33 class ModuleManager final : public MemoryHeap { 34 public: 35 ACE_DISALLOW_COPY_AND_MOVE(ModuleManager); 36 /** 37 * @brief Get the single instance of ModuleManager 38 * 39 * @returns the instance. 40 */ GetInstance()41 static ModuleManager* GetInstance() 42 { 43 static ModuleManager instance; 44 return &instance; 45 } 46 47 /** 48 * @brief require javascript module object with given module name. 49 * 50 * @param moduleName name of the module required 51 * @returns javascript module object 52 */ 53 JSIValue RequireModule(const char * const moduleName); 54 55 /** 56 * @brief Release module object created by RequireModule. 57 * This Method should be called when current js page is 58 * going to destroy 59 */ 60 void CleanUpModule(); 61 62 /** 63 * @brief Call OnTerminate callbacks of required modules. 64 * This Method should be called when current application is 65 * going to terminate 66 */ 67 void OnTerminate(); 68 69 /** 70 * @brief Set product modules getter hook. 71 * 72 * @param the hook 73 */ 74 void SetProductModulesGetter(ProductModulesGetter getter); 75 76 /** 77 * @brief Set private modules getter hook. 78 * 79 * @param the hook 80 */ 81 void SetPrivateModulesGetter(PrivateModulesGetter getter); 82 83 /** 84 * @brief Set bundle name getter hook. 85 * 86 * @param the hook 87 */ 88 void SetBundleNameGetter(BundleNameGetter getter); 89 90 private: 91 struct CallbackNode : public MemoryHeap { 92 ACE_DISALLOW_COPY_AND_MOVE(CallbackNode); CallbackNodeCallbackNode93 CallbackNode() : moduleName(nullptr), callback(nullptr), callbackHandler(nullptr), next(nullptr) {} ~CallbackNodeCallbackNode94 ~CallbackNode() 95 { 96 if (moduleName != nullptr) { 97 ace_free(moduleName); 98 moduleName = nullptr; 99 } 100 } 101 102 char *moduleName; 103 NativeCallback callback; 104 JsiCallback callbackHandler; 105 struct CallbackNode *next; 106 }; 107 ModuleManager()108 ModuleManager() 109 : onDestroyHead_(nullptr), 110 onTerminateHead_(nullptr), 111 productModulesGetter_(nullptr), 112 privateModulesGetter_(nullptr), 113 bundleNameGetter_(nullptr) 114 { 115 } ~ModuleManager()116 virtual ~ModuleManager() {} 117 118 /** 119 * @brief get category and name with given moduleName. 120 * 121 * @param [in] moduleName module name passed from JS, e.g. system.vibrator 122 * @param [out] category pointer to category which this module belongs to 123 * Content should be freed with free() when it is not used anymore 124 * @param [out] name pointer to module name parsed from moduleName, e.g. vibrator 125 * Content should be freed with free() when it is not used anymore 126 * @return whether operation succeed 127 */ 128 bool ParseModuleName(const char * const moduleName, char** category, char** name) const; 129 130 /** 131 * @brief obtains the module object with the given module name. 132 * 133 * @param [in] moduleName module name 134 * @param [in] modules array of module infos 135 * @param [in] modulesCount length of modules 136 * @param [inout] categoryObj pointer to static object, e.g. requiredSystemModules 137 * @param [in] bundleName bundle name of specific private module 138 * @return javascript required module object 139 */ 140 JSIValue GetModuleObject(const char * const moduleName, const Module* modules, uint16_t modulesCount, 141 JSIValue& categoryObj, const char * const bundleName = nullptr); 142 143 JSIValue InitModuleObject(const char * const name, Module module, JSIValue& categoryObj); 144 145 // *resPtr should be freed by caller when it's not nullptr and won't be used any more 146 bool CreateString(const char * const srcStr, char** resPtr) const; 147 148 void InsertCallback(CallbackNode *&head, NativeCallback callback, 149 JsiCallback callbackHandler, const char * const moduleName = nullptr) const; 150 void InsertTerminateCallback(const char * const name, const JSIValue& moduleObj); 151 void InvokeCallbacks(CallbackNode *&head) const; 152 void* GetObjectPointer(JSIValue object) const; 153 void* GetObjectNamedPointer(JSIValue object, const char * const name) const; 154 void DeleteObjectProperty(JSIValue object, const char * const name) const; 155 NativeCallback GetNativeCallback(JSIValue object, const char * const name) const; 156 JsiCallback GetJsiCallback(JSIValue object, const char * const name) const; 157 158 // For different native module type 159 const char* CATEGORY_SYSTEM = "system"; 160 const char* CATEGORY_OHOS = "ohos"; 161 162 // JS object for caching modules 163 static JSIValue requiredSystemModules; 164 165 CallbackNode *onDestroyHead_; 166 CallbackNode *onTerminateHead_; 167 ProductModulesGetter productModulesGetter_; 168 PrivateModulesGetter privateModulesGetter_; 169 BundleNameGetter bundleNameGetter_; 170 }; 171 } // namespace ACELite 172 } // namespace OHOS 173 174 #endif // OHOS_ACELITE_MODULE_MANAGER_H 175