1 /*
2 * Copyright (c) 2022-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 "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 "perm_state_callback_death_recipient.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 time_t MAX_TIMEOUT_SEC = 30;
34 static const uint32_t MAX_CALLBACK_SIZE = 1024;
35 static const int MAX_PTHREAD_NAME_LEN = 15; // pthread name max length
36 }
37
GetInstance()38 CallbackManager& CallbackManager::GetInstance()
39 {
40 static CallbackManager instance;
41 return instance;
42 }
43
CallbackManager()44 CallbackManager::CallbackManager() : callbackDeathRecipient_(sptr<IRemoteObject::DeathRecipient>(
45 new (std::nothrow) PermStateCallbackDeathRecipient()))
46 {
47 }
48
~CallbackManager()49 CallbackManager::~CallbackManager()
50 {
51 }
52
AddCallback(const PermStateChangeScope & scopeRes,const sptr<IRemoteObject> & callback)53 int32_t CallbackManager::AddCallback(const PermStateChangeScope& scopeRes, const sptr<IRemoteObject>& callback)
54 {
55 if (callback == nullptr) {
56 ACCESSTOKEN_LOG_ERROR(LABEL, "input is nullptr");
57 return AccessTokenError::ERR_PARAM_INVALID;
58 }
59 auto callbackScopePtr = std::make_shared<PermStateChangeScope>(scopeRes);
60
61 std::lock_guard<std::mutex> lock(mutex_);
62 if (callbackInfoList_.size() >= MAX_CALLBACK_SIZE) {
63 ACCESSTOKEN_LOG_ERROR(LABEL, "callback size has reached limitation");
64 return AccessTokenError::ERR_EXCEEDED_MAXNUM_REGISTRATION_LIMIT;
65 }
66 callback->AddDeathRecipient(callbackDeathRecipient_);
67
68 CallbackRecord recordInstance;
69 recordInstance.callbackObject_ = callback;
70 recordInstance.scopePtr_ = callbackScopePtr;
71
72 callbackInfoList_.emplace_back(recordInstance);
73
74 ACCESSTOKEN_LOG_INFO(LABEL, "recordInstance is added");
75 return RET_SUCCESS;
76 }
77
RemoveCallback(const sptr<IRemoteObject> & callback)78 int32_t CallbackManager::RemoveCallback(const sptr<IRemoteObject>& callback)
79 {
80 if (callback == nullptr) {
81 ACCESSTOKEN_LOG_ERROR(LABEL, "callback is nullptr");
82 return AccessTokenError::ERR_PARAM_INVALID;
83 }
84
85 std::lock_guard<std::mutex> lock(mutex_);
86
87 for (auto it = callbackInfoList_.begin(); it != callbackInfoList_.end(); ++it) {
88 if (callback == (*it).callbackObject_) {
89 ACCESSTOKEN_LOG_INFO(LABEL, "find callback");
90 if (callbackDeathRecipient_ != nullptr) {
91 callback->RemoveDeathRecipient(callbackDeathRecipient_);
92 }
93 (*it).callbackObject_ = nullptr;
94 callbackInfoList_.erase(it);
95 break;
96 }
97 }
98 ACCESSTOKEN_LOG_INFO(LABEL, "callbackInfoList_ %{public}u", (uint32_t)callbackInfoList_.size());
99 return RET_SUCCESS;
100 }
101
CalledAccordingToTokenIdLlist(const std::vector<AccessTokenID> & tokenIDList,AccessTokenID tokenID)102 bool CallbackManager::CalledAccordingToTokenIdLlist(
103 const std::vector<AccessTokenID>& tokenIDList, AccessTokenID tokenID)
104 {
105 if (tokenIDList.empty()) {
106 return true;
107 }
108 return std::any_of(tokenIDList.begin(), tokenIDList.end(),
109 [tokenID](AccessTokenID id) { return id == tokenID; });
110 }
111
CalledAccordingToPermLlist(const std::vector<std::string> & permList,const std::string & permName)112 bool CallbackManager::CalledAccordingToPermLlist(const std::vector<std::string>& permList, const std::string& permName)
113 {
114 if (permList.empty()) {
115 return true;
116 }
117 return std::any_of(permList.begin(), permList.end(),
118 [permName](const std::string& perm) { return perm == permName; });
119 }
120
ExecuteCallbackAsync(AccessTokenID tokenID,const std::string & permName,int32_t changeType)121 void CallbackManager::ExecuteCallbackAsync(AccessTokenID tokenID, const std::string& permName, int32_t changeType)
122 {
123 ACCESSTOKEN_LOG_INFO(LABEL, "entry");
124 auto callbackStart = [&]() {
125 ACCESSTOKEN_LOG_INFO(LABEL, "callbackStart");
126 std::string name = "AtmCallback";
127 pthread_setname_np(pthread_self(), name.substr(0, MAX_PTHREAD_NAME_LEN).c_str());
128 std::vector<sptr<IRemoteObject>> list;
129 {
130 std::lock_guard<std::mutex> lock(mutex_);
131 for (auto it = callbackInfoList_.begin(); it != callbackInfoList_.end(); ++it) {
132 std::shared_ptr<PermStateChangeScope> scopePtr_ = (*it).scopePtr_;
133 if (scopePtr_ == nullptr) {
134 ACCESSTOKEN_LOG_ERROR(LABEL, "scopePtr_ is nullptr");
135 continue;
136 }
137 if (!CalledAccordingToTokenIdLlist(scopePtr_->tokenIDs, tokenID) ||
138 !CalledAccordingToPermLlist(scopePtr_->permList, permName)) {
139 ACCESSTOKEN_LOG_DEBUG(LABEL,
140 "tokenID is %{public}u, permName is %{public}s", tokenID, permName.c_str());
141 continue;
142 }
143 list.emplace_back((*it).callbackObject_);
144 }
145 }
146 for (auto it = list.begin(); it != list.end(); ++it) {
147 auto callback = iface_cast<IPermissionStateCallback>(*it);
148 if (callback != nullptr) {
149 ACCESSTOKEN_LOG_INFO(LABEL, "callback execute");
150 PermStateChangeInfo resInfo;
151 resInfo.permStateChangeType = changeType;
152 resInfo.permissionName = permName;
153 resInfo.tokenID = tokenID;
154 callback->PermStateChangeCallback(resInfo);
155 }
156 }
157 };
158
159 std::packaged_task<void()> callbackTask(callbackStart);
160 std::future<void> fut = callbackTask.get_future();
161 std::make_unique<std::thread>(std::move(callbackTask))->detach();
162
163 ACCESSTOKEN_LOG_DEBUG(LABEL, "Waiting for the callback execution complete...");
164 std::future_status status = fut.wait_for(std::chrono::seconds(MAX_TIMEOUT_SEC));
165 if (status == std::future_status::timeout) {
166 ACCESSTOKEN_LOG_WARN(LABEL, "callbackTask callback execution timeout");
167 }
168 ACCESSTOKEN_LOG_DEBUG(LABEL, "The callback execution is complete");
169 }
170 } // namespace AccessToken
171 } // namespace Security
172 } // namespace OHOS
173