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 
Start()69 bool BaseContext::Start()
70 {
71     std::lock_guard<std::mutex> guard(mutex_);
72     IAM_LOGI("%{public}s start", GetDescription());
73     if (hasStarted_) {
74         IAM_LOGI("%{public}s context has started, cannot start again", GetDescription());
75         return false;
76     }
77     hasStarted_ = true;
78     return OnStart();
79 }
80 
Stop()81 bool BaseContext::Stop()
82 {
83     IAM_LOGI("%{public}s start", GetDescription());
84     return OnStop();
85 }
86 
GetScheduleNode(uint64_t scheduleId) const87 std::shared_ptr<ScheduleNode> BaseContext::GetScheduleNode(uint64_t scheduleId) const
88 {
89     for (auto const &schedule : scheduleList_) {
90         if (schedule == nullptr) {
91             continue;
92         }
93         if (schedule->GetScheduleId() == scheduleId) {
94             return schedule;
95         }
96     }
97     return nullptr;
98 }
99 
OnScheduleStarted()100 void BaseContext::OnScheduleStarted()
101 {
102     IAM_LOGI("%{public}s start", GetDescription());
103 }
104 
OnScheduleProcessed(ExecutorRole src, int32_t moduleType, const std::vector<uint8_t> &acquireMsg)105 void BaseContext::OnScheduleProcessed(ExecutorRole src, int32_t moduleType, const std::vector<uint8_t> &acquireMsg)
106 {
107     IAM_LOGI("%{public}s start", GetDescription());
108     IF_FALSE_LOGE_AND_RETURN(callback_ != nullptr);
109     callback_->OnAcquireInfo(src, moduleType, acquireMsg);
110 }
111 
OnScheduleStoped(int32_t resultCode, const std::shared_ptr<Attributes> &finalResult)112 void BaseContext::OnScheduleStoped(int32_t resultCode, const std::shared_ptr<Attributes> &finalResult)
113 {
114     OnResult(resultCode, finalResult);
115     return;
116 }
117 
GetDescription() const118 const char *BaseContext::GetDescription() const
119 {
120     return description_.c_str();
121 }
122 } // namespace UserAuth
123 } // namespace UserIam
124 } // namespace OHOS
125