• 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 #include "context_callback_impl.h"
16 
17 #include <sstream>
18 
19 #include "iam_check.h"
20 #include "iam_logger.h"
21 #include "iam_mem.h"
22 #include "iam_ptr.h"
23 
24 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_SA
25 namespace OHOS {
26 namespace UserIam {
27 namespace UserAuth {
ContextCallbackImpl(sptr<IamCallbackInterface> iamCallback,OperationType operationType)28 ContextCallbackImpl::ContextCallbackImpl(sptr<IamCallbackInterface> iamCallback, OperationType operationType)
29     : iamCallback_(iamCallback)
30 {
31     metaData_.operationType = operationType;
32     metaData_.startTime = std::chrono::steady_clock::now();
33     std::ostringstream ss;
34     ss << "IDM(operation:" << operationType << ")";
35     iamHitraceHelper_ = Common::MakeShared<IamHitraceHelper>(ss.str());
36 }
37 
OnAcquireInfo(ExecutorRole src,int32_t moduleType,const std::vector<uint8_t> & acquireMsg) const38 void ContextCallbackImpl::OnAcquireInfo(ExecutorRole src, int32_t moduleType,
39     const std::vector<uint8_t> &acquireMsg) const
40 {
41     if (iamCallback_ == nullptr) {
42         IAM_LOGE("iam callback is nullptr");
43         return;
44     }
45     int32_t acquireInfo;
46     if (Common::UnpackInt32(acquireMsg, 0, acquireInfo) != SUCCESS) {
47         IAM_LOGE("failed to unpack acquireMsg");
48         return;
49     }
50     Attributes attr = {};
51     iamCallback_->OnAcquireInfo(moduleType, acquireInfo, attr);
52 }
53 
OnResult(int32_t resultCode,const Attributes & finalResult)54 void ContextCallbackImpl::OnResult(int32_t resultCode, const Attributes &finalResult)
55 {
56     int32_t remainTime;
57     int32_t freezingTime;
58     metaData_.operationResult = resultCode;
59     if (finalResult.GetInt32Value(Attributes::ATTR_REMAIN_TIMES, remainTime)) {
60         metaData_.remainTime = remainTime;
61     }
62     if (finalResult.GetInt32Value(Attributes::ATTR_FREEZING_TIME, freezingTime)) {
63         metaData_.freezingTime = freezingTime;
64     }
65     metaData_.endTime = std::chrono::steady_clock::now();
66 
67     if (iamCallback_ != nullptr) {
68         iamCallback_->OnResult(resultCode, finalResult);
69     }
70 
71     ContextCallbackNotifyListener::GetInstance().Process(metaData_);
72     if (stopCallback_ != nullptr) {
73         stopCallback_();
74     }
75 }
76 
SetTraceUserId(int32_t userId)77 void ContextCallbackImpl::SetTraceUserId(int32_t userId)
78 {
79     metaData_.userId = userId;
80 }
81 
SetTraceRemainTime(int32_t remainTime)82 void ContextCallbackImpl::SetTraceRemainTime(int32_t remainTime)
83 {
84     metaData_.remainTime = remainTime;
85 }
86 
SetTraceFreezingTime(int32_t freezingTime)87 void ContextCallbackImpl::SetTraceFreezingTime(int32_t freezingTime)
88 {
89     metaData_.freezingTime = freezingTime;
90 }
91 
SetTraceSdkVersion(int32_t version)92 void ContextCallbackImpl::SetTraceSdkVersion(int32_t version)
93 {
94     metaData_.sdkVersion = version;
95 }
96 
SetTraceCallingUid(uint64_t callingUid)97 void ContextCallbackImpl::SetTraceCallingUid(uint64_t callingUid)
98 {
99     metaData_.callingUid = callingUid;
100 }
101 
SetTraceAuthType(AuthType authType)102 void ContextCallbackImpl::SetTraceAuthType(AuthType authType)
103 {
104     metaData_.authType = authType;
105 }
106 
SetTraceAuthTrustLevel(AuthTrustLevel atl)107 void ContextCallbackImpl::SetTraceAuthTrustLevel(AuthTrustLevel atl)
108 {
109     metaData_.atl = atl;
110 }
111 
SetCleaner(Context::ContextStopCallback callback)112 void ContextCallbackImpl::SetCleaner(Context::ContextStopCallback callback)
113 {
114     stopCallback_ = callback;
115 }
116 
AddNotifier(const Notify & notify)117 void ContextCallbackNotifyListener::AddNotifier(const Notify &notify)
118 {
119     notifierList_.emplace_back(notify);
120 }
121 
Process(const MetaData & metaData)122 void ContextCallbackNotifyListener::Process(const MetaData &metaData)
123 {
124     for (const auto &notify : notifierList_) {
125         if (notify != nullptr) {
126             notify(metaData);
127         }
128     }
129 }
130 
NewInstance(sptr<IamCallbackInterface> iamCallback,OperationType operationType)131 std::shared_ptr<ContextCallback> ContextCallback::NewInstance(sptr<IamCallbackInterface> iamCallback,
132     OperationType operationType)
133 {
134     if (iamCallback == nullptr) {
135         IAM_LOGE("iam callback is nullptr, parameter is invalid");
136         return nullptr;
137     }
138     return UserIam::Common::MakeShared<ContextCallbackImpl>(iamCallback, operationType);
139 }
140 } // namespace UserAuth
141 } // namespace UserIam
142 } // namespace OHOS