1 /*
2  * Copyright (c) 2021-2023 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_RUNTIME_H
17 #define OHOS_ABILITY_RUNTIME_RUNTIME_H
18 
19 #include <map>
20 #include <string>
21 #include <vector>
22 
23 class ModuleCheckerDelegate;
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 class EventRunner;
28 } // namespace AppExecFwk
29 namespace AbilityRuntime {
30 class Runtime {
31 public:
32     enum class Language {
33         JS = 0,
34         CJ
35     };
36 
37     struct Options {
38         Language lang = Language::JS;
39         std::string bundleName;
40         std::string moduleName;
41         std::string codePath;
42         std::string bundleCodeDir;
43         std::string hapPath;
44         std::string arkNativeFilePath;
45         std::string packagePathStr;
46         std::vector<std::string> assetBasePathStr;
47         std::shared_ptr<AppExecFwk::EventRunner> eventRunner = nullptr;
48         std::map<std::string, std::string> hapModulePath;
49         bool loadAce = true;
50         bool preload = false;
51         bool isBundle = true;
52         bool isDebugVersion = false;
53         bool isJsFramework = false;
54         bool isStageModel = true;
55         bool isTestFramework = false;
56         bool jitEnabled = false;
57         bool isMultiThread = false;
58         bool isErrorInfoEnhance = false;
59         int32_t uid = -1;
60         // ArkTsCard start
61         bool isUnique = false;
62         // ArkTsCard end
63         std::shared_ptr<ModuleCheckerDelegate> moduleCheckerDelegate = nullptr;
64         int32_t apiTargetVersion = 0;
65         std::map<std::string, std::string> pkgContextInfoJsonStringMap;
66         std::map<std::string, std::string> packageNameList;
67         std::map<std::string, int32_t> aotCompileStatusMap;
68     };
69 
70     struct DebugOption {
71         std::string bundleName = "";
72         std::string perfCmd;
73         std::string processName = "";
74         bool isDebugApp = true;
75         bool isStartWithDebug = false;
76         bool isStartWithNative = false;
77     };
78 
79     static std::unique_ptr<Runtime> Create(const Options& options);
80     static void SavePreloaded(std::unique_ptr<Runtime>&& instance);
81     static std::unique_ptr<Runtime> GetPreloaded();
82 
83     Runtime() = default;
84     virtual ~Runtime() = default;
85 
86     virtual Language GetLanguage() const = 0;
87 
88     virtual void StartDebugMode(const DebugOption debugOption) = 0;
89     virtual void DumpHeapSnapshot(bool isPrivate) = 0;
90     virtual void DumpCpuProfile() = 0;
91     virtual void DestroyHeapProfiler() = 0;
92     virtual void ForceFullGC() = 0;
93     virtual void ForceFullGC(uint32_t tid) = 0;
94     virtual void DumpHeapSnapshot(uint32_t tid, bool isFullGC) = 0;
95     virtual void AllowCrossThreadExecution() = 0;
96     virtual void GetHeapPrepare() = 0;
97     virtual void NotifyApplicationState(bool isBackground) = 0;
98     virtual bool SuspendVM(uint32_t tid) = 0;
99     virtual void ResumeVM(uint32_t tid) = 0;
100     virtual void PreloadSystemModule(const std::string& moduleName) = 0;
101     virtual void PreloadMainAbility(const std::string& moduleName, const std::string& srcPath,
102         const std::string& hapPath,  bool isEsMode, const std::string& srcEntrance) = 0;
103     virtual void PreloadModule(const std::string& moduleName, const std::string& srcPath,
104         const std::string& hapPath, bool isEsMode, bool useCommonTrunk) = 0;
105     virtual void FinishPreload() = 0;
106     virtual bool LoadRepairPatch(const std::string& patchFile, const std::string& baseFile) = 0;
107     virtual bool NotifyHotReloadPage() = 0;
108     virtual bool UnLoadRepairPatch(const std::string& patchFile) = 0;
109     virtual void RegisterQuickFixQueryFunc(const std::map<std::string, std::string>& moduleAndPath) = 0;
110     virtual void StartProfiler(const DebugOption debugOption) = 0;
DoCleanWorkAfterStageCleaned()111     virtual void DoCleanWorkAfterStageCleaned() {}
SetModuleLoadChecker(const std::shared_ptr<ModuleCheckerDelegate> moduleCheckerDelegate)112     virtual void SetModuleLoadChecker(const std::shared_ptr<ModuleCheckerDelegate> moduleCheckerDelegate) const {}
113     virtual void SetDeviceDisconnectCallback(const std::function<bool()> &cb) = 0;
114     virtual void UpdatePkgContextInfoJson(std::string moduleName, std::string hapPath, std::string packageName) = 0;
115     Runtime(const Runtime&) = delete;
116     Runtime(Runtime&&) = delete;
117     Runtime& operator=(const Runtime&) = delete;
118     Runtime& operator=(Runtime&&) = delete;
119 };
120 }  // namespace AbilityRuntime
121 }  // namespace OHOS
122 #endif  // OHOS_ABILITY_RUNTIME_RUNTIME_H
123