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 21 #include <string> 22 #include <functional> 23 24 #ifdef WINDOWS_PLATFORM 25 #define CJ_EXPORT __declspec(dllexport) 26 #else 27 #define CJ_EXPORT __attribute__((visibility("default"))) 28 #endif 29 30 namespace OHOS { 31 struct CJRuntimeAPI; 32 33 using TaskFuncType = void(*)(); 34 35 class CJ_EXPORT CJEnvironment final { 36 public: 37 static CJEnvironment* GetInstance(); 38 static void InitSpawnEnv(); 39 static void SetAppPath(const std::string& paths); 40 static CJEnvMethods* CreateEnvMethods(); 41 42 enum class NSMode { 43 SINK, 44 APP, 45 }; 46 47 CJEnvironment(NSMode mode); 48 ~CJEnvironment(); 49 IsRuntimeStarted()50 bool IsRuntimeStarted() 51 { 52 return isRuntimeStarted_; 53 } 54 SetSanitizerKindRuntimeVersion(SanitizerKind kind)55 void SetSanitizerKindRuntimeVersion(SanitizerKind kind) 56 { 57 sanitizerKind_ = kind; 58 } 59 60 bool StartRuntime(); 61 void StopRuntime(); 62 void RegisterCJUncaughtExceptionHandler(const CJUncaughtExceptionInfo& handle); IsUISchedulerStarted()63 bool IsUISchedulerStarted() 64 { 65 return isUISchedulerStarted_; 66 } 67 bool StartUIScheduler(); 68 void StopUIScheduler(); 69 enum LibraryKind { 70 SYSTEM, 71 SDK, 72 APP, 73 }; 74 void* LoadCJLibrary(const char* dlName); 75 void* LoadCJLibrary(LibraryKind kind, const char* dlName); 76 void UnLoadCJLibrary(void* handle); GetUIScheduler()77 void* GetUIScheduler() 78 { 79 if (!isUISchedulerStarted_) { 80 return nullptr; 81 } 82 return uiScheduler_; 83 } 84 void* GetSymbol(void* dso, const char* symbol); 85 bool StartDebugger(); 86 bool PostTask(TaskFuncType task); 87 bool HasHigherPriorityTask(); GetLazyApis()88 CJRuntimeAPI* GetLazyApis() { return lazyApis_; } SetLazyApis(CJRuntimeAPI * apis)89 void SetLazyApis(CJRuntimeAPI* apis) { lazyApis_ = apis; } SetRuntimeState(bool state)90 void SetRuntimeState(bool state) { isRuntimeStarted_ = state; } SetUISchedulerState(bool state)91 void SetUISchedulerState(bool state) { isUISchedulerStarted_ = state; } 92 93 void PreloadLibs(); 94 void InitCJAppNS(const std::string& path); 95 void InitCJSDKNS(const std::string& path); 96 void InitNewCJAppNS(const std::string& path); 97 void InitNewCJSDKNS(const std::string& path); 98 void InitCJSysNS(const std::string& path); 99 void InitCJChipSDKNS(const std::string& path); 100 void InitNewCJChipSDKNS(const std::string& path); 101 void InitRuntimeNS(); 102 void InitCJNS(const std::string& path); 103 static NSMode DetectAppNSMode(); 104 105 static const char *cjAppNSName; 106 static const char *cjSDKNSName; 107 static const char *cjSysNSName; 108 static const char *cjChipSDKNSName; 109 static const char *cjNewAppNSName; 110 static const char *cjNewSDKNSName; 111 static const char *cjNewSysNSName; 112 static const char *cjNDKNSName; 113 114 private: 115 bool LoadRuntimeApis(); 116 bool isRuntimeApiLoaded {false}; 117 CJRuntimeAPI* lazyApis_ {nullptr}; 118 bool isRuntimeStarted_{false}; 119 bool isUISchedulerStarted_{false}; 120 void* uiScheduler_ {nullptr}; 121 SanitizerKind sanitizerKind_ {SanitizerKind::NONE}; 122 NSMode nsMode_; 123 }; 124 125 } 126 127 #endif //OHOS_ABILITY_RUNTIME_CJ_ENVIRONMENT_H 128