1 /* 2 * Copyright (c) 2021-2022 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 SHUTDOWN_REBOOT_THREAD_H 17 #define SHUTDOWN_REBOOT_THREAD_H 18 19 #include <mutex> 20 #include <set> 21 #include <string> 22 23 #include "device_power_action.h" 24 #include "actions/idevice_state_action.h" 25 #include "ishutdown_callback.h" 26 #include "want.h" 27 28 namespace OHOS { 29 namespace PowerMgr { 30 class ShutdownService { 31 public: 32 ShutdownService(); 33 ~ShutdownService() = default; 34 void Reboot(const std::string& reason); 35 void Shutdown(const std::string& reason); 36 void AddShutdownCallback(IShutdownCallback::ShutdownPriority priority, const sptr<IShutdownCallback>& callback); 37 void DelShutdownCallback(const sptr<IShutdownCallback>& callback); 38 bool IsShuttingDown(); EnableMock(IDevicePowerAction * mockPowerAction,IDeviceStateAction * mockStateAction)39 void EnableMock(IDevicePowerAction* mockPowerAction, IDeviceStateAction* mockStateAction) 40 { 41 std::unique_ptr<IDevicePowerAction> mockPower(mockPowerAction); 42 devicePowerAction_ = std::move(mockPower); 43 std::unique_ptr<IDeviceStateAction> mockState(mockStateAction); 44 deviceStateAction_ = std::move(mockState); 45 started_ = false; 46 } 47 private: 48 using IntentWant = OHOS::AAFwk::Want; 49 class CallbackManager : public IRemoteObject::DeathRecipient { 50 public: 51 void OnRemoteDied(const wptr<IRemoteObject>& remote) override; 52 void AddCallback(const sptr<IShutdownCallback>& callback); 53 void RemoveCallback(const sptr<IShutdownCallback>& callback); 54 void WaitingCallback(); 55 56 private: 57 std::mutex mutex_; 58 std::set<sptr<IRemoteObject>> callbacks_; 59 }; 60 void RebootOrShutdown(const std::string& reason, bool isReboot); 61 void Prepare(); 62 void TurnOffScreen(); 63 void PublishShutdownEvent() const; 64 65 sptr<CallbackManager> lowCallbackMgr_; 66 sptr<CallbackManager> defaultCallbackMgr_; 67 sptr<CallbackManager> highCallbackMgr_; 68 std::atomic<bool> started_; 69 std::unique_ptr<IDevicePowerAction> devicePowerAction_; 70 std::unique_ptr<IDeviceStateAction> deviceStateAction_; 71 }; 72 } // namespace PowerMgr 73 } // namespace OHOS 74 #endif // SHUTDOWN_REBOOT_THREAD_H 75