• 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     Attributes attr(acquireMsg);
47     bool getAcquireInfoRet = attr.GetInt32Value(Attributes::ATTR_TIP_INFO, acquireInfo);
48     IF_FALSE_LOGE_AND_RETURN(getAcquireInfoRet);
49 
50     iamCallback_->OnAcquireInfo(moduleType, acquireInfo, attr);
51 }
52 
OnResult(int32_t resultCode,const Attributes & finalResult)53 void ContextCallbackImpl::OnResult(int32_t resultCode, const Attributes &finalResult)
54 {
55     int32_t remainTime;
56     int32_t freezingTime;
57     metaData_.operationResult = resultCode;
58     if (finalResult.GetInt32Value(Attributes::ATTR_REMAIN_TIMES, remainTime)) {
59         metaData_.remainTime = remainTime;
60     }
61     if (finalResult.GetInt32Value(Attributes::ATTR_FREEZING_TIME, freezingTime)) {
62         metaData_.freezingTime = freezingTime;
63     }
64     metaData_.endTime = std::chrono::steady_clock::now();
65 
66     if (iamCallback_ != nullptr) {
67         iamCallback_->OnResult(resultCode, finalResult);
68     }
69 
70     ContextCallbackNotifyListener::GetInstance().Process(metaData_);
71     if (stopCallback_ != nullptr) {
72         stopCallback_();
73     }
74 }
75 
SetTraceUserId(int32_t userId)76 void ContextCallbackImpl::SetTraceUserId(int32_t userId)
77 {
78     metaData_.userId = userId;
79 }
80 
SetTraceRemainTime(int32_t remainTime)81 void ContextCallbackImpl::SetTraceRemainTime(int32_t remainTime)
82 {
83     metaData_.remainTime = remainTime;
84 }
85 
SetTraceFreezingTime(int32_t freezingTime)86 void ContextCallbackImpl::SetTraceFreezingTime(int32_t freezingTime)
87 {
88     metaData_.freezingTime = freezingTime;
89 }
90 
SetTraceSdkVersion(int32_t version)91 void ContextCallbackImpl::SetTraceSdkVersion(int32_t version)
92 {
93     metaData_.sdkVersion = version;
94 }
95 
SetTraceCallingUid(uint64_t callingUid)96 void ContextCallbackImpl::SetTraceCallingUid(uint64_t callingUid)
97 {
98     metaData_.callingUid = callingUid;
99 }
100 
SetTraceAuthType(AuthType authType)101 void ContextCallbackImpl::SetTraceAuthType(AuthType authType)
102 {
103     metaData_.authType = authType;
104 }
105 
SetTraceAuthWidgetType(uint32_t authWidgetType)106 void ContextCallbackImpl::SetTraceAuthWidgetType(uint32_t authWidgetType)
107 {
108     metaData_.authWidgetType = authWidgetType;
109 }
110 
SetTraceAuthTrustLevel(AuthTrustLevel atl)111 void ContextCallbackImpl::SetTraceAuthTrustLevel(AuthTrustLevel atl)
112 {
113     metaData_.atl = atl;
114 }
115 
SetCleaner(Context::ContextStopCallback callback)116 void ContextCallbackImpl::SetCleaner(Context::ContextStopCallback callback)
117 {
118     stopCallback_ = callback;
119 }
120 
GetInstance()121 ContextCallbackNotifyListener &ContextCallbackNotifyListener::GetInstance()
122 {
123     static ContextCallbackNotifyListener contextCallbackNotifyListener;
124     return contextCallbackNotifyListener;
125 }
126 
AddNotifier(const Notify & notify)127 void ContextCallbackNotifyListener::AddNotifier(const Notify &notify)
128 {
129     notifierList_.emplace_back(notify);
130 }
131 
Process(const MetaData & metaData)132 void ContextCallbackNotifyListener::Process(const MetaData &metaData)
133 {
134     for (const auto &notify : notifierList_) {
135         if (notify != nullptr) {
136             notify(metaData);
137         }
138     }
139 }
140 
NewInstance(sptr<IamCallbackInterface> iamCallback,OperationType operationType)141 std::shared_ptr<ContextCallback> ContextCallback::NewInstance(sptr<IamCallbackInterface> iamCallback,
142     OperationType operationType)
143 {
144     if (iamCallback == nullptr) {
145         IAM_LOGE("iam callback is nullptr, parameter is invalid");
146         return nullptr;
147     }
148     return UserIam::Common::MakeShared<ContextCallbackImpl>(iamCallback, operationType);
149 }
150 } // namespace UserAuth
151 } // namespace UserIam
152 } // namespace OHOS