1/*
2 * Copyright (c) 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_APP_UTILS_H
17#define OHOS_ABILITY_RUNTIME_APP_UTILS_H
18
19#include <mutex>
20#include <string>
21
22#include "nocopyable.h"
23
24namespace OHOS {
25namespace AAFwk {
26constexpr const int32_t DEFAULT_MAX_EXT_PER_PROC = 10;
27constexpr const int32_t DEFAULT_MAX_EXT_PER_DEV = 100;
28constexpr const int32_t DEFAULT_INVALID_VALUE = -1;
29constexpr const int32_t DEFAULT_MAX_CHILD_PROCESS = 0;
30template<typename T>
31class DeviceConfiguration {
32public:
33    bool isLoaded = false;
34    T value;
35};
36
37/**
38 * @class AppUtils
39 * provides app utils.
40 */
41class AppUtils {
42public:
43    /**
44     * GetInstance, get an instance of AppUtils.
45     *
46     * @return An instance of AppUtils.
47     */
48    static AppUtils &GetInstance();
49
50    /**
51     * AppUtils, destructor.
52     *
53     */
54    ~AppUtils();
55
56    /**
57     * IsLauncher, check if it is a launcher.
58     *
59     * @param bundleName The bundle name.
60     * @return Whether it is a launcher.
61     */
62    bool IsLauncher(const std::string &bundleName) const;
63
64    /**
65     * IsLauncherAbility, check if it is a launcher ability.
66     *
67     * @param abilityName The ability name.
68     * @return Whether it is a launcher ability.
69     */
70    bool IsLauncherAbility(const std::string &abilityName) const;
71
72    /**
73     * IsInheritWindowSplitScreenMode, check if it is inherit window split screen mode.
74     *
75     * @return Whether it is inherit window split screen mode.
76     */
77    bool IsInheritWindowSplitScreenMode();
78
79    /**
80     * IsSupportAncoApp, check if it supports anco app.
81     *
82     * @return Whether it supports anco app.
83     */
84    bool IsSupportAncoApp();
85
86    /**
87     * GetTimeoutUnitTimeRatio, get timeout unit time ratio.
88     *
89     * @return Timeout unit time ratio.
90     */
91    int32_t GetTimeoutUnitTimeRatio();
92
93    /**
94     * IsSelectorDialogDefaultPossion, check if selector dialog is on the default position.
95     *
96     * @return Whether selector dialog is on the default position.
97     */
98    bool IsSelectorDialogDefaultPossion();
99
100    /**
101     * IsStartSpecifiedProcess, check whether or not to start specified process.
102     *
103     * @return Whether or not to start specified process.
104     */
105    bool IsStartSpecifiedProcess();
106
107    /**
108     * IsUseMultiRenderProcess, check whether uses multi-render process.
109     *
110     * @return Whether uses multi-render process.
111     */
112    bool IsUseMultiRenderProcess();
113
114    /**
115     * IsLimitMaximumOfRenderProcess, check limit maximum of render process.
116     *
117     * @return Whether limit maximum of render process.
118     */
119    bool IsLimitMaximumOfRenderProcess();
120
121    /**
122     * IsGrantPersistUriPermission, check whether to grant persist uri permission.
123     *
124     * @return Whether to grant persist uri permission.
125     */
126    bool IsGrantPersistUriPermission();
127
128    /**
129     * IsStartOptionsWithAnimation, check whether the start options have animation.
130     *
131     * @return Whether the start options have animation.
132     */
133    bool IsStartOptionsWithAnimation();
134
135    /**
136     * IsStartOptionsWithAnimation, check whether it is a multi-process model.
137     *
138     * @return Whether it is a multi-process model.
139     */
140    bool IsMultiProcessModel();
141
142    /**
143     * IsStartOptionsWithProcessOptions, check whether the start options have process options.
144     *
145     * @return Whether the start options have process options.
146     */
147    bool IsStartOptionsWithProcessOptions();
148
149    /**
150     * EnableMoveUIAbilityToBackgroundApi, enable move ui-ability to background api.
151     *
152     * @return Whether the enable move ui-ability to background api is successful.
153     */
154    bool EnableMoveUIAbilityToBackgroundApi();
155
156    /**
157     * IsLaunchEmbededUIAbility, check if it is to launch embedded ui-ability.
158     *
159     * @return Whether it is to launch embedded ui-ability.
160     */
161    bool IsLaunchEmbededUIAbility();
162
163    /**
164     * IsSupportNativeChildProcess, check if it supports native child process.
165     *
166     * @return Whether it supports native child process.
167     */
168    bool IsSupportNativeChildProcess();
169
170    /**
171     * IsSupportMultiInstance, check if it supports multi-instance.
172     *
173     * @return Whether it supports multi-instance.
174     */
175    bool IsSupportMultiInstance();
176
177    /**
178     * IsAllowResidentInExtremeMemory, check if it allows resident in extrem low memory.
179     *
180     * @param bundleName The bundle name.
181     * @param abilityName The ability name.
182     * @return Whether it allows resident in extrem low memory.
183     */
184    bool IsAllowResidentInExtremeMemory(const std::string& bundleName, const std::string& abilityName = "");
185
186    /**
187     * IsAllowNativeChildProcess, check if it allows native child process.
188     *
189     * @param appIdentifier The app identifier.
190     * @return Whether it allows native child process.
191     */
192    bool IsAllowNativeChildProcess(const std::string &appIdentifier);
193
194    /**
195     * GetLimitMaximumExtensionsPerProc, get limit max extensions per proc.
196     *
197     * @return Limit max extensions per proc.
198     */
199    int32_t GetLimitMaximumExtensionsPerProc();
200
201    /**
202     * GetLimitMaximumExtensionsPerDevice, get limit max extensions per device.
203     *
204     * @return Limit max extensions per device.
205     */
206    int32_t GetLimitMaximumExtensionsPerDevice();
207
208    /**
209     * GetCacheExtensionTypeList, get cache extension type list.
210     *
211     * @return Cache extension type list.
212     */
213    std::string GetCacheExtensionTypeList();
214
215    /**
216     * IsAllowStartAbilityWithoutCallerToken, check if it allows start ability without caller token.
217     *
218     * @param bundleName The bundle name.
219     * @param abilityName The ability name.
220     * @return Whether it allows start ability without caller token.
221     */
222    bool IsAllowStartAbilityWithoutCallerToken(const std::string& bundleName, const std::string& abilityName);
223
224    /**
225     * GetBrokerDelegateBundleName, get broker delegate bundle name.
226     *
227     * @return Broker delegate bundle name.
228     */
229    std::string GetBrokerDelegateBundleName();
230
231    /**
232     * GetCollaboratorBrokerUID, get collaborator broker id.
233     *
234     * @return Collaborator broker id.
235     */
236    int32_t GetCollaboratorBrokerUID();
237
238    /**
239     * GetCollaboratorBrokerReserveUID, get collaborator broker reserve uid.
240     *
241     * @return Collaborator broker reserve uid.
242     */
243    int32_t GetCollaboratorBrokerReserveUID();
244
245    /**
246     * MaxChildProcess, get max child process.
247     *
248     * @return Max child process.
249     */
250    int32_t MaxChildProcess();
251
252    /**
253     * GetMigrateClientBundleName, get migrate client bundle name.
254     *
255     * @return Migrate client bundle name.
256     */
257    std::string GetMigrateClientBundleName();
258
259private:
260    /**
261     * LoadResidentProcessInExtremeMemory, load resident process in extreme low memory.
262     *
263     */
264    void LoadResidentProcessInExtremeMemory();
265
266    /**
267     * LoadAllowNativeChildProcessApps, load allow native child process apps.
268     *
269     */
270    void LoadAllowNativeChildProcessApps();
271
272    /**
273     * LoadStartAbilityWithoutCallerToken, load start ability without caller token.
274     *
275     */
276    void LoadStartAbilityWithoutCallerToken();
277
278    /**
279     * AppUtils, private constructor.
280     *
281     */
282    AppUtils();
283
284    volatile bool isSceneBoard_ = false;
285    volatile DeviceConfiguration<bool> isInheritWindowSplitScreenMode_ = {false, true};
286    volatile DeviceConfiguration<bool> isSupportAncoApp_ = {false, false};
287    volatile DeviceConfiguration<int32_t> timeoutUnitTimeRatio_ = {false, 1};
288    volatile DeviceConfiguration<bool> isSelectorDialogDefaultPossion_ = {false, true};
289    volatile DeviceConfiguration<bool> isStartSpecifiedProcess_ = {false, false};
290    volatile DeviceConfiguration<bool> isUseMultiRenderProcess_ = {false, true};
291    volatile DeviceConfiguration<bool> isLimitMaximumOfRenderProcess_ = {false, true};
292    volatile DeviceConfiguration<bool> isGrantPersistUriPermission_ = {false, false};
293    volatile DeviceConfiguration<bool> isStartOptionsWithAnimation_ = {false, false};
294    volatile DeviceConfiguration<bool> isMultiProcessModel_ = {false, false};
295    volatile DeviceConfiguration<bool> isStartOptionsWithProcessOptions_ = {false, false};
296    volatile DeviceConfiguration<bool> enableMoveUIAbilityToBackgroundApi_ = {false, true};
297    volatile DeviceConfiguration<bool> isLaunchEmbededUIAbility_ = {false, false};
298    volatile DeviceConfiguration<bool> isSupportNativeChildProcess_ = {false, false};
299    volatile DeviceConfiguration<bool> isSupportMultiInstance_ = {false, false};
300    DeviceConfiguration<std::vector<std::pair<std::string, std::string>>>
301        residentProcessInExtremeMemory_ = {false, {}};
302    std::mutex residentProcessInExtremeMemoryMutex_;
303    DeviceConfiguration<std::vector<std::string>>
304        allowStartNativeProcessApps_ = {false, {}};
305    volatile DeviceConfiguration<int32_t> limitMaximumExtensionsPerProc_ = {false, DEFAULT_MAX_EXT_PER_PROC};
306    volatile DeviceConfiguration<int32_t> limitMaximumExtensionsPerDevice_ = {false, DEFAULT_MAX_EXT_PER_DEV};
307    DeviceConfiguration<std::vector<std::pair<std::string, std::string>>>
308        startAbilityWithoutCallerToken_ = {false, {}};
309    std::mutex startAbilityWithoutCallerTokenMutex_;
310    DeviceConfiguration<std::string> brokerDelegateBundleName_ = {false, ""};
311    volatile DeviceConfiguration<int32_t> collaboratorBrokerUid_ = {false, DEFAULT_INVALID_VALUE};
312    volatile DeviceConfiguration<int32_t> collaboratorBrokerReserveUid_ = {false, DEFAULT_INVALID_VALUE};
313    volatile DeviceConfiguration<int32_t> maxChildProcess_ = {false, DEFAULT_MAX_CHILD_PROCESS};
314    DeviceConfiguration<std::string> migrateClientBundleName_ = {true, "com.huwei.hmos.migratecilent"};
315    DISALLOW_COPY_AND_MOVE(AppUtils);
316};
317}  // namespace AAFwk
318}  // namespace OHOS
319#endif  // OHOS_ABILITY_RUNTIME_APP_UTILS_H
320