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 static const int MAX_PTHREAD_NAME_LEN = 15; // pthread name max length
34 std::recursive_mutex g_instanceMutex;
35 }
36
GetInstance()37 CallbackManager& CallbackManager::GetInstance()
38 {
39 static CallbackManager* instance = nullptr;
40 if (instance == nullptr) {
41 std::lock_guard<std::recursive_mutex> lock(g_instanceMutex);
42 if (instance == nullptr) {
43 CallbackManager* tmp = new (std::nothrow) CallbackManager();
44 instance = std::move(tmp);
45 }
46 }
47 return *instance;
48 }
49
CallbackManager()50 CallbackManager::CallbackManager() : callbackDeathRecipient_(sptr<IRemoteObject::DeathRecipient>(
51 new (std::nothrow) PermStateCallbackDeathRecipient()))
52 {
53 }
54
~CallbackManager()55 CallbackManager::~CallbackManager()
56 {
57 }
58
AddCallback(const PermStateChangeScope & scopeRes,const sptr<IRemoteObject> & callback)59 int32_t CallbackManager::AddCallback(const PermStateChangeScope& scopeRes, const sptr<IRemoteObject>& callback)
60 {
61 if (callback == nullptr) {
62 LOGE(ATM_DOMAIN, ATM_TAG, "Input is nullptr");
63 return AccessTokenError::ERR_PARAM_INVALID;
64 }
65 auto callbackScopePtr = std::make_shared<PermStateChangeScope>(scopeRes);
66
67 std::lock_guard<std::mutex> lock(mutex_);
68 if (callbackInfoList_.size() >= MAX_CALLBACK_SIZE) {
69 LOGE(ATM_DOMAIN, ATM_TAG, "Callback size has reached limitation");
70 return AccessTokenError::ERR_CALLBACKS_EXCEED_LIMITATION;
71 }
72 if (callback->IsProxyObject() &&
73 ((callbackDeathRecipient_ == nullptr) || !callback->AddDeathRecipient(callbackDeathRecipient_))) {
74 LOGE(ATM_DOMAIN, ATM_TAG, "add death recipient failed");
75 return AccessTokenError::ERR_ADD_DEATH_RECIPIENT_FAILED;
76 }
77
78 CallbackRecord recordInstance;
79 recordInstance.callbackObject_ = callback;
80 recordInstance.scopePtr_ = callbackScopePtr;
81
82 callbackInfoList_.emplace_back(recordInstance);
83
84 return RET_SUCCESS;
85 }
86
RemoveCallback(const sptr<IRemoteObject> & callback)87 int32_t CallbackManager::RemoveCallback(const sptr<IRemoteObject>& callback)
88 {
89 if (callback == nullptr) {
90 LOGE(ATM_DOMAIN, ATM_TAG, "Callback is nullptr.");
91 return AccessTokenError::ERR_PARAM_INVALID;
92 }
93
94 std::lock_guard<std::mutex> lock(mutex_);
95
96 for (auto it = callbackInfoList_.begin(); it != callbackInfoList_.end(); ++it) {
97 if (callback == (*it).callbackObject_) {
98 LOGI(ATM_DOMAIN, ATM_TAG, "Find callback");
99 if (callbackDeathRecipient_ != nullptr) {
100 callback->RemoveDeathRecipient(callbackDeathRecipient_);
101 }
102 (*it).callbackObject_ = nullptr;
103 callbackInfoList_.erase(it);
104 break;
105 }
106 }
107 LOGI(ATM_DOMAIN, ATM_TAG, "CallbackInfoList_ %{public}u", (uint32_t)callbackInfoList_.size());
108 return RET_SUCCESS;
109 }
110
CalledAccordingToTokenIdLlist(const std::vector<AccessTokenID> & tokenIDList,AccessTokenID tokenID)111 bool CallbackManager::CalledAccordingToTokenIdLlist(
112 const std::vector<AccessTokenID>& tokenIDList, AccessTokenID tokenID)
113 {
114 if (tokenIDList.empty()) {
115 return true;
116 }
117 return std::any_of(tokenIDList.begin(), tokenIDList.end(),
118 [tokenID](AccessTokenID id) { return id == tokenID; });
119 }
120
CalledAccordingToPermLlist(const std::vector<std::string> & permList,const std::string & permName)121 bool CallbackManager::CalledAccordingToPermLlist(const std::vector<std::string>& permList, const std::string& permName)
122 {
123 if (permList.empty()) {
124 return true;
125 }
126 return std::any_of(permList.begin(), permList.end(),
127 [permName](const std::string& perm) { return perm == permName; });
128 }
129
ExecuteAllCallback(std::vector<sptr<IRemoteObject>> & list,AccessTokenID tokenID,const std::string & permName,int32_t changeType)130 void CallbackManager::ExecuteAllCallback(std::vector<sptr<IRemoteObject>>& list, AccessTokenID tokenID,
131 const std::string& permName, int32_t changeType)
132 {
133 for (auto it = list.begin(); it != list.end(); ++it) {
134 sptr<IPermissionStateCallback> callback = new (std::nothrow) PermissionStateChangeCallbackProxy(*it);
135 if (callback != nullptr) {
136 LOGI(ATM_DOMAIN, ATM_TAG, "Callback execute");
137 PermStateChangeInfo resInfo;
138 resInfo.permStateChangeType = changeType;
139 resInfo.permissionName = permName;
140 resInfo.tokenID = tokenID;
141 callback->PermStateChangeCallback(resInfo);
142 }
143 }
144 }
145
GetCallbackObjectList(AccessTokenID tokenID,const std::string & permName,std::vector<sptr<IRemoteObject>> & list)146 void CallbackManager::GetCallbackObjectList(AccessTokenID tokenID, const std::string& permName,
147 std::vector<sptr<IRemoteObject>>& list)
148 {
149 std::lock_guard<std::mutex> lock(mutex_);
150 for (auto it = callbackInfoList_.begin(); it != callbackInfoList_.end(); ++it) {
151 std::shared_ptr<PermStateChangeScope> scopePtr = (*it).scopePtr_;
152 if (scopePtr == nullptr) {
153 LOGE(ATM_DOMAIN, ATM_TAG, "ScopePtr is nullptr");
154 continue;
155 }
156 if (!CalledAccordingToTokenIdLlist(scopePtr->tokenIDs, tokenID) ||
157 !CalledAccordingToPermLlist(scopePtr->permList, permName)) {
158 LOGD(ATM_DOMAIN, ATM_TAG,
159 "tokenID is %{public}u, permName is %{public}s", tokenID, permName.c_str());
160 continue;
161 }
162 list.emplace_back((*it).callbackObject_);
163 }
164 }
165
ExecuteCallbackAsync(AccessTokenID tokenID,const std::string & permName,int32_t changeType)166 void CallbackManager::ExecuteCallbackAsync(AccessTokenID tokenID, const std::string& permName, int32_t changeType)
167 {
168 LOGI(ATM_DOMAIN, ATM_TAG, "Entry, id=%{public}u perm=%{public}s changeType=%{public}d",
169 tokenID, permName.c_str(), changeType);
170 auto callbackStart = [this, tokenID, permName, changeType]() {
171 LOGI(ATM_DOMAIN, ATM_TAG, "CallbackStart, id=%{public}u perm=%{public}s changeType=%{public}d",
172 tokenID, permName.c_str(), changeType);
173 std::string name = "AtmCallback";
174 pthread_setname_np(pthread_self(), name.substr(0, MAX_PTHREAD_NAME_LEN).c_str());
175 std::vector<sptr<IRemoteObject>> list;
176 this->GetCallbackObjectList(tokenID, permName, list);
177 this->ExecuteAllCallback(list, tokenID, permName, changeType);
178 };
179
180 std::packaged_task<void()> callbackTask(callbackStart);
181 std::make_unique<std::thread>(std::move(callbackTask))->detach();
182 LOGD(ATM_DOMAIN, ATM_TAG, "The callback execution is complete");
183 }
184 } // namespace AccessToken
185 } // namespace Security
186 } // namespace OHOS
187