1 /*
2 * Copyright (c) 2022-2025 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_mgr.h"
17
18 #include <cinttypes>
19 #include <map>
20
21 #include "app_mgr_interface.h"
22 #include "if_system_ability_manager.h"
23 #include "iservice_registry.h"
24 #include "notifier_mgr.h"
25 #include "res_exe_type.h"
26 #include "res_sa_init.h"
27 #include "res_sched_exe_client.h"
28 #include "res_sched_log.h"
29 #include "res_type.h"
30 #include "plugin_mgr.h"
31 #include "hitrace_meter.h"
32 #include "scene_recognizer_mgr.h"
33 #include "system_ability_definition.h"
34
35 namespace OHOS {
36 namespace ResourceSchedule {
37 using namespace AppExecFwk;
38 namespace {
39 static const std::map<uint32_t, std::string> resTypeToStr = {
40 { ResType::SYNC_RES_TYPE_THAW_ONE_APP, "SYNC_RES_TYPE_THAW_ONE_APP"},
41 { ResType::SYNC_RES_TYPE_GET_ALL_SUSPEND_STATE, "SYNC_RES_TYPE_GET_ALL_SUSPEND_STATE"},
42 { ResType::SYNC_RES_TYPE_GET_THERMAL_DATA, "SYNC_RES_TYPE_GET_THERMAL_DATA"},
43 { ResType::RES_TYPE_SCREEN_STATUS, "SCREEN_STATUS" },
44 { ResType::RES_TYPE_APP_STATE_CHANGE, "APP_STATE_CHANGE" },
45 { ResType::RES_TYPE_ABILITY_STATE_CHANGE, "ABILITY_STATE_CHANGE" },
46 { ResType::RES_TYPE_EXTENSION_STATE_CHANGE, "EXTENSION_STATE_CHANGE" },
47 { ResType::RES_TYPE_PROCESS_STATE_CHANGE, "PROCESS_STATE_CHANGE" },
48 { ResType::RES_TYPE_WINDOW_FOCUS, "WINDOW_FOCUS" },
49 { ResType::RES_TYPE_TRANSIENT_TASK, "TRANSIENT_TASK" },
50 { ResType::RES_TYPE_CONTINUOUS_TASK, "CONTINUOUS_TASK" },
51 { ResType::RES_TYPE_CGROUP_ADJUSTER, "CGROUP_ADJUSTER" },
52 { ResType::RES_TYPE_CLICK_RECOGNIZE, "CLICK_RECOGNIZE" },
53 { ResType::RES_TYPE_PUSH_PAGE, "PUSH_PAGE" },
54 { ResType::RES_TYPE_SLIDE_RECOGNIZE, "SLIDE_RECOGNIZE" },
55 { ResType::RES_TYPE_WINDOW_VISIBILITY_CHANGE, "WINDOW_VISIBILITY_CHANGE" },
56 { ResType::RES_TYPE_REPORT_MMI_PROCESS, "REPORT_MMI_PROCESS" },
57 { ResType::RES_TYPE_REPORT_RENDER_THREAD, "REPORT_RENDER_THREAD" },
58 { ResType::RES_TYPE_APP_INSTALL_UNINSTALL, "APP_INSTALL_UNINSTALL" },
59 { ResType::RES_TYPE_WIFI_CONNECT_STATE_CHANGE, "WIFI_CONNECT_STATE_CHANGE" },
60 { ResType::RES_TYPE_USER_SWITCH, "USER_SWITCH" },
61 { ResType::RES_TYPE_USER_REMOVE, "USER_REMOVE" },
62 { ResType::RES_TYPE_SCREEN_LOCK, "SCREEN_LOCK" },
63 { ResType::RES_TYPE_BLUETOOTH_A2DP_CONNECT_STATE_CHANGE, "BLUETOOTH_A2DP_CONNECT_STATE_CHANGE" },
64 { ResType::RES_TYPE_NETWORK_LATENCY_REQUEST, "NETWORK_LATENCY_REQUEST" },
65 { ResType::RES_TYPE_CALL_STATE_UPDATE, "CALL_STATE_UPDATE" },
66 { ResType::RES_TYPE_THREAD_QOS_CHANGE, "THREAD_QOS_CHANGE" },
67 { ResType::RES_TYPE_AUDIO_RENDER_STATE_CHANGE, "AUDIO_RENDER_STATE_CHANGE" },
68 { ResType::RES_TYPE_AUDIO_RING_MODE_CHANGE, "AUDIO_RING_MODE_CHANGE" },
69 { ResType::RES_TYPE_AUDIO_VOLUME_KEY_CHANGE, "AUDIO_VOLUME_KEY_CHANGE" },
70 { ResType::RES_TYPE_APP_ABILITY_START, "APP_ABILITY_START" },
71 { ResType::RES_TYPE_PRELOAD_APPLICATION, "RES_TYPE_PRELOAD_APPLICATION" },
72 { ResType::RES_TYPE_POP_PAGE, "POP_PAGE" },
73 { ResType::RES_TYPE_WEB_GESTURE, "WEB_GESTURE" },
74 { ResType::RES_TYPE_DEVICE_STILL_STATE_CHANGE, "DEVICE_STILL_STATE_CHANGE" },
75 { ResType::RES_TYPE_RESIZE_WINDOW, "RES_TYPE_RESIZE_WINDOW" },
76 { ResType::RES_TYPE_MOVE_WINDOW, "RES_TYPE_MOVE_WINDOW" },
77 { ResType::RES_TYPE_SHOW_REMOTE_ANIMATION, "RES_TYPE_SHOW_REMOTE_ANIMATION" },
78 { ResType::RES_TYPE_LOAD_PAGE, "RES_TYPE_LOAD_PAGE" },
79 { ResType::RES_TYPE_REPORT_CAMERA_STATE, "RES_TYPE_REPORT_CAMERA_STATE" },
80 { ResType::RES_TYPE_RUNNINGLOCK_STATE, "RES_TYPE_RUNNINGLOCK_STATE" },
81 { ResType::RES_TYPE_DRAG_STATUS_BAR, "RES_TYPE_DRAG_STATUS_BAR" },
82 { ResType::RES_TYPE_REPORT_SCENE_BOARD, "RES_TYPE_REPORT_SCENE_BOARD" },
83 { ResType::RES_TYPE_REPORT_KEY_THREAD, "RES_TYPE_REPORT_KEY_THREAD" },
84 { ResType::RES_TYPE_REPORT_WINDOW_STATE, "RES_TYPE_REPORT_WINDOW_STATE" },
85 { ResType::RES_TYPE_REPORT_SCENE_SCHED, "RES_TYPE_REPORT_SCENE_SCHED" },
86 { ResType::RES_TYPE_WEB_GESTURE_MOVE, "RES_TYPE_WEB_GESTURE_MOVE" },
87 { ResType::RES_TYPE_WEB_SLIDE_NORMAL, "RES_TYPE_WEB_SLIDE_NORMAL" },
88 { ResType::RES_TYPE_LOAD_URL, "RES_TYPE_LOAD_URL" },
89 { ResType::RES_TYPE_MOUSEWHEEL, "RES_TYPE_MOUSEWHEEL" },
90 { ResType::RES_TYPE_WEBVIEW_AUDIO_STATUS_CHANGE, "RES_TYPE_WEBVIEW_AUDIO_STATUS_CHANGE" },
91 { ResType::RES_TYPE_MMI_INPUT_STATE, "RES_TYPE_MMI_INPUT_STATE" },
92 { ResType::RES_TYPE_ANCO_CUST, "RES_TYPE_ANCO_CUST" },
93 { ResType::RES_TYPE_TIMEZONE_CHANGED, "RES_TYPE_TIMEZONE_CHANGED" },
94 { ResType::RES_TYPE_TIME_CHANGED, "RES_TYPE_TIME_CHANGED" },
95 { ResType::RES_TYPE_NITZ_TIME_CHANGED, "RES_TYPE_NITZ_TIME_CHANGED" },
96 { ResType::RES_TYPE_NITZ_TIMEZONE_CHANGED, "RES_TYPE_NITZ_TIMEZONE_CHANGED" },
97 { ResType::RES_TYPE_CHARGING_DISCHARGING, "RES_TYPE_CHARGING_DISCHARGING" },
98 { ResType::RES_TYPE_USB_DEVICE, "RES_TYPE_USB_DEVICE" },
99 { ResType::RES_TYPE_CALL_STATE_CHANGED, "RES_TYPE_CALL_STATE_CHANGED" },
100 { ResType::RES_TYPE_WIFI_P2P_STATE_CHANGED, "RES_TYPE_WIFI_P2P_STATE_CHANGED" },
101 { ResType::RES_TYPE_CONNECTION_OBSERVER, "RES_TYPE_CONNECTION_OBSERVER" },
102 { ResType::RES_TYPE_AV_SESSION_ON_SESSION_CREATE, "RES_TYPE_AV_SESSION_ON_SESSION_CREATE" },
103 { ResType::RES_TYPE_AV_SESSION_ON_SESSION_RELEASE, "RES_TYPE_AV_SESSION_ON_SESSION_RELEASE" },
104 { ResType::RES_TYPE_AV_SESSION_ON_TOP_SESSION_CHANGE, "RES_TYPE_AV_SESSION_ON_TOP_SESSION_CHANGE" },
105 { ResType::RES_TYPE_ON_APP_STATE_CHANGED, "RES_TYPE_ON_APP_STATE_CHANGED" },
106 { ResType::RES_TYPE_PROCESS_PRE_FOREGROUND_CHANGE, "RES_TYPE_PROCESS_PRE_FOREGROUND_CHANGE" },
107 { ResType::RES_TYPE_EFFICIENCY_RESOURCES_STATE_CHANGED, "RES_TYPE_EFFICIENCY_RESOURCES_STATE_CHANGED" },
108 { ResType::RES_TYPE_AV_CODEC_STATE, "RES_TYPE_AV_CODEC_STATE" },
109 { ResType::RES_TYPE_APP_ASSOCIATED_START, "RES_TYPE_APP_ASSOCIATED_START" },
110 { ResType::RES_TYPE_THERMAL_STATE, "RES_TYPE_THERMAL_STATE"},
111 { ResType::RES_TYPE_SOCPERF_CUST_EVENT_BEGIN, "RES_TYPE_SOCPERF_CUST_EVENT_BEGIN" },
112 { ResType::RES_TYPE_SOCPERF_CUST_EVENT_END, "RES_TYPE_SOCPERF_CUST_EVENT_END" },
113 { ResType::RES_TYPE_WINDOW_DRAWING_CONTENT_CHANGE, "RES_TYPE_WINDOW_DRAWING_CONTENT_CHANGE" },
114 { ResType::RES_TYPE_REPORT_SCREEN_CAPTURE, "RES_TYPE_REPORT_SCREEN_CAPTURE" },
115 { ResType::RES_TYPE_LONG_FRAME, "RES_TYPE_LONG_FRAME" },
116 { ResType::RES_TYPE_KEY_PERF_SCENE, "RES_TYPE_KEY_PERF_SCENE" },
117 { ResType::RES_TYPE_SUPER_LAUNCHER, "RES_TYPE_SUPER_LAUNCHER" },
118 { ResType::RES_TYPE_CAST_SCREEN, "RES_TYPE_CAST_SCREEN" },
119 { ResType::RES_TYPR_SCREEN_COLLABROATION, "RES_TYPR_SCREEN_COLLABROATION" },
120 { ResType::RES_TYPE_SA_CONTROL_APP_EVENT, "RES_TYPE_SA_CONTROL_APP_EVENT" },
121 { ResType::RES_TYPE_SYSTEM_CPU_LOAD, "RES_TYPE_SYSTEM_CPU_LOAD" },
122 { ResType::RES_TYPE_UPLOAD_DOWNLOAD, "RES_TYPE_UPLOAD_DOWNLOAD" },
123 { ResType::RES_TYPE_SPLIT_SCREEN, "RES_TYPE_SPLIT_SCREEN" },
124 { ResType::RES_TYPE_FLOATING_WINDOW, "RES_TYPE_FLOATING_WINDOW" },
125 { ResType::RES_TYPE_REPORT_DISTRIBUTE_TID, "RES_TYPE_REPORT_DISTRIBUTE_TID"},
126 { ResType::RES_TYPE_FRAME_RATE_REPORT, "RES_TYPE_FRAME_RATE_REPORT"},
127 { ResType::RES_TYPE_WEB_DRAG_RESIZE, "RES_TYPE_WEB_DRAG_RESIZE"},
128 { ResType::RES_TYPE_WEBVIEW_SCREEN_CAPTURE, "RES_TYPE_WEBVIEW_SCREEN_CAPTURE"},
129 { ResType::RES_TYPE_WEBVIEW_VIDEO_STATUS_CHANGE, "RES_TYPE_WEBVIEW_VIDEO_STATUS_CHANGE"},
130 { ResType::RES_TYPE_LOCATION_STATUS_CHANGE, "RES_TYPE_LOCATION_STATUS_CHANGE"},
131 { ResType::RES_TYPE_DEVICE_MODE_STATUS, "RES_TYPE_DEVICE_MODE_STATUS"},
132 { ResType::RES_TYPE_REPORT_DISTRIBUTE_COMPONENT_CHANGE, "RES_TYPE_REPORT_DISTRIBUTE_COMPONENT_CHANGE"},
133 { ResType::RES_TYPE_FORM_STATE_CHANGE_EVENT, "RES_TYPE_FORM_STATE_CHANGE_EVENT"},
134 { ResType::RES_TYPE_POWER_MODE_CHANGED, "RES_TYPE_POWER_MODE_CHANGED" },
135 { ResType::RES_TYPE_THERMAL_SCENARIO_REPORT, "RES_TYPE_THERMAL_SCENARIO_REPORT" },
136 { ResType::SYNC_RES_TYPE_SHOULD_FORCE_KILL_PROCESS, "SYNC_RES_TYPE_SHOULD_FORCE_KILL_PROCESS" },
137 { ResType::RES_TYPE_BOOT_COMPLETED, "RES_TYPE_BOOT_COMPLETED" },
138 { ResType::RES_TYPE_CONTINUOUS_STARTUP, "RES_TYPE_CONTINUOUS_STARTUP" },
139 { ResType::RES_TYPE_AUDIO_RENDERER_SILENT_PLAYBACK, "RES_TYPE_AUDIO_RENDERER_SILENT_PLAYBACK"},
140 { ResType::RES_TYPE_REPORT_GAME_SCHED, "RES_TYPE_REPORT_GAME_SCHED" },
141 { ResType::RES_TYPE_REPORT_VSYNC_TID, "RES_TYPE_REPORT_VSYNC_TID" },
142 { ResType::RES_TYPE_BT_SERVICE_EVENT, "RES_TYPE_BT_SERVICE_EVENT"},
143 { ResType::RES_TYPE_APP_FRAME_DROP, "RES_TYPE_APP_FRAME_DROP"},
144 { ResType::RES_TYPE_CLOUD_CONFIG_UPDATE, "RES_TYPE_CLOUD_CONFIG_UPDATE" },
145 { ResType::RES_TYPE_RSS_CLOUD_CONFIG_UPDATE, "RES_TYPE_RSS_CLOUD_CONFIG_UPDATE" },
146 { ResType::RES_TYPE_GAME_INFO_NOTIFY, "RES_TYPE_GAME_INFO_NOTIFY"},
147 { ResType::RES_TYPE_AUDIO_RENDERER_STANDBY, "RES_TYPE_AUDIO_RENDERER_STANDBY" },
148 { ResType::RES_TYPE_APP_GAME_BOOST_EVENT, "RES_TYPE_APP_GAME_BOOST_EVENT" },
149 { ResType::RES_TYPE_GESTURE_ANIMATION, "RES_TYPE_GESTURE_ANIMATION" },
150 { ResType::RES_TYPE_AXIS_EVENT, "RES_TYPE_AXIS_EVENT" },
151 { ResType::RES_TYPE_DISPLAY_MULTI_SCREEN, "RES_TYPE_DISPLAY_MULTI_SCREEN" },
152 { ResType::RES_TYPE_INTENT_CTRL_APP, "RES_TYPE_INTENT_CTRL_APP" },
153 { ResType::RES_TYPE_CROWN_ROTATION_STATUS, "RES_TYPE_CROWN_ROTATION_STATUS" },
154 { ResType::RES_TYPE_RAISE_WORKER_THREAD_PRIORITY, "RES_TYPE_RAISE_WORKER_THREAD_PRIORITY" },
155 { ResType::RES_TYPE_GET_GAME_SCENE_INFO, "RES_TYPE_GET_GAME_SCENE_INFO"},
156 { ResType::RES_TYPE_RED_ENVELOPE, "RES_TYPE_RED_ENVELOPE"},
157 { ResType::RES_TYPE_RECV_ABC_LOAD_COMPLETED, "RES_TYPE_RECV_ABC_LOAD_COMPLETED"},
158 { ResType::RES_TYPE_CHECK_APP_IS_IN_SCHEDULE_LIST, "RES_TYPE_CHECK_APP_IS_IN_SCHEDULE_LIST" },
159 { ResType::RES_TYPE_DYNAMICALLY_SET_SUSPEND_EXEMPT, "RES_TYPE_DYNAMICALLY_SET_SUSPEND_EXEMPT" },
160 { ResType::SYNC_RES_TYPE_GET_SUSPEND_STATE_BY_UID, "SYNC_RES_TYPE_GET_SUSPEND_STATE_BY_UID" },
161 { ResType::SYNC_RES_TYPE_GET_SUSPEND_STATE_BY_PID, "SYNC_RES_TYPE_GET_SUSPEND_STATE_BY_PID" },
162 { ResType::RES_TYPE_CAMERA_RESET_PRIORITY, "RES_TYPE_CAMERA_RESET_PRIORITY"},
163 #ifdef RESSCHED_RESOURCESCHEDULE_FILE_COPY_SOC_PERF_ENABLE
164 { ResType::RES_TYPE_FILE_COPY_STATUS, "RES_TYPE_FILE_COPY_STATUS" },
165 #endif
166 { ResType::RES_TYPE_PAGE_TRANSITION, "RES_TYPE_PAGE_TRANSITION" },
167 { ResType::RES_TYPE_PARAM_UPADTE_EVENT, "RES_TYPE_PARAM_UPADTE_EVENT" },
168 { ResType::RES_TYPE_WEB_SLIDE_SCROLL, "RES_TYPE_WEB_SLIDE_SCROLL" },
169 { ResType::RES_TYPE_OVERLAY_EVENT, "RES_TYPE_OVERLAY_EVENT" },
170 { ResType::RES_TYPE_WIFI_POWER_STATE_CHANGE, "RES_TYPE_WIFI_POWER_STATE_CHANGE" },
171 { ResType::RES_TYPE_APP_OPT_FROM_RECENT, "RES_TYPE_APP_OPT_FROM_RECENT" },
172 { ResType::RES_TYPE_SWIPE_DIRECTION_UP, "RES_TYPE_SWIPE_DIRECTION_UP" },
173 { ResType::RES_TYPE_WINDOW_PANEL, "RES_TYPE_WINDOW_PANEL" },
174 { ResType::RES_TYPE_WINDOW_SPLIT_SCREEN, "RES_TYPE_WINDOW_SPLIT_SCREEN" },
175 { ResType::RES_TYPE_SUPPLY_LEVEL, "RES_TYPE_SUPPLY_LEVEL" },
176 { ResType::RES_TYPE_GC_THREAD_QOS_STATUS_CHANGE, "RES_TYPE_GC_THREAD_QOS_STATUS_CHANGE"},
177 { ResType::RES_TYPE_DEVICE_IDLE_CHANGED, "RES_TYPE_DEVICE_IDLE_CHANGED"},
178 { ResType::RES_TYPE_USER_SLEEP_STATE_CHANGED, "RES_TYPE_USER_SLEEP_STATE_CHANGED"},
179 { ResType::RES_TYPE_ADJUST_PROTECTLRU_RECLAIM_RATIO, "RES_TYPE_ADJUST_PROTECTLRU_RECLAIM_RATIO"},
180 };
GetAppManagerInstance()181 OHOS::sptr<OHOS::AppExecFwk::IAppMgr> GetAppManagerInstance()
182 {
183 OHOS::sptr<OHOS::ISystemAbilityManager> systemAbilityManager =
184 OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
185 if (!systemAbilityManager) {
186 RESSCHED_LOGE("%{public}s : systemAbilityManager nullptr!", __func__);
187 return nullptr;
188 }
189 OHOS::sptr<OHOS::IRemoteObject> object = systemAbilityManager->GetSystemAbility(OHOS::APP_MGR_SERVICE_ID);
190 return OHOS::iface_cast<OHOS::AppExecFwk::IAppMgr>(object);
191 }
192 }
193
194 IMPLEMENT_SINGLE_INSTANCE(ResSchedMgr);
195
Init()196 void ResSchedMgr::Init()
197 {
198 PluginMgr::GetInstance().Init();
199 PluginMgr::GetInstance().SetResTypeStrMap(resTypeToStr);
200
201 if (!killProcess_) {
202 killProcess_ = std::make_shared<KillProcess>();
203 }
204
205 InitExecutorPlugin(true);
206 }
207
Stop()208 void ResSchedMgr::Stop()
209 {
210 PluginMgr::GetInstance().Stop();
211 }
212
ReportData(uint32_t resType,int64_t value,const nlohmann::json & payload)213 void ResSchedMgr::ReportData(uint32_t resType, int64_t value, const nlohmann::json& payload)
214 {
215 ReportDataInner(resType, value, payload);
216 }
217
ReportDataInner(uint32_t resType,int64_t value,const nlohmann::json & payload)218 void ResSchedMgr::ReportDataInner(uint32_t resType, int64_t value, const nlohmann::json& payload)
219 {
220 RESSCHED_LOGD("%{public}s, receive resType = %{public}u, value = %{public}lld.", __func__,
221 resType, (long long)value);
222 std::string trace_str(__func__);
223 trace_str.append(",resType[").append(std::to_string(resType)).append("]");
224 trace_str.append(",value[").append(std::to_string(value)).append("]");
225 StartTrace(HITRACE_TAG_OHOS, trace_str, -1);
226 auto resData = std::make_shared<ResData>(resType, value, payload);
227 PluginMgr::GetInstance().DispatchResource(resData);
228 SceneRecognizerMgr::GetInstance().DispatchResource(resData);
229 FinishTrace(HITRACE_TAG_OHOS);
230 }
231
KillProcessByClient(const nlohmann::json & payload)232 int32_t ResSchedMgr::KillProcessByClient(const nlohmann::json& payload)
233 {
234 return killProcess_->KillProcessByPidWithClient(payload);
235 }
236
InitExecutorPlugin(bool isProcessInit)237 void ResSchedMgr::InitExecutorPlugin(bool isProcessInit)
238 {
239 std::vector<std::string> configStrs = PluginMgr::GetInstance().GetConfigReaderStr();
240 std::vector<std::string> switchStrs = PluginMgr::GetInstance().GetPluginSwitchStr();
241
242 nlohmann::json payload;
243 nlohmann::json context;
244 payload["config"] = configStrs;
245 payload["switch"] = switchStrs;
246 RESSCHED_LOGI("ResSchedMgr start InitExecutorPlugin");
247 ResSchedExeClient::GetInstance().SendRequestSync(ResExeType::RES_TYPE_EXECUTOR_PLUGIN_INIT, 0, payload, context);
248
249 if (isProcessInit) {
250 PluginMgr::GetInstance().ParseConfigReader(configStrs);
251 PluginMgr::GetInstance().ParsePluginSwitch(switchStrs);
252 }
253 ResSchedIpcThread::GetInstance().SetInitFlag(true);
254 }
255
OnApplicationStateChange(int32_t state,int32_t pid)256 void ResSchedMgr::OnApplicationStateChange(int32_t state, int32_t pid)
257 {
258 RESSCHED_LOGD("OnApplicationStateChange called, state: %{public}d, pid : %{public}d .", state, pid);
259 std::lock_guard<std::mutex> autoLock(foregroundPidsMutex_);
260
261 if (state == static_cast<int32_t>(ApplicationState::APP_STATE_FOREGROUND)) {
262 if (foregroundPids_.find(pid) == foregroundPids_.end()) {
263 foregroundPids_.emplace(pid);
264 }
265 }
266 if (state == static_cast<int32_t>(ApplicationState::APP_STATE_BACKGROUND)
267 || state == static_cast<int32_t>(ApplicationState::APP_STATE_TERMINATED)
268 || state == static_cast<int32_t>(ApplicationState::APP_STATE_END)) {
269 auto item = foregroundPids_.find(pid);
270 if (item != foregroundPids_.end()) {
271 foregroundPids_.erase(item);
272 }
273 }
274 }
275
IsForegroundApp(int32_t pid)276 bool ResSchedMgr::IsForegroundApp(int32_t pid)
277 {
278 std::lock_guard<std::mutex> autoLock(foregroundPidsMutex_);
279 auto item = foregroundPids_.find(pid);
280 return item != foregroundPids_.end();
281 }
282
InitForegroundAppInfo()283 void ResSchedMgr::InitForegroundAppInfo()
284 {
285 sptr<OHOS::AppExecFwk::IAppMgr> appManager = GetAppManagerInstance();
286 if (!appManager) {
287 RESSCHED_LOGE("%{public}s app manager nullptr!", __func__);
288 return;
289 }
290 std::vector<AppExecFwk::AppStateData> fgapplist;
291 int32_t err = appManager->GetForegroundApplications(fgapplist);
292 if (err != ERR_OK) {
293 RESSCHED_LOGE("%{public}s GetForegroundApplications failed. err:%{public}d", __func__, err);
294 return;
295 }
296 std::lock_guard<std::mutex> autoLock(foregroundPidsMutex_);
297 for (const auto& item : fgapplist) {
298 if (foregroundPids_.find(item.pid) == foregroundPids_.end()) {
299 foregroundPids_.emplace(item.pid);
300 }
301 }
302 RESSCHED_LOGI("%{public}s succeed", __func__);
303 }
304
ReportAppStateInProcess(int32_t state,int32_t pid)305 void ResSchedMgr::ReportAppStateInProcess(int32_t state, int32_t pid)
306 {
307 NotifierMgr::GetInstance().OnApplicationStateChange(state, pid);
308 ResSchedMgr::GetInstance().OnApplicationStateChange(state, pid);
309 }
310
ReportProcessStateInProcess(int32_t state,int32_t pid)311 void ResSchedMgr::ReportProcessStateInProcess(int32_t state, int32_t pid)
312 {
313 ResSchedMgr::GetInstance().OnApplicationStateChange(state, pid);
314 }
315
GetAllowSCBReportResExt()316 std::unordered_set<uint32_t>& ResSchedMgr::GetAllowSCBReportResExt()
317 {
318 return allowSCBReportResExt_;
319 }
320
GetAllowAllSAReportResExt()321 std::unordered_set<uint32_t>& ResSchedMgr::GetAllowAllSAReportResExt()
322 {
323 return allowAllSAReportResExt_;
324 }
325
GetAllowSomeSAReportResExt()326 std::unordered_map<uint32_t, std::unordered_set<int32_t>>& ResSchedMgr::GetAllowSomeSAReportResExt()
327 {
328 return allowSomeSAReportResExt_;
329 }
330
GetAllowAllAppReportResExt()331 std::unordered_set<uint32_t>& ResSchedMgr::GetAllowAllAppReportResExt()
332 {
333 return allowAllAppReportResExt_;
334 }
335
GetAllowFgAppReportResExt()336 std::unordered_set<uint32_t>& ResSchedMgr::GetAllowFgAppReportResExt()
337 {
338 return allowFgAppReportResExt_;
339 }
340
SetAllowSCBReportResExt(const std::unordered_set<uint32_t> & allowSCBReportResExt)341 void ResSchedMgr::SetAllowSCBReportResExt(const std::unordered_set<uint32_t>& allowSCBReportResExt)
342 {
343 allowSCBReportResExt_ = allowSCBReportResExt;
344 }
345
346
SetAllowAllSAReportResExt(const std::unordered_set<uint32_t> & allowAllSAReportResExt)347 void ResSchedMgr::SetAllowAllSAReportResExt(const std::unordered_set<uint32_t>& allowAllSAReportResExt)
348 {
349 allowAllSAReportResExt_ = allowAllSAReportResExt;
350 }
351
352
SetAllowSomeSAReportResExt(const std::unordered_map<uint32_t,std::unordered_set<int32_t>> & allowSomeSAReportResExt)353 void ResSchedMgr::SetAllowSomeSAReportResExt(const std::unordered_map<uint32_t, std::unordered_set<int32_t>>&
354 allowSomeSAReportResExt)
355 {
356 allowSomeSAReportResExt_ = allowSomeSAReportResExt;
357 }
358
SetAllowAllAppReportResExt(const std::unordered_set<uint32_t> & allowAllAppReportResExt)359 void ResSchedMgr::SetAllowAllAppReportResExt(const std::unordered_set<uint32_t>& allowAllAppReportResExt)
360 {
361 allowAllAppReportResExt_ = allowAllAppReportResExt;
362 }
363
SetAllowFgAppReportResExt(const std::unordered_set<uint32_t> & allowFgAppReportResExt)364 void ResSchedMgr::SetAllowFgAppReportResExt(const std::unordered_set<uint32_t>& allowFgAppReportResExt)
365 {
366 allowFgAppReportResExt_ = allowFgAppReportResExt;
367 }
368
SetAllowSCBReportResExt(const std::unordered_set<uint32_t> & allowSCBReportResExt)369 extern "C" void SetAllowSCBReportResExt(const std::unordered_set<uint32_t>& allowSCBReportResExt)
370 {
371 ResSchedMgr::GetInstance().SetAllowSCBReportResExt(allowSCBReportResExt);
372 }
373
SetAllowAllSAReportResExt(const std::unordered_set<uint32_t> & allowAllSAReportResExt)374 extern "C" void SetAllowAllSAReportResExt(const std::unordered_set<uint32_t>& allowAllSAReportResExt)
375 {
376 ResSchedMgr::GetInstance().SetAllowAllSAReportResExt(allowAllSAReportResExt);
377 }
378
SetAllowSomeSAReportResExt(const std::unordered_map<uint32_t,std::unordered_set<int32_t>> & allowSomeSAReportResExt)379 extern "C" void SetAllowSomeSAReportResExt(const std::unordered_map<uint32_t, std::unordered_set<int32_t>>&
380 allowSomeSAReportResExt)
381 {
382 ResSchedMgr::GetInstance().SetAllowSomeSAReportResExt(allowSomeSAReportResExt);
383 }
384
SetAllowAllAppReportResExt(const std::unordered_set<uint32_t> & allowAllAppReportResExt)385 extern "C" void SetAllowAllAppReportResExt(const std::unordered_set<uint32_t>& allowAllAppReportResExt)
386 {
387 ResSchedMgr::GetInstance().SetAllowAllAppReportResExt(allowAllAppReportResExt);
388 }
389
SetAllowFgAppReportResExt(const std::unordered_set<uint32_t> & allowFgAppReportResExt)390 extern "C" void SetAllowFgAppReportResExt(const std::unordered_set<uint32_t>& allowFgAppReportResExt)
391 {
392 ResSchedMgr::GetInstance().SetAllowFgAppReportResExt(allowFgAppReportResExt);
393 }
394
ReportDataInProcess(uint32_t resType,int64_t value,const nlohmann::json & payload)395 extern "C" void ReportDataInProcess(uint32_t resType, int64_t value, const nlohmann::json& payload)
396 {
397 ResSchedMgr::GetInstance().ReportData(resType, value, payload);
398 }
399
KillProcessInProcess(const nlohmann::json & payload)400 extern "C" int32_t KillProcessInProcess(const nlohmann::json& payload)
401 {
402 return ResSchedMgr::GetInstance().KillProcessByClient(payload);
403 }
404 } // namespace ResourceSchedule
405 } // namespace OHOS
406