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)29 BaseContext::BaseContext(const std::string &type, uint64_t contextId, std::shared_ptr<ContextCallback> callback)
30 : callback_(callback),
31 contextId_(contextId)
32 {
33 std::ostringstream ss;
34 ss << "Context(type:" << type << ", contextId:" << GET_MASKED_STRING(contextId_) << ")";
35 description_ = ss.str();
36 AddDeathRecipient(callback_, contextId_);
37 SubscribeAppState(callback_, contextId_);
38 }
39
~BaseContext()40 BaseContext::~BaseContext()
41 {
42 IAM_LOGI("%{public}s start", GetDescription());
43 RemoveDeathRecipient(callback_);
44 UnSubscribeAppState();
45 }
46
SetLatestError(int32_t error)47 void BaseContext::SetLatestError(int32_t error)
48 {
49 if (error != ResultCode::SUCCESS) {
50 latestError_ = error;
51 }
52 }
53
GetLatestError() const54 int32_t BaseContext::GetLatestError() const
55 {
56 return latestError_;
57 }
58
GetContextId() const59 uint64_t BaseContext::GetContextId() const
60 {
61 return contextId_;
62 }
63
GetUserId() const64 int32_t BaseContext::GetUserId() const
65 {
66 return INVALID_USER_ID;
67 }
68
GetAuthType() const69 int32_t BaseContext::GetAuthType() const
70 {
71 return INVALID_AUTH_TYPE;
72 }
73
GetCallerName() const74 std::string BaseContext::GetCallerName() const
75 {
76 return "";
77 }
78
Start()79 bool BaseContext::Start()
80 {
81 std::lock_guard<std::mutex> guard(mutex_);
82 IAM_LOGI("%{public}s start", GetDescription());
83 if (hasStarted_) {
84 IAM_LOGI("%{public}s context has started, cannot start again", GetDescription());
85 return false;
86 }
87 hasStarted_ = true;
88 return OnStart();
89 }
90
Stop()91 bool BaseContext::Stop()
92 {
93 IAM_LOGI("%{public}s start", GetDescription());
94 return OnStop();
95 }
96
GetScheduleNode(uint64_t scheduleId) const97 std::shared_ptr<ScheduleNode> BaseContext::GetScheduleNode(uint64_t scheduleId) const
98 {
99 for (auto const &schedule : scheduleList_) {
100 if (schedule == nullptr) {
101 continue;
102 }
103 if (schedule->GetScheduleId() == scheduleId) {
104 return schedule;
105 }
106 }
107 return nullptr;
108 }
109
OnScheduleStarted()110 void BaseContext::OnScheduleStarted()
111 {
112 IAM_LOGI("%{public}s start", GetDescription());
113 }
114
OnScheduleProcessed(ExecutorRole src,int32_t moduleType,const std::vector<uint8_t> & acquireMsg)115 void BaseContext::OnScheduleProcessed(ExecutorRole src, int32_t moduleType, const std::vector<uint8_t> &acquireMsg)
116 {
117 IAM_LOGI("%{public}s start", GetDescription());
118 IF_FALSE_LOGE_AND_RETURN(callback_ != nullptr);
119 callback_->OnAcquireInfo(src, moduleType, acquireMsg);
120 }
121
OnScheduleStoped(int32_t resultCode,const std::shared_ptr<Attributes> & finalResult)122 void BaseContext::OnScheduleStoped(int32_t resultCode, const std::shared_ptr<Attributes> &finalResult)
123 {
124 OnResult(resultCode, finalResult);
125 return;
126 }
127
GetDescription() const128 const char *BaseContext::GetDescription() const
129 {
130 return description_.c_str();
131 }
132 } // namespace UserAuth
133 } // namespace UserIam
134 } // namespace OHOS
135