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_subscriber.h" 46 #include "istate_manager_adapter.h" 47 #include "istrategy_manager_adapter.h" 48 #include "nlohmann/json.hpp" 49 #ifdef STANDBY_POWER_MANAGER_ENABLE 50 #include "power_mode_info.h" 51 #endif 52 #include "resource_request.h" 53 #include "res_type.h" 54 #include "singleton.h" 55 #include "standby_state_subscriber.h" 56 57 namespace OHOS { 58 namespace DevStandbyMgr { 59 enum class TelCallState { 60 /** 61 * Indicates the call is unknown 62 */ 63 CALL_STATUS_UNKNOWN = -1, 64 /** 65 * Indicates the call is disconnected 66 */ 67 CALL_STATUS_DISCONNECTED = 6, 68 /** 69 * Indicates the call is idle 70 */ 71 CALL_STATUS_IDLE = 8, 72 }; 73 enum P2pState { 74 P2P_STATE_NONE = 0, 75 P2P_STATE_IDLE, 76 P2P_STATE_STARTING, 77 P2P_STATE_STARTED, 78 P2P_STATE_CLOSING, 79 P2P_STATE_CLOSED, 80 }; 81 82 class StandbyServiceImpl : public std::enable_shared_from_this<StandbyServiceImpl> { 83 DECLARE_DELAYED_SINGLETON(StandbyServiceImpl); 84 public: 85 static std::shared_ptr<StandbyServiceImpl> GetInstance(); 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(ResourceRequest& resourceRequest); 103 ErrCode UnapplyAllowResource(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(int32_t 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 bool IsServiceReady(); 137 void UpdateSaDependValue(const bool& isAdd, const uint32_t& saId); 138 uint32_t GetSaDependValue(); 139 140 void OnProcessStatusChanged(int32_t uid, int32_t pid, const std::string& bundleName, bool isCreated); 141 private: 142 StandbyServiceImpl(const StandbyServiceImpl&) = delete; 143 StandbyServiceImpl& operator= (const StandbyServiceImpl&) = delete; 144 StandbyServiceImpl(StandbyServiceImpl&&) = delete; 145 StandbyServiceImpl& operator= (StandbyServiceImpl&&) = delete; 146 void ApplyAllowResInner(const ResourceRequest& resourceRequest, int32_t pid); 147 void UpdateRecord(std::shared_ptr<AllowRecord>& allowRecord, const ResourceRequest& resourceRequest); 148 void UnapplyAllowResInner(int32_t uid, const std::string& name, uint32_t allowType, bool removeAll); 149 void GetTemporaryAllowList(uint32_t allowTypeIndex, std::vector<AllowInfo>& allowInfoList, 150 uint32_t reasonCode); 151 void GetPersistAllowList(uint32_t allowTypeIndex, std::vector<AllowInfo>& allowInfoList, bool isAllow, bool isApp); 152 void GetRestrictListInner(uint32_t restrictType, std::vector<AllowInfo>& restrictInfoList, 153 uint32_t reasonCode); 154 void NotifyAllowListChanged(int32_t uid, const std::string& name, uint32_t allowType, bool added); 155 156 void RecoverTimeLimitedTask(); 157 bool ParsePersistentData(); 158 void GetPidAndProcName(std::unordered_map<int32_t, std::string>& pidNameMap); 159 void DumpPersistantData(); 160 uint32_t dependsReady_ = 0; 161 162 ErrCode CheckCallerPermission(uint32_t reasonCode); 163 ErrCode IsSystemAppWithPermission(int32_t uid, Security::AccessToken::AccessTokenID tokenId, uint32_t reasonCode); 164 ErrCode CheckNativePermission(Security::AccessToken::AccessTokenID tokenId); 165 bool CheckAllowTypeInfo(uint32_t allowType); 166 uint32_t GetExemptedResourceType(uint32_t resourceType); 167 std::vector<int32_t> QueryRunningResourcesApply(const int32_t uid, const std::string& bundleName); 168 int32_t GetUserIdByUid(int32_t uid); 169 170 void DumpUsage(std::string& result); 171 void DumpShowDetailInfo(const std::vector<std::string>& argsInStr, std::string& result); 172 void DumpAllowListInfo(std::string& result); 173 void DumpStandbyConfigInfo(std::string& result); 174 void DumpChangeModeSwitch(const std::vector<std::string>& argsInStr, std::string& result); 175 void DumpEnterSpecifiedState(const std::vector<std::string>& argsInStr, std::string& result); 176 void DumpModifyAllowList(const std::vector<std::string>& argsInStr, std::string& result); 177 void DumpSubScriberObserver(const std::vector<std::string>& argsInStr, std::string& result); 178 179 void DumpTurnOnOffSwitch(const std::vector<std::string>& argsInStr, std::string& result); 180 void DumpChangeConfigParam(const std::vector<std::string>& argsInStr, std::string& result); 181 void DumpPushStrategyChange(const std::vector<std::string>& argsInStr, std::string& result); 182 // dispatch dumper command to plugin 183 void OnPluginShellDump(const std::vector<std::string>& argsInStr, std::string& result); 184 void AppEventHandler(const uint32_t resType, const int64_t value, const std::string &sceneInfo); 185 void HandleCallStateChanged(const std::string &sceneInfo); 186 void HandleP2PStateChanged(int32_t state); 187 void HandleScreenClickRecognize(const int64_t value); 188 void HandleMmiInputPowerKeyDown(const int64_t value); 189 #ifdef STANDBY_POWER_MANAGER_ENABLE 190 void HandlePowerModeChanged(PowerMgr::PowerMode mode); 191 #endif 192 void HandleResourcesStateChanged(const int64_t value, const std::string &sceneInfo); 193 void HandleScreenStateChanged(const int64_t value); 194 void HandleChargeStateChanged(const int64_t value); 195 196 // handle abnormal power use 197 void HandlePowerOverused([[maybe_unused]]uint32_t resType, const std::string &module, uint32_t level); 198 void DumpOnPowerOverused(const std::vector<std::string> &argsInStr, std::string &result); 199 200 private: 201 std::atomic<bool> isServiceReady_ {false}; 202 sptr<AppStateObserver> appStateObserver_ = nullptr; 203 std::shared_ptr<AppExecFwk::EventHandler> handler_ {nullptr}; 204 std::mutex appStateObserverMutex_ {}; 205 std::mutex eventObserverMutex_ {}; 206 std::recursive_mutex timerObserverMutex_ {}; 207 std::mutex allowRecordMutex_ {}; 208 std::unique_ptr<AppExecFwk::AppMgrClient> appMgrClient_ {nullptr}; 209 std::shared_ptr<CommonEventObserver> commonEventObserver_ {nullptr}; 210 uint64_t dayNightSwitchTimerId_ {0}; 211 std::unordered_map<std::string, std::shared_ptr<AllowRecord>> allowInfoMap_ {}; 212 bool ready_ = false; 213 void* registerPlugin_ {nullptr}; 214 std::shared_ptr<IConstraintManagerAdapter> constraintManager_ {nullptr}; 215 std::shared_ptr<IListenerManagerAdapter> listenerManager_ {nullptr}; 216 std::shared_ptr<IStrategyManagerAdapter> strategyManager_ {nullptr}; 217 std::shared_ptr<IStateManagerAdapter> standbyStateManager_ {nullptr}; 218 bool debugMode_ {false}; 219 }; 220 221 class DeviceStateCache { 222 DECLARE_DELAYED_SINGLETON(DeviceStateCache); 223 public: 224 static std::shared_ptr<DeviceStateCache> GetInstance(); 225 bool SetDeviceState(int32_t type, bool enabled); 226 bool GetDeviceState(int32_t type); 227 private: 228 DeviceStateCache(const DeviceStateCache&) = delete; 229 DeviceStateCache& operator= (const DeviceStateCache&) = delete; 230 DeviceStateCache(DeviceStateCache&&) = delete; 231 DeviceStateCache& operator= (DeviceStateCache&&) = delete; 232 std::mutex mutex_ {}; 233 const static std::int32_t DEVICE_STATE_NUM = 3; 234 std::array<bool, DEVICE_STATE_NUM> deviceState_; 235 }; 236 } // namespace DevStandbyMgr 237 } // namespace OHOS 238 #endif // FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_SERVICES_CORE_INCLUDE_STANDBY_SERVICE_IMPL_H 239