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 #include "shutdown_callback_holer.h"
17
18 #include "power_common.h"
19
20 namespace OHOS {
21 namespace PowerMgr {
OnRemoteDied(const wptr<IRemoteObject> & object)22 void ShutdownCallbackHolder::OnRemoteDied(const wptr<IRemoteObject>& object)
23 {
24 RETURN_IF((object == nullptr) || (object.promote() == nullptr))
25 POWER_HILOGW(FEATURE_SHUTDOWN, "object dead, need remove the callback");
26 RemoveCallback(object.promote());
27 }
28
AddCallback(const sptr<IRemoteObject> & callback,ShutdownPriority priority)29 void ShutdownCallbackHolder::AddCallback(const sptr<IRemoteObject>& callback, ShutdownPriority priority)
30 {
31 std::unique_lock<std::mutex> lock(mutex_);
32 switch (priority) {
33 case ShutdownPriority::LOW: {
34 auto iter = lowPriorityCallbacks_.insert(callback);
35 if (iter.second) {
36 callback->AddDeathRecipient(this);
37 }
38 break;
39 }
40 case ShutdownPriority::DEFAULT: {
41 auto iter = defaultPriorityCallbacks_.insert(callback);
42 if (iter.second) {
43 callback->AddDeathRecipient(this);
44 }
45 break;
46 }
47 case ShutdownPriority::HIGH: {
48 auto iter = highPriorityCallbacks_.insert(callback);
49 if (iter.second) {
50 callback->AddDeathRecipient(this);
51 }
52 break;
53 }
54 default: {
55 break;
56 }
57 }
58 }
59
GetHighPriorityCallbacks()60 std::set<sptr<IRemoteObject>> ShutdownCallbackHolder::GetHighPriorityCallbacks()
61 {
62 std::unique_lock<std::mutex> lock(mutex_);
63 return highPriorityCallbacks_;
64 }
65
GetDefaultPriorityCallbacks()66 std::set<sptr<IRemoteObject>> ShutdownCallbackHolder::GetDefaultPriorityCallbacks()
67 {
68 std::unique_lock<std::mutex> lock(mutex_);
69 return defaultPriorityCallbacks_;
70 }
71
GetLowPriorityCallbacks()72 std::set<sptr<IRemoteObject>> ShutdownCallbackHolder::GetLowPriorityCallbacks()
73 {
74 std::unique_lock<std::mutex> lock(mutex_);
75 return lowPriorityCallbacks_;
76 }
77
RemoveCallback(const sptr<IRemoteObject> & callback)78 void ShutdownCallbackHolder::RemoveCallback(const sptr<IRemoteObject>& callback)
79 {
80 std::unique_lock<std::mutex> lock(mutex_);
81 RemoveCallback(lowPriorityCallbacks_, callback);
82 RemoveCallback(defaultPriorityCallbacks_, callback);
83 RemoveCallback(highPriorityCallbacks_, callback);
84 }
85
RemoveCallback(std::set<sptr<IRemoteObject>> & callbacks,const sptr<IRemoteObject> & callback)86 void ShutdownCallbackHolder::RemoveCallback(
87 std::set<sptr<IRemoteObject>>& callbacks, const sptr<IRemoteObject>& callback)
88 {
89 auto iter = callbacks.find(callback);
90 if (iter == callbacks.end()) {
91 return;
92 }
93 callbacks.erase(iter);
94 }
95
96 } // namespace PowerMgr
97 } // namespace OHOS
98