1 /*
2 * Copyright (c) 2023-2024 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 "hiappevent_userinfo.h"
16
17 #include <mutex>
18 #include <string>
19
20 #include "app_event_store.h"
21 #include "app_event_observer_mgr.h"
22 #include "hiappevent_base.h"
23 #include "hiappevent_verify.h"
24 #include "hilog/log.h"
25
26 #undef LOG_DOMAIN
27 #define LOG_DOMAIN 0xD002D07
28
29 #undef LOG_TAG
30 #define LOG_TAG "UserInfo"
31
32 namespace OHOS {
33 namespace HiviewDFX {
34 namespace HiAppEvent {
35
36 namespace {
37 constexpr int DB_FAILED = -1;
38
39 std::mutex g_mutex;
40 }
41
GetInstance()42 UserInfo& UserInfo::GetInstance()
43 {
44 static UserInfo userInfo;
45 return userInfo;
46 }
47
UserInfo()48 UserInfo::UserInfo() : userIdVersion_(0), userPropertyVersion_(0)
49 {
50 InitUserIds();
51 InitUserProperties();
52 }
53
SetUserId(const std::string & name,const std::string & value)54 int UserInfo::SetUserId(const std::string& name, const std::string& value)
55 {
56 HILOG_DEBUG(LOG_CORE, "start to set userId.");
57
58 std::string out;
59 if (AppEventStore::GetInstance().QueryUserId(name, out) == DB_FAILED) {
60 HILOG_WARN(LOG_CORE, "failed to query user id.");
61 return -1;
62 }
63 if (out.empty()) {
64 if (AppEventStore::GetInstance().InsertUserId(name, value) == DB_FAILED) {
65 HILOG_WARN(LOG_CORE, "failed to insert user id.");
66 return -1;
67 }
68 } else {
69 if (AppEventStore::GetInstance().UpdateUserId(name, value) == DB_FAILED) {
70 HILOG_WARN(LOG_CORE, "failed to update user id.");
71 return -1;
72 }
73 }
74 std::lock_guard<std::mutex> lockGuard(g_mutex);
75 userIds_[name] = value;
76 userIdVersion_++;
77
78 return 0;
79 }
80
GetUserId(const std::string & name,std::string & out)81 int UserInfo::GetUserId(const std::string& name, std::string& out)
82 {
83 HILOG_DEBUG(LOG_CORE, "start to get userId.");
84
85 std::lock_guard<std::mutex> lockGuard(g_mutex);
86 if (userIds_.find(name) == userIds_.end()) {
87 HILOG_INFO(LOG_CORE, "no userid.");
88 return 0;
89 }
90 out = userIds_.at(name);
91
92 return 0;
93 }
94
RemoveUserId(const std::string & name)95 int UserInfo::RemoveUserId(const std::string& name)
96 {
97 HILOG_DEBUG(LOG_CORE, "start to remove userId.");
98
99 if (AppEventStore::GetInstance().DeleteUserId(name) == DB_FAILED) {
100 HILOG_WARN(LOG_CORE, "failed to remove userid.");
101 return -1;
102 }
103 std::lock_guard<std::mutex> lockGuard(g_mutex);
104 if (userIds_.find(name) != userIds_.end()) {
105 userIds_.erase(name);
106 userIdVersion_++;
107 }
108
109 return 0;
110 }
111
SetUserProperty(const std::string & name,const std::string & value)112 int UserInfo::SetUserProperty(const std::string& name, const std::string& value)
113 {
114 HILOG_DEBUG(LOG_CORE, "start to set userProperty.");
115
116 std::string out;
117 if (AppEventStore::GetInstance().QueryUserProperty(name, out) == DB_FAILED) {
118 HILOG_WARN(LOG_CORE, "failed to query user property.");
119 return -1;
120 }
121 if (out.empty()) {
122 if (AppEventStore::GetInstance().InsertUserProperty(name, value) == DB_FAILED) {
123 HILOG_WARN(LOG_CORE, "failed to insert user property.");
124 return -1;
125 }
126 } else {
127 if (AppEventStore::GetInstance().UpdateUserProperty(name, value) == DB_FAILED) {
128 HILOG_WARN(LOG_CORE, "failed to update user property.");
129 return -1;
130 }
131 }
132 std::lock_guard<std::mutex> lockGuard(g_mutex);
133 userProperties_[name] = value;
134 userPropertyVersion_++;
135
136 return 0;
137 }
138
GetUserProperty(const std::string & name,std::string & out)139 int UserInfo::GetUserProperty(const std::string& name, std::string& out)
140 {
141 HILOG_DEBUG(LOG_CORE, "start to get userProperty.");
142
143 std::lock_guard<std::mutex> lockGuard(g_mutex);
144 if (userProperties_.find(name) == userProperties_.end()) {
145 HILOG_INFO(LOG_CORE, "no user property.");
146 return 0;
147 }
148 out = userProperties_.at(name);
149
150 return 0;
151 }
152
RemoveUserProperty(const std::string & name)153 int UserInfo::RemoveUserProperty(const std::string& name)
154 {
155 HILOG_DEBUG(LOG_CORE, "start to remove userProperty.");
156
157 if (AppEventStore::GetInstance().DeleteUserProperty(name) == DB_FAILED) {
158 HILOG_WARN(LOG_CORE, "failed to remove user property.");
159 return -1;
160 }
161 std::lock_guard<std::mutex> lockGuard(g_mutex);
162 if (userProperties_.find(name) != userProperties_.end()) {
163 userProperties_.erase(name);
164 userPropertyVersion_++;
165 }
166
167 return 0;
168 }
169
InitUserIds()170 void UserInfo::InitUserIds()
171 {
172 userIds_.clear();
173 if (AppEventStore::GetInstance().QueryUserIds(userIds_) == DB_FAILED) {
174 HILOG_WARN(LOG_CORE, "failed to get user ids.");
175 return;
176 }
177 }
178
InitUserProperties()179 void UserInfo::InitUserProperties()
180 {
181 userProperties_.clear();
182 if (AppEventStore::GetInstance().QueryUserProperties(userProperties_) == DB_FAILED) {
183 HILOG_WARN(LOG_CORE, "failed to get user properties.");
184 return;
185 }
186 }
187
GetUserIds()188 std::vector<HiAppEvent::UserId> UserInfo::GetUserIds()
189 {
190 std::lock_guard<std::mutex> lockGuard(g_mutex);
191 std::vector<HiAppEvent::UserId> userIds;
192 for (auto it = userIds_.begin(); it != userIds_.end(); it++) {
193 HiAppEvent::UserId userId;
194 userId.name = it->first;
195 userId.value = it->second;
196 userIds.emplace_back(userId);
197 }
198 return userIds;
199 }
200
GetUserProperties()201 std::vector<HiAppEvent::UserProperty> UserInfo::GetUserProperties()
202 {
203 std::lock_guard<std::mutex> lockGuard(g_mutex);
204 std::vector<HiAppEvent::UserProperty> userProperties;
205 for (auto it = userProperties_.begin(); it != userProperties_.end(); it++) {
206 HiAppEvent::UserProperty userProperty;
207 userProperty.name = it->first;
208 userProperty.value = it->second;
209 userProperties.emplace_back(userProperty);
210 }
211 return userProperties;
212 }
213
GetUserIdVersion()214 int64_t UserInfo::GetUserIdVersion()
215 {
216 std::lock_guard<std::mutex> lockGuard(g_mutex);
217 return userIdVersion_;
218 }
219
GetUserPropertyVersion()220 int64_t UserInfo::GetUserPropertyVersion()
221 {
222 std::lock_guard<std::mutex> lockGuard(g_mutex);
223 return userPropertyVersion_;
224 }
225
ClearData()226 void UserInfo::ClearData()
227 {
228 std::lock_guard<std::mutex> lockGuard(g_mutex);
229 userIdVersion_ = 0;
230 userPropertyVersion_ = 0;
231 userIds_.clear();
232 userProperties_.clear();
233 }
234 } // namespace HiAppEvent
235 } // namespace HiviewDFX
236 } // namespace OHOS
237