• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "preinstall_data_storage_rdb.h"
17 
18 #include "app_log_wrapper.h"
19 
20 namespace OHOS {
21 namespace AppExecFwk {
PreInstallDataStorageRdb()22 PreInstallDataStorageRdb::PreInstallDataStorageRdb()
23 {
24     APP_LOGI("PreInstallDataStorageRdb instance is created");
25     BmsRdbConfig bmsRdbConfig;
26     bmsRdbConfig.dbName = Constants::BUNDLE_RDB_NAME;
27     bmsRdbConfig.tableName = Constants::PRE_BUNDLE_RDB_TABLE_NAME;
28     rdbDataManager_ = std::make_shared<RdbDataManager>(bmsRdbConfig);
29     rdbDataManager_->CreateTable();
30 }
31 
~PreInstallDataStorageRdb()32 PreInstallDataStorageRdb::~PreInstallDataStorageRdb()
33 {
34     APP_LOGI("PreInstallDataStorageRdb instance is destroyed");
35 }
36 
LoadAllPreInstallBundleInfos(std::vector<PreInstallBundleInfo> & preInstallBundleInfos)37 bool PreInstallDataStorageRdb::LoadAllPreInstallBundleInfos(
38     std::vector<PreInstallBundleInfo> &preInstallBundleInfos)
39 {
40     APP_LOGI("Load all prebundle data to vector");
41     if (rdbDataManager_ == nullptr) {
42         APP_LOGE("rdbDataManager is null");
43         return false;
44     }
45 
46     std::map<std::string, std::string> datas;
47     if (!rdbDataManager_->QueryAllData(datas)) {
48         APP_LOGE("QueryAllData failed");
49         return false;
50     }
51 
52     TransformStrToInfo(datas, preInstallBundleInfos);
53     return !preInstallBundleInfos.empty();
54 }
55 
TransformStrToInfo(const std::map<std::string,std::string> & datas,std::vector<PreInstallBundleInfo> & preInstallBundleInfos)56 void PreInstallDataStorageRdb::TransformStrToInfo(
57     const std::map<std::string, std::string> &datas,
58     std::vector<PreInstallBundleInfo> &preInstallBundleInfos)
59 {
60     APP_LOGD("TransformStrToInfo start");
61     if (rdbDataManager_ == nullptr || datas.empty()) {
62         APP_LOGE("data is null");
63         return;
64     }
65 
66     std::map<std::string, PreInstallBundleInfo> updateInfos;
67     for (const auto &data : datas) {
68         PreInstallBundleInfo preInstallBundleInfo;
69         nlohmann::json jsonObject = nlohmann::json::parse(data.second, nullptr, false);
70         if (jsonObject.is_discarded()) {
71             APP_LOGE("Error key: %{plublic}s", data.first.c_str());
72             rdbDataManager_->DeleteData(data.first);
73             continue;
74         }
75 
76         if (preInstallBundleInfo.FromJson(jsonObject) != ERR_OK) {
77             APP_LOGE("Error key: %{plublic}s", data.first.c_str());
78             rdbDataManager_->DeleteData(data.first);
79             continue;
80         }
81 
82         preInstallBundleInfos.emplace_back(preInstallBundleInfo);
83         // database update
84         std::string key = data.first;
85         if (key != preInstallBundleInfo.GetBundleName()) {
86             updateInfos.emplace(key, preInstallBundleInfo);
87         }
88     }
89 
90     if (updateInfos.size() > 0) {
91         UpdateDataBase(updateInfos);
92     }
93 }
94 
UpdateDataBase(std::map<std::string,PreInstallBundleInfo> & infos)95 void PreInstallDataStorageRdb::UpdateDataBase(
96     std::map<std::string, PreInstallBundleInfo> &infos)
97 {
98     APP_LOGD("Begin to update preInstall database");
99     if (rdbDataManager_ == nullptr) {
100         APP_LOGE("rdbDataManager is null");
101         return;
102     }
103 
104     for (const auto& item : infos) {
105         if (SavePreInstallStorageBundleInfo(item.second)) {
106             rdbDataManager_->DeleteData(item.first);
107         }
108     }
109     APP_LOGD("Update preInstall database done");
110 }
111 
SavePreInstallStorageBundleInfo(const PreInstallBundleInfo & preInstallBundleInfo)112 bool PreInstallDataStorageRdb::SavePreInstallStorageBundleInfo(
113     const PreInstallBundleInfo &preInstallBundleInfo)
114 {
115     if (rdbDataManager_ == nullptr) {
116         APP_LOGE("rdbDataManager is null");
117         return false;
118     }
119 
120     bool ret = rdbDataManager_->InsertData(
121         preInstallBundleInfo.GetBundleName(), preInstallBundleInfo.ToString());
122     APP_LOGD("SavePreInstallStorageBundleInfo %{public}d", ret);
123     return ret;
124 }
125 
DeletePreInstallStorageBundleInfo(const PreInstallBundleInfo & preInstallBundleInfo)126 bool PreInstallDataStorageRdb::DeletePreInstallStorageBundleInfo(
127     const PreInstallBundleInfo &preInstallBundleInfo)
128 {
129     if (rdbDataManager_ == nullptr) {
130         APP_LOGE("rdbDataManager is null");
131         return false;
132     }
133 
134     bool ret = rdbDataManager_->DeleteData(preInstallBundleInfo.GetBundleName());
135     APP_LOGD("DeletePreInstallStorageBundleInfo %{public}d", ret);
136     return ret;
137 }
138 }  // namespace AppExecFwk
139 }  // namespace OHOS