• 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 "default_app_rdb.h"
17 
18 #include "app_log_wrapper.h"
19 #include "bundle_data_mgr.h"
20 #include "bundle_mgr_service.h"
21 #include "bundle_parser.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 namespace {
26 constexpr int32_t INITIAL_USER_ID = -1;
27 const std::string DEFAULT_APP_JSON_PATH = "/etc/app/default_app.json";
28 }
DefaultAppRdb()29 DefaultAppRdb::DefaultAppRdb()
30 {
31     APP_LOGD("create DefaultAppRdb.");
32     BmsRdbConfig bmsRdbConfig;
33     bmsRdbConfig.dbName = Constants::BUNDLE_RDB_NAME;
34     bmsRdbConfig.tableName = Constants::DEFAULT_APP_RDB_TABLE_NAME;
35     rdbDataManager_ = std::make_shared<RdbDataManager>(bmsRdbConfig);
36     rdbDataManager_->CreateTable();
37     LoadDefaultApplicationConfig();
38 }
39 
~DefaultAppRdb()40 DefaultAppRdb::~DefaultAppRdb()
41 {
42     APP_LOGD("destroy DefaultAppRdb.");
43 }
44 
GetDefaultApplicationInfos(int32_t userId,std::map<std::string,Element> & infos)45 bool DefaultAppRdb::GetDefaultApplicationInfos(int32_t userId, std::map<std::string, Element>& infos)
46 {
47     APP_LOGD("begin to GetDefaultApplicationInfos, userId : %{public}d.", userId);
48     bool ret = GetDataFromDb(userId, infos);
49     if (!ret) {
50         APP_LOGE("GetDataFromDb failed.");
51         return false;
52     }
53 
54     APP_LOGD("GetDefaultApplicationInfos success.");
55     return true;
56 }
57 
GetDefaultApplicationInfo(int32_t userId,const std::string & type,Element & element)58 bool DefaultAppRdb::GetDefaultApplicationInfo(int32_t userId, const std::string& type, Element& element)
59 {
60     APP_LOGD("begin to GetDefaultApplicationInfo, userId : %{public}d, type : %{public}s.",
61         userId, type.c_str());
62     std::map<std::string, Element> infos;
63     bool ret = GetDefaultApplicationInfos(userId, infos);
64     if (!ret) {
65         APP_LOGE("GetDefaultApplicationInfos failed.");
66         return false;
67     }
68 
69     if (infos.find(type) == infos.end()) {
70         APP_LOGD("type is not saved in db.");
71         return false;
72     }
73 
74     element = infos.find(type)->second;
75     APP_LOGD("GetDefaultApplicationInfo success.");
76     return true;
77 }
78 
SetDefaultApplicationInfos(int32_t userId,const std::map<std::string,Element> & infos)79 bool DefaultAppRdb::SetDefaultApplicationInfos(int32_t userId, const std::map<std::string, Element>& infos)
80 {
81     APP_LOGD("begin to SetDefaultApplicationInfos, userId : %{public}d.", userId);
82     bool ret = SaveDataToDb(userId, infos);
83     if (!ret) {
84         APP_LOGE("SaveDataToDb failed.");
85         return false;
86     }
87 
88     APP_LOGD("SetDefaultApplicationInfos success.");
89     return true;
90 }
91 
SetDefaultApplicationInfo(int32_t userId,const std::string & type,const Element & element)92 bool DefaultAppRdb::SetDefaultApplicationInfo(int32_t userId, const std::string& type, const Element& element)
93 {
94     APP_LOGD("begin to SetDefaultApplicationInfo, userId : %{public}d, type : %{public}s.", userId, type.c_str());
95     std::map<std::string, Element> infos;
96     GetDefaultApplicationInfos(userId, infos);
97     if (infos.find(type) == infos.end()) {
98         APP_LOGD("add default app info.");
99         infos.emplace(type, element);
100     } else {
101         APP_LOGD("modify default app info.");
102         infos[type] = element;
103     }
104 
105     bool ret = SaveDataToDb(userId, infos);
106     if (!ret) {
107         APP_LOGE("SaveDataToDb failed.");
108         return false;
109     }
110 
111     APP_LOGD("SetDefaultApplicationInfo success.");
112     return true;
113 }
114 
DeleteDefaultApplicationInfos(int32_t userId)115 bool DefaultAppRdb::DeleteDefaultApplicationInfos(int32_t userId)
116 {
117     APP_LOGD("begin to DeleteDefaultApplicationInfos, userId : %{public}d.", userId);
118     bool ret = DeleteDataFromDb(userId);
119     if (!ret) {
120         APP_LOGE("DeleteDataFromDb failed.");
121         return false;
122     }
123 
124     APP_LOGD("DeleteDefaultApplicationInfos success.");
125     return true;
126 }
127 
DeleteDefaultApplicationInfo(int32_t userId,const std::string & type)128 bool DefaultAppRdb::DeleteDefaultApplicationInfo(int32_t userId, const std::string& type)
129 {
130     APP_LOGD("begin to DeleteDefaultApplicationInfo, userId : %{public}d, type : %{public}s.", userId, type.c_str());
131     std::map<std::string, Element> infos;
132     bool ret = GetDataFromDb(userId, infos);
133     if (!ret) {
134         APP_LOGE("GetDataFromDb failed.");
135         return true;
136     }
137 
138     if (infos.find(type) == infos.end()) {
139         APP_LOGD("type doesn't exists in db.");
140         return true;
141     }
142 
143     infos.erase(type);
144     ret = SaveDataToDb(userId, infos);
145     if (!ret) {
146         APP_LOGE("SaveDataToDb failed.");
147         return false;
148     }
149 
150     APP_LOGD("DeleteDefaultApplicationInfo success.");
151     return true;
152 }
153 
ParseConfig(DefaultAppData & defaultAppData)154 bool DefaultAppRdb::ParseConfig(DefaultAppData &defaultAppData)
155 {
156     // load default app config from json file
157     std::vector<std::string> rootDirs;
158     BMSEventHandler::GetPreInstallRootDirList(rootDirs);
159     if (rootDirs.empty()) {
160         APP_LOGW("rootDirs empty");
161         return false;
162     }
163     std::for_each(rootDirs.cbegin(), rootDirs.cend(), [&defaultAppData](const auto &rootDir) {
164         std::string path = rootDir + DEFAULT_APP_JSON_PATH;
165         APP_LOGD("default app json path : %{public}s", path.c_str());
166         nlohmann::json jsonObject;
167         if (!BundleParser::ReadFileIntoJson(path, jsonObject)) {
168             APP_LOGW("read default app json failed");
169             return;
170         }
171         defaultAppData.ParseDefaultApplicationConfig(jsonObject);
172     });
173     return !defaultAppData.infos.empty();
174 }
175 
LoadDefaultApplicationConfig()176 void DefaultAppRdb::LoadDefaultApplicationConfig()
177 {
178     APP_LOGD("begin to LoadDefaultApplicationConfig.");
179     DefaultAppData defaultAppData;
180     if (!ParseConfig(defaultAppData)) {
181         APP_LOGD("default app config empty");
182         return;
183     }
184     // get pre default app config
185     std::map<std::string, Element> preInfos;
186     GetDefaultApplicationInfos(INITIAL_USER_ID, preInfos);
187     // save to each user
188     std::shared_ptr<BundleDataMgr> dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
189     if (dataMgr == nullptr) {
190         APP_LOGW("get BundleDataMgr failed.");
191         return;
192     }
193 
194     std::set<int32_t> allUsers = dataMgr->GetAllUser();
195     for (int32_t userId : allUsers) {
196         std::map<std::string, Element> infos;
197         GetDefaultApplicationInfos(userId, infos);
198         for (const auto& item : defaultAppData.infos) {
199             const std::string& type = item.first;
200             const Element& element = item.second;
201             if (infos.find(type) != infos.end() && preInfos.find(type) != preInfos.end()
202                 && infos.find(type)->second == preInfos.find(type)->second) {
203                 infos[type] = element;
204             } else {
205                 infos.try_emplace(type, element);
206             }
207         }
208 
209         SetDefaultApplicationInfos(userId, infos);
210     }
211 
212     // save default app config to db
213     SetDefaultApplicationInfos(INITIAL_USER_ID, defaultAppData.infos);
214     APP_LOGD("LoadDefaultApplicationConfig done.");
215 }
216 
GetDataFromDb(int32_t userId,std::map<std::string,Element> & infos)217 bool DefaultAppRdb::GetDataFromDb(int32_t userId, std::map<std::string, Element>& infos)
218 {
219     if (rdbDataManager_ == nullptr) {
220         APP_LOGE("rdbDataManager is null");
221         return false;
222     }
223 
224     std::string key = std::to_string(userId);
225     std::string value;
226     bool result = rdbDataManager_->QueryData(key, value);
227     if (!result) {
228         APP_LOGE("QueryData failed by key %{public}d", userId);
229         return false;
230     }
231 
232     DefaultAppData defaultAppData;
233     nlohmann::json jsonObject = nlohmann::json::parse(value, nullptr, false);
234     if (jsonObject.is_discarded() || defaultAppData.FromJson(jsonObject) != ERR_OK) {
235         APP_LOGE("error key : %{public}s", key.c_str());
236         rdbDataManager_->DeleteData(key);
237         return false;
238     }
239 
240     infos = defaultAppData.infos;
241     return true;
242 }
243 
SaveDataToDb(int32_t userId,const std::map<std::string,Element> & infos)244 bool DefaultAppRdb::SaveDataToDb(int32_t userId, const std::map<std::string, Element>& infos)
245 {
246     if (rdbDataManager_ == nullptr) {
247         APP_LOGE("rdbDataManager is null");
248         return false;
249     }
250 
251     DefaultAppData defaultAppData;
252     defaultAppData.infos = infos;
253     return rdbDataManager_->InsertData(std::to_string(userId), defaultAppData.ToString());
254 }
255 
DeleteDataFromDb(int32_t userId)256 bool DefaultAppRdb::DeleteDataFromDb(int32_t userId)
257 {
258     if (rdbDataManager_ == nullptr) {
259         APP_LOGE("rdbDataManager is null");
260         return false;
261     }
262 
263     return rdbDataManager_->DeleteData(std::to_string(userId));
264 }
265 
RegisterDeathListener()266 void DefaultAppRdb::RegisterDeathListener()
267 {
268     APP_LOGD("RegisterDeathListener.");
269 }
270 
UnRegisterDeathListener()271 void DefaultAppRdb::UnRegisterDeathListener()
272 {
273     APP_LOGD("UnRegisterDeathListener.");
274 }
275 }
276 }
277