1 /*
2 * Copyright (c) 2023-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 "ability_auto_startup_data_manager.h"
17
18 #include <unistd.h>
19
20 #include "ability_manager_constants.h"
21 #include "accesstoken_kit.h"
22 #include "hilog_tag_wrapper.h"
23 #include "json_utils.h"
24 #include "os_account_manager_wrapper.h"
25
26 namespace OHOS {
27 namespace AbilityRuntime {
28 using namespace OHOS::AAFwk;
29 namespace {
30 constexpr int32_t CHECK_INTERVAL = 100000; // 100ms
31 constexpr int32_t MAX_TIMES = 5; // 5 * 100ms = 500ms
32 constexpr const char *AUTO_STARTUP_STORAGE_DIR = "/data/service/el1/public/database/auto_startup_service";
33 const std::string JSON_KEY_BUNDLE_NAME = "bundleName";
34 const std::string JSON_KEY_ABILITY_NAME = "abilityName";
35 const std::string JSON_KEY_MODULE_NAME = "moduleName";
36 const std::string JSON_KEY_IS_AUTO_STARTUP = "isAutoStartup";
37 const std::string JSON_KEY_IS_EDM_FORCE = "isEdmForce";
38 const std::string JSON_KEY_TYPE_NAME = "abilityTypeName";
39 const std::string JSON_KEY_APP_CLONE_INDEX = "appCloneIndex";
40 const std::string JSON_KEY_ACCESS_TOKENID = "accessTokenId";
41 const std::string JSON_KEY_SETTER_USERID = "setterUserId";
42 const std::string JSON_KEY_USERID = "userId";
43 const std::string JSON_KEY_SETTER_TYPE = "setterType";
44 } // namespace
45 const DistributedKv::AppId AbilityAutoStartupDataManager::APP_ID = { "auto_startup_storage" };
46 const DistributedKv::StoreId AbilityAutoStartupDataManager::STORE_ID = { "auto_startup_infos" };
AbilityAutoStartupDataManager()47 AbilityAutoStartupDataManager::AbilityAutoStartupDataManager() {}
48
~AbilityAutoStartupDataManager()49 AbilityAutoStartupDataManager::~AbilityAutoStartupDataManager()
50 {
51 if (kvStorePtr_ != nullptr) {
52 dataManager_.CloseKvStore(APP_ID, kvStorePtr_);
53 }
54 }
55
RestoreKvStore(DistributedKv::Status status)56 DistributedKv::Status AbilityAutoStartupDataManager::RestoreKvStore(DistributedKv::Status status)
57 {
58 if (status == DistributedKv::Status::DATA_CORRUPTED) {
59 DistributedKv::Options options = { .createIfMissing = true,
60 .encrypt = false,
61 .autoSync = false,
62 .syncable = false,
63 .securityLevel = DistributedKv::SecurityLevel::S2,
64 .area = DistributedKv::EL1,
65 .kvStoreType = DistributedKv::KvStoreType::SINGLE_VERSION,
66 .baseDir = AUTO_STARTUP_STORAGE_DIR };
67 TAG_LOGI(AAFwkTag::AUTO_STARTUP, "corrupted, deleting db");
68 dataManager_.DeleteKvStore(APP_ID, STORE_ID, options.baseDir);
69 TAG_LOGI(AAFwkTag::AUTO_STARTUP, "deleted corrupted db, recreating db");
70 status = dataManager_.GetSingleKvStore(options, APP_ID, STORE_ID, kvStorePtr_);
71 TAG_LOGI(AAFwkTag::AUTO_STARTUP, "recreate db result:%{public}d", status);
72 }
73 return status;
74 }
75
GetKvStore()76 DistributedKv::Status AbilityAutoStartupDataManager::GetKvStore()
77 {
78 DistributedKv::Options options = { .createIfMissing = true,
79 .encrypt = false,
80 .autoSync = false,
81 .syncable = false,
82 .securityLevel = DistributedKv::SecurityLevel::S2,
83 .area = DistributedKv::EL1,
84 .kvStoreType = DistributedKv::KvStoreType::SINGLE_VERSION,
85 .baseDir = AUTO_STARTUP_STORAGE_DIR };
86
87 DistributedKv::Status status = dataManager_.GetSingleKvStore(options, APP_ID, STORE_ID, kvStorePtr_);
88 if (status != DistributedKv::Status::SUCCESS) {
89 TAG_LOGE(AAFwkTag::AUTO_STARTUP, "Error: %{public}d", status);
90 status = RestoreKvStore(status);
91 return status;
92 }
93
94 TAG_LOGD(AAFwkTag::AUTO_STARTUP, "Get kvStore success");
95 return status;
96 }
97
CheckKvStore()98 bool AbilityAutoStartupDataManager::CheckKvStore()
99 {
100 if (kvStorePtr_ != nullptr) {
101 return true;
102 }
103 int32_t tryTimes = MAX_TIMES;
104 while (tryTimes > 0) {
105 DistributedKv::Status status = GetKvStore();
106 if (status == DistributedKv::Status::SUCCESS && kvStorePtr_ != nullptr) {
107 return true;
108 }
109 TAG_LOGD(AAFwkTag::AUTO_STARTUP, "Try times: %{public}d", tryTimes);
110 usleep(CHECK_INTERVAL);
111 tryTimes--;
112 }
113 return kvStorePtr_ != nullptr;
114 }
115
InsertAutoStartupData(const AutoStartupInfo & info,bool isAutoStartup,bool isEdmForce)116 int32_t AbilityAutoStartupDataManager::InsertAutoStartupData(
117 const AutoStartupInfo &info, bool isAutoStartup, bool isEdmForce)
118 {
119 if (info.bundleName.empty() || info.abilityName.empty() || info.accessTokenId.empty() ||
120 info.setterUserId == -1 || info.userId == -1 || info.setterType == AutoStartupSetterType::UNSPECIFIED) {
121 TAG_LOGE(AAFwkTag::AUTO_STARTUP, "Invalid value");
122 return ERR_INVALID_VALUE;
123 }
124
125 TAG_LOGD(AAFwkTag::AUTO_STARTUP,
126 "bundleName: %{public}s, moduleName: %{public}s, abilityName: %{public}s,"
127 " accessTokenId: %{public}s, setterUserId: %{public}d, userId: %{public}d, setterType: %{public}d",
128 info.bundleName.c_str(), info.moduleName.c_str(),
129 info.abilityName.c_str(), info.accessTokenId.c_str(), info.setterUserId, info.userId,
130 static_cast<int32_t>(info.setterType));
131 {
132 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
133 if (!CheckKvStore()) {
134 TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null kvStore");
135 return ERR_NO_INIT;
136 }
137 }
138
139 DistributedKv::Key key = ConvertAutoStartupDataToKey(info);
140 DistributedKv::Value value = ConvertAutoStartupStatusToValue(info, isAutoStartup, isEdmForce);
141 DistributedKv::Status status;
142 {
143 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
144 status = kvStorePtr_->Put(key, value);
145 }
146
147 if (status != DistributedKv::Status::SUCCESS) {
148 TAG_LOGE(AAFwkTag::AUTO_STARTUP, "kvStore insert error: %{public}d", status);
149 {
150 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
151 status = RestoreKvStore(status);
152 }
153 return ERR_INVALID_OPERATION;
154 }
155 return ERR_OK;
156 }
157
UpdateAutoStartupData(const AutoStartupInfo & info,bool isAutoStartup,bool isEdmForce)158 int32_t AbilityAutoStartupDataManager::UpdateAutoStartupData(
159 const AutoStartupInfo &info, bool isAutoStartup, bool isEdmForce)
160 {
161 if (info.bundleName.empty() || info.abilityName.empty() || info.accessTokenId.empty() ||
162 info.setterUserId == -1 || info.userId == -1 || info.setterType == AutoStartupSetterType::UNSPECIFIED) {
163 TAG_LOGW(AAFwkTag::AUTO_STARTUP, "Invalid value");
164 return ERR_INVALID_VALUE;
165 }
166
167 TAG_LOGD(AAFwkTag::AUTO_STARTUP,
168 "bundleName: %{public}s, moduleName: %{public}s, abilityName: %{public}s,"
169 " accessTokenId: %{public}s, setterUserId: %{public}d, userId: %{public}d, setterType: %{public}d",
170 info.bundleName.c_str(), info.moduleName.c_str(),
171 info.abilityName.c_str(), info.accessTokenId.c_str(), info.setterUserId, info.userId,
172 static_cast<int32_t>(info.setterType));
173 {
174 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
175 if (!CheckKvStore()) {
176 TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null kvStore");
177 return ERR_NO_INIT;
178 }
179 }
180
181 DistributedKv::Key key = ConvertAutoStartupDataToKey(info);
182 DistributedKv::Status status;
183 {
184 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
185 status = kvStorePtr_->Delete(key);
186 }
187 if (status != DistributedKv::Status::SUCCESS) {
188 TAG_LOGE(AAFwkTag::AUTO_STARTUP, "kvStore delete error: %{public}d", status);
189 {
190 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
191 status = RestoreKvStore(status);
192 }
193 return ERR_INVALID_OPERATION;
194 }
195 DistributedKv::Value value = ConvertAutoStartupStatusToValue(info, isAutoStartup, isEdmForce);
196 {
197 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
198 status = kvStorePtr_->Put(key, value);
199 }
200 if (status != DistributedKv::Status::SUCCESS) {
201 TAG_LOGE(AAFwkTag::AUTO_STARTUP, "kvStore insert error: %{public}d", status);
202 {
203 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
204 status = RestoreKvStore(status);
205 }
206 return ERR_INVALID_OPERATION;
207 }
208
209 return ERR_OK;
210 }
211
DeleteAutoStartupData(const AutoStartupInfo & info)212 int32_t AbilityAutoStartupDataManager::DeleteAutoStartupData(const AutoStartupInfo &info)
213 {
214 if (info.bundleName.empty() || info.abilityName.empty() || info.accessTokenId.empty() || info.userId == -1) {
215 TAG_LOGW(AAFwkTag::AUTO_STARTUP, "Invalid value");
216 return ERR_INVALID_VALUE;
217 }
218
219 TAG_LOGD(AAFwkTag::AUTO_STARTUP,
220 "bundleName: %{public}s, moduleName: %{public}s, abilityName: %{public}s,"
221 " accessTokenId: %{public}s, userId:%{public}d",
222 info.bundleName.c_str(), info.moduleName.c_str(),
223 info.abilityName.c_str(), info.accessTokenId.c_str(), info.userId);
224 {
225 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
226 if (!CheckKvStore()) {
227 TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null kvStore");
228 return ERR_NO_INIT;
229 }
230 }
231
232 DistributedKv::Key key = ConvertAutoStartupDataToKey(info);
233 DistributedKv::Status status;
234 {
235 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
236 status = kvStorePtr_->Delete(key);
237 }
238
239 if (status != DistributedKv::Status::SUCCESS) {
240 TAG_LOGE(AAFwkTag::AUTO_STARTUP, "kvStore delete error: %{public}d", status);
241 {
242 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
243 status = RestoreKvStore(status);
244 }
245 return ERR_INVALID_OPERATION;
246 }
247 return ERR_OK;
248 }
249
DeleteAutoStartupData(const std::string & bundleName,int32_t accessTokenId)250 int32_t AbilityAutoStartupDataManager::DeleteAutoStartupData(const std::string &bundleName, int32_t accessTokenId)
251 {
252 auto accessTokenIdStr = std::to_string(accessTokenId);
253 if (bundleName.empty() || accessTokenIdStr.empty()) {
254 TAG_LOGW(AAFwkTag::AUTO_STARTUP, "Invalid value");
255 return ERR_INVALID_VALUE;
256 }
257
258 TAG_LOGD(AAFwkTag::AUTO_STARTUP, "bundleName: %{public}s, accessTokenId: %{public}s",
259 bundleName.c_str(), accessTokenIdStr.c_str());
260 {
261 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
262 if (!CheckKvStore()) {
263 TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null kvStore");
264 return ERR_NO_INIT;
265 }
266 }
267
268 std::vector<DistributedKv::Entry> allEntries;
269 DistributedKv::Status status = DistributedKv::Status::SUCCESS;
270 {
271 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
272 status = kvStorePtr_->GetEntries(nullptr, allEntries);
273 }
274 if (status != DistributedKv::Status::SUCCESS) {
275 TAG_LOGE(AAFwkTag::AUTO_STARTUP, "GetEntries error: %{public}d", status);
276 {
277 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
278 status = RestoreKvStore(status);
279 }
280 return ERR_INVALID_OPERATION;
281 }
282
283 for (const auto &item : allEntries) {
284 if (IsEqual(item.key, accessTokenIdStr)) {
285 {
286 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
287 status = kvStorePtr_->Delete(item.key);
288 }
289 if (status != DistributedKv::Status::SUCCESS) {
290 TAG_LOGE(AAFwkTag::AUTO_STARTUP, "kvStore delete error: %{public}d", status);
291 {
292 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
293 status = RestoreKvStore(status);
294 }
295 return ERR_INVALID_OPERATION;
296 }
297 }
298 }
299
300 return ERR_OK;
301 }
302
QueryAutoStartupData(const AutoStartupInfo & info)303 AutoStartupStatus AbilityAutoStartupDataManager::QueryAutoStartupData(const AutoStartupInfo &info)
304 {
305 AutoStartupStatus startupStatus;
306 if (info.bundleName.empty() || info.abilityName.empty() || info.accessTokenId.empty() || info.userId == -1) {
307 TAG_LOGW(AAFwkTag::AUTO_STARTUP, "Invalid value");
308 startupStatus.code = ERR_INVALID_VALUE;
309 return startupStatus;
310 }
311
312 TAG_LOGD(AAFwkTag::AUTO_STARTUP,
313 "bundleName: %{public}s, moduleName: %{public}s, abilityName: %{public}s,"
314 " accessTokenId: %{public}s, userId: %{public}d",
315 info.bundleName.c_str(), info.moduleName.c_str(),
316 info.abilityName.c_str(), info.accessTokenId.c_str(), info.userId);
317 {
318 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
319 if (!CheckKvStore()) {
320 TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null kvStore");
321 startupStatus.code = ERR_NO_INIT;
322 return startupStatus;
323 }
324 }
325
326 std::vector<DistributedKv::Entry> allEntries;
327 DistributedKv::Status status = DistributedKv::Status::SUCCESS;
328 {
329 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
330 status = kvStorePtr_->GetEntries(nullptr, allEntries);
331 }
332 if (status != DistributedKv::Status::SUCCESS) {
333 TAG_LOGE(AAFwkTag::AUTO_STARTUP, "GetEntries error: %{public}d", status);
334 {
335 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
336 status = RestoreKvStore(status);
337 }
338 startupStatus.code = ERR_INVALID_OPERATION;
339 return startupStatus;
340 }
341
342 startupStatus.code = ERR_NAME_NOT_FOUND;
343 for (const auto &item : allEntries) {
344 if (IsEqual(item.key, info)) {
345 ConvertAutoStartupStatusFromValue(item.value, startupStatus);
346 startupStatus.code = ERR_OK;
347 }
348 }
349
350 return startupStatus;
351 }
352
QueryAllAutoStartupApplications(std::vector<AutoStartupInfo> & infoList,int32_t userId,bool isCalledByEDM)353 int32_t AbilityAutoStartupDataManager::QueryAllAutoStartupApplications(std::vector<AutoStartupInfo> &infoList,
354 int32_t userId, bool isCalledByEDM)
355 {
356 TAG_LOGD(AAFwkTag::AUTO_STARTUP, "called");
357 {
358 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
359 if (!CheckKvStore()) {
360 TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null kvStore");
361 return ERR_NO_INIT;
362 }
363 }
364
365 std::vector<DistributedKv::Entry> allEntries;
366 DistributedKv::Status status = DistributedKv::Status::SUCCESS;
367 {
368 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
369 status = kvStorePtr_->GetEntries(nullptr, allEntries);
370 }
371 if (status != DistributedKv::Status::SUCCESS) {
372 TAG_LOGE(AAFwkTag::AUTO_STARTUP, "GetEntries: %{public}d", status);
373 {
374 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
375 status = RestoreKvStore(status);
376 }
377 return ERR_INVALID_OPERATION;
378 }
379
380 for (const auto &item : allEntries) {
381 if ((!isCalledByEDM) && (!IsEqual(item.key, userId))) {
382 continue;
383 }
384 AutoStartupStatus startupStatus;
385 ConvertAutoStartupStatusFromValue(item.value, startupStatus);
386 if (startupStatus.isAutoStartup) {
387 infoList.emplace_back(ConvertAutoStartupInfoFromKeyAndValue(item.key, item.value));
388 }
389 }
390 TAG_LOGD(AAFwkTag::AUTO_STARTUP, "InfoList.size: %{public}zu", infoList.size());
391 return ERR_OK;
392 }
393
GetCurrentAppAutoStartupData(const std::string & bundleName,std::vector<AutoStartupInfo> & infoList,const std::string & accessTokenId)394 int32_t AbilityAutoStartupDataManager::GetCurrentAppAutoStartupData(
395 const std::string &bundleName, std::vector<AutoStartupInfo> &infoList, const std::string &accessTokenId)
396 {
397 TAG_LOGD(AAFwkTag::AUTO_STARTUP, "called");
398 {
399 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
400 if (!CheckKvStore()) {
401 TAG_LOGE(AAFwkTag::AUTO_STARTUP, "null kvStore");
402 return ERR_NO_INIT;
403 }
404 }
405
406 std::vector<DistributedKv::Entry> allEntries;
407 DistributedKv::Status status = DistributedKv::Status::SUCCESS;
408 {
409 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
410 status = kvStorePtr_->GetEntries(nullptr, allEntries);
411 }
412 if (status != DistributedKv::Status::SUCCESS) {
413 TAG_LOGE(AAFwkTag::AUTO_STARTUP, "GetEntries error: %{public}d", status);
414 {
415 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
416 status = RestoreKvStore(status);
417 }
418 return ERR_INVALID_OPERATION;
419 }
420
421 for (const auto &item : allEntries) {
422 if (IsEqual(item.key, accessTokenId)) {
423 infoList.emplace_back(ConvertAutoStartupInfoFromKeyAndValue(item.key, item.value));
424 }
425 }
426 TAG_LOGD(AAFwkTag::AUTO_STARTUP, "InfoList.size: %{public}zu", infoList.size());
427 return ERR_OK;
428 }
429
ConvertAutoStartupStatusToValue(const AutoStartupInfo & info,bool isAutoStartup,bool isEdmForce)430 DistributedKv::Value AbilityAutoStartupDataManager::ConvertAutoStartupStatusToValue(
431 const AutoStartupInfo &info, bool isAutoStartup, bool isEdmForce)
432 {
433 nlohmann::json jsonObject = nlohmann::json {
434 { JSON_KEY_IS_AUTO_STARTUP, isAutoStartup },
435 { JSON_KEY_IS_EDM_FORCE, isEdmForce },
436 { JSON_KEY_TYPE_NAME, info.abilityTypeName },
437 { JSON_KEY_SETTER_USERID, info.setterUserId },
438 { JSON_KEY_SETTER_TYPE, info.setterType },
439 };
440 DistributedKv::Value value(jsonObject.dump());
441 TAG_LOGD(AAFwkTag::AUTO_STARTUP, "value: %{public}s", value.ToString().c_str());
442 return value;
443 }
444
ConvertAutoStartupStatusFromValue(const DistributedKv::Value & value,AutoStartupStatus & startupStatus)445 void AbilityAutoStartupDataManager::ConvertAutoStartupStatusFromValue(
446 const DistributedKv::Value &value, AutoStartupStatus &startupStatus)
447 {
448 nlohmann::json jsonObject = nlohmann::json::parse(value.ToString(), nullptr, false);
449 if (jsonObject.is_discarded()) {
450 TAG_LOGE(AAFwkTag::AUTO_STARTUP, "parse jsonObject fail");
451 return;
452 }
453 if (jsonObject.contains(JSON_KEY_IS_AUTO_STARTUP) && jsonObject[JSON_KEY_IS_AUTO_STARTUP].is_boolean()) {
454 startupStatus.isAutoStartup = jsonObject.at(JSON_KEY_IS_AUTO_STARTUP).get<bool>();
455 }
456 if (jsonObject.contains(JSON_KEY_IS_EDM_FORCE) && jsonObject[JSON_KEY_IS_EDM_FORCE].is_boolean()) {
457 startupStatus.isEdmForce = jsonObject.at(JSON_KEY_IS_EDM_FORCE).get<bool>();
458 }
459 if (jsonObject.contains(JSON_KEY_SETTER_USERID) && jsonObject[JSON_KEY_SETTER_USERID].is_number()) {
460 startupStatus.setterUserId = jsonObject.at(JSON_KEY_SETTER_USERID).get<int32_t>();
461 }
462 if (jsonObject.contains(JSON_KEY_SETTER_TYPE) && jsonObject[JSON_KEY_SETTER_TYPE].is_number()) {
463 startupStatus.setterType = jsonObject.at(JSON_KEY_SETTER_TYPE).get<AutoStartupSetterType>();
464 }
465 }
466
ConvertAutoStartupDataToKey(const AutoStartupInfo & info)467 DistributedKv::Key AbilityAutoStartupDataManager::ConvertAutoStartupDataToKey(const AutoStartupInfo &info)
468 {
469 nlohmann::json jsonObject = nlohmann::json {
470 { JSON_KEY_BUNDLE_NAME, info.bundleName },
471 { JSON_KEY_MODULE_NAME, info.moduleName },
472 { JSON_KEY_ABILITY_NAME, info.abilityName },
473 { JSON_KEY_APP_CLONE_INDEX, info.appCloneIndex },
474 { JSON_KEY_ACCESS_TOKENID, info.accessTokenId },
475 { JSON_KEY_USERID, info.userId },
476 };
477 DistributedKv::Key key(jsonObject.dump());
478 TAG_LOGD(AAFwkTag::AUTO_STARTUP, "key: %{public}s", key.ToString().c_str());
479 return key;
480 }
481
ConvertAutoStartupInfoFromKeyAndValue(const DistributedKv::Key & key,const DistributedKv::Value & value)482 AutoStartupInfo AbilityAutoStartupDataManager::ConvertAutoStartupInfoFromKeyAndValue(
483 const DistributedKv::Key &key, const DistributedKv::Value &value)
484 {
485 AutoStartupInfo info;
486 ConvertAutoStartupInfoFromKey(key, info);
487 ConvertAutoStartupInfoFromValue(value, info);
488 return info;
489 }
490
ConvertAutoStartupInfoFromKey(const DistributedKv::Key & key,AutoStartupInfo & info)491 void AbilityAutoStartupDataManager::ConvertAutoStartupInfoFromKey(
492 const DistributedKv::Key &key, AutoStartupInfo &info)
493 {
494 nlohmann::json jsonObject = nlohmann::json::parse(key.ToString(), nullptr, false);
495 if (jsonObject.is_discarded()) {
496 TAG_LOGE(AAFwkTag::AUTO_STARTUP, "parse jsonObject fail");
497 return;
498 }
499
500 if (jsonObject.contains(JSON_KEY_BUNDLE_NAME) && jsonObject[JSON_KEY_BUNDLE_NAME].is_string()) {
501 info.bundleName = jsonObject.at(JSON_KEY_BUNDLE_NAME).get<std::string>();
502 }
503
504 if (jsonObject.contains(JSON_KEY_MODULE_NAME) && jsonObject[JSON_KEY_MODULE_NAME].is_string()) {
505 info.moduleName = jsonObject.at(JSON_KEY_MODULE_NAME).get<std::string>();
506 }
507
508 if (jsonObject.contains(JSON_KEY_ABILITY_NAME) && jsonObject[JSON_KEY_ABILITY_NAME].is_string()) {
509 info.abilityName = jsonObject.at(JSON_KEY_ABILITY_NAME).get<std::string>();
510 }
511
512 if (jsonObject.contains(JSON_KEY_APP_CLONE_INDEX) && jsonObject[JSON_KEY_APP_CLONE_INDEX].is_number()) {
513 info.appCloneIndex = jsonObject.at(JSON_KEY_APP_CLONE_INDEX).get<int32_t>();
514 }
515
516 if (jsonObject.contains(JSON_KEY_ACCESS_TOKENID) && jsonObject[JSON_KEY_ACCESS_TOKENID].is_string()) {
517 info.accessTokenId = jsonObject.at(JSON_KEY_ACCESS_TOKENID).get<std::string>();
518 }
519
520 if (jsonObject.contains(JSON_KEY_USERID) && jsonObject[JSON_KEY_USERID].is_number()) {
521 info.userId = jsonObject.at(JSON_KEY_USERID).get<int32_t>();
522 }
523 }
524
ConvertAutoStartupInfoFromValue(const DistributedKv::Value & value,AutoStartupInfo & info)525 void AbilityAutoStartupDataManager::ConvertAutoStartupInfoFromValue(
526 const DistributedKv::Value &value, AutoStartupInfo &info)
527 {
528 nlohmann::json jsonValueObject = nlohmann::json::parse(value.ToString(), nullptr, false);
529 if (jsonValueObject.is_discarded()) {
530 TAG_LOGE(AAFwkTag::AUTO_STARTUP, "parse jsonValueObject fail");
531 return;
532 }
533
534 if (jsonValueObject.contains(JSON_KEY_TYPE_NAME) && jsonValueObject[JSON_KEY_TYPE_NAME].is_string()) {
535 info.abilityTypeName = jsonValueObject.at(JSON_KEY_TYPE_NAME).get<std::string>();
536 }
537
538 if (jsonValueObject.contains(JSON_KEY_IS_EDM_FORCE) && jsonValueObject[JSON_KEY_IS_EDM_FORCE].is_boolean()) {
539 info.canUserModify = !(jsonValueObject.at(JSON_KEY_IS_EDM_FORCE).get<bool>());
540 }
541
542 if (jsonValueObject.contains(JSON_KEY_SETTER_USERID) && jsonValueObject[JSON_KEY_SETTER_USERID].is_number()) {
543 info.setterUserId = jsonValueObject.at(JSON_KEY_SETTER_USERID).get<int32_t>();
544 }
545 }
546
IsEqual(const DistributedKv::Key & key,const AutoStartupInfo & info)547 bool AbilityAutoStartupDataManager::IsEqual(const DistributedKv::Key &key, const AutoStartupInfo &info)
548 {
549 nlohmann::json jsonObject = nlohmann::json::parse(key.ToString(), nullptr, false);
550 if (jsonObject.is_discarded()) {
551 TAG_LOGE(AAFwkTag::AUTO_STARTUP, "parse jsonObject fail");
552 return false;
553 }
554
555 if (!AAFwk::JsonUtils::GetInstance().IsEqual(jsonObject, JSON_KEY_BUNDLE_NAME, info.bundleName)
556 || !AAFwk::JsonUtils::GetInstance().IsEqual(jsonObject, JSON_KEY_ABILITY_NAME, info.abilityName)
557 || !AAFwk::JsonUtils::GetInstance().IsEqual(jsonObject, JSON_KEY_MODULE_NAME, info.moduleName, true)
558 || !AAFwk::JsonUtils::GetInstance().IsEqual(jsonObject, JSON_KEY_APP_CLONE_INDEX, info.appCloneIndex)
559 || !AAFwk::JsonUtils::GetInstance().IsEqual(jsonObject, JSON_KEY_ACCESS_TOKENID, info.accessTokenId)
560 || !AAFwk::JsonUtils::GetInstance().IsEqual(jsonObject, JSON_KEY_USERID, info.userId)) {
561 return false;
562 }
563 return true;
564 }
565
IsEqual(const DistributedKv::Key & key,const std::string & accessTokenId)566 bool AbilityAutoStartupDataManager::IsEqual(const DistributedKv::Key &key, const std::string &accessTokenId)
567 {
568 nlohmann::json jsonObject = nlohmann::json::parse(key.ToString(), nullptr, false);
569 if (jsonObject.is_discarded()) {
570 TAG_LOGE(AAFwkTag::AUTO_STARTUP, "parse jsonObject fail");
571 return false;
572 }
573
574 if (jsonObject.contains(JSON_KEY_ACCESS_TOKENID) && jsonObject[JSON_KEY_ACCESS_TOKENID].is_string()) {
575 if (accessTokenId == jsonObject.at(JSON_KEY_ACCESS_TOKENID).get<std::string>()) {
576 return true;
577 }
578 }
579 return false;
580 }
581
IsEqual(const DistributedKv::Key & key,int32_t userId)582 bool AbilityAutoStartupDataManager::IsEqual(const DistributedKv::Key &key, int32_t userId)
583 {
584 nlohmann::json jsonObject = nlohmann::json::parse(key.ToString(), nullptr, false);
585 if (jsonObject.is_discarded()) {
586 TAG_LOGE(AAFwkTag::AUTO_STARTUP, "parse jsonObject fail");
587 return false;
588 }
589 auto &jsonUtil = AAFwk::JsonUtils::GetInstance();
590 if (jsonUtil.IsEqual(jsonObject, JSON_KEY_USERID, userId) ||
591 jsonUtil.IsEqual(jsonObject, JSON_KEY_USERID, U0_USER_ID) ||
592 jsonUtil.IsEqual(jsonObject, JSON_KEY_USERID, U1_USER_ID)) {
593 return true;
594 }
595 return false;
596 }
597 } // namespace AbilityRuntime
598 } // namespace OHOS
599