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_callback_holer.h" 29 30 namespace OHOS { 31 namespace PowerMgr { 32 class ShutdownController { 33 public: 34 ShutdownController(); 35 ~ShutdownController() = default; 36 37 void Reboot(const std::string& reason); 38 void Shutdown(const std::string& reason); 39 bool IsShuttingDown(); EnableMock(IDevicePowerAction * mockPowerAction,IDeviceStateAction * mockStateAction)40 void EnableMock(IDevicePowerAction* mockPowerAction, IDeviceStateAction* mockStateAction) 41 { 42 std::unique_ptr<IDevicePowerAction> mockPower(mockPowerAction); 43 devicePowerAction_ = std::move(mockPower); 44 std::unique_ptr<IDeviceStateAction> mockState(mockStateAction); 45 deviceStateAction_ = std::move(mockState); 46 started_ = false; 47 } 48 49 void AddCallback(const sptr<ITakeOverShutdownCallback>& callback, ShutdownPriority priority); 50 void AddCallback(const sptr<IAsyncShutdownCallback>& callback, ShutdownPriority priority); 51 void AddCallback(const sptr<ISyncShutdownCallback>& callback, ShutdownPriority priority); 52 void RemoveCallback(const sptr<ITakeOverShutdownCallback>& callback); 53 void RemoveCallback(const sptr<IAsyncShutdownCallback>& callback); 54 void RemoveCallback(const sptr<ISyncShutdownCallback>& callback); 55 56 bool TriggerTakeOverShutdownCallback(bool isReboot); 57 void TriggerAsyncShutdownCallback(); 58 void TriggerSyncShutdownCallback(); 59 60 private: 61 using IntentWant = OHOS::AAFwk::Want; 62 void RebootOrShutdown(const std::string& reason, bool isReboot); 63 void Prepare(); 64 void TurnOffScreen(); 65 void PublishShutdownEvent() const; 66 67 static bool TriggerTakeOverShutdownCallbackInner(std::set<sptr<IRemoteObject>>& callbacks, bool isReboot); 68 static void TriggerAsyncShutdownCallbackInner(std::set<sptr<IRemoteObject>>& callbacks); 69 static void TriggerSyncShutdownCallbackInner(std::set<sptr<IRemoteObject>>& callbacks); 70 71 sptr<ShutdownCallbackHolder> takeoverShutdownCallbackHolder_; 72 sptr<ShutdownCallbackHolder> asyncShutdownCallbackHolder_; 73 sptr<ShutdownCallbackHolder> syncShutdownCallbackHolder_; 74 75 std::atomic<bool> started_; 76 std::unique_ptr<IDevicePowerAction> devicePowerAction_; 77 std::shared_ptr<IDeviceStateAction> deviceStateAction_; 78 }; 79 } // namespace PowerMgr 80 } // namespace OHOS 81 82 #endif // POWERMGR_POWER_MANAGER_SHUTDOWN_CONTROLLER_H 83