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