1 /*
2  * Copyright (c) 2021-2024 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_APP_LIFECYCLE_DEAL_H
17 #define OHOS_ABILITY_RUNTIME_APP_LIFECYCLE_DEAL_H
18 
19 #include "app_scheduler_proxy.h"
20 #include "app_launch_data.h"
21 #include "ability_running_record.h"
22 #include "fault_data.h"
23 #include "hap_module_info.h"
24 #include "want.h"
25 #include "app_malloc_info.h"
26 #include "app_jsheap_mem_info.h"
27 
28 namespace OHOS {
29 namespace AppExecFwk {
30 class AppLifeCycleDeal {
31 public:
32     AppLifeCycleDeal();
33     virtual ~AppLifeCycleDeal();
34 
35     /**
36      * LaunchApplication, call ScheduleLaunchApplication() through proxy project,
37      * Notify application to launch application.
38      *
39      * @param launchData The app data when launch.
40      * @param config The app config when launch.
41      * @return
42      */
43     void LaunchApplication(const AppLaunchData &launchData, const Configuration &config);
44 
45     /**
46      * update the application info after new module installed.
47      *
48      * @param appInfo The latest application info obtained from bms for update abilityRuntimeContext.
49      *
50      * @return
51      */
52     void UpdateApplicationInfoInstalled(const ApplicationInfo &appInfo);
53 
54     /**
55      * AddAbilityStageInfo, call ScheduleAbilityStageInfo() through proxy project,
56      * Notify application to launch application.
57      *
58      * @param abilityStage The app data value.
59      *
60      * @return
61      */
62     void AddAbilityStage(const HapModuleInfo &abilityStage);
63 
64     /**
65      * LaunchAbility, call ScheduleLaunchAbility() through proxy project,
66      * Notify application to launch ability.
67      *
68      * @param ability The ability info.
69      * @return
70      */
71     void LaunchAbility(const std::shared_ptr<AbilityRunningRecord> &ability);
72 
73     /**
74      * ScheduleTerminate, call ScheduleTerminateApplication() through proxy project,
75      * Notify application to terminate.
76      *
77      * @param isLastProcess When it is the last application process, pass in true.
78      *
79      * @return
80      */
81     void ScheduleTerminate(bool isLastProcess = false);
82 
83     /**
84      * ScheduleForegroundRunning, call ScheduleForegroundApplication() through proxy project,
85      * Notify application to switch to foreground.
86      *
87      * @return bool operation status
88      */
89     bool ScheduleForegroundRunning();
90 
91     /**
92      * ScheduleBackgroundRunning, call ScheduleBackgroundApplication() through proxy project,
93      * Notify application to switch to background.
94      *
95      * @return
96      */
97     void ScheduleBackgroundRunning();
98 
99     /**
100      * ScheduleTrimMemory, call ScheduleShrinkMemory() through proxy project,
101      * Notifies the application of the memory seen.
102      *
103      * @param timeLevel The memory value.
104      *
105      * @return
106      */
107     void ScheduleTrimMemory(int32_t timeLevel);
108 
109     /**
110      * ScheduleMemoryLevel, call ScheduleMemoryLevel() through proxy project,
111      * Notifies the application of the current memory.
112      *
113      * @param The memory level.
114      *
115      * @return
116      */
117     void ScheduleMemoryLevel(int32_t Level);
118 
119     /**
120      * ScheduleHeapMemory, call ScheduleHeapMemory() through proxy project,
121      * Get the application's memory allocation info.
122      *
123      * @param pid, pid input.
124      * @param mallocInfo, dynamic storage information output.
125      *
126      * @return
127      */
128     void ScheduleHeapMemory(const int32_t pid, OHOS::AppExecFwk::MallocInfo &mallocInfo);
129 
130     /**
131      * ScheduleJsHeapMemory, call ScheduleJsHeapMemory() through proxy project,
132      * triggerGC and dump the application's jsheap memory info.
133      *
134      * @param info, pid, tid, needGc, needSnapshot
135      *
136      * @return
137      */
138     void ScheduleJsHeapMemory(OHOS::AppExecFwk::JsHeapDumpInfo &info);
139 
140     /**
141      * LowMemoryWarning, call ScheduleLowMemory() through proxy project,
142      * Notify application to low memory.
143      *
144      * @return
145      */
146     void LowMemoryWarning();
147 
148     /**
149      * ScheduleCleanAbility, call ScheduleCleanAbility() through proxy project,
150      * Notify application to clean ability.
151      *
152      * @param token, The ability token.
153      * @return
154      */
155     void ScheduleCleanAbility(const sptr<IRemoteObject> &token, bool isCacheProcess = false);
156 
157     /**
158      * ScheduleProcessSecurityExit, call ScheduleTerminateApplication() through proxy project,
159      * Notify application process exit safely.
160      *
161      * @return
162      */
163     void ScheduleProcessSecurityExit();
164 
165     /**
166      * scheduleClearPageStack, call scheduleClearPageStack() through proxy project,
167      * Notify application clear recovery page stack.
168      *
169      * @return
170      */
171     void ScheduleClearPageStack();
172 
173     /**
174      * @brief Setting client for application record.
175      *
176      * @param thread, the application client.
177      */
178     void SetApplicationClient(const sptr<IAppScheduler> &thread);
179 
180     /**
181      * @brief Obtains the client of the application record.
182      *
183      * @return Returns the application client.
184      */
185     sptr<IAppScheduler> GetApplicationClient() const;
186 
187     /**
188      * @brief Schedule the given module the onAcceptWant lifecycle call.
189      *
190      * @param want the param passed to onAcceptWant lifecycle.
191      * @param want the moduleName of which being scheduled.
192      */
193     void ScheduleAcceptWant(const AAFwk::Want &want, const std::string &moduleName);
194 
195     void ScheduleNewProcessRequest(const AAFwk::Want &want, const std::string &moduleName);
196 
197     /**
198      * UpdateConfiguration, ANotify application update system environment changes.
199      *
200      * @param config, System environment change parameters.
201      * @return Returns ERR_OK on success, others on failure.
202      */
203     int32_t UpdateConfiguration(const Configuration &config);
204 
205     /**
206      * @brief Notify application load patch.
207      *
208      * @param bundleName Bundle name
209      * @param callback called when LoadPatch finished.
210      * @param recordId callback data
211      * @return Returns 0 on success, error code on failure.
212      */
213     int32_t NotifyLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback,
214         const int32_t recordId);
215 
216     /**
217      * @brief Notify application reload page.
218      *
219      * @param callback called when HotReload finished.
220      * @param recordId callback data
221      * @return Returns 0 on success, error code on failure.
222      */
223     int32_t NotifyHotReloadPage(const sptr<IQuickFixCallback> &callback, const int32_t recordId);
224 
225     /**
226      * @brief Notify application unload patch.
227      *
228      * @param bundleName Bundle name
229      * @param callback called when UnloadPatch finished.
230      * @param recordId callback data
231      * @return Returns 0 on success, error code on failure.
232      */
233     int32_t NotifyUnLoadRepairPatch(const std::string &bundleName, const sptr<IQuickFixCallback> &callback,
234         const int32_t recordId);
235 
236     /**
237      * Notify Fault Data
238      *
239      * @param faultData the fault data.
240      * @return Returns ERR_OK on success, others on failure.
241      */
242     int32_t NotifyAppFault(const FaultData &faultData);
243 
244     /**
245      * @brief Notify NativeEngine GC of status change.
246      *
247      * @param state GC state
248      *
249      * @return Is the status change completed.
250      */
251     int32_t ChangeAppGcState(int32_t state);
252 
253     /**
254      * @brief attach to a process to debug.
255      *
256      * @return ERR_OK, return back success, others fail.
257      */
258     int32_t AttachAppDebug();
259 
260     /**
261      * @brief detach a debugging process.
262      *
263      * @return ERR_OK, return back success, others fail.
264      */
265     int32_t DetachAppDebug();
266 
267     /**
268      * Whether the current application process is the last surviving process.
269      *
270      * @return Returns true is final application process, others return false.
271      */
272     bool IsFinalAppProcess();
273 
274     int DumpIpcStart(std::string& result);
275 
276     int DumpIpcStop(std::string& result);
277 
278     int DumpIpcStat(std::string& result);
279 
280     /**
281      * Notifies the application of process caching.
282      */
283     void ScheduleCacheProcess();
284 
285     int DumpFfrt(std::string& result);
286 
287 private:
288     mutable std::mutex schedulerMutex_;
289     sptr<IAppScheduler> appThread_ = nullptr;
290 };
291 }  // namespace AppExecFwk
292 }  // namespace OHOS
293 
294 #endif  // OHOS_ABILITY_RUNTIME_APP_LIFECYCLE_DEAL_H
295