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