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 RESSCHED_SERVICES_RESSCHEDMGR_RESSCHEDFWK_INCLUDE_NOTIFIER_MGR_H 17 #define RESSCHED_SERVICES_RESSCHEDMGR_RESSCHEDFWK_INCLUDE_NOTIFIER_MGR_H 18 19 #include <map> 20 #include <set> 21 #include <mutex> 22 #include <string> 23 24 #include "ffrt.h" 25 #include "iremote_object.h" 26 #include "refbase.h" 27 #include "res_type.h" 28 #include "single_instance.h" 29 #include "app_mgr_client.h" 30 31 namespace OHOS { 32 namespace ResourceSchedule { 33 class NotifierMgr { 34 DECLARE_SINGLE_INSTANCE_BASE(NotifierMgr); 35 public: 36 struct NotifierInfo { 37 sptr<IRemoteObject> notifier = nullptr; 38 bool foreground = true; 39 bool hapApp = false; 40 int32_t level = ResType::SystemloadLevel::LOW; 41 }; 42 using NotifierMap = std::map<int32_t, NotifierInfo>; 43 void Init(); 44 void Deinit(); 45 void RegisterNotifier(int32_t pid, const sptr<IRemoteObject>& notifier); 46 void UnRegisterNotifier(int32_t pid); 47 void OnRemoteNotifierDied(const sptr<IRemoteObject>& notifier); 48 void OnDeviceLevelChanged(int32_t type, int32_t level); 49 void OnApplicationStateChange(int32_t state, int32_t pid); 50 int32_t GetSystemloadLevel(); 51 std::vector<std::pair<int32_t, bool>> DumpRegisterInfo(); 52 private: 53 NotifierMgr() = default; 54 ~NotifierMgr(); 55 void RemoveNotifierLock(const sptr<IRemoteObject>& notifier); 56 void OnDeviceLevelChangedLock(int32_t level); 57 void HandleDeviceLevelChange(std::vector<sptr<IRemoteObject>>& notifierVec, int32_t level); 58 bool IsHapApp(); 59 void ReportPidToHisysevent(const int32_t pid); 60 void NotifierEventReportDelay(int64_t delay); 61 void NotifierEventReportPeriod(); 62 void NotifierEventReport(); 63 std::string GetBundleNameByPid(int32_t pid); 64 65 bool initialized_ = false; 66 bool isTaskSubmit_ = false; 67 std::mutex notifierMutex_; 68 NotifierMap notifierMap_; 69 ffrt::mutex hisyseventMutex_; 70 std::set<std::string> hisyseventBundleNames_; 71 ResType::SystemloadLevel systemloadLevel_ = ResType::SystemloadLevel::LOW; 72 sptr<IRemoteObject::DeathRecipient> notifierDeathRecipient_ = nullptr; 73 std::shared_ptr<ffrt::queue> notifierHandler_ = nullptr; 74 std::shared_ptr<AppExecFwk::AppMgrClient> appMgrClient_ = nullptr; 75 }; 76 } // ResourceSchedule 77 } // OHOS 78 #endif // RESSCHED_SERVICES_RESSCHEDMGR_RESSCHEDFWK_INCLUDE_NOTIFIER_MGR_H