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 #ifndef RESSCHED_SERVICES_RESSCHEDSERVICE_INCLUDE_RES_SCHED_SERVICE_H 17 #define RESSCHED_SERVICES_RESSCHEDSERVICE_INCLUDE_RES_SCHED_SERVICE_H 18 19 #include "plugin_mgr.h" 20 #include "res_sched_service_stub.h" 21 #include "suspend_state_observer_base_stub.h" 22 #ifdef RESOURCE_SCHEDULE_SERVICE_WITH_EXT_RES_ENABLE 23 #include "res_type.h" 24 #endif 25 #include "res_sched_lru_cache.h" 26 #include "tokenid_kit.h" 27 #include "accesstoken_kit.h" 28 29 namespace OHOS { 30 namespace ResourceSchedule { 31 using namespace OHOS::Security; 32 class ResSchedService : public ResSchedServiceStub { 33 public: 34 ResSchedService() = default; 35 ~ResSchedService() override = default; 36 37 ErrCode ReportData(uint32_t resType, int64_t value, const std::string& payload) override; 38 39 ErrCode ReportSyncEvent(uint32_t resType, int64_t value, const std::string& payload, 40 std::string& reply, int32_t& resultValue) override; 41 42 ErrCode KillProcess(const std::string& payload, int32_t& resultValue) override; 43 44 ErrCode RegisterSystemloadNotifier(const sptr<IRemoteObject>& notifier) override; 45 46 ErrCode UnRegisterSystemloadNotifier() override; 47 48 ErrCode RegisterEventListener(const sptr<IRemoteObject>& eventListener, uint32_t eventType, 49 uint32_t listenerGroup) override; 50 51 ErrCode UnRegisterEventListener(uint32_t eventType, uint32_t listenerGroup) override; 52 53 ErrCode GetSystemloadLevel(int32_t& resultValue) override; 54 55 ErrCode GetResTypeList(std::set<uint32_t>& resTypeList) override; 56 57 void OnDeviceLevelChanged(int32_t type, int32_t level, bool debugReport = false); 58 59 ErrCode IsAllowedAppPreload(const std::string& bundleName, int32_t preloadMode, bool& resultValue) override; 60 61 void LoadAppPreloadPlugin(); 62 63 ErrCode IsAllowedLinkJump(bool isAllowedLinkJump, int32_t& resultValue) override; 64 65 int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; 66 67 void DumpSystemLoadInfo(std::string &result); 68 69 void DumpAllPluginConfig(std::string &result); 70 71 void InitAllowIpcReportRes(); 72 73 ErrCode RegisterSuspendObserver(const sptr<ISuspendStateObserverBase> &observer, int32_t &funcResult) override; 74 75 ErrCode UnregisterSuspendObserver(const sptr<ISuspendStateObserverBase> &observer, int32_t &funcResult) override; 76 77 ErrCode GetSuspendStateByUid(const int32_t uid, bool &isFrozen, int32_t &funcResult) override; 78 79 ErrCode GetSuspendStateByPid(const int32_t pid, bool &isFrozen, int32_t &funcResult) override; 80 81 private: 82 DISALLOW_COPY_AND_MOVE(ResSchedService); 83 84 void DumpAllInfo(std::string &result); 85 void DumpUsage(std::string &result); 86 void DumpExt(const std::vector<std::string>& argsInStr, std::string &result); 87 void DumpExecutorDebugCommand(const std::vector<std::string>& args, std::string& result); 88 void DumpSetSystemLoad(const std::vector<std::string>& args, std::string& result); 89 bool CheckDumpPermission(); 90 bool CheckENGMode(); 91 92 nlohmann::json StringToJsonObj(const std::string& str); 93 int32_t CheckReportDataParcel(const uint32_t& type, const int64_t& value, const std::string& payload, int32_t uid); 94 bool IsLimitRequest(int32_t uid); 95 void CheckAndUpdateLimitData(int64_t nowTime); 96 void PrintLimitLog(int32_t uid); 97 void ReportBigData(); 98 void InreaseBigDataCount(); 99 int32_t RemoteRequestCheck(); 100 bool IsHasPermission(const uint32_t type, int32_t uid); 101 bool IsSBDResType(uint32_t type); 102 bool IsThirdPartType(uint32_t type); 103 #ifdef RESOURCE_SCHEDULE_SERVICE_WITH_EXT_RES_ENABLE 104 int32_t GetExtTypeByResPayload(const std::string& payload); 105 #endif 106 void AddSCBRes(const std::unordered_set<uint32_t>& allowSCBReportRes); 107 void AddAllSARes(const std::unordered_set<uint32_t>& allowAllSAReportRes); 108 void AddSomeSARes(const std::unordered_map<uint32_t, std::unordered_set<int32_t>>& allowSomeSAReportRes); 109 void AddAllAppRes(const std::unordered_set<uint32_t>& allowAllAppReportRes); 110 void AddFgAppRes(const std::unordered_set<uint32_t>& allowFgAppReportRes); 111 bool CheckSuspendPermission(); 112 113 OnIsAllowedAppPreloadFunc appPreloadFunc_ = nullptr; 114 bool isLoadAppPreloadPlugin_ = false; 115 using RequestFuncType = std::function<int32_t (MessageParcel& data, MessageParcel& reply)>; 116 std::map<uint32_t, RequestFuncType> funcMap_; 117 std::map<int32_t, int32_t> appRequestCountMap_; 118 std::atomic<int32_t> allRequestCount_ {0}; 119 std::atomic<int32_t> bigDataReportCount_ {0}; 120 std::atomic<int64_t> nextCheckTime_ = {0}; 121 std::atomic<int64_t> nextReportBigDataTime_ = {0}; 122 std::atomic<bool> isReportBigData_ = {false}; 123 std::atomic<bool> isPrintLimitLog_ = {true}; 124 std::atomic<int32_t> actualSystemLoadLevel_ = {0}; 125 std::atomic<int32_t> debugSystemLoadLevel_ = {0}; 126 std::atomic<bool> systemLoadLevelDebugEnable_ = {false}; 127 128 std::mutex mutex_; 129 std::mutex permissionCacheMutex_; 130 std::unordered_set<uint32_t> allowSCBReportRes_; 131 std::unordered_set<uint32_t> allowAllSAReportRes_; 132 std::unordered_map<uint32_t, std::unordered_set<int32_t>> allowSomeSAReportRes_; 133 std::unordered_set<uint32_t> allowAllAppReportRes_; 134 std::unordered_set<uint32_t> allowFgAppReportRes_; 135 ResschedLRUCache<AccessToken::AccessTokenID, int32_t> permissionCache_; 136 }; 137 } // namespace ResourceSchedule 138 } // namespace OHOS 139 140 #endif // RESSCHED_SERVICES_RESSCHEDSERVICE_INCLUDE_RES_SCHED_SERVICE_H 141