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 
16 #ifndef OHOS_ABILITY_RUNTIME_ABILITY_AUTO_STARTUP_DATA_MANAGER_H
17 #define OHOS_ABILITY_RUNTIME_ABILITY_AUTO_STARTUP_DATA_MANAGER_H
18 
19 #include <mutex>
20 #include <vector>
21 
22 #include "auto_startup_info.h"
23 #include "distributed_kv_data_manager.h"
24 #include "nlohmann/json.hpp"
25 #include "singleton.h"
26 
27 namespace OHOS {
28 namespace AbilityRuntime {
29 class AbilityAutoStartupDataManager : public DelayedSingleton<AbilityAutoStartupDataManager> {
30 public:
31     AbilityAutoStartupDataManager();
32 
33     virtual ~AbilityAutoStartupDataManager();
34 
35     int32_t InsertAutoStartupData(const AutoStartupInfo &info, bool isAutoStartup, bool isEdmForce);
36 
37     int32_t UpdateAutoStartupData(const AutoStartupInfo &info, bool isAutoStartup, bool isEdmForce);
38 
39     int32_t DeleteAutoStartupData(const AutoStartupInfo &info);
40 
41     int32_t DeleteAutoStartupData(const std::string &bundleName, int32_t accessTokenId);
42 
43     AutoStartupStatus QueryAutoStartupData(const AutoStartupInfo &info);
44 
45     int32_t QueryAllAutoStartupApplications(std::vector<AutoStartupInfo> &infoList, int32_t userId);
46 
47     int32_t GetCurrentAppAutoStartupData(const std::string &bundleName,
48         std::vector<AutoStartupInfo> &infoList, const std::string &accessTokenId);
49 
50 private:
51     DistributedKv::Status RestoreKvStore(DistributedKv::Status status);
52     DistributedKv::Status GetKvStore();
53     bool CheckKvStore();
54     DistributedKv::Value ConvertAutoStartupStatusToValue(
55         bool isAutoStartup, bool isEdmForce, const std::string &abilityTypeName);
56     void ConvertAutoStartupStatusFromValue(const DistributedKv::Value &value, bool &isAutoStartup, bool &isEdmForce);
57     DistributedKv::Key ConvertAutoStartupDataToKey(const AutoStartupInfo &info);
58     AutoStartupInfo ConvertAutoStartupInfoFromKeyAndValue(
59         const DistributedKv::Key &key, const DistributedKv::Value &value);
60     bool IsEqual(nlohmann::json &jsonObject, const std::string &key,
61         const std::string &value, bool checkEmpty = false);
62     bool IsEqual(nlohmann::json &jsonObject, const std::string &key, int32_t value);
63     bool IsEqual(const DistributedKv::Key &key, const AutoStartupInfo &info);
64     bool IsEqual(const DistributedKv::Key &key, const std::string &accessTokenId);
65     bool IsEqual(const DistributedKv::Key &key, int32_t userId);
66 
67     static const DistributedKv::AppId APP_ID;
68     static const DistributedKv::StoreId STORE_ID;
69     DistributedKv::DistributedKvDataManager dataManager_;
70     std::shared_ptr<DistributedKv::SingleKvStore> kvStorePtr_;
71     mutable std::mutex kvStorePtrMutex_;
72 };
73 } // namespace AbilityRuntime
74 } // namespace OHOS
75 #endif // OHOS_ABILITY_RUNTIME_ABILITY_AUTO_STARTUP_DATA_MANAGER_H