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