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 
IsRuntimeStarted()39     bool IsRuntimeStarted()
40     {
41         return isRuntimeStarted_;
42     }
43 
SetSanitizerKindRuntimeVersion(SanitizerKind kind)44     void SetSanitizerKindRuntimeVersion(SanitizerKind kind)
45     {
46         sanitizerKind_ = kind;
47     }
48     void InitCJAppNS(const std::string& path);
49     void InitCJSDKNS(const std::string& path);
50     void InitCJSysNS(const std::string& path);
51     void InitCJChipSDKNS(const std::string& path);
52     bool StartRuntime();
53     void StopRuntime();
54     void RegisterCJUncaughtExceptionHandler(const CJUncaughtExceptionInfo& handle);
IsUISchedulerStarted()55     bool IsUISchedulerStarted()
56     {
57         return isUISchedulerStarted_;
58     }
59     bool StartUIScheduler();
60     void StopUIScheduler();
61     enum LibraryKind {
62         SYSTEM,
63         SDK,
64         APP,
65     };
66     void* LoadCJLibrary(const char* dlName);
67     void* LoadCJLibrary(LibraryKind kind, const char* dlName);
68     void UnLoadCJLibrary(void* handle);
GetUIScheduler()69     void* GetUIScheduler()
70     {
71         if (!isUISchedulerStarted_) {
72             return nullptr;
73         }
74         return uiScheduler_;
75     }
76     void* GetSymbol(void* dso, const char* symbol);
77     bool StartDebugger();
78     bool PostTask(TaskFuncType task);
79     bool HasHigherPriorityTask();
80 
81     static const char *cjAppNSName;
82     static const char *cjSDKNSName;
83     static const char *cjSysNSName;
84     static const char *cjChipSDKNSName;
85 private:
86     bool LoadRuntimeApis();
87     static CJRuntimeAPI lazyApis_;
88     bool isRuntimeStarted_{false};
89     bool isUISchedulerStarted_{false};
90     void* uiScheduler_ {nullptr};
91     SanitizerKind sanitizerKind_ {SanitizerKind::NONE};
92 };
93 
94 }
95 
96 #endif //OHOS_ABILITY_RUNTIME_CJ_ENVIRONMENT_H
97