• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "res_sched_util.h"
17 
18 #include <string>
19 
20 #include "ability_info.h"
21 #include "ui_extension_utils.h"
22 #include "hilog_tag_wrapper.h"
23 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
24 #include "res_sched_client.h"
25 #include "res_type.h"
26 #endif
27 
28 namespace OHOS {
29 namespace AAFwk {
30 using AssociatedStartType = ResourceSchedule::ResType::AssociatedStartType;
GetInstance()31 ResSchedUtil &ResSchedUtil::GetInstance()
32 {
33     static ResSchedUtil instance;
34     return instance;
35 }
36 
convertType(int64_t resSchedType)37 int64_t ResSchedUtil::convertType(int64_t resSchedType)
38 {
39 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
40     if (resSchedType == RES_TYPE_SCB_START_ABILITY) {
41         return static_cast<int64_t>(AssociatedStartType::SCB_START_ABILITY);
42     } else if (resSchedType == RES_TYPE_EXTENSION_START_ABILITY) {
43         return static_cast<int64_t>(AssociatedStartType::EXTENSION_START_ABILITY);
44     } else if (resSchedType == RES_TYPE_MISSION_LIST_START_ABILITY) {
45         return static_cast<int64_t>(AssociatedStartType::MISSION_LIST_START_ABILITY);
46     }
47 #endif
48     TAG_LOGE(AAFwkTag::DEFAULT, "sched invalid");
49     return -1;
50 }
51 
ReportAbilityStartInfoToRSS(const AbilityInfo & abilityInfo,int32_t pid,bool isColdStart,int32_t warmStartType)52 void ResSchedUtil::ReportAbilityStartInfoToRSS(const AbilityInfo &abilityInfo, int32_t pid, bool isColdStart,
53     int32_t warmStartType)
54 {
55 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
56     uint32_t resType = ResourceSchedule::ResType::RES_TYPE_APP_ABILITY_START;
57     std::unordered_map<std::string, std::string> eventParams {
58         { "name", "ability_start" },
59         { "uid", std::to_string(abilityInfo.applicationInfo.uid) },
60         { "bundleName", abilityInfo.applicationInfo.bundleName },
61         { "abilityName", abilityInfo.name },
62         { "pid", std::to_string(pid) },
63         { "warmStartType", std::to_string(warmStartType) }
64     };
65     TAG_LOGD(AAFwkTag::DEFAULT, "call");
66     ResourceSchedule::ResSchedClient::GetInstance().ReportData(resType, isColdStart ? 1 : 0, eventParams);
67 #endif
68 }
69 
ReportAbilityAssociatedStartInfoToRSS(const AbilityInfo & abilityInfo,int64_t resSchedType,int32_t callerUid,int32_t callerPid)70 void ResSchedUtil::ReportAbilityAssociatedStartInfoToRSS(
71     const AbilityInfo &abilityInfo, int64_t resSchedType, int32_t callerUid, int32_t callerPid)
72 {
73 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
74     uint32_t resType = ResourceSchedule::ResType::RES_TYPE_APP_ASSOCIATED_START;
75     std::unordered_map<std::string, std::string> eventParams {
76         { "name", "associated_start" },
77         { "caller_uid", std::to_string(callerUid) },
78         { "caller_pid", std::to_string(callerPid) },
79         { "callee_uid", std::to_string(abilityInfo.applicationInfo.uid) },
80         { "callee_bundle_name", abilityInfo.applicationInfo.bundleName }
81     };
82     int64_t type = convertType(resSchedType);
83     TAG_LOGD(AAFwkTag::DEFAULT, "call");
84     ResourceSchedule::ResSchedClient::GetInstance().ReportData(resType, type, eventParams);
85 #endif
86 }
87 
ReportPreloadApplicationToRSS(const std::shared_ptr<AbilityInfo> & abilityInfo,int32_t preloadMode)88 void ResSchedUtil::ReportPreloadApplicationToRSS(const std::shared_ptr<AbilityInfo>& abilityInfo, int32_t preloadMode)
89 {
90 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
91     uint32_t resType = ResourceSchedule::ResType::RES_TYPE_PRELOAD_APPLICATION;
92     std::unordered_map<std::string, std::string> eventParams {
93         { "name", "preload_application" },
94         { "uid", std::to_string(abilityInfo->applicationInfo.uid) },
95         { "bundleName", abilityInfo->applicationInfo.bundleName },
96         { "preloadMode", std::to_string(preloadMode) }
97     };
98     TAG_LOGD(AAFwkTag::DEFAULT, "call");
99     ResourceSchedule::ResSchedClient::GetInstance().ReportData(resType, preloadMode, eventParams);
100 #endif
101 }
102 
GetThawReasonByAbilityType(const AbilityInfo & abilityInfo)103 std::string ResSchedUtil::GetThawReasonByAbilityType(const AbilityInfo &abilityInfo)
104 {
105     std::string reason;
106     if (abilityInfo.type == AppExecFwk::AbilityType::PAGE) {
107         reason = "THAW_BY_START_PAGE_ABILITY";
108     } else if (abilityInfo.type == AppExecFwk::AbilityType::EXTENSION &&
109                abilityInfo.extensionAbilityType == AppExecFwk::ExtensionAbilityType::SERVICE) {
110         reason = "THAW_BY_START_SERVICE_EXTENSION";
111     } else if (abilityInfo.type == AppExecFwk::AbilityType::EXTENSION &&
112                AAFwk::UIExtensionUtils::IsUIExtension(abilityInfo.extensionAbilityType)) {
113         reason = "THAW_BY_START_UI_EXTENSION";
114     } else {
115         reason = "THAW_BY_START_NOT_PAGE_ABILITY";
116     }
117     return reason;
118 }
119 
ReportAbilityIntentExemptionInfoToRSS(int32_t callerUid,int32_t callerPid)120 void ResSchedUtil::ReportAbilityIntentExemptionInfoToRSS(int32_t callerUid, int32_t callerPid)
121 {
122 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
123     std::unordered_map<std::string, std::string> eventParams {
124         {"uid", std::to_string(callerUid)},
125         {"duration", std::to_string(INTENT_EXEMPTION_DURATION)}
126     };
127 
128     TAG_LOGD(AAFwkTag::DEFAULT, "report Intent ExemptionInfo Uid:%{public}d", callerUid);
129     ResourceSchedule::ResSchedClient::GetInstance().ReportData(
130         ResourceSchedule::ResType::RES_TYPE_INTENT_CTRL_APP, 0, eventParams);
131 #endif
132 }
133 
NeedReportByPidWhenConnect(const AbilityInfo & abilityInfo)134 bool ResSchedUtil::NeedReportByPidWhenConnect(const AbilityInfo &abilityInfo)
135 {
136     return abilityInfo.extensionAbilityType == AppExecFwk::ExtensionAbilityType::REMOTE_NOTIFICATION ||
137            abilityInfo.extensionAbilityType == AppExecFwk::ExtensionAbilityType::BACKUP;
138 }
139 
ReportEventToRSS(const int32_t uid,const std::string & bundleName,const std::string & reason,const int32_t pid,const int32_t callerPid)140 void ResSchedUtil::ReportEventToRSS(const int32_t uid, const std::string &bundleName, const std::string &reason,
141     const int32_t pid, const int32_t callerPid)
142 {
143 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
144     uint32_t resType = ResourceSchedule::ResType::SYNC_RES_TYPE_THAW_ONE_APP;
145     nlohmann::json payload;
146     payload.emplace("uid", uid);
147     payload.emplace("pid", pid);
148     payload.emplace("bundleName", bundleName);
149     payload.emplace("reason", reason);
150     payload.emplace("callerPid", callerPid);
151     nlohmann::json reply;
152     TAG_LOGD(AAFwkTag::DEFAULT, "call");
153     ResourceSchedule::ResSchedClient::GetInstance().ReportSyncEvent(resType, 0, payload, reply);
154 #endif
155 }
156 
GetAllFrozenPidsFromRSS(std::unordered_set<int32_t> & frozenPids)157 void ResSchedUtil::GetAllFrozenPidsFromRSS(std::unordered_set<int32_t> &frozenPids)
158 {
159 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
160     uint32_t resType = ResourceSchedule::ResType::SYNC_RES_TYPE_GET_ALL_SUSPEND_STATE;
161     nlohmann::json payload;
162     nlohmann::json reply;
163     TAG_LOGD(AAFwkTag::DEFAULT, "call");
164     int32_t ret = ResourceSchedule::ResSchedClient::GetInstance().ReportSyncEvent(resType, 0, payload, reply);
165     if (ret != 0 || !reply.contains("allSuspendState") || !reply["allSuspendState"].is_array()) {
166         TAG_LOGE(AAFwkTag::DEFAULT, "ReportSyncEvent fail");
167         return;
168     }
169 
170     for (nlohmann::json &appObj : reply["allSuspendState"]) {
171         // Here can get uid if needed
172         if (!appObj.contains("pidsState") || !appObj["pidsState"].is_array()) {
173             continue;
174         }
175 
176         for (nlohmann::json &pidObj : appObj["pidsState"]) {
177             if (!pidObj.contains("pid") || !pidObj["pid"].is_number() ||
178                 !pidObj.contains("isFrozen") || !pidObj["isFrozen"].is_boolean()) {
179                 break;
180             }
181             int32_t pid = pidObj["pid"].get<int32_t>();
182             bool isFrozen = pidObj["isFrozen"].get<bool>();
183             if (isFrozen) {
184                 frozenPids.insert(pid);
185             }
186         }
187     }
188     if (frozenPids.empty()) {
189         TAG_LOGW(AAFwkTag::DEFAULT, "empty frozen pids");
190     }
191 #endif
192 }
193 
CheckShouldForceKillProcess(int32_t pid)194 bool ResSchedUtil::CheckShouldForceKillProcess(int32_t pid)
195 {
196 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
197     uint32_t resType = ResourceSchedule::ResType::SYNC_RES_TYPE_SHOULD_FORCE_KILL_PROCESS;
198     nlohmann::json payload;
199     nlohmann::json reply;
200     payload.emplace("pid", pid);
201     ResourceSchedule::ResSchedClient::GetInstance().ReportSyncEvent(resType, 0, payload, reply);
202     if (!reply.contains("ShouldForceKillProcess") || !reply["ShouldForceKillProcess"].is_number_integer()) {
203         return true;
204     }
205     return reply["ShouldForceKillProcess"].get<int32_t>() == 1;
206 #else
207     return true;
208 #endif
209 }
210 
ReportLoadingEventToRss(LoadingStage stage,int32_t pid,int32_t uid,int64_t timeDuration,int64_t abilityRecordId)211 void ResSchedUtil::ReportLoadingEventToRss(LoadingStage stage, int32_t pid, int32_t uid,
212     int64_t timeDuration, int64_t abilityRecordId)
213 {
214 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
215     uint32_t resType = ResourceSchedule::ResType::RES_TYPE_KEY_PERF_SCENE;
216     std::unordered_map<std::string, std::string> eventParams {
217         { "extType", "10015"},
218         { "pid", std::to_string(pid) },
219         { "uid", std::to_string(uid) },
220     };
221     if (timeDuration > 0) { // millisecond
222         eventParams.emplace("timeoutDuration", std::to_string(timeDuration));
223     }
224     if (abilityRecordId > 0) {
225         eventParams.emplace("abilityRecordId", std::to_string(abilityRecordId));
226     }
227 
228     int64_t type = static_cast<int64_t>(stage);
229     TAG_LOGD(AAFwkTag::DEFAULT, "call");
230     ResourceSchedule::ResSchedClient::GetInstance().ReportData(resType, type, eventParams);
231 #endif
232 }
233 
GetNWebPreloadSet() const234 std::unordered_set<std::string> ResSchedUtil::GetNWebPreloadSet() const
235 {
236     uint32_t resType = ResourceSchedule::ResType::SYNC_RES_TYPE_GET_NWEB_PRELOAD_SET;
237     nlohmann::json payload;
238     nlohmann::json reply;
239     ResourceSchedule::ResSchedClient::GetInstance().ReportSyncEvent(resType, 0, payload, reply);
240     if (!reply.contains("NWebPreloadSet")) {
241         TAG_LOGW(AAFwkTag::DEFAULT, "does not get preload process set");
242         return {};
243     }
244     auto jsonObj = reply["NWebPreloadSet"];
245     return { jsonObj.begin(), jsonObj.end() };
246 }
247 } // namespace AAFwk
248 } // namespace OHOS
249