1 /* 2 * Copyright (c) 2025 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 #ifndef POWERMGR_POWER_MANAGER_TAKEOVER_CALLBACK_HOLDER_H 16 #define POWERMGR_POWER_MANAGER_TAKEOVER_CALLBACK_HOLDER_H 17 18 #include <set> 19 #include <map> 20 #include <singleton.h> 21 #include "ipc_skeleton.h" 22 #include "iremote_object.h" 23 #include "suspend/itake_over_suspend_callback.h" 24 25 namespace OHOS { 26 namespace PowerMgr { 27 class TakeOverSuspendCallbackHolder final : public DelayedRefSingleton<TakeOverSuspendCallbackHolder> { 28 DECLARE_DELAYED_REF_SINGLETON(TakeOverSuspendCallbackHolder); 29 DISALLOW_COPY_AND_MOVE(TakeOverSuspendCallbackHolder); 30 public: 31 struct TakeoverSuspendCallbackCompare { operatorTakeoverSuspendCallbackCompare32 bool operator()(const sptr<ITakeOverSuspendCallback>& lhs, const sptr<ITakeOverSuspendCallback>& rhs) const 33 { 34 return lhs->AsObject() < rhs->AsObject(); 35 } 36 }; 37 38 using TakeoverSuspendCallbackContainerType = std::set<sptr<ITakeOverSuspendCallback>, 39 TakeoverSuspendCallbackCompare>; 40 using TakeoverSuspendCallbackCachedRegister = 41 std::map<sptr<ITakeOverSuspendCallback>, std::pair<int32_t, int32_t>, TakeoverSuspendCallbackCompare>; 42 void AddCallback(const sptr<ITakeOverSuspendCallback>& callback, TakeOverSuspendPriority priority); 43 void RemoveCallback(const sptr<ITakeOverSuspendCallback>& callback); 44 TakeoverSuspendCallbackContainerType GetHighPriorityCallbacks(); 45 TakeoverSuspendCallbackContainerType GetDefaultPriorityCallbacks(); 46 TakeoverSuspendCallbackContainerType GetLowPriorityCallbacks(); 47 std::pair<int32_t, int32_t> FindCallbackPidUid(const sptr<ITakeOverSuspendCallback>& callback); 48 private: 49 static void RemoveCallback( 50 TakeoverSuspendCallbackContainerType& callbacks, const sptr<ITakeOverSuspendCallback>& callback); 51 void AddCallbackPidUid(const sptr<ITakeOverSuspendCallback>& callback); 52 void RemoveCallbackPidUid(const sptr<ITakeOverSuspendCallback>& callback); 53 54 std::mutex mutex_; 55 TakeoverSuspendCallbackContainerType highPriorityCallbacks_; 56 TakeoverSuspendCallbackContainerType defaultPriorityCallbacks_; 57 TakeoverSuspendCallbackContainerType lowPriorityCallbacks_; 58 TakeoverSuspendCallbackCachedRegister cachedRegister_; 59 }; 60 } // namespace PowerMgr 61 } // namespace OHOS 62 #endif // POWERMGR_POWER_MANAGER_TAKEOVER_CALLBACK_HOLDER_H