1 /*
2  * Copyright (c) 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_RES_SCHED_UTIL_H
17 #define OHOS_ABILITY_RUNTIME_RES_SCHED_UTIL_H
18 
19 #include <unordered_set>
20 
21 #include "nocopyable.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 struct AbilityInfo;
26 }
27 namespace AAFwk {
28 using AbilityInfo = AppExecFwk::AbilityInfo;
29 constexpr int64_t RES_TYPE_SCB_START_ABILITY = 0;
30 constexpr int64_t RES_TYPE_EXTENSION_START_ABILITY = 1;
31 constexpr int64_t RES_TYPE_MISSION_LIST_START_ABILITY = 2;
32 
33 enum class LoadingStage : int32_t {
34     LOAD_BEGIN = 1,
35     LOAD_END,
36     FOREGROUND_BEGIN,
37     FOREGROUND_END,
38     DESTROY_BEGIN = 8,
39     DESTROY_END = 9,
40     PRELOAD_BEGIN,
41 };
42 
43 class ResSchedUtil final {
44 public:
45     static ResSchedUtil &GetInstance();
46     void ReportAbilityStartInfoToRSS(const AbilityInfo &abilityInfo, int32_t pid, bool isColdStart);
47     void ReportAbilityAssociatedStartInfoToRSS(
48         const AbilityInfo &abilityInfo, int64_t resSchedType, int32_t callerUid, int32_t callerPid);
49     void ReportEventToRSS(const int32_t uid, const std::string &bundleName, const std::string &reason,
50         const int32_t pid = -1, const int32_t callerPid = -1);
51     std::string GetThawReasonByAbilityType(const AbilityInfo &abilityInfo);
52     void GetAllFrozenPidsFromRSS(std::unordered_set<int32_t> &frozenPids);
53     bool CheckShouldForceKillProcess(int32_t pid);
54     void ReportLoadingEventToRss(LoadingStage stage, int32_t pid, int32_t uid,
55         int64_t timeDuration = 0, int64_t abilityRecordId = -1);
56     std::unordered_set<std::string> GetNWebPreloadSet() const;
57 private:
58     ResSchedUtil() = default;
59     ~ResSchedUtil() = default;
60     DISALLOW_COPY_AND_MOVE(ResSchedUtil);
61 
62     int64_t convertType(int64_t resSchedType);
63 };
64 } // namespace AAFwk
65 } // namespace OHOS
66 
67 #endif // OHOS_ABILITY_RUNTIME_RES_SCHED_UTIL_H