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 19 #include <memory> 20 #include <list> 21 #include <unordered_map> 22 #include <vector> 23 #include <string> 24 #include <map> 25 #include <set> 26 #include <array> 27 28 #include "accesstoken_kit.h" 29 #include "ipc_skeleton.h" 30 #include "event_runner.h" 31 #include "event_handler.h" 32 33 #include "istandby_service.h" 34 #include "allow_info.h" 35 #include "allow_record.h" 36 #include "resourcce_request.h" 37 #include "standby_state_subscriber.h" 38 #include "istandby_service_subscriber.h" 39 #include "iconstraint_manager_adapter.h" 40 #include "ilistener_manager_adapter.h" 41 #include "istrategy_manager_adapter.h" 42 #include "istate_manager_adapter.h" 43 #include "standby_service_log.h" 44 #include "single_instance.h" 45 46 #include "app_mgr_client.h" 47 #include "app_state_observer.h" 48 #include "app_mgr_helper.h" 49 #include "common_event_observer.h" 50 51 namespace OHOS { 52 namespace DevStandbyMgr { 53 class StandbyServiceImpl : public std::enable_shared_from_this<StandbyServiceImpl> { 54 DECLARE_SINGLE_INSTANCE(StandbyServiceImpl); 55 public: 56 bool Init(); 57 void InitReadyState(); 58 ErrCode RegisterCommEventObserver(); 59 ErrCode RegisterAppStateObserver(); 60 ErrCode RegisterTimeObserver(); 61 ErrCode UnregisterCommEventObserver(); 62 ErrCode UnregisterAppStateObserver(); 63 ErrCode UnregisterTimeObserver(); 64 ErrCode ResetTimeObserver(); 65 void DayNightSwitchCallback(); 66 ErrCode RegisterPlugin(const std::string& pluginName); 67 void UninitReadyState(); 68 void UnInit(); 69 70 ErrCode SubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber); 71 ErrCode UnsubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber); 72 ErrCode ApplyAllowResource(const sptr<ResourceRequest>& resourceRequest); 73 ErrCode UnapplyAllowResource(const sptr<ResourceRequest>& resourceRequest); 74 ErrCode GetAllowList(uint32_t allowType, std::vector<AllowInfo>& allowInfoList, 75 uint32_t reasonCode); 76 ErrCode GetEligiableRestrictSet(uint32_t allowType, const std::string& strategyName, 77 uint32_t resonCode, std::set<std::string>& restrictSet); 78 ErrCode IsDeviceInStandby(bool& isStandby); 79 ErrCode ReportWorkSchedulerStatus(bool started, int32_t uid, const std::string& bundleName); 80 ErrCode GetRestrictList(uint32_t restrictType, std::vector<AllowInfo>& restrictInfoList, 81 uint32_t reasonCode); 82 ErrCode IsStrategyEnabled(const std::string& strategyName, bool& isEnabled); 83 ErrCode ReportDeviceStateChanged(DeviceStateType type, bool enabled); 84 85 void RegisterPluginInner(IConstraintManagerAdapter* constraintManager, 86 IListenerManagerAdapter* listenerManager, 87 IStrategyManagerAdapter* strategyManager, 88 IStateManagerAdapter* stateManager); 89 90 std::shared_ptr<AppExecFwk::EventHandler>& GetHandler(); 91 std::shared_ptr<IConstraintManagerAdapter>& GetConstraintManager(); 92 std::shared_ptr<IListenerManagerAdapter>& GetListenerManager(); 93 std::shared_ptr<IStrategyManagerAdapter>& GetStrategyManager(); 94 std::shared_ptr<IStateManagerAdapter>& GetStateManager(); 95 96 ErrCode RemoveAppAllowRecord(int32_t uid, const std::string &bundleName, bool resetAll); 97 98 void ShellDump(const std::vector<std::string>& argsInStr, std::string& result); 99 void ShellDumpInner(const std::vector<std::string>& argsInStr, std::string& result); 100 void GetAllowListInner(uint32_t allowType, std::vector<AllowInfo>& allowInfoList, 101 uint32_t reasonCode); 102 void DispatchEvent(const StandbyMessage& message); 103 bool IsDebugMode(); 104 105 void OnProcessStatusChanged(int32_t uid, int32_t pid, const std::string& bundleName, bool isCreated); 106 private: 107 void ApplyAllowResInner(const sptr<ResourceRequest>& resourceRequest, int32_t pid); 108 void UpdateRecord(std::shared_ptr<AllowRecord>& allowRecord, const sptr<ResourceRequest>& resourceRequest); 109 void UnapplyAllowResInner(int32_t uid, const std::string& name, uint32_t allowType, bool removeAll); 110 void GetTemporaryAllowList(uint32_t allowTypeIndex, std::vector<AllowInfo>& allowInfoList, 111 uint32_t reasonCode); 112 void GetPersistAllowList(uint32_t allowTypeIndex, std::vector<AllowInfo>& allowInfoList, bool isAllow, bool isApp); 113 void GetRestrictListInner(uint32_t restrictType, std::vector<AllowInfo>& restrictInfoList, 114 uint32_t reasonCode); 115 void NotifyAllowListChanged(int32_t uid, const std::string& name, uint32_t allowType, bool added); 116 117 void RecoverTimeLimitedTask(); 118 bool ParsePersistentData(); 119 void GetPidAndProcName(std::unordered_map<int32_t, std::string>& pidNameMap); 120 void DumpPersistantData(); 121 122 ErrCode CheckCallerPermission(uint32_t reasonCode); 123 ErrCode IsSystemAppWithPermission(int32_t uid, Security::AccessToken::AccessTokenID tokenId, uint32_t reasonCode); 124 ErrCode CheckNativePermission(Security::AccessToken::AccessTokenID tokenId); 125 bool CheckAllowTypeInfo(uint32_t allowType); 126 ErrCode CheckRunningResourcesApply(const int32_t uid, const std::string& bundleName); 127 int32_t GetUserIdByUid(int32_t uid); 128 129 void DumpUsage(std::string& result); 130 void DumpShowDetailInfo(const std::vector<std::string>& argsInStr, std::string& result); 131 void DumpAllowListInfo(std::string& result); 132 void DumpStandbyConfigInfo(std::string& result); 133 void DumpChangeModeSwitch(const std::vector<std::string>& argsInStr, std::string& result); 134 void DumpEnterSpecifiedState(const std::vector<std::string>& argsInStr, std::string& result); 135 void DumpModifyAllowList(const std::vector<std::string>& argsInStr, std::string& result); 136 void DumpActivateMotion(const std::vector<std::string>& argsInStr, std::string& result); 137 void DumpSubScriberObserver(const std::vector<std::string>& argsInStr, std::string& result); 138 139 void DumpTurnOnOffSwitch(const std::vector<std::string>& argsInStr, std::string& result); 140 void DumpChangeConfigParam(const std::vector<std::string>& argsInStr, std::string& result); 141 private: 142 std::atomic<bool> isServiceReady_ {false}; 143 sptr<AppStateObserver> appStateObserver_ = nullptr; 144 std::shared_ptr<AppExecFwk::EventHandler> handler_ {nullptr}; 145 std::mutex appStateObserverMutex_ {}; 146 std::mutex eventObserverMutex_ {}; 147 std::recursive_mutex timerObserverMutex_ {}; 148 std::mutex allowRecordMutex_ {}; 149 std::unique_ptr<AppExecFwk::AppMgrClient> appMgrClient_ {nullptr}; 150 std::shared_ptr<CommonEventObserver> commonEventObserver_ {nullptr}; 151 uint64_t dayNightSwitchTimerId_ {0}; 152 std::unordered_map<std::string, std::shared_ptr<AllowRecord>> allowInfoMap_ {}; 153 bool ready_ = false; 154 void* registerPlugin_ {nullptr}; 155 std::shared_ptr<IConstraintManagerAdapter> constraintManager_ {nullptr}; 156 std::shared_ptr<IListenerManagerAdapter> listenerManager_ {nullptr}; 157 std::shared_ptr<IStrategyManagerAdapter> strategyManager_ {nullptr}; 158 std::shared_ptr<IStateManagerAdapter> standbyStateManager_ {nullptr}; 159 bool debugMode_ {false}; 160 }; 161 162 class DeviceStateCache { 163 DECLARE_SINGLE_INSTANCE(DeviceStateCache); 164 public: 165 bool SetDeviceState(int32_t type, bool enabled); 166 bool GetDeviceState(int32_t type); 167 private: 168 std::mutex mutex_ {}; 169 const static std::int32_t DEVICE_STATE_NUM = 3; 170 std::array<bool, DEVICE_STATE_NUM> deviceState_; 171 }; 172 } // namespace DevStandbyMgr 173 } // namespace OHOS 174 #endif // FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_SERVICES_CORE_INCLUDE_STANDBY_SERVICE_IMPL_H