1 /* 2 * Copyright (c) 2021 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 <ohos/aafwk/content/want.h> 24 25 #include "device_power_action.h" 26 #include "ishutdown_callback.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 * mockAction)39 void EnableMock(IDevicePowerAction* mockAction) 40 { 41 std::unique_ptr<IDevicePowerAction> mock(mockAction); 42 devicePowerAction_ = std::move(mock); 43 started_ = false; 44 } 45 private: 46 using IntentWant = OHOS::AAFwk::Want; 47 class CallbackManager : public IRemoteObject::DeathRecipient { 48 public: 49 void OnRemoteDied(const wptr<IRemoteObject>& remote) override; 50 void AddCallback(const sptr<IShutdownCallback>& callback); 51 void RemoveCallback(const sptr<IShutdownCallback>& callback); 52 void WaitingCallback(); 53 54 private: 55 std::mutex mutex_; 56 std::set<sptr<IRemoteObject>> callbacks_; 57 }; 58 void RebootOrShutdown(const std::string& reason, bool isReboot); 59 void Prepare(); 60 void PublishShutdownEvent() const; 61 62 sptr<CallbackManager> lowCallbackMgr_; 63 sptr<CallbackManager> defaultCallbackMgr_; 64 sptr<CallbackManager> highCallbackMgr_; 65 std::atomic<bool> started_; 66 std::unique_ptr<IDevicePowerAction> devicePowerAction_; 67 }; 68 } // namespace PowerMgr 69 } // namespace OHOS 70 #endif // SHUTDOWN_REBOOT_THREAD_H 71