• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "bundle_sandbox_manager_rdb.h"
17 
18 #include "app_log_wrapper.h"
19 #include "appexecfwk_errors.h"
20 #include "json_serializer.h"
21 
22 namespace OHOS {
23 namespace AppExecFwk {
SandboxManagerRdb()24 SandboxManagerRdb::SandboxManagerRdb()
25 {
26     APP_LOGI("create SandboxManagerRdb.");
27     BmsRdbConfig bmsRdbConfig;
28     bmsRdbConfig.dbName = Constants::BUNDLE_RDB_NAME;
29     bmsRdbConfig.tableName = Constants::SAND_BOX_RDB_TABLE_NAME;
30     rdbDataManager_ = std::make_shared<RdbDataManager>(bmsRdbConfig);
31     rdbDataManager_->CreateTable();
32 }
33 
~SandboxManagerRdb()34 SandboxManagerRdb::~SandboxManagerRdb()
35 {
36     APP_LOGI("destroy SandboxManagerRdb.");
37 }
38 
QueryAllSandboxInnerBundleInfo(std::unordered_map<std::string,InnerBundleInfo> & innerBundleInfos)39 bool SandboxManagerRdb::QueryAllSandboxInnerBundleInfo(
40     std::unordered_map<std::string, InnerBundleInfo> &innerBundleInfos)
41 {
42     APP_LOGI("begin to QueryAllSandboxInnerBundleInfo");
43     bool ret = GetAllDataFromDb(innerBundleInfos);
44     if (!ret) {
45         APP_LOGE("GetAllDataFromDb failed.");
46         return false;
47     }
48     return true;
49 }
50 
QuerySandboxInnerBundleInfo(const std::string & bundleName,InnerBundleInfo & innerBundleInfos)51 bool SandboxManagerRdb::QuerySandboxInnerBundleInfo(const std::string &bundleName, InnerBundleInfo &innerBundleInfos)
52 {
53     APP_LOGI("begin to QuerySandboxInnerBundleInfo");
54     bool ret = GetDataFromDb(bundleName, innerBundleInfos);
55     if (!ret) {
56         APP_LOGE("GetDataFromDb failed.");
57         return false;
58     }
59     return true;
60 }
61 
SaveSandboxInnerBundleInfo(const std::string & bundleName,const InnerBundleInfo & innerBundleInfos)62 bool SandboxManagerRdb::SaveSandboxInnerBundleInfo(const std::string &bundleName,
63     const InnerBundleInfo &innerBundleInfos)
64 {
65     APP_LOGI("begin to SaveSandboxInnerBundleInfo");
66     bool ret = SaveDataToDb(bundleName, innerBundleInfos);
67     if (!ret) {
68         APP_LOGE("SaveDataToDb failed.");
69         return false;
70     }
71     return true;
72 }
73 
DeleteSandboxInnerBundleInfo(const std::string & bundleName)74 bool SandboxManagerRdb::DeleteSandboxInnerBundleInfo(const std::string &bundleName)
75 {
76     APP_LOGI("begin to DeleteSandboxInnerBundleInfo");
77     bool ret = DeleteDataFromDb(bundleName);
78     if (!ret) {
79         APP_LOGE("DeleteDataFromDb failed.");
80         return false;
81     }
82     return true;
83 }
84 
GetAllDataFromDb(std::unordered_map<std::string,InnerBundleInfo> & innerBundleInfos)85 bool SandboxManagerRdb::GetAllDataFromDb(std::unordered_map<std::string, InnerBundleInfo> &innerBundleInfos)
86 {
87     if (rdbDataManager_ == nullptr) {
88         APP_LOGE("rdbDataManager is null");
89         return false;
90     }
91 
92     std::map<std::string, std::string> values;
93     bool result = rdbDataManager_->QueryAllData(values);
94     if (!result) {
95         APP_LOGE("QueryAllData failed");
96         return false;
97     }
98     for (auto iter = values.begin(); iter != values.end(); ++iter) {
99         nlohmann::json jsonObject = nlohmann::json::parse(iter->second, nullptr, false);
100         InnerBundleInfo innerBundleInfo;
101         if (jsonObject.is_discarded() || (innerBundleInfo.FromJson(jsonObject) != ERR_OK)) {
102             APP_LOGE("error key : %{public}s", iter->first.c_str());
103             rdbDataManager_->DeleteData(iter->first);
104             continue;
105         }
106         if (innerBundleInfos.find(iter->first) == innerBundleInfos.end()) {
107             innerBundleInfos.emplace(iter->first, innerBundleInfo);
108         } else {
109             innerBundleInfos.at(iter->first) = innerBundleInfo;
110         }
111     }
112     return true;
113 }
114 
GetDataFromDb(const std::string & bundleName,InnerBundleInfo & innerBundleInfo)115 bool SandboxManagerRdb::GetDataFromDb(const std::string &bundleName, InnerBundleInfo &innerBundleInfo)
116 {
117     if (rdbDataManager_ == nullptr) {
118         APP_LOGE("rdbDataManager is null");
119         return false;
120     }
121 
122     std::string value;
123     bool result = rdbDataManager_->QueryData(bundleName, value);
124     if (!result) {
125         APP_LOGE("QueryData failed by bundleName %{public}s", bundleName.c_str());
126         return false;
127     }
128 
129     nlohmann::json jsonObject = nlohmann::json::parse(value, nullptr, false);
130     if (jsonObject.is_discarded() || (innerBundleInfo.FromJson(jsonObject) != ERR_OK)) {
131         APP_LOGE("error key : %{public}s", bundleName.c_str());
132         rdbDataManager_->DeleteData(bundleName);
133         return false;
134     }
135     return true;
136 }
137 
SaveDataToDb(const std::string & bundleName,const InnerBundleInfo & innerBundleInfo)138 bool SandboxManagerRdb::SaveDataToDb(const std::string &bundleName, const InnerBundleInfo &innerBundleInfo)
139 {
140     if (rdbDataManager_ == nullptr) {
141         APP_LOGE("rdbDataManager is null");
142         return false;
143     }
144 
145     return rdbDataManager_->InsertData(bundleName, innerBundleInfo.ToString());
146 }
147 
DeleteDataFromDb(const std::string & bundleName)148 bool SandboxManagerRdb::DeleteDataFromDb(const std::string &bundleName)
149 {
150     if (rdbDataManager_ == nullptr) {
151         APP_LOGE("rdbDataManager is null");
152         return false;
153     }
154 
155     return rdbDataManager_->DeleteData(bundleName);
156 }
157 } // namespace AppExecFwk
158 } // namespace OHOS
159