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