1 /*
2 * Copyright (c) 2022 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 "user_delegate.h"
17
18 #define LOG_TAG "UserDelegate"
19
20 #include "account_delegate.h"
21 #include "communication_provider.h"
22 #include "device_kvstore_impl.h"
23 #include "executor_factory.h"
24 #include "kvstore_meta_manager.h"
25 #include "log_print.h"
26 #include "os_account_manager.h"
27
28 namespace OHOS::DistributedData {
29 using OHOS::AppDistributedKv::CommunicationProvider;
30 using namespace OHOS::DistributedKv;
31 using namespace OHOS::AccountSA;
GetLocalUserStatus()32 std::vector<UserStatus> UserDelegate::GetLocalUserStatus()
33 {
34 ZLOGI("begin");
35 auto deviceInfo = CommunicationProvider::GetInstance().GetLocalDevice();
36 if (deviceInfo.deviceId.empty()) {
37 ZLOGE("failed to get local device id");
38 return {};
39 }
40 return GetUsers(deviceInfo.deviceId);
41 }
42
GetRemoteUserStatus(const std::string & deviceId)43 std::vector<DistributedData::UserStatus> UserDelegate::GetRemoteUserStatus(const std::string &deviceId)
44 {
45 if (deviceId.empty()) {
46 ZLOGE("error input device id");
47 return {};
48 }
49 return GetUsers(deviceId);
50 }
51
GetUsers(const std::string & deviceId)52 std::vector<UserStatus> UserDelegate::GetUsers(const std::string &deviceId)
53 {
54 std::vector<UserStatus> userStatus;
55 if (!deviceUserMap_.Contains(deviceId)) {
56 LoadFromMeta(deviceId);
57 }
58 for (const auto &entry : deviceUserMap_[deviceId]) {
59 userStatus.emplace_back(entry.first, entry.second);
60 }
61 ZLOGI("device:%{public}.10s, users:%{public}s", deviceId.c_str(), Serializable::Marshall(userStatus).c_str());
62 return userStatus;
63 }
64
DeleteUsers(const std::string & deviceId)65 void UserDelegate::DeleteUsers(const std::string &deviceId)
66 {
67 deviceUserMap_.Erase(deviceId);
68 }
69
UpdateUsers(const std::string & deviceId,const std::vector<UserStatus> & userStatus)70 void UserDelegate::UpdateUsers(const std::string &deviceId, const std::vector<UserStatus> &userStatus)
71 {
72 ZLOGI("begin, device:%{public}.10s, users:%{public}u", deviceId.c_str(), userStatus.size());
73 deviceUserMap_.ComputeIfPresent(deviceId, [](const auto &key, std::map<int, bool> &userMap) {
74 for (auto &user : userMap) {
75 user.second = false;
76 }
77 });
78 for (auto &user : userStatus) {
79 deviceUserMap_[deviceId][user.id] = user.isActive;
80 }
81 ZLOGI("end, device:%{public}.10s, users:%{public}u", deviceId.c_str(), deviceUserMap_[deviceId].size());
82 }
83
InitLocalUserMeta()84 bool UserDelegate::InitLocalUserMeta()
85 {
86 std::vector<int> osAccountIds = { { 0, true } }; // system user default
87 auto ret = OsAccountManager::QueryActiveOsAccountIds(osAccountIds);
88 if (ret != 0 || osAccountIds.empty()) {
89 ZLOGE("failed to query os accounts, ret:%{public}d", ret);
90 return false;
91 }
92 std::vector<UserStatus> userStatus = { { 0, true } };
93 for (const auto &user : osAccountIds) {
94 userStatus.emplace_back(user, true);
95 }
96 UserMetaData userMetaData;
97 userMetaData.deviceId = DeviceKvStoreImpl::GetLocalDeviceId();
98 UpdateUsers(userMetaData.deviceId, userStatus);
99 for (auto &pair : deviceUserMap_[userMetaData.deviceId]) {
100 userMetaData.users.emplace_back(pair.first, pair.second);
101 }
102
103 auto &metaDelegate = KvStoreMetaManager::GetInstance().GetMetaKvStore();
104 if (metaDelegate == nullptr) {
105 ZLOGE("GetMetaKvStore return nullptr.");
106 return false;
107 }
108 auto dbKey = UserMetaRow::GetKeyFor(userMetaData.deviceId);
109 std::string jsonData = UserMetaData::Marshall(userMetaData);
110 DistributedDB::Value dbValue{ jsonData.begin(), jsonData.end() };
111 ret = metaDelegate->Put(dbKey, dbValue);
112 ZLOGI("put user meta data ret %{public}d", ret);
113 return ret == DistributedDB::DBStatus::OK;
114 }
115
LoadFromMeta(const std::string & deviceId)116 void UserDelegate::LoadFromMeta(const std::string &deviceId)
117 {
118 auto &metaDelegate = KvStoreMetaManager::GetInstance().GetMetaKvStore();
119 if (metaDelegate == nullptr) {
120 ZLOGE("GetMetaKvStore return nullptr.");
121 return;
122 }
123
124 auto dbKey = UserMetaRow::GetKeyFor(deviceId);
125 DistributedDB::Value dbValue;
126 auto ret = metaDelegate->Get(dbKey, dbValue);
127 UserMetaData userMetaData;
128 ZLOGI("get user meta data ret %{public}d", ret);
129 if (ret != DistributedDB::DBStatus::OK) {
130 return;
131 }
132 userMetaData.Unmarshall({ dbValue.begin(), dbValue.end() });
133 std::map<int, bool> userMap;
134 for (const auto &user : userMetaData.users) {
135 userMap[user.id] = user.isActive;
136 }
137 deviceUserMap_[deviceId] = userMap;
138 }
139
GetInstance()140 UserDelegate &UserDelegate::GetInstance()
141 {
142 static UserDelegate instance;
143 return instance;
144 }
145
Init()146 void UserDelegate::Init()
147 {
148 KvStoreTask retryTask([this]() {
149 do {
150 static constexpr int RETRY_INTERVAL = 500; // millisecond
151 std::this_thread::sleep_for(std::chrono::milliseconds(RETRY_INTERVAL));
152 if (!InitLocalUserMeta()) {
153 continue;
154 }
155 break;
156 } while (true);
157 ZLOGI("update user meta ok");
158 });
159 ExecutorFactory::GetInstance().Execute(std::move(retryTask));
160 auto ret = AccountDelegate::GetInstance()->Subscribe(std::make_shared<LocalUserObserver>(*this));
161 // subscribe user meta in other devices
162 KvStoreMetaManager::GetInstance().SubscribeMeta(UserMetaRow::KEY_PREFIX,
163 [this](const std::vector<uint8_t> &key, const std::vector<uint8_t> &value, CHANGE_FLAG flag) {
164 UserMetaData metaData;
165 metaData.Unmarshall({ value.begin(), value.end() });
166 ZLOGD("flag:%{public}d, value:%{public}.10s", flag, metaData.deviceId.c_str());
167 if (metaData.deviceId == DeviceKvStoreImpl::GetLocalDeviceId()) {
168 ZLOGD("ignore local device user meta change");
169 return;
170 }
171 if (flag == CHANGE_FLAG::INSERT || flag == CHANGE_FLAG::UPDATE) {
172 UpdateUsers(metaData.deviceId, metaData.users);
173 } else if (flag == CHANGE_FLAG::DELETE) {
174 DeleteUsers(metaData.deviceId);
175 } else {
176 ZLOGD("ignored operation");
177 }
178 });
179 ZLOGD("subscribe os account ret:%{public}d", ret);
180 }
181
NotifyUserEvent(const UserDelegate::UserEvent & userEvent)182 bool UserDelegate::NotifyUserEvent(const UserDelegate::UserEvent &userEvent)
183 {
184 // update all local user status
185 return InitLocalUserMeta();
186 }
187
LocalUserObserver(UserDelegate & userDelegate)188 UserDelegate::LocalUserObserver::LocalUserObserver(UserDelegate &userDelegate) : userDelegate_(userDelegate)
189 {
190 }
191
OnAccountChanged(const DistributedKv::AccountEventInfo & eventInfo)192 void UserDelegate::LocalUserObserver::OnAccountChanged(const DistributedKv::AccountEventInfo &eventInfo)
193 {
194 ZLOGI("event info:%{public}s, %{public}d", eventInfo.deviceAccountId.c_str(), eventInfo.status);
195 userDelegate_.NotifyUserEvent({}); // just notify
196 }
197
Name()198 std::string UserDelegate::LocalUserObserver::Name()
199 {
200 return "user_delegate";
201 }
202 } // namespace OHOS::DistributedData
203