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 SetSanitizerKindRuntimeVersion(SanitizerKind kind)56 void SetSanitizerKindRuntimeVersion(SanitizerKind kind) 57 { 58 sanitizerKind_ = kind; 59 } 60 61 bool StartRuntime(); 62 void StopRuntime(); 63 void RegisterArkVMInRuntime(unsigned long long externalVM); 64 void RegisterStackInfoCallbacks(UpdateStackInfoFuncType uFunc); 65 void RegisterCJUncaughtExceptionHandler(const CJUncaughtExceptionInfo& handle); 66 bool RegisterCangjieCallback(); IsUISchedulerStarted()67 bool IsUISchedulerStarted() 68 { 69 return isUISchedulerStarted_; 70 } 71 bool StartUIScheduler(); 72 bool CheckLoadCJLibrary(); 73 void StopUIScheduler(); 74 enum LibraryKind { 75 SYSTEM, 76 SDK, 77 APP, 78 }; 79 void* LoadCJLibrary(const char* dlName); 80 void* LoadCJLibrary(LibraryKind kind, const char* dlName); 81 void UnLoadCJLibrary(void* handle); GetUIScheduler()82 void* GetUIScheduler() 83 { 84 if (!isUISchedulerStarted_) { 85 return nullptr; 86 } 87 return uiScheduler_; 88 } 89 void* GetSymbol(void* dso, const char* symbol); 90 bool StartDebugger(); 91 bool PostTask(TaskFuncType task); 92 bool HasHigherPriorityTask(); GetLazyApis()93 CJRuntimeAPI* GetLazyApis() { return lazyApis_; } SetLazyApis(CJRuntimeAPI * apis)94 void SetLazyApis(CJRuntimeAPI* apis) { lazyApis_ = apis; } 95 96 void PreloadLibs(); 97 void InitCJAppNS(const std::string& path); 98 void InitCJSDKNS(const std::string& path); 99 void InitNewCJAppNS(const std::string& path); 100 void InitNewCJSDKNS(const std::string& path); 101 void InitCJSysNS(const std::string& path); 102 void InitCJChipSDKNS(const std::string& path); 103 void InitNewCJChipSDKNS(const std::string& path); 104 void InitRuntimeNS(); 105 void InitCJNS(const std::string& path); 106 static NSMode DetectAppNSMode(); 107 108 static const char *cjAppNSName; 109 static const char *cjSDKNSName; 110 static const char *cjSysNSName; 111 static const char *cjChipSDKNSName; 112 static const char *cjNewAppNSName; 113 static const char *cjNewSDKNSName; 114 static const char *cjNewSysNSName; 115 static const char *cjNDKNSName; 116 117 private: 118 bool LoadRuntimeApis(); 119 bool isRuntimeApiLoaded {false}; 120 CJRuntimeAPI* lazyApis_ {nullptr}; 121 bool isRuntimeStarted_{false}; 122 bool isLoadCJLibrary_{false}; 123 bool isUISchedulerStarted_{false}; 124 void* uiScheduler_ {nullptr}; 125 SanitizerKind sanitizerKind_ {SanitizerKind::NONE}; 126 NSMode nsMode_; 127 std::vector<void*> preloadLibs_; 128 }; 129 130 } 131 132 #endif //OHOS_ABILITY_RUNTIME_CJ_ENVIRONMENT_H 133