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