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_log.h"
25 #include "privacy_error.h"
26
27 namespace OHOS {
28 namespace Security {
29 namespace AccessToken {
30 namespace {
31 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
32 LOG_CORE, SECURITY_DOMAIN_PRIVACY, "ActiveStatusCallbackManager"
33 };
34 static const time_t MAX_TIMEOUT_SEC = 30;
35 static const uint32_t MAX_CALLBACK_SIZE = 1024;
36 static const int MAX_PTHREAD_NAME_LEN = 15; // pthread name max length
37 }
38
GetInstance()39 ActiveStatusCallbackManager& ActiveStatusCallbackManager::GetInstance()
40 {
41 static ActiveStatusCallbackManager instance;
42 return instance;
43 }
44
ActiveStatusCallbackManager()45 ActiveStatusCallbackManager::ActiveStatusCallbackManager()
46 : callbackDeathRecipient_(sptr<IRemoteObject::DeathRecipient>(
47 new (std::nothrow) PermActiveStatusCallbackDeathRecipient()))
48 {
49 }
50
~ActiveStatusCallbackManager()51 ActiveStatusCallbackManager::~ActiveStatusCallbackManager()
52 {
53 }
54
AddCallback(const std::vector<std::string> & permList,const sptr<IRemoteObject> & callback)55 int32_t ActiveStatusCallbackManager::AddCallback(
56 const std::vector<std::string>& permList, const sptr<IRemoteObject>& callback)
57 {
58 if (callback == nullptr) {
59 ACCESSTOKEN_LOG_ERROR(LABEL, "input is nullptr");
60 return PrivacyError::ERR_PARAM_INVALID;
61 }
62
63 std::lock_guard<std::mutex> lock(mutex_);
64 if (callbackDataList_.size() >= MAX_CALLBACK_SIZE) {
65 ACCESSTOKEN_LOG_ERROR(LABEL, "list size has reached max value");
66 return PrivacyError::ERR_CALLBACKS_EXCEED_LIMITATION;
67 }
68 callback->AddDeathRecipient(callbackDeathRecipient_);
69
70 CallbackData recordInstance;
71 recordInstance.callbackObject_ = callback;
72 recordInstance.permList_ = permList;
73
74 callbackDataList_.emplace_back(recordInstance);
75
76 ACCESSTOKEN_LOG_INFO(LABEL, "recordInstance is added");
77 return RET_SUCCESS;
78 }
79
RemoveCallback(const sptr<IRemoteObject> & callback)80 int32_t ActiveStatusCallbackManager::RemoveCallback(const sptr<IRemoteObject>& callback)
81 {
82 ACCESSTOKEN_LOG_INFO(LABEL, "called");
83 if (callback == nullptr) {
84 ACCESSTOKEN_LOG_ERROR(LABEL, "callback is nullptr");
85 return PrivacyError::ERR_PARAM_INVALID;
86 }
87
88 std::lock_guard<std::mutex> lock(mutex_);
89
90 for (auto it = callbackDataList_.begin(); it != callbackDataList_.end(); ++it) {
91 if (callback == (*it).callbackObject_) {
92 ACCESSTOKEN_LOG_INFO(LABEL, "find callback");
93 if (callbackDeathRecipient_ != nullptr) {
94 callback->RemoveDeathRecipient(callbackDeathRecipient_);
95 }
96 (*it).callbackObject_ = nullptr;
97 callbackDataList_.erase(it);
98 break;
99 }
100 }
101 return RET_SUCCESS;
102 }
103
NeedCalled(const std::vector<std::string> & permList,const std::string & permName)104 bool ActiveStatusCallbackManager::NeedCalled(const std::vector<std::string>& permList, const std::string& permName)
105 {
106 if (permList.empty()) {
107 return true;
108 }
109 return std::any_of(permList.begin(), permList.end(),
110 [permName](const std::string& perm) { return perm == permName; });
111 }
112
ExecuteCallbackAsync(AccessTokenID tokenId,const std::string & permName,const std::string & deviceId,ActiveChangeType changeType)113 void ActiveStatusCallbackManager::ExecuteCallbackAsync(
114 AccessTokenID tokenId, const std::string& permName, const std::string& deviceId, ActiveChangeType changeType)
115 {
116 ACCESSTOKEN_LOG_INFO(LABEL, "entry");
117 if (changeType == PERM_ACTIVE_IN_BACKGROUND) {
118 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::ACCESS_TOKEN, "PERMISSION_CHECK_EVENT",
119 HiviewDFX::HiSysEvent::EventType::BEHAVIOR, "CODE", BACKGROUND_CALL_EVENT,
120 "CALLER_TOKENID", tokenId, "PERMISSION_NAME", permName, "REASON", "background call");
121 }
122 auto callbackFunc = [&]() {
123 ACCESSTOKEN_LOG_INFO(LABEL, "callbackStart");
124 std::string name = "PrivacyCallback";
125 pthread_setname_np(pthread_self(), name.substr(0, MAX_PTHREAD_NAME_LEN).c_str());
126 std::vector<sptr<IRemoteObject>> list;
127 {
128 std::lock_guard<std::mutex> lock(mutex_);
129 for (auto it = callbackDataList_.begin(); it != callbackDataList_.end(); ++it) {
130 std::vector<std::string> permList = (*it).permList_;
131 if (!NeedCalled(permList, permName)) {
132 ACCESSTOKEN_LOG_INFO(LABEL, "tokenId %{public}u, permName %{public}s", tokenId, permName.c_str());
133 continue;
134 }
135 list.emplace_back((*it).callbackObject_);
136 }
137 }
138 for (auto it = list.begin(); it != list.end(); ++it) {
139 auto callback = iface_cast<IPermActiveStatusCallback>(*it);
140 if (callback != nullptr) {
141 ActiveChangeResponse resInfo;
142 resInfo.type = changeType;
143 resInfo.permissionName = permName;
144 resInfo.tokenID = tokenId;
145 resInfo.deviceId = deviceId;
146 ACCESSTOKEN_LOG_INFO(LABEL, "callback excute changeType %{public}d", changeType);
147 callback->ActiveStatusChangeCallback(resInfo);
148 }
149 }
150 };
151
152 std::packaged_task<void()> callbackTask(callbackFunc);
153 std::future<void> fut = callbackTask.get_future();
154 std::make_unique<std::thread>(std::move(callbackTask))->detach();
155
156 ACCESSTOKEN_LOG_INFO(LABEL, "Waiting for the callback execution complete...");
157 std::future_status status = fut.wait_for(std::chrono::seconds(MAX_TIMEOUT_SEC));
158 if (status == std::future_status::timeout) {
159 ACCESSTOKEN_LOG_WARN(LABEL, "callbackTask callback execution timeout");
160 }
161 ACCESSTOKEN_LOG_INFO(LABEL, "The callback execution is complete");
162 }
163 } // namespace AccessToken
164 } // namespace Security
165 } // namespace OHOS
166