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 POWERMGR_POWER_MANAGER_SHUTDOWN_CONTROLLER_H 17 #define POWERMGR_POWER_MANAGER_SHUTDOWN_CONTROLLER_H 18 19 #include "actions/idevice_state_action.h" 20 #include "device_power_action.h" 21 #include "want.h" 22 #include <atomic> 23 #include <string> 24 25 #include "shutdown/iasync_shutdown_callback.h" 26 #include "shutdown/isync_shutdown_callback.h" 27 #include "shutdown/itakeover_shutdown_callback.h" 28 #include "shutdown/takeover_info.h" 29 #include "shutdown_callback_holer.h" 30 31 namespace OHOS { 32 namespace PowerMgr { 33 class ShutdownController { 34 public: 35 ShutdownController(); 36 virtual ~ShutdownController() = default; 37 38 virtual void Reboot(const std::string& reason); 39 virtual void Shutdown(const std::string& reason); 40 bool IsShuttingDown(); EnableMock(IDevicePowerAction * mockPowerAction,IDeviceStateAction * mockStateAction)41 void EnableMock(IDevicePowerAction* mockPowerAction, IDeviceStateAction* mockStateAction) 42 { 43 std::unique_ptr<IDevicePowerAction> mockPower(mockPowerAction); 44 devicePowerAction_ = std::move(mockPower); 45 std::unique_ptr<IDeviceStateAction> mockState(mockStateAction); 46 deviceStateAction_ = std::move(mockState); 47 started_ = false; 48 } 49 50 void AddCallback(const sptr<ITakeOverShutdownCallback>& callback, ShutdownPriority priority); 51 void AddCallback(const sptr<IAsyncShutdownCallback>& callback, ShutdownPriority priority); 52 void AddCallback(const sptr<ISyncShutdownCallback>& callback, ShutdownPriority priority); 53 void RemoveCallback(const sptr<ITakeOverShutdownCallback>& callback); 54 void RemoveCallback(const sptr<IAsyncShutdownCallback>& callback); 55 void RemoveCallback(const sptr<ISyncShutdownCallback>& callback); 56 57 bool TriggerTakeOverShutdownCallback(const TakeOverInfo& info); 58 bool TriggerTakeOverHibernateCallback(const TakeOverInfo& info); 59 void TriggerAsyncShutdownCallback(bool isReboot); 60 void TriggerSyncShutdownCallback(bool isReboot); 61 62 private: 63 using IntentWant = OHOS::AAFwk::Want; 64 void RebootOrShutdown(const std::string& reason, bool isReboot); 65 void Prepare(bool isReboot); 66 void TurnOffScreen(); 67 void PublishShutdownEvent() const; 68 bool TakeOverShutdownAction(const std::string& reason, bool isReboot); 69 bool AllowedToBeTakenOver(const std::string& reason) const; 70 71 bool TriggerTakeOverShutdownCallbackInner( 72 std::set<sptr<IRemoteObject>>& callbacks, const TakeOverInfo& info); 73 bool TriggerTakeOverHibernateCallbackInner( 74 std::set<sptr<IRemoteObject>>& callbacks, const TakeOverInfo& info); 75 void TriggerAsyncShutdownCallbackInner(std::set<sptr<IRemoteObject>>& callbacks, bool isReboot); 76 void TriggerSyncShutdownCallbackInner(std::set<sptr<IRemoteObject>>& callbacks, bool isReboot); 77 78 sptr<ShutdownCallbackHolder> takeoverShutdownCallbackHolder_; 79 sptr<ShutdownCallbackHolder> asyncShutdownCallbackHolder_; 80 sptr<ShutdownCallbackHolder> syncShutdownCallbackHolder_; 81 82 std::atomic<bool> started_; 83 std::unique_ptr<IDevicePowerAction> devicePowerAction_; 84 std::shared_ptr<IDeviceStateAction> deviceStateAction_; 85 }; 86 } // namespace PowerMgr 87 } // namespace OHOS 88 89 #endif // POWERMGR_POWER_MANAGER_SHUTDOWN_CONTROLLER_H 90