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 #ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_STATE_STORAGE_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_STATE_STORAGE_H
18 
19 #include <map>
20 #include <mutex>
21 
22 #include "bundle_user_info.h"
23 #include "json_serializer.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 class BundleStateStorage : public std::enable_shared_from_this<BundleStateStorage> {
28 public:
29     BundleStateStorage() = default;
30     ~BundleStateStorage() = default;
31     /**
32      * @brief Judge whether json db exists. If it does not exist, create it and return the judgment result.
33      * @return Returns true if the json db exists; returns false otherwise.
34      */
35     bool HasBundleUserInfoJsonDb();
36     /**
37      * @brief Load all bundles state from json db to BundleUserInfo.
38      * @param infos Indicates the map to save all installed bundles.
39      * @return Returns true if load the data successfully; returns false otherwise.
40      */
41     bool LoadAllBundleStateData(std::map<std::string, std::map<int32_t, BundleUserInfo>> &infos);
42     /**
43      * @brief Save the bundles state to json db.
44      * @param bundleName Indicates the bundleName object to be save.
45      * @param userId Indicates the userId object to be save.
46      * @param bundleUserInfo Indicates the bundleUserInfo object to be save.
47      * @return Returns true if save the data successfully saved; returns false otherwise.
48      */
49     bool SaveBundleStateStorage(
50         const std::string bundleName, int32_t userId, const BundleUserInfo &bundleUserInfo);
51     /**
52      * @brief Save the bundles state from json db.
53      * @param bundleName Indicates the bundleName object to be save.
54      * @param userId Indicates the userId object to be save.
55      * @param bundleUserInfo Indicates the bundleUserInfo object to be save.
56      * @return Returns true if get the data successfully; returns false otherwise.
57      */
58     bool GetBundleStateStorage(
59         const std::string bundleName, int32_t userId, BundleUserInfo &bundleUserInfo);
60     /**
61      * @brief Delete the bundles state from json db.
62      * @param bundleName Indicates the bundleName object to be save.
63      * @param userId Indicates the userId object to be save.
64      * @return Returns true if delete the data successfully; returns false otherwise.
65      */
66     bool DeleteBundleState(const std::string bundleName, int32_t userId);
67 
68 private:
69     bool LoadAllBundleStateDataFromJson(
70         nlohmann::json &jParse, std::map<std::string, std::map<int32_t, BundleUserInfo>> &infos);
71     bool GetBundleStateJson(nlohmann::json &jParse);
72 
73     mutable std::mutex bundleStateMutex_;
74 };
75 }  // namespace AppExecFwk
76 }  // namespace OHOS
77 #endif  // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_STATE_STORAGE_H
78