1 /*
2 * Copyright (c) 2022-2024 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 "callback_manager.h"
17
18 #include <future>
19 #include <thread>
20 #include <datetime_ex.h>
21 #include <pthread.h>
22
23 #include "access_token.h"
24 #include "access_token_error.h"
25 #include "callback_death_recipients.h"
26
27
28 namespace OHOS {
29 namespace Security {
30 namespace AccessToken {
31 namespace {
32 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "CallbackManager"};
33 static const uint32_t MAX_CALLBACK_SIZE = 1024;
34 #ifndef RESOURCESCHEDULE_FFRT_ENABLE
35 static const int MAX_PTHREAD_NAME_LEN = 15; // pthread name max length
36 #endif
37 std::recursive_mutex g_instanceMutex;
38 }
39
GetInstance()40 CallbackManager& CallbackManager::GetInstance()
41 {
42 static CallbackManager* instance = nullptr;
43 if (instance == nullptr) {
44 std::lock_guard<std::recursive_mutex> lock(g_instanceMutex);
45 if (instance == nullptr) {
46 instance = new CallbackManager();
47 }
48 }
49 return *instance;
50 }
51
CallbackManager()52 CallbackManager::CallbackManager() : callbackDeathRecipient_(sptr<IRemoteObject::DeathRecipient>(
53 new (std::nothrow) PermStateCallbackDeathRecipient()))
54 {
55 }
56
~CallbackManager()57 CallbackManager::~CallbackManager()
58 {
59 }
60
AddCallback(const PermStateChangeScope& scopeRes, const sptr<IRemoteObject>& callback)61 int32_t CallbackManager::AddCallback(const PermStateChangeScope& scopeRes, const sptr<IRemoteObject>& callback)
62 {
63 if (callback == nullptr) {
64 ACCESSTOKEN_LOG_ERROR(LABEL, "Input is nullptr");
65 return AccessTokenError::ERR_PARAM_INVALID;
66 }
67 auto callbackScopePtr = std::make_shared<PermStateChangeScope>(scopeRes);
68
69 #ifdef RESOURCESCHEDULE_FFRT_ENABLE
70 std::lock_guard<ffrt::mutex> lock(mutex_);
71 #else
72 std::lock_guard<std::mutex> lock(mutex_);
73 #endif
74 if (callbackInfoList_.size() >= MAX_CALLBACK_SIZE) {
75 ACCESSTOKEN_LOG_ERROR(LABEL, "Callback size has reached limitation");
76 return AccessTokenError::ERR_CALLBACKS_EXCEED_LIMITATION;
77 }
78 int32_t ret = callback->AddDeathRecipient(callbackDeathRecipient_);
79 if (ret != ERR_NONE) {
80 ACCESSTOKEN_LOG_ERROR(LABEL, "add death recipient failed ret is %{public}d", ret);
81 }
82
83 CallbackRecord recordInstance;
84 recordInstance.callbackObject_ = callback;
85 recordInstance.scopePtr_ = callbackScopePtr;
86
87 callbackInfoList_.emplace_back(recordInstance);
88
89 ACCESSTOKEN_LOG_INFO(LABEL, "RecordInstance is added");
90 return RET_SUCCESS;
91 }
92
RemoveCallback(const sptr<IRemoteObject>& callback)93 int32_t CallbackManager::RemoveCallback(const sptr<IRemoteObject>& callback)
94 {
95 if (callback == nullptr) {
96 ACCESSTOKEN_LOG_ERROR(LABEL, "Callback is nullptr.");
97 return AccessTokenError::ERR_PARAM_INVALID;
98 }
99
100 #ifdef RESOURCESCHEDULE_FFRT_ENABLE
101 std::lock_guard<ffrt::mutex> lock(mutex_);
102 #else
103 std::lock_guard<std::mutex> lock(mutex_);
104 #endif
105
106 for (auto it = callbackInfoList_.begin(); it != callbackInfoList_.end(); ++it) {
107 if (callback == (*it).callbackObject_) {
108 ACCESSTOKEN_LOG_INFO(LABEL, "Find callback");
109 if (callbackDeathRecipient_ != nullptr) {
110 callback->RemoveDeathRecipient(callbackDeathRecipient_);
111 }
112 (*it).callbackObject_ = nullptr;
113 callbackInfoList_.erase(it);
114 break;
115 }
116 }
117 ACCESSTOKEN_LOG_INFO(LABEL, "CallbackInfoList_ %{public}u", (uint32_t)callbackInfoList_.size());
118 return RET_SUCCESS;
119 }
120
CalledAccordingToTokenIdLlist( const std::vector<AccessTokenID>& tokenIDList, AccessTokenID tokenID)121 bool CallbackManager::CalledAccordingToTokenIdLlist(
122 const std::vector<AccessTokenID>& tokenIDList, AccessTokenID tokenID)
123 {
124 if (tokenIDList.empty()) {
125 return true;
126 }
127 return std::any_of(tokenIDList.begin(), tokenIDList.end(),
128 [tokenID](AccessTokenID id) { return id == tokenID; });
129 }
130
CalledAccordingToPermLlist(const std::vector<std::string>& permList, const std::string& permName)131 bool CallbackManager::CalledAccordingToPermLlist(const std::vector<std::string>& permList, const std::string& permName)
132 {
133 if (permList.empty()) {
134 return true;
135 }
136 return std::any_of(permList.begin(), permList.end(),
137 [permName](const std::string& perm) { return perm == permName; });
138 }
139
ExecuteAllCallback(std::vector<sptr<IRemoteObject>>& list, AccessTokenID tokenID, const std::string& permName, int32_t changeType)140 void CallbackManager::ExecuteAllCallback(std::vector<sptr<IRemoteObject>>& list, AccessTokenID tokenID,
141 const std::string& permName, int32_t changeType)
142 {
143 for (auto it = list.begin(); it != list.end(); ++it) {
144 #ifdef RESOURCESCHEDULE_FFRT_ENABLE
145 auto callbackSingle = [it, tokenID, permName, changeType]() {
146 sptr<IPermissionStateCallback> callback = new PermissionStateChangeCallbackProxy(*it);
147 if (callback != nullptr) {
148 ACCESSTOKEN_LOG_INFO(LABEL, "Callback execute");
149 PermStateChangeInfo resInfo;
150 resInfo.permStateChangeType = changeType;
151 resInfo.permissionName = permName;
152 resInfo.tokenID = tokenID;
153 callback->PermStateChangeCallback(resInfo);
154 ACCESSTOKEN_LOG_INFO(LABEL, "Callback execute end");
155 }
156 };
157 ffrt::submit(callbackSingle, {}, {}, ffrt::task_attr().qos(ffrt::qos_default));
158 #else
159 sptr<IPermissionStateCallback> callback = new PermissionStateChangeCallbackProxy(*it);
160 if (callback != nullptr) {
161 ACCESSTOKEN_LOG_INFO(LABEL, "Callback execute");
162 PermStateChangeInfo resInfo;
163 resInfo.permStateChangeType = changeType;
164 resInfo.permissionName = permName;
165 resInfo.tokenID = tokenID;
166 callback->PermStateChangeCallback(resInfo);
167 }
168 #endif
169 }
170 #ifdef RESOURCESCHEDULE_FFRT_ENABLE
171 ffrt::wait();
172 #endif
173 }
174
GetCallbackObjectList(AccessTokenID tokenID, const std::string& permName, std::vector<sptr<IRemoteObject>>& list)175 void CallbackManager::GetCallbackObjectList(AccessTokenID tokenID, const std::string& permName,
176 std::vector<sptr<IRemoteObject>>& list)
177 {
178 #ifdef RESOURCESCHEDULE_FFRT_ENABLE
179 std::lock_guard<ffrt::mutex> lock(mutex_);
180 #else
181 std::lock_guard<std::mutex> lock(mutex_);
182 #endif
183 for (auto it = callbackInfoList_.begin(); it != callbackInfoList_.end(); ++it) {
184 std::shared_ptr<PermStateChangeScope> scopePtr = (*it).scopePtr_;
185 if (scopePtr == nullptr) {
186 ACCESSTOKEN_LOG_ERROR(LABEL, "ScopePtr is nullptr");
187 continue;
188 }
189 if (!CalledAccordingToTokenIdLlist(scopePtr->tokenIDs, tokenID) ||
190 !CalledAccordingToPermLlist(scopePtr->permList, permName)) {
191 ACCESSTOKEN_LOG_DEBUG(LABEL,
192 "tokenID is %{public}u, permName is %{public}s", tokenID, permName.c_str());
193 continue;
194 }
195 list.emplace_back((*it).callbackObject_);
196 }
197 }
198
ExecuteCallbackAsync(AccessTokenID tokenID, const std::string& permName, int32_t changeType)199 void CallbackManager::ExecuteCallbackAsync(AccessTokenID tokenID, const std::string& permName, int32_t changeType)
200 {
201 ACCESSTOKEN_LOG_INFO(LABEL, "Entry");
202 auto callbackStart = [this, tokenID, permName, changeType]() {
203 ACCESSTOKEN_LOG_INFO(LABEL, "CallbackStart");
204 #ifndef RESOURCESCHEDULE_FFRT_ENABLE
205 std::string name = "AtmCallback";
206 pthread_setname_np(pthread_self(), name.substr(0, MAX_PTHREAD_NAME_LEN).c_str());
207 #endif
208 std::vector<sptr<IRemoteObject>> list;
209 this->GetCallbackObjectList(tokenID, permName, list);
210 this->ExecuteAllCallback(list, tokenID, permName, changeType);
211 };
212
213 #ifdef RESOURCESCHEDULE_FFRT_ENABLE
214 std::string taskName = "AtmCallback";
215 ffrt::submit_h(callbackStart, {}, {},
216 ffrt::task_attr().qos(ffrt::qos_default).name(taskName.c_str()));
217 #else
218 std::packaged_task<void()> callbackTask(callbackStart);
219 std::make_unique<std::thread>(std::move(callbackTask))->detach();
220 #endif
221 ACCESSTOKEN_LOG_DEBUG(LABEL, "The callback execution is complete");
222 }
223 } // namespace AccessToken
224 } // namespace Security
225 } // namespace OHOS
226