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 #include "resource_request.h" 50 #include "res_type.h" 51 #include "singleton.h" 52 #include "standby_state_subscriber.h" 53 54 namespace OHOS { 55 namespace DevStandbyMgr { 56 enum class TelCallState { 57 /** 58 * Indicates the call is unknown 59 */ 60 CALL_STATUS_UNKNOWN = -1, 61 /** 62 * Indicates the call is disconnected 63 */ 64 CALL_STATUS_DISCONNECTED = 6, 65 /** 66 * Indicates the call is idle 67 */ 68 CALL_STATUS_IDLE = 8, 69 }; 70 enum P2pState { 71 P2P_STATE_NONE = 0, 72 P2P_STATE_IDLE, 73 P2P_STATE_STARTING, 74 P2P_STATE_STARTED, 75 P2P_STATE_CLOSING, 76 P2P_STATE_CLOSED, 77 }; 78 79 struct CloneFileHead { 80 char moduleName[32]; 81 uint32_t fileOffset; 82 uint32_t fileSize; 83 uint8_t data[0]; 84 }; 85 86 class StandbyServiceImpl : public std::enable_shared_from_this<StandbyServiceImpl> { 87 DECLARE_DELAYED_SINGLETON(StandbyServiceImpl); 88 public: 89 static std::shared_ptr<StandbyServiceImpl> GetInstance(); 90 bool Init(); 91 void InitReadyState(); 92 void AddWatchDog(); 93 ErrCode RegisterCommEventObserver(); 94 ErrCode RegisterAppStateObserver(); 95 ErrCode RegisterTimeObserver(); 96 ErrCode UnregisterCommEventObserver(); 97 ErrCode UnregisterAppStateObserver(); 98 ErrCode UnregisterTimeObserver(); 99 ErrCode ResetTimeObserver(); 100 void DayNightSwitchCallback(); 101 ErrCode RegisterPlugin(const std::string& pluginName); 102 void UninitReadyState(); 103 void UnInit(); 104 ErrCode SubscribeBackupRestoreCallback(const std::string& moduleName, 105 const std::function<ErrCode(std::vector<char>&)>& onBackupFunc, 106 const std::function<ErrCode(std::vector<char>&)>& onRestoreFunc); 107 ErrCode UnsubscribeBackupRestoreCallback(const std::string& moduleName); 108 ErrCode OnBackup(MessageParcel& data, MessageParcel& reply); 109 ErrCode OnRestore(MessageParcel& data, MessageParcel& reply); 110 111 ErrCode SubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber); 112 ErrCode UnsubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber); 113 ErrCode ApplyAllowResource(ResourceRequest& resourceRequest); 114 ErrCode UnapplyAllowResource(ResourceRequest& resourceRequest); 115 ErrCode GetAllowList(uint32_t allowType, std::vector<AllowInfo>& allowInfoList, 116 uint32_t reasonCode); 117 ErrCode GetEligiableRestrictSet(uint32_t allowType, const std::string& strategyName, 118 uint32_t resonCode, std::set<std::string>& restrictSet); 119 ErrCode IsDeviceInStandby(bool& isStandby); 120 ErrCode ReportWorkSchedulerStatus(bool started, int32_t uid, const std::string& bundleName); 121 ErrCode GetRestrictList(uint32_t restrictType, std::vector<AllowInfo>& restrictInfoList, 122 uint32_t reasonCode); 123 ErrCode IsStrategyEnabled(const std::string& strategyName, bool& isEnabled); 124 ErrCode ReportDeviceStateChanged(int32_t type, bool enabled); 125 ErrCode HandleCommonEvent(const uint32_t resType, const int64_t value, const std::string &sceneInfo); 126 ErrCode ReportPowerOverused(const std::string &module, uint32_t level); 127 ErrCode ReportSceneInfo(uint32_t resType, int64_t value, const std::string &sceneInfo); 128 129 void RegisterPluginInner(IConstraintManagerAdapter* constraintManager, 130 IListenerManagerAdapter* listenerManager, 131 IStrategyManagerAdapter* strategyManager, 132 IStateManagerAdapter* stateManager); 133 134 std::shared_ptr<AppExecFwk::EventHandler>& GetHandler(); 135 std::shared_ptr<IConstraintManagerAdapter>& GetConstraintManager(); 136 std::shared_ptr<IListenerManagerAdapter>& GetListenerManager(); 137 std::shared_ptr<IStrategyManagerAdapter>& GetStrategyManager(); 138 std::shared_ptr<IStateManagerAdapter>& GetStateManager(); 139 140 ErrCode RemoveAppAllowRecord(int32_t uid, const std::string &bundleName, bool resetAll); 141 142 void ShellDump(const std::vector<std::string>& argsInStr, std::string& result); 143 void ShellDumpInner(const std::vector<std::string>& argsInStr, std::string& result); 144 void GetAllowListInner(uint32_t allowType, std::vector<AllowInfo>& allowInfoList, 145 uint32_t reasonCode); 146 void DispatchEvent(const StandbyMessage& message); 147 bool IsDebugMode(); 148 bool IsServiceReady(); 149 void UpdateSaDependValue(const bool& isAdd, const uint32_t& saId); 150 uint32_t GetSaDependValue(); 151 152 void OnProcessStatusChanged(int32_t uid, int32_t pid, const std::string& bundleName, bool isCreated); 153 private: 154 StandbyServiceImpl(const StandbyServiceImpl&) = delete; 155 StandbyServiceImpl& operator= (const StandbyServiceImpl&) = delete; 156 StandbyServiceImpl(StandbyServiceImpl&&) = delete; 157 StandbyServiceImpl& operator= (StandbyServiceImpl&&) = delete; 158 void ApplyAllowResInner(const ResourceRequest& resourceRequest, int32_t pid); 159 void UpdateRecord(std::shared_ptr<AllowRecord>& allowRecord, const ResourceRequest& resourceRequest); 160 void UnapplyAllowResInner(int32_t uid, const std::string& name, uint32_t allowType, bool removeAll); 161 void GetTemporaryAllowList(uint32_t allowTypeIndex, std::vector<AllowInfo>& allowInfoList, 162 uint32_t reasonCode); 163 void GetPersistAllowList(uint32_t allowTypeIndex, std::vector<AllowInfo>& allowInfoList, bool isAllow, bool isApp); 164 void GetRestrictListInner(uint32_t restrictType, std::vector<AllowInfo>& restrictInfoList, 165 uint32_t reasonCode); 166 void NotifyAllowListChanged(int32_t uid, const std::string& name, uint32_t allowType, bool added); 167 std::string BuildBackupReplyCode(int32_t replyCode); 168 169 void RecoverTimeLimitedTask(); 170 bool ParsePersistentData(); 171 void GetPidAndProcName(std::unordered_map<int32_t, std::string>& pidNameMap); 172 void DumpPersistantData(); 173 uint32_t dependsReady_ = 0; 174 175 ErrCode CheckCallerPermission(uint32_t reasonCode = ReasonCodeEnum::REASON_APP_API); 176 ErrCode IsSystemAppWithPermission(int32_t uid, Security::AccessToken::AccessTokenID tokenId, uint32_t reasonCode); 177 ErrCode CheckNativePermission(Security::AccessToken::AccessTokenID tokenId); 178 bool CheckAllowTypeInfo(uint32_t allowType); 179 uint32_t GetExemptedResourceType(uint32_t resourceType); 180 std::vector<int32_t> QueryRunningResourcesApply(const int32_t uid, const std::string& bundleName); 181 int32_t GetUserIdByUid(int32_t uid); 182 183 void DumpUsage(std::string& result); 184 void DumpShowDetailInfo(const std::vector<std::string>& argsInStr, std::string& result); 185 void DumpAllowListInfo(std::string& result); 186 void DumpStandbyConfigInfo(std::string& result); 187 void DumpChangeModeSwitch(const std::vector<std::string>& argsInStr, std::string& result); 188 void DumpEnterSpecifiedState(const std::vector<std::string>& argsInStr, std::string& result); 189 void DumpModifyAllowList(const std::vector<std::string>& argsInStr, std::string& result); 190 void DumpSubScriberObserver(const std::vector<std::string>& argsInStr, std::string& result); 191 192 void DumpTurnOnOffSwitch(const std::vector<std::string>& argsInStr, std::string& result); 193 void DumpChangeConfigParam(const std::vector<std::string>& argsInStr, std::string& result); 194 void DumpPushStrategyChange(const std::vector<std::string>& argsInStr, std::string& result); 195 // dispatch dumper command to plugin 196 void OnPluginShellDump(const std::vector<std::string>& argsInStr, std::string& result); 197 void AppEventHandler(const uint32_t resType, const int64_t value, const std::string &sceneInfo); 198 void HandleCallStateChanged(const std::string &sceneInfo); 199 void HandleP2PStateChanged(int32_t state); 200 void HandleReportFileSizeEvent(); 201 void HandleScreenClickRecognize(const int64_t value); 202 void HandleMmiInputPowerKeyDown(const int64_t value); 203 void HandleBTServiceEvent(const int64_t value, const std::string &sceneInfo); 204 void HandleBrokerGattConnect(const int64_t value, const std::string &sceneInfo); 205 void HandleThermalScenarioReport(const int64_t value, const std::string &sceneInfo); 206 void HandlePowerModeChanged(const int64_t value); 207 void HandleResourcesStateChanged(const int64_t value, const std::string &sceneInfo); 208 void HandleScreenStateChanged(const int64_t value); 209 void HandleChargeStateChanged(const int64_t value); 210 void HandleBootCompleted(); 211 212 // handle abnormal power use 213 void HandlePowerOverused([[maybe_unused]]uint32_t resType, const std::string &module, uint32_t level); 214 void DumpOnPowerOverused(const std::vector<std::string> &argsInStr, std::string &result); 215 216 void HandleActionChanged([[maybe_unused]]uint32_t resType, const std::string &module, uint32_t action); 217 void DumpOnActionChanged(const std::vector<std::string> &argsInStr, std::string &result); 218 219 private: 220 std::atomic<bool> isServiceReady_ {false}; 221 sptr<AppStateObserver> appStateObserver_ = nullptr; 222 std::shared_ptr<AppExecFwk::EventHandler> handler_ {nullptr}; 223 std::mutex appStateObserverMutex_ {}; 224 std::mutex eventObserverMutex_ {}; 225 std::recursive_mutex timerObserverMutex_ {}; 226 std::mutex allowRecordMutex_ {}; 227 std::mutex backupRestoreMutex_ {}; 228 std::map<std::string, std::function<ErrCode(std::vector<char>&)>> onBackupFuncMap_ {}; 229 std::map<std::string, std::function<ErrCode(std::vector<char>&)>> onRestoreFuncMap_ {}; 230 std::unique_ptr<AppExecFwk::AppMgrClient> appMgrClient_ {nullptr}; 231 std::shared_ptr<CommonEventObserver> commonEventObserver_ {nullptr}; 232 uint64_t dayNightSwitchTimerId_ {0}; 233 std::unordered_map<std::string, std::shared_ptr<AllowRecord>> allowInfoMap_ {}; 234 bool ready_ = false; 235 void* registerPlugin_ {nullptr}; 236 std::shared_ptr<IConstraintManagerAdapter> constraintManager_ {nullptr}; 237 std::shared_ptr<IListenerManagerAdapter> listenerManager_ {nullptr}; 238 std::shared_ptr<IStrategyManagerAdapter> strategyManager_ {nullptr}; 239 std::shared_ptr<IStateManagerAdapter> standbyStateManager_ {nullptr}; 240 bool debugMode_ {false}; 241 }; 242 243 class DeviceStateCache { 244 DECLARE_DELAYED_SINGLETON(DeviceStateCache); 245 public: 246 static std::shared_ptr<DeviceStateCache> GetInstance(); 247 bool SetDeviceState(int32_t type, bool enabled); 248 bool GetDeviceState(int32_t type); 249 private: 250 DeviceStateCache(const DeviceStateCache&) = delete; 251 DeviceStateCache& operator= (const DeviceStateCache&) = delete; 252 DeviceStateCache(DeviceStateCache&&) = delete; 253 DeviceStateCache& operator= (DeviceStateCache&&) = delete; 254 std::mutex mutex_ {}; 255 const static std::int32_t DEVICE_STATE_NUM = 3; 256 std::array<bool, DEVICE_STATE_NUM> deviceState_; 257 }; 258 } // namespace DevStandbyMgr 259 } // namespace OHOS 260 #endif // FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_SERVICES_CORE_INCLUDE_STANDBY_SERVICE_IMPL_H 261