1 /*
2 * Copyright (c) 2025 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 "ohos.userIAM.faceAuth.impl.hpp"
17
18 #include <sstream>
19
20 #include "ohos.userIAM.faceAuth.FaceAuthManager.proj.2.hpp"
21 #include "taihe/runtime.hpp"
22
23 #include "iam_check.h"
24 #include "iam_logger.h"
25
26 #include "face_auth_client.h"
27 #include "face_auth_defines.h"
28 #include "surface_utils.h"
29
30 using namespace OHOS;
31 using namespace taihe;
32
33 #define LOG_TAG "FACE_AUTH_ANI"
34
35 namespace OHOS {
36 namespace UserIam {
37 namespace FaceAuth {
38 namespace {
GetBufferProducerBySurfaceId(uint64_t surfaceId,sptr<IBufferProducer> & bufferProducer)39 bool GetBufferProducerBySurfaceId(uint64_t surfaceId, sptr<IBufferProducer> &bufferProducer)
40 {
41 if (surfaceId == 0) {
42 IAM_LOGI("surface id is 0, get buffer producer null");
43 bufferProducer = nullptr;
44 return true;
45 }
46
47 auto surfaceUtils = SurfaceUtils::GetInstance();
48 if (surfaceUtils == nullptr) {
49 IAM_LOGE("Get SurfaceUtils failed!");
50 return false;
51 }
52
53 sptr<Surface> previewSurface = surfaceUtils->GetSurface(surfaceId);
54 if (previewSurface == nullptr) {
55 IAM_LOGE("GetSurface failed!");
56 return false;
57 }
58
59 bufferProducer = previewSurface->GetProducer();
60 if (bufferProducer == nullptr) {
61 IAM_LOGE("GetProducer Failed!");
62 return false;
63 }
64
65 IAM_LOGI("get buffer producer success");
66 return true;
67 }
68
SetSurfaceId(const std::string & surfaceIdStr)69 int32_t SetSurfaceId(const std::string &surfaceIdStr)
70 {
71 IAM_LOGI("SetSurfaceId start");
72
73 std::istringstream surfaceIdStream(surfaceIdStr);
74 uint64_t surfaceId;
75 surfaceIdStream >> surfaceId;
76
77 sptr<IBufferProducer> bufferProducer(nullptr);
78 if (!GetBufferProducerBySurfaceId(surfaceId, bufferProducer)) {
79 IAM_LOGE("GetBufferProducerBySurfaceId fail");
80 return FACE_AUTH_ERROR;
81 }
82
83 int32_t result = FaceAuthClient::GetInstance().SetBufferProducer(bufferProducer);
84 if (result != FACE_AUTH_SUCCESS) {
85 IAM_LOGE("SetBufferProducer fail, result: %{public}d", result);
86 return result;
87 }
88
89 IAM_LOGI("SetSurfaceId success");
90 return FACE_AUTH_SUCCESS;
91 }
92
ThrowBusinessError(int32_t result)93 void ThrowBusinessError(int32_t result)
94 {
95 const int32_t napiGeneralError = 12700001;
96 const std::map<int32_t, std::string> errorCode2Str = {
97 { FACE_AUTH_CHECK_PERMISSION_FAILED, "Permission verification failed." },
98 { FACE_AUTH_CHECK_SYSTEM_PERMISSION_FAILED, "The caller is not a system application." },
99 { napiGeneralError, "The operation is failed." },
100 };
101
102 if (result == FACE_AUTH_SUCCESS) {
103 return;
104 }
105
106 auto pair = errorCode2Str.find(result);
107 if (pair == errorCode2Str.end()) {
108 pair = errorCode2Str.find(napiGeneralError);
109 }
110 IF_FALSE_LOGE_AND_RETURN(pair != errorCode2Str.end());
111 IAM_LOGE("ThrowBusinessError, result: %{public}d, errorCode: %{public}d, errmsg: %{public}s", result, pair->first,
112 pair->second.c_str());
113 set_business_error(pair->first, pair->second);
114 }
115
116 class FaceAuthManagerImpl {
117 public:
FaceAuthManagerImpl()118 FaceAuthManagerImpl()
119 {
120 }
setSurfaceId(string_view surfaceId)121 void setSurfaceId(string_view surfaceId)
122 {
123 std::string surfaceIdStr(surfaceId);
124 int32_t ret = SetSurfaceId(surfaceIdStr);
125 if (ret != FACE_AUTH_SUCCESS) {
126 IAM_LOGE("SetSurfaceId fail, result: %{public}d", ret);
127 ThrowBusinessError(ret);
128 return;
129 }
130 }
131 };
132 } // namespace
133 } // namespace FaceAuth
134 } // namespace UserIam
135 } // namespace OHOS
136
137 namespace {
CreateFaceAuthManager()138 ::ohos::userIAM::faceAuth::FaceAuthManager CreateFaceAuthManager()
139 {
140 // The parameters in the make_holder function should be of the same type
141 // as the parameters in the constructor of the actual implementation class.
142 return make_holder<OHOS::UserIam::FaceAuth::FaceAuthManagerImpl, ohos::userIAM::faceAuth::FaceAuthManager>();
143 }
144 } // namespace
145
146 TH_EXPORT_CPP_API_CreateFaceAuthManager(CreateFaceAuthManager);
147