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 AddCallbackPidUid(callback);
59 }
60
AddCallbackPidUid(const sptr<IRemoteObject> & callback)61 void ShutdownCallbackHolder::AddCallbackPidUid(const sptr<IRemoteObject>& callback)
62 {
63 pid_t pid = IPCSkeleton::GetCallingPid();
64 auto uid = IPCSkeleton::GetCallingUid();
65 cachedRegister_.emplace(callback, std::make_pair(pid, uid));
66 }
67
GetHighPriorityCallbacks()68 std::set<sptr<IRemoteObject>> ShutdownCallbackHolder::GetHighPriorityCallbacks()
69 {
70 std::unique_lock<std::mutex> lock(mutex_);
71 return highPriorityCallbacks_;
72 }
73
GetDefaultPriorityCallbacks()74 std::set<sptr<IRemoteObject>> ShutdownCallbackHolder::GetDefaultPriorityCallbacks()
75 {
76 std::unique_lock<std::mutex> lock(mutex_);
77 return defaultPriorityCallbacks_;
78 }
79
GetLowPriorityCallbacks()80 std::set<sptr<IRemoteObject>> ShutdownCallbackHolder::GetLowPriorityCallbacks()
81 {
82 std::unique_lock<std::mutex> lock(mutex_);
83 return lowPriorityCallbacks_;
84 }
85
RemoveCallback(const sptr<IRemoteObject> & callback)86 void ShutdownCallbackHolder::RemoveCallback(const sptr<IRemoteObject>& callback)
87 {
88 std::unique_lock<std::mutex> lock(mutex_);
89 RemoveCallback(lowPriorityCallbacks_, callback);
90 RemoveCallback(defaultPriorityCallbacks_, callback);
91 RemoveCallback(highPriorityCallbacks_, callback);
92 RemoveCallbackPidUid(callback);
93 }
94
RemoveCallback(std::set<sptr<IRemoteObject>> & callbacks,const sptr<IRemoteObject> & callback)95 void ShutdownCallbackHolder::RemoveCallback(
96 std::set<sptr<IRemoteObject>>& callbacks, const sptr<IRemoteObject>& callback)
97 {
98 auto iter = callbacks.find(callback);
99 if (iter == callbacks.end()) {
100 return;
101 }
102 callbacks.erase(iter);
103 }
104
RemoveCallbackPidUid(const sptr<IRemoteObject> & callback)105 void ShutdownCallbackHolder::RemoveCallbackPidUid(const sptr<IRemoteObject>& callback)
106 {
107 auto iter = cachedRegister_.find(callback);
108 if (iter == cachedRegister_.end()) {
109 return;
110 }
111 cachedRegister_.erase(iter);
112 }
113
FindCallbackPidUid(const sptr<IRemoteObject> & callback)114 std::pair<int32_t, int32_t> ShutdownCallbackHolder::FindCallbackPidUid(const sptr<IRemoteObject>& callback)
115 {
116 std::unique_lock<std::mutex> lock(mutex_);
117 auto iter = cachedRegister_.find(callback);
118 if (iter != cachedRegister_.end()) {
119 return iter->second;
120 } else {
121 return std::make_pair(0, 0);
122 }
123 }
124
125 } // namespace PowerMgr
126 } // namespace OHOS
127