• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SLEEP_CALLBACK_HOLER_H
17 #define POWERMGR_POWER_MANAGER_SLEEP_CALLBACK_HOLER_H
18 
19 #include <set>
20 #include <map>
21 
22 #include "ipc_skeleton.h"
23 #include <singleton.h>
24 #include "iremote_object.h"
25 #include "suspend/isync_sleep_callback.h"
26 
27 namespace OHOS {
28 namespace PowerMgr {
29 class SleepCallbackHolder final : public DelayedRefSingleton<SleepCallbackHolder> {
30     DECLARE_DELAYED_REF_SINGLETON(SleepCallbackHolder);
31     DISALLOW_COPY_AND_MOVE(SleepCallbackHolder);
32 public:
33     struct SleepCallbackCompare {
operatorSleepCallbackCompare34         bool operator()(const sptr<ISyncSleepCallback>& lhs, const sptr<ISyncSleepCallback>& rhs) const
35         {
36             return lhs->AsObject() < rhs->AsObject();
37         }
38     };
39 
40     using SleepCallbackContainerType = std::set<sptr<ISyncSleepCallback>, SleepCallbackCompare>;
41     using SleepCallbackCachedRegister =
42         std::map<sptr<ISyncSleepCallback>, std::pair<int32_t, int32_t>, SleepCallbackCompare>;
43     void AddCallback(const sptr<ISyncSleepCallback>& callback, SleepPriority priority);
44     void RemoveCallback(const sptr<ISyncSleepCallback>& callback);
45     SleepCallbackContainerType GetHighPriorityCallbacks();
46     SleepCallbackContainerType GetDefaultPriorityCallbacks();
47     SleepCallbackContainerType GetLowPriorityCallbacks();
48     std::pair<int32_t, int32_t> FindCallbackPidUid(const sptr<ISyncSleepCallback>& callback);
49 
50 private:
51     static void RemoveCallback(SleepCallbackContainerType& callbacks, const sptr<ISyncSleepCallback>& callback);
52     void AddCallbackPidUid(const sptr<ISyncSleepCallback>& callback);
53     void RemoveCallbackPidUid(const sptr<ISyncSleepCallback>& callback);
54 
55     std::mutex mutex_;
56     SleepCallbackContainerType highPriorityCallbacks_;
57     SleepCallbackContainerType defaultPriorityCallbacks_;
58     SleepCallbackContainerType lowPriorityCallbacks_;
59     SleepCallbackCachedRegister cachedRegister_;
60 };
61 } // namespace PowerMgr
62 } // namespace OHOS
63 
64 #endif // POWERMGR_POWER_MANAGER_SLEEP_CALLBACK_HOLER_H
65