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 callback->AddDeathRecipient(callbackDeathRecipient_);
79
80 CallbackRecord recordInstance;
81 recordInstance.callbackObject_ = callback;
82 recordInstance.scopePtr_ = callbackScopePtr;
83
84 callbackInfoList_.emplace_back(recordInstance);
85
86 ACCESSTOKEN_LOG_INFO(LABEL, "RecordInstance is added");
87 return RET_SUCCESS;
88 }
89
RemoveCallback(const sptr<IRemoteObject> & callback)90 int32_t CallbackManager::RemoveCallback(const sptr<IRemoteObject>& callback)
91 {
92 if (callback == nullptr) {
93 ACCESSTOKEN_LOG_ERROR(LABEL, "Callback is nullptr.");
94 return AccessTokenError::ERR_PARAM_INVALID;
95 }
96
97 #ifdef RESOURCESCHEDULE_FFRT_ENABLE
98 std::lock_guard<ffrt::mutex> lock(mutex_);
99 #else
100 std::lock_guard<std::mutex> lock(mutex_);
101 #endif
102
103 for (auto it = callbackInfoList_.begin(); it != callbackInfoList_.end(); ++it) {
104 if (callback == (*it).callbackObject_) {
105 ACCESSTOKEN_LOG_INFO(LABEL, "Find callback");
106 if (callbackDeathRecipient_ != nullptr) {
107 callback->RemoveDeathRecipient(callbackDeathRecipient_);
108 }
109 (*it).callbackObject_ = nullptr;
110 callbackInfoList_.erase(it);
111 break;
112 }
113 }
114 ACCESSTOKEN_LOG_INFO(LABEL, "CallbackInfoList_ %{public}u", (uint32_t)callbackInfoList_.size());
115 return RET_SUCCESS;
116 }
117
CalledAccordingToTokenIdLlist(const std::vector<AccessTokenID> & tokenIDList,AccessTokenID tokenID)118 bool CallbackManager::CalledAccordingToTokenIdLlist(
119 const std::vector<AccessTokenID>& tokenIDList, AccessTokenID tokenID)
120 {
121 if (tokenIDList.empty()) {
122 return true;
123 }
124 return std::any_of(tokenIDList.begin(), tokenIDList.end(),
125 [tokenID](AccessTokenID id) { return id == tokenID; });
126 }
127
CalledAccordingToPermLlist(const std::vector<std::string> & permList,const std::string & permName)128 bool CallbackManager::CalledAccordingToPermLlist(const std::vector<std::string>& permList, const std::string& permName)
129 {
130 if (permList.empty()) {
131 return true;
132 }
133 return std::any_of(permList.begin(), permList.end(),
134 [permName](const std::string& perm) { return perm == permName; });
135 }
136
ExcuteAllCallback(std::vector<sptr<IRemoteObject>> & list,AccessTokenID tokenID,const std::string & permName,int32_t changeType)137 void CallbackManager::ExcuteAllCallback(std::vector<sptr<IRemoteObject>>& list, AccessTokenID tokenID,
138 const std::string& permName, int32_t changeType)
139 {
140 for (auto it = list.begin(); it != list.end(); ++it) {
141 #ifdef RESOURCESCHEDULE_FFRT_ENABLE
142 auto callbackSingle = [it, tokenID, permName, changeType]() {
143 sptr<IPermissionStateCallback> callback = new PermissionStateChangeCallbackProxy(*it);
144 if (callback != nullptr) {
145 ACCESSTOKEN_LOG_INFO(LABEL, "Callback execute");
146 PermStateChangeInfo resInfo;
147 resInfo.permStateChangeType = changeType;
148 resInfo.permissionName = permName;
149 resInfo.tokenID = tokenID;
150 callback->PermStateChangeCallback(resInfo);
151 ACCESSTOKEN_LOG_INFO(LABEL, "Callback execute end");
152 }
153 };
154 ffrt::submit(callbackSingle, {}, {}, ffrt::task_attr().qos(ffrt::qos_default));
155 #else
156 sptr<IPermissionStateCallback> callback = new PermissionStateChangeCallbackProxy(*it);
157 if (callback != nullptr) {
158 ACCESSTOKEN_LOG_INFO(LABEL, "Callback execute");
159 PermStateChangeInfo resInfo;
160 resInfo.permStateChangeType = changeType;
161 resInfo.permissionName = permName;
162 resInfo.tokenID = tokenID;
163 callback->PermStateChangeCallback(resInfo);
164 }
165 #endif
166 }
167 #ifdef RESOURCESCHEDULE_FFRT_ENABLE
168 ffrt::wait();
169 #endif
170 }
171
GetCallbackObjectList(AccessTokenID tokenID,const std::string & permName,std::vector<sptr<IRemoteObject>> & list)172 void CallbackManager::GetCallbackObjectList(AccessTokenID tokenID, const std::string& permName,
173 std::vector<sptr<IRemoteObject>>& list)
174 {
175 #ifdef RESOURCESCHEDULE_FFRT_ENABLE
176 std::lock_guard<ffrt::mutex> lock(mutex_);
177 #else
178 std::lock_guard<std::mutex> lock(mutex_);
179 #endif
180 for (auto it = callbackInfoList_.begin(); it != callbackInfoList_.end(); ++it) {
181 std::shared_ptr<PermStateChangeScope> scopePtr = (*it).scopePtr_;
182 if (scopePtr == nullptr) {
183 ACCESSTOKEN_LOG_ERROR(LABEL, "ScopePtr is nullptr");
184 continue;
185 }
186 if (!CalledAccordingToTokenIdLlist(scopePtr->tokenIDs, tokenID) ||
187 !CalledAccordingToPermLlist(scopePtr->permList, permName)) {
188 ACCESSTOKEN_LOG_DEBUG(LABEL,
189 "tokenID is %{public}u, permName is %{public}s", tokenID, permName.c_str());
190 continue;
191 }
192 list.emplace_back((*it).callbackObject_);
193 }
194 }
195
ExecuteCallbackAsync(AccessTokenID tokenID,const std::string & permName,int32_t changeType)196 void CallbackManager::ExecuteCallbackAsync(AccessTokenID tokenID, const std::string& permName, int32_t changeType)
197 {
198 ACCESSTOKEN_LOG_INFO(LABEL, "Entry");
199 auto callbackStart = [this, tokenID, permName, changeType]() {
200 ACCESSTOKEN_LOG_INFO(LABEL, "CallbackStart");
201 #ifndef RESOURCESCHEDULE_FFRT_ENABLE
202 std::string name = "AtmCallback";
203 pthread_setname_np(pthread_self(), name.substr(0, MAX_PTHREAD_NAME_LEN).c_str());
204 #endif
205 std::vector<sptr<IRemoteObject>> list;
206 this->GetCallbackObjectList(tokenID, permName, list);
207 this->ExcuteAllCallback(list, tokenID, permName, changeType);
208 };
209
210 #ifdef RESOURCESCHEDULE_FFRT_ENABLE
211 std::string taskName = "AtmCallback";
212 ffrt::submit_h(callbackStart, {}, {},
213 ffrt::task_attr().qos(ffrt::qos_default).name(taskName.c_str()));
214 #else
215 std::packaged_task<void()> callbackTask(callbackStart);
216 std::make_unique<std::thread>(std::move(callbackTask))->detach();
217 #endif
218 ACCESSTOKEN_LOG_DEBUG(LABEL, "The callback execution is complete");
219 }
220 } // namespace AccessToken
221 } // namespace Security
222 } // namespace OHOS
223