• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "active_status_callback_manager.h"
17 
18 #include <future>
19 #include <thread>
20 #include <datetime_ex.h>
21 #include <pthread.h>
22 
23 #include "accesstoken_dfx_define.h"
24 #include "accesstoken_common_log.h"
25 #include "ipc_skeleton.h"
26 #include "privacy_error.h"
27 
28 namespace OHOS {
29 namespace Security {
30 namespace AccessToken {
31 namespace {
32 static const uint32_t MAX_CALLBACK_SIZE = 1024;
33 std::recursive_mutex g_instanceMutex;
34 }
35 
GetInstance()36 ActiveStatusCallbackManager& ActiveStatusCallbackManager::GetInstance()
37 {
38     static ActiveStatusCallbackManager* instance = nullptr;
39     if (instance == nullptr) {
40         std::lock_guard<std::recursive_mutex> lock(g_instanceMutex);
41         if (instance == nullptr) {
42             ActiveStatusCallbackManager* tmp = new (std::nothrow) ActiveStatusCallbackManager();
43             instance = std::move(tmp);
44         }
45     }
46     return *instance;
47 }
48 
ActiveStatusCallbackManager()49 ActiveStatusCallbackManager::ActiveStatusCallbackManager()
50     : callbackDeathRecipient_(sptr<IRemoteObject::DeathRecipient>(
51         new (std::nothrow) PermActiveStatusCallbackDeathRecipient()))
52 {
53 }
54 
~ActiveStatusCallbackManager()55 ActiveStatusCallbackManager::~ActiveStatusCallbackManager()
56 {
57 }
58 
59 #ifdef EVENTHANDLER_ENABLE
InitEventHandler(const std::shared_ptr<AccessEventHandler> & eventHandler)60 void ActiveStatusCallbackManager::InitEventHandler(const std::shared_ptr<AccessEventHandler>& eventHandler)
61 {
62     eventHandler_ = eventHandler;
63 }
64 #endif
65 
AddCallback(AccessTokenID registerTokenId,const std::vector<std::string> & permList,const sptr<IRemoteObject> & callback)66 int32_t ActiveStatusCallbackManager::AddCallback(
67     AccessTokenID registerTokenId, const std::vector<std::string>& permList, const sptr<IRemoteObject>& callback)
68 {
69     if (callback == nullptr) {
70         LOGE(PRI_DOMAIN, PRI_TAG, "Input is nullptr");
71         return PrivacyError::ERR_PARAM_INVALID;
72     }
73 
74     std::lock_guard<std::mutex> lock(mutex_);
75     if (callbackDataList_.size() >= MAX_CALLBACK_SIZE) {
76         LOGE(PRI_DOMAIN, PRI_TAG, "List size has reached max value");
77         return PrivacyError::ERR_CALLBACKS_EXCEED_LIMITATION;
78     }
79     if (callback->IsProxyObject() &&
80         ((callbackDeathRecipient_ == nullptr) || !callback->AddDeathRecipient(callbackDeathRecipient_))) {
81         LOGE(PRI_DOMAIN, PRI_TAG, "add death recipient failed");
82         return PrivacyError::ERR_ADD_DEATH_RECIPIENT_FAILED;
83     }
84 
85     CallbackData recordInstance;
86     recordInstance.registerTokenId = registerTokenId;
87     recordInstance.callbackObject_ = callback;
88     recordInstance.permList_ = permList;
89 
90     callbackDataList_.emplace_back(recordInstance);
91 
92     LOGI(PRI_DOMAIN, PRI_TAG, "RecordInstance is added");
93     return RET_SUCCESS;
94 }
95 
RemoveCallback(const sptr<IRemoteObject> & callback)96 int32_t ActiveStatusCallbackManager::RemoveCallback(const sptr<IRemoteObject>& callback)
97 {
98     LOGI(PRI_DOMAIN, PRI_TAG, "Called");
99     if (callback == nullptr) {
100         LOGE(PRI_DOMAIN, PRI_TAG, "Callback is nullptr.");
101         return PrivacyError::ERR_PARAM_INVALID;
102     }
103 
104     std::lock_guard<std::mutex> lock(mutex_);
105 
106     for (auto it = callbackDataList_.begin(); it != callbackDataList_.end(); ++it) {
107         if (callback == (*it).callbackObject_) {
108             LOGI(PRI_DOMAIN, PRI_TAG, "Find callback");
109             if (callbackDeathRecipient_ != nullptr) {
110                 callback->RemoveDeathRecipient(callbackDeathRecipient_);
111             }
112             (*it).callbackObject_ = nullptr;
113             callbackDataList_.erase(it);
114             break;
115         }
116     }
117     return RET_SUCCESS;
118 }
119 
NeedCalled(const std::vector<std::string> & permList,const std::string & permName)120 bool ActiveStatusCallbackManager::NeedCalled(const std::vector<std::string>& permList, const std::string& permName)
121 {
122     if (permList.empty()) {
123         return true;
124     }
125     return std::any_of(permList.begin(), permList.end(),
126         [permName](const std::string& perm) { return perm == permName; });
127 }
128 
129 
ActiveStatusChange(ActiveChangeResponse & info)130 void ActiveStatusCallbackManager::ActiveStatusChange(ActiveChangeResponse& info)
131 {
132     std::vector<sptr<IRemoteObject>> list;
133     {
134         std::lock_guard<std::mutex> lock(mutex_);
135         for (auto it = callbackDataList_.begin(); it != callbackDataList_.end(); ++it) {
136             std::vector<std::string> permList = (*it).permList_;
137             if (!NeedCalled(permList, info.permissionName)) {
138                 LOGI(PRI_DOMAIN, PRI_TAG, "TokenId %{public}u, perm %{public}s", info.tokenID,
139                     info.permissionName.c_str());
140                 continue;
141             }
142             list.emplace_back((*it).callbackObject_);
143         }
144     }
145     for (auto it = list.begin(); it != list.end(); ++it) {
146         sptr<IPermActiveStatusCallback> callback = new (std::nothrow) PermActiveStatusChangeCallbackProxy(*it);
147         if (callback != nullptr) {
148             LOGI(PRI_DOMAIN, PRI_TAG, "Callback execute callingTokenId %{public}u, tokenId %{public}u, "
149                 "permission %{public}s, changeType %{public}d, usedType %{public}d, pid %{public}d",
150                 info.callingTokenID, info.tokenID, info.permissionName.c_str(), info.type, info.usedType, info.pid);
151             callback->ActiveStatusChangeCallback(info);
152         }
153     }
154 }
155 
ExecuteCallbackAsync(ActiveChangeResponse & info)156 void ActiveStatusCallbackManager::ExecuteCallbackAsync(ActiveChangeResponse& info)
157 {
158 #ifdef EVENTHANDLER_ENABLE
159     if (eventHandler_ == nullptr) {
160         LOGE(PRI_DOMAIN, PRI_TAG, "Fail to get EventHandler");
161         return;
162     }
163     std::string taskName = info.permissionName + std::to_string(info.tokenID);
164     LOGI(PRI_DOMAIN, PRI_TAG, "Add permission task name:%{public}s", taskName.c_str());
165     std::function<void()> task = ([info]() mutable {
166         ActiveStatusCallbackManager::GetInstance().ActiveStatusChange(info);
167         LOGI(PRI_DOMAIN, PRI_TAG,
168             "Token: %{public}u, pid: %{public}d, permName:  %{public}s, changeType: %{public}d, ActiveStatusChange end",
169             info.tokenID, info.pid, info.permissionName.c_str(), info.type);
170     });
171     eventHandler_->ProxyPostTask(task, taskName);
172     LOGI(PRI_DOMAIN, PRI_TAG, "The callback execution is complete");
173     return;
174 #else
175     LOGI(PRI_DOMAIN, PRI_TAG, "Event handler is unenabled");
176     return;
177 #endif
178 }
179 } // namespace AccessToken
180 } // namespace Security
181 } // namespace OHOS
182