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 "base_context.h"
16
17 #include <sstream>
18
19 #include "context_death_recipient.h"
20 #include "iam_check.h"
21 #include "iam_logger.h"
22 #include "iam_para2str.h"
23 #include "system_ability_definition.h"
24
25 #define LOG_TAG "USER_AUTH_SA"
26 namespace OHOS {
27 namespace UserIam {
28 namespace UserAuth {
BaseContext(const std::string & type,uint64_t contextId,std::shared_ptr<ContextCallback> callback,bool needSubscribeAppState)29 BaseContext::BaseContext(const std::string &type, uint64_t contextId, std::shared_ptr<ContextCallback> callback,
30 bool needSubscribeAppState)
31 : callback_(callback),
32 contextId_(contextId)
33 {
34 std::ostringstream ss;
35 ss << "Context(type:" << type << ", contextId:" << GET_MASKED_STRING(contextId_) << ")";
36 description_ = ss.str();
37 AddDeathRecipient(callback_, contextId_);
38 if (needSubscribeAppState) {
39 SubscribeAppState(callback_, contextId_);
40 }
41 }
42
~BaseContext()43 BaseContext::~BaseContext()
44 {
45 IAM_LOGD("%{public}s start", GetDescription());
46 RemoveDeathRecipient(callback_);
47 UnSubscribeAppState();
48 }
49
SetLatestError(int32_t error)50 void BaseContext::SetLatestError(int32_t error)
51 {
52 if (error != ResultCode::SUCCESS) {
53 latestError_ = error;
54 }
55 }
56
GetLatestError() const57 int32_t BaseContext::GetLatestError() const
58 {
59 return latestError_;
60 }
61
GetContextId() const62 uint64_t BaseContext::GetContextId() const
63 {
64 return contextId_;
65 }
66
GetUserId() const67 int32_t BaseContext::GetUserId() const
68 {
69 return INVALID_USER_ID;
70 }
71
GetAuthType() const72 int32_t BaseContext::GetAuthType() const
73 {
74 return INVALID_AUTH_TYPE;
75 }
76
GetCallerName() const77 std::string BaseContext::GetCallerName() const
78 {
79 return "";
80 }
81
Start()82 bool BaseContext::Start()
83 {
84 std::lock_guard<std::mutex> guard(mutex_);
85 IAM_LOGD("%{public}s start", GetDescription());
86 if (hasStarted_) {
87 IAM_LOGI("%{public}s context has started, cannot start again", GetDescription());
88 return false;
89 }
90 hasStarted_ = true;
91 return OnStart();
92 }
93
Stop()94 bool BaseContext::Stop()
95 {
96 IAM_LOGD("%{public}s start", GetDescription());
97 return OnStop();
98 }
99
GetScheduleNode(uint64_t scheduleId) const100 std::shared_ptr<ScheduleNode> BaseContext::GetScheduleNode(uint64_t scheduleId) const
101 {
102 for (auto const &schedule : scheduleList_) {
103 if (schedule == nullptr) {
104 continue;
105 }
106 if (schedule->GetScheduleId() == scheduleId) {
107 return schedule;
108 }
109 }
110 return nullptr;
111 }
112
OnScheduleStarted()113 void BaseContext::OnScheduleStarted()
114 {
115 IAM_LOGI("%{public}s start", GetDescription());
116 }
117
OnScheduleProcessed(ExecutorRole src,int32_t moduleType,const std::vector<uint8_t> & acquireMsg)118 void BaseContext::OnScheduleProcessed(ExecutorRole src, int32_t moduleType, const std::vector<uint8_t> &acquireMsg)
119 {
120 IAM_LOGD("%{public}s start", GetDescription());
121 IF_FALSE_LOGE_AND_RETURN(callback_ != nullptr);
122 callback_->OnAcquireInfo(src, moduleType, acquireMsg);
123 }
124
OnScheduleStoped(int32_t resultCode,const std::shared_ptr<Attributes> & finalResult)125 void BaseContext::OnScheduleStoped(int32_t resultCode, const std::shared_ptr<Attributes> &finalResult)
126 {
127 OnResult(resultCode, finalResult);
128 return;
129 }
130
GetDescription() const131 const char *BaseContext::GetDescription() const
132 {
133 return description_.c_str();
134 }
135 } // namespace UserAuth
136 } // namespace UserIam
137 } // namespace OHOS
138