1 /*
2 * Copyright (c) 2023 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
16 #include "sa_command_manager.h"
17
18 #include "iam_check.h"
19
20 #define LOG_LABEL UserIam::Common::LABEL_FACE_AUTH_SA
21
22 namespace OHOS {
23 namespace UserIam {
24 namespace FaceAuth {
GetInstance()25 SaCommandManager &SaCommandManager::GetInstance()
26 {
27 static SaCommandManager manager;
28 return manager;
29 }
30
RegisterSaCommandProcessor(std::vector<SaCommandId> commandIds,std::shared_ptr<ISaCommandProcessor> processor)31 void SaCommandManager::RegisterSaCommandProcessor(std::vector<SaCommandId> commandIds,
32 std::shared_ptr<ISaCommandProcessor> processor)
33 {
34 IF_FALSE_LOGE_AND_RETURN(processor != nullptr);
35
36 std::unique_lock<std::shared_mutex> lock(mutex_);
37
38 processors_.insert(processor);
39
40 for (const auto &commandId : commandIds) {
41 if (commandId2Processors_.find(commandId) == commandId2Processors_.end()) {
42 commandId2Processors_[commandId] = std::set<std::shared_ptr<ISaCommandProcessor>>();
43 }
44
45 commandId2Processors_[commandId].insert(processor);
46 }
47 }
48
UnregisterSaCommandProcessor(std::vector<SaCommandId> commandIds,std::shared_ptr<ISaCommandProcessor> processor)49 void SaCommandManager::UnregisterSaCommandProcessor(std::vector<SaCommandId> commandIds,
50 std::shared_ptr<ISaCommandProcessor> processor)
51 {
52 IF_FALSE_LOGE_AND_RETURN(processor != nullptr);
53
54 std::unique_lock<std::shared_mutex> lock(mutex_);
55
56 processors_.erase(processor);
57
58 for (const auto &commandId : commandIds) {
59 if (commandId2Processors_.find(commandId) == commandId2Processors_.end()) {
60 continue;
61 }
62
63 commandId2Processors_[commandId].erase(processor);
64 }
65 }
66
ProcessSaCommands(std::shared_ptr<FaceAuthExecutorHdi> executor,const std::vector<SaCommand> & commands)67 UserAuth::ResultCode SaCommandManager::ProcessSaCommands(std::shared_ptr<FaceAuthExecutorHdi> executor,
68 const std::vector<SaCommand> &commands)
69 {
70 std::shared_lock<std::shared_mutex> lock(mutex_);
71
72 for (const auto &command : commands) {
73 auto it = commandId2Processors_.find(command.id);
74 IF_FALSE_LOGE_AND_RETURN_VAL(it != commandId2Processors_.end(), UserAuth::GENERAL_ERROR);
75 for (const auto &processor : commandId2Processors_[command.id]) {
76 IF_FALSE_LOGE_AND_RETURN_VAL(processor != nullptr, UserAuth::GENERAL_ERROR);
77 UserAuth::ResultCode result = processor->ProcessSaCommand(executor, command);
78 IF_FALSE_LOGE_AND_RETURN_VAL(result == UserAuth::SUCCESS, UserAuth::GENERAL_ERROR);
79 }
80 }
81
82 return UserAuth::SUCCESS;
83 }
84
OnHdiDisconnect(std::shared_ptr<FaceAuthExecutorHdi> executor)85 void SaCommandManager::OnHdiDisconnect(std::shared_ptr<FaceAuthExecutorHdi> executor)
86 {
87 std::shared_lock<std::shared_mutex> lock(mutex_);
88
89 for (const auto &processor : processors_) {
90 IF_FALSE_LOGE_AND_RETURN(processor != nullptr);
91 processor->OnHdiDisconnect(executor);
92 }
93
94 return;
95 }
96 } // namespace FaceAuth
97 } // namespace UserIam
98 } // namespace OHOS