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_OHOS_APPLICATION_H
17 #define OHOS_ABILITY_RUNTIME_OHOS_APPLICATION_H
18 
19 #include <functional>
20 #include <string>
21 #include <list>
22 #include <memory>
23 
24 #include "ability_lifecycle_callbacks.h"
25 #include "ability_stage.h"
26 #include "app_context.h"
27 #include "context.h"
28 #include "element_callback.h"
29 #include "ability_stage_context.h"
30 #include "application_configuration_manager.h"
31 
32 namespace OHOS {
33 namespace AbilityRuntime {
34 class Runtime;
35 } // namespace AbilityRuntime
36 namespace AppExecFwk {
37 class ElementsCallback;
38 class ApplicationImpl;
39 class Configuration;
40 class AbilityRecordMgr;
41 class OHOSApplication : public AppContext,
42                         public AbilityLifecycleCallbacks {
43 public:
44     OHOSApplication();
45     virtual ~OHOSApplication();
46 
47     /**
48      * @brief dump OHOSApplication info
49      *
50      * @param extra dump OHOSApplication info
51      */
52     void DumpApplication();
53 
54     /**
55      * @brief Set Runtime
56      *
57      * @param runtime Runtime instance.
58      */
59     void SetRuntime(std::unique_ptr<AbilityRuntime::Runtime>&& runtime);
60 
61     /**
62      * @brief Set ApplicationContext
63      *
64      * @param context ApplicationContext instance.
65      */
66     void SetApplicationContext(const std::shared_ptr<AbilityRuntime::ApplicationContext> &abilityRuntimeContext);
67 
68     /**
69      *
70      * @brief Set the abilityRecordMgr to the OHOSApplication.
71      *
72      * @param abilityRecordMgr
73      */
74     void SetAbilityRecordMgr(const std::shared_ptr<AbilityRecordMgr> &abilityRecordMgr);
75 
76     /**
77      *
78      * @brief Register AbilityLifecycleCallbacks with OHOSApplication
79      *
80      * @param callBack callBack When the life cycle of the ability in the application changes,
81      */
82     void RegisterAbilityLifecycleCallbacks(const std::shared_ptr<AbilityLifecycleCallbacks> &callBack);
83 
84     /**
85      *
86      * @brief Unregister AbilityLifecycleCallbacks with OHOSApplication
87      *
88      * @param callBack RegisterAbilityLifecycleCallbacks`s callBack
89      */
90     void UnregisterAbilityLifecycleCallbacks(const std::shared_ptr<AbilityLifecycleCallbacks> &callBack);
91 
92     /**
93      *
94      * @brief Will be called when the given ability calls Ability->onStart
95      *
96      * @param Ability Indicates the ability object that calls the onStart() method.
97      */
98     void OnAbilityStart(const std::shared_ptr<AbilityRuntime::UIAbility> &ability);
99 
100     /**
101      *
102      * @brief Will be called when the given ability calls Ability->onInactive
103      *
104      * @param Ability Indicates the Ability object that calls the onInactive() method.
105      */
106     void OnAbilityInactive(const std::shared_ptr<AbilityRuntime::UIAbility> &ability);
107 
108     /**
109      *
110      * @brief Will be called when the given ability calls Ability->onBackground
111      *
112      * @param Ability Indicates the Ability object that calls the onBackground() method.
113      */
114     void OnAbilityBackground(const std::shared_ptr<AbilityRuntime::UIAbility> &ability);
115 
116     /**
117      *
118      * @brief Will be called when the given ability calls Ability->onForeground
119      *
120      * @param Ability Indicates the Ability object that calls the onForeground() method.
121      */
122     void OnAbilityForeground(const std::shared_ptr<AbilityRuntime::UIAbility> &ability);
123 
124     /**
125      *
126      * @brief Will be called when the given ability calls Ability->onActive
127      *
128      * @param Ability Indicates the Ability object that calls the onActive() method.
129      */
130     void OnAbilityActive(const std::shared_ptr<AbilityRuntime::UIAbility> &ability);
131 
132     /**
133      *
134      * @brief Will be called when the given ability calls Ability->onStop
135      *
136      * @param Ability Indicates the Ability object that calls the onStop() method.
137      */
138     void OnAbilityStop(const std::shared_ptr<AbilityRuntime::UIAbility> &ability);
139 
140     /**
141      *
142      * Called when Ability#onSaveAbilityState(PacMap) was called on an ability.
143      *
144      * @param outState Indicates the PacMap object passed to Ability#onSaveAbilityState(PacMap)
145      * for storing user data and states. This parameter cannot be null.
146      */
147     void DispatchAbilitySavedState(const PacMap &outState);
148 
149     /**
150      *
151      * @brief Called when an ability calls Ability#onSaveAbilityState(PacMap).
152      * You can implement your own logic in this method.
153      * @param outState IIndicates the {@link PacMap} object passed to the onSaveAbilityState() callback.
154      *
155      */
156     void OnAbilitySaveState(const PacMap &outState);
157 
158     /**
159      *
160      * @brief Register ElementsCallback with OHOSApplication
161      *
162      * @param callBack callBack when the system configuration of the device changes.
163      */
164     void RegisterElementsCallbacks(const std::shared_ptr<ElementsCallback> &callback);
165 
166     /**
167      *
168      * @brief Unregister ElementsCallback with OHOSApplication
169      *
170      * @param callback RegisterElementsCallbacks`s callback
171      */
172     void UnregisterElementsCallbacks(const std::shared_ptr<ElementsCallback> &callback);
173 
174     /**
175      *
176      * @brief Will be Called when the system configuration of the device changes.
177      *
178      * @param config Indicates the new Configuration object.
179      */
180     virtual void OnConfigurationUpdated(Configuration config,
181         AbilityRuntime::SetLevel level = AbilityRuntime::SetLevel::System);
182 
183     /**
184      *
185      * @brief Will be Called when the application font of the device changes.
186      *
187      * @param config Indicates the new Configuration object.
188      */
189     virtual void OnFontUpdated(Configuration config);
190 
191     /**
192      *
193      * @brief Called when the system has determined to trim the memory, for example,
194      * when the ability is running in the background and there is no enough memory for
195      * running as many background processes as possible.
196      *
197      * @param level Indicates the memory trim level, which shows the current memory usage status.
198      */
199     virtual void OnMemoryLevel(int level);
200 
201     /**
202      *
203      * @brief Will be called the application foregrounds
204      *
205      */
206     virtual void OnForeground();
207 
208     /**
209      *
210      * @brief Will be called the application backgrounds
211      *
212      */
213     virtual void OnBackground();
214 
215     /**
216      *
217      * @brief Will be called the application starts
218      *
219      */
220     virtual void OnStart();
221 
222     /**
223      *
224      * @brief Will be called the application ends
225      */
226     virtual void OnTerminate();
227 
228     /**
229      * @brief add the ability stage when a hap first load
230      *
231      * @param abilityRecord
232      * @return abilityStage context
233      */
234     std::shared_ptr<AbilityRuntime::Context> AddAbilityStage(
235         const std::shared_ptr<AbilityLocalRecord> &abilityRecord,
236         const std::function<void(const std::shared_ptr<AbilityRuntime::Context> &)> &callback, bool &isAsyncCallback);
237 
238     /**
239      *
240      * @brief update the application info after new module installed.
241      *
242      * @param appInfo The latest application info obtained from bms for update abilityRuntimeContext.
243      *
244      */
245     void UpdateApplicationInfoInstalled(const AppExecFwk::ApplicationInfo &appInfo);
246 
247     /**
248      * @brief add the ability stage when a hap first load
249      *
250      * @param hapModuleInfo
251      * @return Returns true on success, false on failure
252      */
253     bool AddAbilityStage(
254         const AppExecFwk::HapModuleInfo &hapModuleInfo,
255         const std::function<void()> &callback, bool &isAsyncCallback);
256 
257     /**
258      * @brief remove the ability stage when all of the abilities in the hap have been removed
259      *
260      * @param abilityInfo
261      */
262     void CleanAbilityStage(const sptr<IRemoteObject> &token, const std::shared_ptr<AbilityInfo> &abilityInfo,
263         bool isCacheProcess);
264 
265     /**
266      * @brief return the application context
267      *
268      * @param context
269      */
270     std::shared_ptr<AbilityRuntime::Context> GetAppContext() const;
271 
272     /**
273      * @brief return the application runtime
274      *
275      * @param runtime
276      */
277     const std::unique_ptr<AbilityRuntime::Runtime>& GetRuntime();
278 
279     /*
280      *
281      * @brief Will be called the application ends
282      *
283      */
284     virtual void SetConfiguration(const Configuration &config);
285 
286     void ScheduleAcceptWant(const AAFwk::Want &want, const std::string &moduleName, std::string &flag);
287 
288     void ScheduleNewProcessRequest(const AAFwk::Want &want, const std::string &moduleName, std::string &flag);
289 
290     virtual std::shared_ptr<Configuration> GetConfiguration();
291 
GetExtensionNameByType(int32_t type,std::string & name)292     void GetExtensionNameByType(int32_t type, std::string &name)
293     {
294         std::map<int32_t, std::string>::iterator it = extensionTypeMap_.find(type);
295         if (it == extensionTypeMap_.end()) {
296             return;
297         }
298         name = it->second;
299     }
300 
301     /**
302      * @brief Set extension types.
303      *
304      * @param map The extension types.
305      */
306     void SetExtensionTypeMap(std::map<int32_t, std::string> map);
307 
308     bool NotifyLoadRepairPatch(const std::string &hqfFile, const std::string &hapPath);
309 
310     bool NotifyHotReloadPage();
311 
312     bool NotifyUnLoadRepairPatch(const std::string &hqfFile);
313 
314     void CleanAppTempData(bool isLastProcess = false);
315 
316     void CleanUselessTempData();
317 
318     void SetAppEnv(const std::vector<AppEnvironment>& appEnvironments);
319 
320     void AutoStartupDone(const std::shared_ptr<AbilityLocalRecord> &abilityRecord,
321         const std::shared_ptr<AbilityRuntime::AbilityStage> &abilityStage, const std::string &moduleName);
322 
323     void AutoStartupDone(const std::shared_ptr<AbilityRuntime::AbilityStage> &abilityStage,
324         const AppExecFwk::HapModuleInfo &hapModuleInfo);
325 
326     void CleanEmptyAbilityStage();
327 
328 private:
329     void UpdateAppContextResMgr(const Configuration &config);
330     bool IsUpdateColorNeeded(Configuration &config, AbilityRuntime::SetLevel level);
331     bool isUpdateFontSize(Configuration &config, AbilityRuntime::SetLevel level);
332     bool IsUpdateLanguageNeeded(Configuration &config, AbilityRuntime::SetLevel level);
333     const std::function<void()> CreateAutoStartupCallback(
334         const std::shared_ptr<AbilityRuntime::AbilityStage> abilityStage,
335         const std::shared_ptr<AbilityLocalRecord> abilityRecord,
336         const std::function<void(const std::shared_ptr<AbilityRuntime::Context>&)>& callback);
337     const std::function<void()> CreateAutoStartupCallback(
338         const std::shared_ptr<AbilityRuntime::AbilityStage> &abilityStage,
339         const AppExecFwk::HapModuleInfo &hapModuleInfo,
340         const std::function<void()>& callback);
341     bool IsMainProcess(const std::string &bundleName, const std::string &process);
342 
343 private:
344     std::list<std::shared_ptr<AbilityLifecycleCallbacks>> abilityLifecycleCallbacks_;
345     std::list<std::shared_ptr<ElementsCallback>> elementsCallbacks_;
346     std::shared_ptr<AbilityRecordMgr> abilityRecordMgr_ = nullptr;
347     std::shared_ptr<AbilityRuntime::ApplicationContext> abilityRuntimeContext_ = nullptr;
348     std::unordered_map<std::string, std::shared_ptr<AbilityRuntime::AbilityStage>> abilityStages_;
349     std::unique_ptr<AbilityRuntime::Runtime> runtime_;
350     std::shared_ptr<Configuration> configuration_ = nullptr;
351     std::map<int32_t, std::string> extensionTypeMap_;
352 };
353 }  // namespace AppExecFwk
354 }  // namespace OHOS
355 #endif  // OHOS_ABILITY_RUNTIME_OHOS_APPLICATION_H
356