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