1 /* 2 * Copyright (c) 2022 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_ABILITY_RUNTIME_CJ_ENVIRONMENT_H 17 #define OHOS_ABILITY_RUNTIME_CJ_ENVIRONMENT_H 18 19 #include "cj_envsetup.h" 20 #include "cj_invoker.h" 21 22 #include <string> 23 #include <functional> 24 25 #ifdef WINDOWS_PLATFORM 26 #define CJ_EXPORT __declspec(dllexport) 27 #else 28 #define CJ_EXPORT __attribute__((visibility("default"))) 29 #endif 30 31 namespace OHOS { 32 struct CJRuntimeAPI; 33 34 using TaskFuncType = void(*)(); 35 36 class CJ_EXPORT CJEnvironment final { 37 public: 38 static CJEnvironment* GetInstance(); 39 static void InitSpawnEnv(); 40 static void SetAppPath(const std::string& paths); 41 static CJEnvMethods* CreateEnvMethods(); 42 43 enum class NSMode { 44 SINK, 45 APP, 46 }; 47 48 CJEnvironment(NSMode mode); 49 ~CJEnvironment(); 50 IsRuntimeStarted()51 bool IsRuntimeStarted() 52 { 53 return isRuntimeStarted_; 54 } 55 56 static void SetSanitizerKindRuntimeVersion(SanitizerKind kind); 57 58 bool StartRuntime(); 59 void StopRuntime(); 60 void RegisterArkVMInRuntime(unsigned long long externalVM); 61 void RegisterStackInfoCallbacks(UpdateStackInfoFuncType uFunc); 62 void RegisterCJUncaughtExceptionHandler(const CJUncaughtExceptionInfo& handle); 63 bool RegisterCangjieCallback(); 64 void RegisterEventHandlerCallbacks(); 65 int InitCJRuntime(); IsUISchedulerStarted()66 bool IsUISchedulerStarted() 67 { 68 return isUISchedulerStarted_; 69 } 70 bool StartUIScheduler(); 71 bool CheckLoadCJLibrary(); 72 void StopUIScheduler(); 73 enum LibraryKind { 74 SYSTEM, 75 SDK, 76 APP, 77 }; 78 void* LoadCJLibrary(const char* dlName); 79 void* LoadCJLibrary(LibraryKind kind, const char* dlName); 80 void UnLoadCJLibrary(void* handle); GetUIScheduler()81 void* GetUIScheduler() 82 { 83 if (!isUISchedulerStarted_) { 84 return nullptr; 85 } 86 return uiScheduler_; 87 } 88 void* GetSymbol(void* dso, const char* symbol); 89 bool StartDebugger(); 90 bool PostTask(TaskFuncType task); 91 bool HasHigherPriorityTask(); SetLazyApis(CJRuntimeAPI * apis)92 void SetLazyApis(CJRuntimeAPI* apis) { lazyApis_ = apis; } 93 94 void InitCJAppNS(const std::string& path); 95 void InitCJAppSDKNS(const std::string& path); 96 void InitCJRomSDKNS(const std::string& path); 97 void InitCJCompatibilitySDKNS(const std::string& path); 98 void InitCJChipSDKNS(const std::string& path); 99 void InitCJRuntimeNS(const std::string& path); 100 void InitCJMockNS(const std::string& path); 101 void InitRuntimeNS(); 102 void* InitUIScheduler(); 103 int FiniCJRuntime(); 104 int InitCJLibrary(const char* dlName); 105 void InitCJNS(const std::string& path); 106 static NSMode DetectAppNSMode(); 107 static void SetAppVersion(std::string& version); 108 void DumpHeapSnapshot(int fd); 109 void ForceFullGC(); 110 void* LoadRuntimeLib(const char* runtimeLibName); 111 void UnLoadRuntimeApis(); 112 113 static const char *cjChipSDKNSName; 114 static const char *cjAppNSName; 115 static const char *cjRomSDKNSName; 116 static const char *cjSysNSName; 117 static const char *cjCompatibilitySDKNSName; 118 static const char *cjRuntimeNSName; 119 static const char *cjMockNSName; 120 static const char *cjAppSDKNSName; 121 static std::string appVersion; 122 static const std::string checkVersion; 123 static SanitizerKind sanitizerKind; 124 125 private: 126 bool LoadRuntimeApis(); 127 bool isRuntimeApiLoaded {false}; 128 CJRuntimeAPI* lazyApis_ {nullptr}; 129 bool isRuntimeStarted_{false}; 130 bool isLoadCJLibrary_{false}; 131 bool isUISchedulerStarted_{false}; 132 void* uiScheduler_ {nullptr}; 133 NSMode nsMode_ {NSMode::SINK}; 134 }; 135 136 } 137 138 #endif //OHOS_ABILITY_RUNTIME_CJ_ENVIRONMENT_H 139