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