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 #include "napi_hiappevent_userinfo.h"
16
17 #include <map>
18 #include <string>
19
20 #include "hiappevent_base.h"
21 #include "hiappevent_userinfo.h"
22 #include "hiappevent_verify.h"
23 #include "hilog/log.h"
24 #include "napi_error.h"
25 #include "napi_util.h"
26
27 #undef LOG_DOMAIN
28 #define LOG_DOMAIN 0xD002D07
29
30 #undef LOG_TAG
31 #define LOG_TAG "NapiUserInfo"
32
33 namespace OHOS {
34 namespace HiviewDFX {
35 namespace NapiHiAppEventUserInfo {
36 namespace {
IsStringEmptyOrNull(const napi_env env,const napi_value name)37 bool IsStringEmptyOrNull(const napi_env env, const napi_value name)
38 {
39 napi_valuetype napiType = NapiUtil::GetType(env, name);
40 if (napiType == napi_null) {
41 return true;
42 }
43 if (napiType != napi_string) {
44 return false;
45 }
46 std::string strName = NapiUtil::GetString(env, name);
47 return strName.empty();
48 }
49 }
50
SetUserId(const napi_env env,const napi_value name,const napi_value userId)51 bool SetUserId(const napi_env env, const napi_value name, const napi_value userId)
52 {
53 if (!NapiUtil::IsString(env, name)) {
54 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, NapiUtil::CreateErrMsg("name", "string"));
55 return false;
56 }
57 std::string strName = NapiUtil::GetString(env, name);
58 if (!IsValidUserIdName(strName)) {
59 HILOG_WARN(LOG_CORE, "Parameter error. The name parameter is invalid.");
60 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, "Parameter error. The name parameter is invalid.");
61 return false;
62 }
63 if (IsStringEmptyOrNull(env, userId)) {
64 if (HiAppEvent::UserInfo::GetInstance().RemoveUserId(strName) != 0) {
65 HILOG_ERROR(LOG_CORE, "failed to remove userId");
66 return false;
67 }
68 return true;
69 }
70 if (!NapiUtil::IsString(env, userId)) {
71 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, NapiUtil::CreateErrMsg("userId", "string"));
72 return false;
73 }
74 std::string strUserId = NapiUtil::GetString(env, userId);
75 if (!IsValidUserIdValue(strUserId)) {
76 HILOG_WARN(LOG_CORE, "Parameter error. The value parameter is invalid.");
77 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, "Parameter error. The value parameter is invalid.");
78 return false;
79 }
80 if (HiAppEvent::UserInfo::GetInstance().SetUserId(strName, strUserId) != 0) {
81 HILOG_ERROR(LOG_CORE, "failed to set userId");
82 return false;
83 }
84 return true;
85 }
86
GetUserId(const napi_env env,const napi_value name,napi_value & out)87 bool GetUserId(const napi_env env, const napi_value name, napi_value& out)
88 {
89 if (!NapiUtil::IsString(env, name)) {
90 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, NapiUtil::CreateErrMsg("name", "string"));
91 return false;
92 }
93 std::string strName = NapiUtil::GetString(env, name);
94 if (!IsValidUserIdName(strName)) {
95 HILOG_WARN(LOG_CORE, "Parameter error. The name parameter is invalid.");
96 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, "Parameter error. The name parameter is invalid.");
97 return false;
98 }
99 std::string strUserId;
100 if (HiAppEvent::UserInfo::GetInstance().GetUserId(strName, strUserId) != 0) {
101 HILOG_ERROR(LOG_CORE, "failed to get userId");
102 return false;
103 }
104 out = NapiUtil::CreateString(env, strUserId);
105 return true;
106 }
107
SetUserProperty(const napi_env env,const napi_value name,const napi_value userProperty)108 bool SetUserProperty(const napi_env env, const napi_value name, const napi_value userProperty)
109 {
110 if (!NapiUtil::IsString(env, name)) {
111 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, NapiUtil::CreateErrMsg("name", "string"));
112 return false;
113 }
114 std::string strName = NapiUtil::GetString(env, name);
115 if (!IsValidUserPropName(strName)) {
116 HILOG_WARN(LOG_CORE, "Parameter error. The name parameter is invalid.");
117 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, "Parameter error. The name parameter is invalid.");
118 return false;
119 }
120 if (IsStringEmptyOrNull(env, userProperty)) {
121 if (HiAppEvent::UserInfo::GetInstance().RemoveUserProperty(strName) != 0) {
122 HILOG_ERROR(LOG_CORE, "failed to remove user property");
123 return false;
124 }
125 return true;
126 }
127 if (!NapiUtil::IsString(env, userProperty)) {
128 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, NapiUtil::CreateErrMsg("user property", "string"));
129 return false;
130 }
131 std::string strUserProperty = NapiUtil::GetString(env, userProperty);
132 if (!IsValidUserPropValue(strUserProperty)) {
133 HILOG_WARN(LOG_CORE, "Parameter error. The value parameter is invalid.");
134 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, "Parameter error. The value parameter is invalid.");
135 return false;
136 }
137 if (HiAppEvent::UserInfo::GetInstance().SetUserProperty(strName, strUserProperty) != 0) {
138 HILOG_ERROR(LOG_CORE, "failed to set user property");
139 return false;
140 }
141 return true;
142 }
143
GetUserProperty(const napi_env env,const napi_value name,napi_value & out)144 bool GetUserProperty(const napi_env env, const napi_value name, napi_value& out)
145 {
146 if (!NapiUtil::IsString(env, name)) {
147 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, NapiUtil::CreateErrMsg("name", "string"));
148 return false;
149 }
150 std::string strName = NapiUtil::GetString(env, name);
151 if (!IsValidUserPropName(strName)) {
152 HILOG_WARN(LOG_CORE, "Parameter error. The name parameter is invalid.");
153 NapiUtil::ThrowError(env, NapiError::ERR_PARAM, "Parameter error. The name parameter is invalid.");
154 return false;
155 }
156 std::string strUserProperty;
157 if (HiAppEvent::UserInfo::GetInstance().GetUserProperty(strName, strUserProperty) != 0) {
158 HILOG_ERROR(LOG_CORE, "failed to get user property");
159 return false;
160 }
161 out = NapiUtil::CreateString(env, strUserProperty);
162 return true;
163 }
164 } // namespace NapiHiAppEventUserInfo
165 } // namespace HiviewDFX
166 } // namespace OHOS
167