• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include "res_sched_service_ability.h"
18 
19 #include "res_common_util.h"
20 #include "ffrt_inner.h"
21 #include "hisysevent.h"
22 #include "notifier_mgr.h"
23 #include "observer_manager_intf.h"
24 #include "res_sched_log.h"
25 #include "res_sched_mgr.h"
26 #include "res_sched_service.h"
27 #include "event_controller_intf.h"
28 #include "event_listener_mgr.h"
29 #include "system_ability_definition.h"
30 
31 #define GAME_SERVICE_SERVICE_ID 66058
32 
33 namespace OHOS {
34 namespace ResourceSchedule {
35 const bool REGISTER_RESULT =
36     SystemAbility::MakeAndRegisterAbility(DelayedSingleton<ResSchedServiceAbility>::GetInstance().get());
37 
ResSchedServiceAbility()38 ResSchedServiceAbility::ResSchedServiceAbility() : SystemAbility(RES_SCHED_SYS_ABILITY_ID, true)
39 {
40 }
41 
~ResSchedServiceAbility()42 ResSchedServiceAbility::~ResSchedServiceAbility()
43 {
44 }
45 
OnStart()46 void ResSchedServiceAbility::OnStart()
47 {
48     ResSchedMgr::GetInstance().Init();
49     NotifierMgr::GetInstance().Init();
50     EventListenerMgr::GetInstance().Init();
51     if (!service_) {
52         service_ = new (std::nothrow) ResSchedService();
53     }
54     if (service_ == nullptr) {
55         RESSCHED_LOGE("ResSchedServiceAbility:: New ResSchedService failed.");
56         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
57                         "COMPONENT_NAME", "MAIN",
58                         "ERR_TYPE", "others",
59                         "ERR_MSG", "New ResSchedService object failed!");
60     }
61     if (!Publish(service_)) {
62         RESSCHED_LOGE("ResSchedServiceAbility:: Register service failed.");
63         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
64                         "COMPONENT_NAME", "MAIN",
65                         "ERR_TYPE", "register failure",
66                         "ERR_MSG", "Register ResSchedService failed.");
67     }
68     SystemAbilityListenerInit();
69     EventControllerInit();
70     ObserverManagerInit();
71     ReclaimProcessMemory();
72     RESSCHED_LOGI("ResSchedServiceAbility ::OnStart.");
73 }
74 
OnStop()75 void ResSchedServiceAbility::OnStop()
76 {
77     ObserverManagerDisable();
78     EventControllerStop();
79     RemoveSystemAbilityListener(BACKGROUND_TASK_MANAGER_SERVICE_ID);
80     RemoveSystemAbilityListener(WINDOW_MANAGER_SERVICE_ID);
81     RemoveSystemAbilityListener(APP_MGR_SERVICE_ID);
82     RemoveSystemAbilityListener(POWER_MANAGER_SERVICE_ID);
83     ResSchedMgr::GetInstance().Stop();
84     RESSCHED_LOGI("ResSchedServiceAbility::OnStop!");
85 }
86 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)87 void ResSchedServiceAbility::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
88 {
89     if (systemAbilityId == RES_SCHED_EXE_ABILITY_ID) {
90         ResSchedMgr::GetInstance().InitExecutorPlugin();
91     } else if (systemAbilityId == APP_MGR_SERVICE_ID) {
92         ResSchedMgr::GetInstance().InitForegroundAppInfo();
93     }
94     nlohmann::json payload;
95     payload["saId"] = systemAbilityId;
96     payload["deviceId"] = deviceId;
97     ResSchedMgr::GetInstance().ReportData(ResType::RES_TYPE_SYSTEM_ABILITY_STATUS_CHANGE,
98         ResType::SystemAbilityStatus::SA_ADD, payload);
99     RESSCHED_LOGI("OnAddSystemAbility systemAbilityId:%{public}d", systemAbilityId);
100 }
101 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)102 void ResSchedServiceAbility::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
103 {
104     nlohmann::json payload;
105     payload["saId"] = systemAbilityId;
106     payload["deviceId"] = deviceId;
107     ResSchedMgr::GetInstance().ReportData(ResType::RES_TYPE_SYSTEM_ABILITY_STATUS_CHANGE,
108         ResType::SystemAbilityStatus::SA_REMOVE, payload);
109 }
110 
OnDeviceLevelChanged(int32_t type,int32_t level,std::string & action)111 void ResSchedServiceAbility::OnDeviceLevelChanged(int32_t type, int32_t level, std::string& action)
112 {
113     if (service_ == nullptr) {
114         RESSCHED_LOGE("On Device Level Changed failed due to service nullptr!");
115         return;
116     }
117     service_->OnDeviceLevelChanged(type, level);
118 }
119 
ReclaimProcessMemory()120 void ResSchedServiceAbility::ReclaimProcessMemory()
121 {
122     const int32_t delayTime = 60 * 1000 * 1000;
123     ffrt::task_attr taskattr;
124     taskattr.delay(delayTime);
125     ffrt::submit([]() {ResCommonUtil::WriteFileReclaim(getpid());}, {}, {}, {taskattr});
126 }
127 
SystemAbilityListenerInit()128 void ResSchedServiceAbility::SystemAbilityListenerInit()
129 {
130     if (!AddSystemAbilityListener(APP_MGR_SERVICE_ID)) {
131         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
132                         "COMPONENT_NAME", "MAIN",
133                         "ERR_TYPE", "register failure",
134                         "ERR_MSG", "Register a listener of app manager service failed.");
135         RESSCHED_LOGI("AddSystemAbilityListener failed saId:%{public}d", APP_MGR_SERVICE_ID);
136     }
137     if (!AddSystemAbilityListener(WINDOW_MANAGER_SERVICE_ID)) {
138         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
139                         "COMPONENT_NAME", "MAIN",
140                         "ERR_TYPE", "register failure",
141                         "ERR_MSG", "Register a listener of window manager service failed.");
142         RESSCHED_LOGI("AddSystemAbilityListener failed saId:%{public}d", WINDOW_MANAGER_SERVICE_ID);
143     }
144     if (!AddSystemAbilityListener(BACKGROUND_TASK_MANAGER_SERVICE_ID)) {
145         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
146                         "COMPONENT_NAME", "MAIN",
147                         "ERR_TYPE", "register failure",
148                         "ERR_MSG", "Register a listener of background task manager service failed.");
149         RESSCHED_LOGI("AddSystemAbilityListener failed saId:%{public}d", BACKGROUND_TASK_MANAGER_SERVICE_ID);
150     }
151     if (!AddSystemAbilityListener(RES_SCHED_EXE_ABILITY_ID)) {
152         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
153                         "COMPONENT_NAME", "MAIN",
154                         "ERR_TYPE", "register failure",
155                         "ERR_MSG", "Register a listener of background task manager service failed.");
156         RESSCHED_LOGI("AddSystemAbilityListener failed saId:%{public}d", RES_SCHED_EXE_ABILITY_ID);
157     }
158     if (!AddSystemAbilityListener(POWER_MANAGER_SERVICE_ID)) {
159         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
160                         "COMPONENT_NAME", "MAIN",
161                         "ERR_TYPE", "register failure",
162                         "ERR_MSG", "Register a listener of power manager service failed.");
163         RESSCHED_LOGI("AddSystemAbilityListener failed saId:%{public}d", POWER_MANAGER_SERVICE_ID);
164     }
165     if (!AddSystemAbilityListener(GAME_SERVICE_SERVICE_ID)) {
166         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
167                         "COMPONENT_NAME", "MAIN",
168                         "ERR_TYPE", "register failure",
169                         "ERR_MSG", "Register a listener of power manager service failed.");
170         RESSCHED_LOGI("AddSystemAbilityListener failed saId:%{public}d", GAME_SERVICE_SERVICE_ID);
171     }
172     SystemAbilityListenerInitExt();
173     RESSCHED_LOGI("Init SystemAbilityListener finish");
174 }
175 
SystemAbilityListenerInitExt()176 void ResSchedServiceAbility::SystemAbilityListenerInitExt()
177 {
178     if (!AddSystemAbilityListener(WIFI_DEVICE_SYS_ABILITY_ID)) {
179         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
180                         "COMPONENT_NAME", "MAIN",
181                         "ERR_TYPE", "register failure",
182                         "ERR_MSG", "Register a listener of wifi manager service failed.");
183         RESSCHED_LOGI("AddSystemAbilityListener failed saId:%{public}d", WIFI_DEVICE_SYS_ABILITY_ID);
184     }
185 }
186 } // namespace ResourceSchedule
187 } // namespace OHOS
188 
189