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