1 /* 2 * Copyright (c) 2023 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_STANDBY_SERVICE_SERVICES_CORE_INCLUDE_STANDBY_SERVICE_IMPL_H 17 #define FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_SERVICES_CORE_INCLUDE_STANDBY_SERVICE_IMPL_H 18 #ifdef STANDBY_SERVICE_UNIT_TEST 19 #define WEAK_FUNC __attribute__((weak)) 20 #else 21 #define WEAK_FUNC 22 #endif 23 24 #include <array> 25 #include <map> 26 #include <memory> 27 #include <list> 28 #include <set> 29 #include <string> 30 #include <unordered_map> 31 #include <vector> 32 33 #include "accesstoken_kit.h" 34 #include "allow_info.h" 35 #include "allow_record.h" 36 #include "app_mgr_client.h" 37 #include "app_mgr_helper.h" 38 #include "app_state_observer.h" 39 #include "common_event_observer.h" 40 #include "event_runner.h" 41 #include "event_handler.h" 42 #include "iconstraint_manager_adapter.h" 43 #include "ilistener_manager_adapter.h" 44 #include "ipc_skeleton.h" 45 #include "istandby_service.h" 46 #include "istandby_service_subscriber.h" 47 #include "istate_manager_adapter.h" 48 #include "istrategy_manager_adapter.h" 49 #include "nlohmann/json.hpp" 50 #ifdef STANDBY_POWER_MANAGER_ENABLE 51 #include "power_mode_info.h" 52 #endif 53 #include "resourcce_request.h" 54 #include "res_type.h" 55 #include "single_instance.h" 56 #include "standby_state_subscriber.h" 57 58 namespace OHOS { 59 namespace DevStandbyMgr { 60 enum class TelCallState { 61 /** 62 * Indicates the call is unknown 63 */ 64 CALL_STATUS_UNKNOWN = -1, 65 /** 66 * Indicates the call is disconnected 67 */ 68 CALL_STATUS_DISCONNECTED = 6, 69 /** 70 * Indicates the call is idle 71 */ 72 CALL_STATUS_IDLE = 8, 73 }; 74 enum P2pState { 75 P2P_STATE_NONE = 0, 76 P2P_STATE_IDLE, 77 P2P_STATE_STARTING, 78 P2P_STATE_STARTED, 79 P2P_STATE_CLOSING, 80 P2P_STATE_CLOSED, 81 }; 82 83 class StandbyServiceImpl : public std::enable_shared_from_this<StandbyServiceImpl> { 84 DECLARE_SINGLE_INSTANCE(StandbyServiceImpl); 85 public: 86 bool Init(); 87 void InitReadyState(); 88 ErrCode RegisterCommEventObserver(); 89 ErrCode RegisterAppStateObserver(); 90 ErrCode RegisterTimeObserver(); 91 ErrCode UnregisterCommEventObserver(); 92 ErrCode UnregisterAppStateObserver(); 93 ErrCode UnregisterTimeObserver(); 94 ErrCode ResetTimeObserver(); 95 void DayNightSwitchCallback(); 96 ErrCode RegisterPlugin(const std::string& pluginName); 97 void UninitReadyState(); 98 void UnInit(); 99 100 ErrCode SubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber); 101 ErrCode UnsubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber); 102 ErrCode ApplyAllowResource(const sptr<ResourceRequest>& resourceRequest); 103 ErrCode UnapplyAllowResource(const sptr<ResourceRequest>& resourceRequest); 104 ErrCode GetAllowList(uint32_t allowType, std::vector<AllowInfo>& allowInfoList, 105 uint32_t reasonCode); 106 ErrCode GetEligiableRestrictSet(uint32_t allowType, const std::string& strategyName, 107 uint32_t resonCode, std::set<std::string>& restrictSet); 108 ErrCode IsDeviceInStandby(bool& isStandby); 109 ErrCode ReportWorkSchedulerStatus(bool started, int32_t uid, const std::string& bundleName); 110 ErrCode GetRestrictList(uint32_t restrictType, std::vector<AllowInfo>& restrictInfoList, 111 uint32_t reasonCode); 112 ErrCode IsStrategyEnabled(const std::string& strategyName, bool& isEnabled); 113 ErrCode ReportDeviceStateChanged(DeviceStateType type, bool enabled); 114 ErrCode HandleCommonEvent(const uint32_t resType, const int64_t value, const std::string &sceneInfo); 115 ErrCode ReportPowerOverused(const std::string &module, uint32_t level); 116 117 void RegisterPluginInner(IConstraintManagerAdapter* constraintManager, 118 IListenerManagerAdapter* listenerManager, 119 IStrategyManagerAdapter* strategyManager, 120 IStateManagerAdapter* stateManager); 121 122 std::shared_ptr<AppExecFwk::EventHandler>& GetHandler(); 123 std::shared_ptr<IConstraintManagerAdapter>& GetConstraintManager(); 124 std::shared_ptr<IListenerManagerAdapter>& GetListenerManager(); 125 std::shared_ptr<IStrategyManagerAdapter>& GetStrategyManager(); 126 std::shared_ptr<IStateManagerAdapter>& GetStateManager(); 127 128 ErrCode RemoveAppAllowRecord(int32_t uid, const std::string &bundleName, bool resetAll); 129 130 void ShellDump(const std::vector<std::string>& argsInStr, std::string& result); 131 void ShellDumpInner(const std::vector<std::string>& argsInStr, std::string& result); 132 void GetAllowListInner(uint32_t allowType, std::vector<AllowInfo>& allowInfoList, 133 uint32_t reasonCode); 134 void DispatchEvent(const StandbyMessage& message); 135 bool IsDebugMode(); 136 137 void OnProcessStatusChanged(int32_t uid, int32_t pid, const std::string& bundleName, bool isCreated); 138 private: 139 void ApplyAllowResInner(const sptr<ResourceRequest>& resourceRequest, int32_t pid); 140 void UpdateRecord(std::shared_ptr<AllowRecord>& allowRecord, const sptr<ResourceRequest>& resourceRequest); 141 void UnapplyAllowResInner(int32_t uid, const std::string& name, uint32_t allowType, bool removeAll); 142 void GetTemporaryAllowList(uint32_t allowTypeIndex, std::vector<AllowInfo>& allowInfoList, 143 uint32_t reasonCode); 144 void GetPersistAllowList(uint32_t allowTypeIndex, std::vector<AllowInfo>& allowInfoList, bool isAllow, bool isApp); 145 void GetRestrictListInner(uint32_t restrictType, std::vector<AllowInfo>& restrictInfoList, 146 uint32_t reasonCode); 147 void NotifyAllowListChanged(int32_t uid, const std::string& name, uint32_t allowType, bool added); 148 149 void RecoverTimeLimitedTask(); 150 bool ParsePersistentData(); 151 void GetPidAndProcName(std::unordered_map<int32_t, std::string>& pidNameMap); 152 void DumpPersistantData(); 153 154 ErrCode CheckCallerPermission(uint32_t reasonCode); 155 ErrCode IsSystemAppWithPermission(int32_t uid, Security::AccessToken::AccessTokenID tokenId, uint32_t reasonCode); 156 ErrCode CheckNativePermission(Security::AccessToken::AccessTokenID tokenId); 157 bool CheckAllowTypeInfo(uint32_t allowType); 158 uint32_t GetExemptedResourceType(uint32_t resourceType); 159 std::vector<int32_t> QueryRunningResourcesApply(const int32_t uid, const std::string& bundleName); 160 int32_t GetUserIdByUid(int32_t uid); 161 162 void DumpUsage(std::string& result); 163 void DumpShowDetailInfo(const std::vector<std::string>& argsInStr, std::string& result); 164 void DumpAllowListInfo(std::string& result); 165 void DumpStandbyConfigInfo(std::string& result); 166 void DumpChangeModeSwitch(const std::vector<std::string>& argsInStr, std::string& result); 167 void DumpEnterSpecifiedState(const std::vector<std::string>& argsInStr, std::string& result); 168 void DumpModifyAllowList(const std::vector<std::string>& argsInStr, std::string& result); 169 void DumpSubScriberObserver(const std::vector<std::string>& argsInStr, std::string& result); 170 171 void DumpTurnOnOffSwitch(const std::vector<std::string>& argsInStr, std::string& result); 172 void DumpChangeConfigParam(const std::vector<std::string>& argsInStr, std::string& result); 173 void DumpPushStrategyChange(const std::vector<std::string>& argsInStr, std::string& result); 174 // dispatch dumper command to plugin 175 void OnPluginShellDump(const std::vector<std::string>& argsInStr, std::string& result); 176 void AppEventHandler(const uint32_t resType, const int64_t value, const std::string &sceneInfo); 177 void HandleCallStateChanged(const std::string &sceneInfo); 178 void HandleP2PStateChanged(int32_t state); 179 #ifdef STANDBY_POWER_MANAGER_ENABLE 180 void HandlePowerModeChanged(PowerMgr::PowerMode mode); 181 #endif 182 void HandleResourcesStateChanged(const int64_t value, const std::string &sceneInfo); 183 void HandleScreenStateChanged(const int64_t value); 184 185 // handle abnormal power use 186 void HandlePowerOverused([[maybe_unused]]uint32_t resType, const std::string &module, uint32_t level); 187 void DumpOnPowerOverused(const std::vector<std::string> &argsInStr, std::string &result); 188 189 private: 190 std::atomic<bool> isServiceReady_ {false}; 191 sptr<AppStateObserver> appStateObserver_ = nullptr; 192 std::shared_ptr<AppExecFwk::EventHandler> handler_ {nullptr}; 193 std::mutex appStateObserverMutex_ {}; 194 std::mutex eventObserverMutex_ {}; 195 std::recursive_mutex timerObserverMutex_ {}; 196 std::mutex allowRecordMutex_ {}; 197 std::unique_ptr<AppExecFwk::AppMgrClient> appMgrClient_ {nullptr}; 198 std::shared_ptr<CommonEventObserver> commonEventObserver_ {nullptr}; 199 uint64_t dayNightSwitchTimerId_ {0}; 200 std::unordered_map<std::string, std::shared_ptr<AllowRecord>> allowInfoMap_ {}; 201 bool ready_ = false; 202 void* registerPlugin_ {nullptr}; 203 std::shared_ptr<IConstraintManagerAdapter> constraintManager_ {nullptr}; 204 std::shared_ptr<IListenerManagerAdapter> listenerManager_ {nullptr}; 205 std::shared_ptr<IStrategyManagerAdapter> strategyManager_ {nullptr}; 206 std::shared_ptr<IStateManagerAdapter> standbyStateManager_ {nullptr}; 207 bool debugMode_ {false}; 208 }; 209 210 class DeviceStateCache { 211 DECLARE_SINGLE_INSTANCE(DeviceStateCache); 212 public: 213 bool SetDeviceState(int32_t type, bool enabled); 214 bool GetDeviceState(int32_t type); 215 private: 216 std::mutex mutex_ {}; 217 const static std::int32_t DEVICE_STATE_NUM = 3; 218 std::array<bool, DEVICE_STATE_NUM> deviceState_; 219 }; 220 } // namespace DevStandbyMgr 221 } // namespace OHOS 222 #endif // FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_SERVICES_CORE_INCLUDE_STANDBY_SERVICE_IMPL_H