1 /* 2 * Copyright (c) 2022 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 FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_EFFICIENCY_RESOURCES_INCLUDE_EFFICIENCY_RES_MGR_H 17 #define FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_EFFICIENCY_RESOURCES_INCLUDE_EFFICIENCY_RES_MGR_H 18 19 #include <memory> 20 #include <list> 21 #include <unordered_map> 22 23 #include "ipc_skeleton.h" 24 #include "singleton.h" 25 #include "event_runner.h" 26 #include "event_handler.h" 27 #include "event_info.h" 28 #include "running_process_info.h" 29 #include "app_mgr_client.h" 30 31 #include "efficiency_resource_info.h" 32 #include "ibackground_task_subscriber.h" 33 #include "resource_callback_info.h" 34 #include "bundle_manager_helper.h" 35 #include "resource_application_record.h" 36 #include "resources_subscriber_mgr.h" 37 #include "app_state_observer.h" 38 #include "data_storage_helper.h" 39 40 namespace OHOS { 41 namespace BackgroundTaskMgr { 42 class DataStorageHelper; 43 class BgEfficiencyResourcesMgr : public DelayedSingleton<BgEfficiencyResourcesMgr>, 44 public std::enable_shared_from_this<BgEfficiencyResourcesMgr> { 45 using ResourceRecordMap = std::unordered_map<int32_t, std::shared_ptr<ResourceApplicationRecord>>; 46 using ResourceRecordPair = std::pair<const int32_t, std::shared_ptr< 47 OHOS::BackgroundTaskMgr::ResourceApplicationRecord>>; 48 public: 49 ErrCode ShellDump(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo); 50 bool Init(const std::shared_ptr<AppExecFwk::EventRunner>& runner); 51 void InitNecessaryState(); 52 void Clear(); 53 ErrCode ApplyEfficiencyResources(const sptr<EfficiencyResourceInfo> &resourceInfo); 54 ErrCode ResetAllEfficiencyResources(); 55 ErrCode GetEfficiencyResourcesInfos(std::vector<std::shared_ptr<ResourceCallbackInfo>> &appList, 56 std::vector<std::shared_ptr<ResourceCallbackInfo>> &procList); 57 ErrCode AddSubscriber(const sptr<IBackgroundTaskSubscriber> &subscriber); 58 ErrCode RemoveSubscriber(const sptr<IBackgroundTaskSubscriber> &subscriber); 59 ErrCode RemoveProcessRecord(int32_t uid, int32_t pid, const std::string &bundleName); 60 ErrCode RemoveAppRecord(int32_t uid, const std::string &bundleName, bool resetAll); 61 void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId); 62 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId); 63 64 private: 65 void ApplyEfficiencyResourcesInner(std::shared_ptr<ResourceCallbackInfo> callbackInfo, 66 const sptr<EfficiencyResourceInfo> &resourceInfo); 67 void UpdateResourcesEndtime(const std::shared_ptr<ResourceCallbackInfo> &callbackInfo, 68 std::shared_ptr<ResourceApplicationRecord> &record, const sptr<EfficiencyResourceInfo> &resourceInfo); 69 void ResetEfficiencyResourcesInner(const std::shared_ptr<ResourceCallbackInfo> &callbackInfo, 70 bool isProcess); 71 ErrCode ResetAllEfficiencyResourcesInner(const std::shared_ptr<ResourceCallbackInfo> &callbackInfo, 72 bool isProcess); 73 void RemoveRelativeProcessRecord(int32_t uid, uint32_t resourceNumber); 74 ErrCode ShellDumpInner(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo); 75 void DumpAllApplicationInfo(std::vector<std::string> &dumpInfo); 76 void DumpResetAllResource(const std::vector<std::string> &dumpOption); 77 void DumpResetResource(const std::vector<std::string> &dumpOption, bool cleanApp, bool cleanAll); 78 void DumpApplicationInfoMap(std::unordered_map<int32_t, 79 std::shared_ptr<ResourceApplicationRecord>> &infoMap, std::vector<std::string> &dumpInfo, 80 std::stringstream &stream, const char *headInfo); 81 void ResetTimeOutResource(int32_t mapKey, bool isProcess); 82 bool RemoveTargetResourceRecord(std::unordered_map<int32_t, 83 std::shared_ptr<ResourceApplicationRecord>> &infoMap, int32_t mapKey, 84 uint32_t cleanResource, EfficiencyResourcesEventType type); 85 bool GetBundleNamesForUid(int32_t uid, std::string &bundleName); 86 bool IsCallingInfoLegal(int32_t uid, int32_t pid, std::string &bundleName); 87 void EraseRecordIf(ResourceRecordMap &infoMap, const std::function<bool(ResourceRecordPair)> &fun); 88 void RecoverDelayedTask(bool isProcess, ResourceRecordMap& infoMap); 89 void HandlePersistenceData(); 90 void CheckPersistenceData(const std::vector<AppExecFwk::RunningProcessInfo> &allProcesses); 91 void RemoveListRecord(std::list<PersistTime> &resourceUnitList, uint32_t eraseBit); 92 void GetEfficiencyResourcesInfosInner(const ResourceRecordMap &infoMap, 93 std::vector<std::shared_ptr<ResourceCallbackInfo>> &list); 94 uint32_t GetExemptedResourceType(uint32_t resourceNumber, const int32_t uid, const std::string &bundleName); 95 std::vector<int32_t> QueryRunningResourcesApply(const int32_t uid, const std::string &bundleName); 96 bool CheckAlivedApp(int32_t uid); 97 int32_t GetUserIdByUid(int32_t uid); 98 void ApplyResourceForPkgAndProc(int32_t uid, int32_t pid, const std::string &bundleName, 99 const sptr<EfficiencyResourceInfo> &resourceInfo); 100 void SendResourceApplyTask(int32_t uid, int32_t pid, const std::string &bundleName, 101 const sptr<EfficiencyResourceInfo> &resourceInfo); 102 bool IsServiceExtensionType(const pid_t pid); 103 104 private: 105 std::atomic<bool> isSysReady_ {false}; 106 std::mutex sysAbilityLock_; 107 std::shared_ptr<AppExecFwk::EventHandler> handler_ {nullptr}; 108 std::unordered_map<int32_t, std::shared_ptr<ResourceApplicationRecord>> appResourceApplyMap_ {}; 109 std::unordered_map<int32_t, std::shared_ptr<ResourceApplicationRecord>> procResourceApplyMap_ {}; 110 std::shared_ptr<AppStateObserver> appStateObserver_ {nullptr}; 111 std::shared_ptr<ResourcesSubscriberMgr> subscriberMgr_ {nullptr}; 112 std::unique_ptr<AppExecFwk::AppMgrClient> appMgrClient_ {nullptr}; 113 uint32_t dependsReady_ = 0; 114 115 DECLARE_DELAYED_SINGLETON(BgEfficiencyResourcesMgr); 116 }; 117 } // namespace BackgroundTaskMgr 118 } // namespace OHOS 119 #endif // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_EFFICIENCY_RESOURCES_INCLUDE_EFFICIENCY_RES_MGR_H