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 "form_trust_mgr.h"
17
18 #include "fms_log_wrapper.h"
19 #include "form_constants.h"
20
21 namespace OHOS {
22 namespace AppExecFwk {
23 namespace {
24 const std::string UNTRUST_LIST = "untrust_list";
25 } // namespace
26
FormTrustMgr()27 FormTrustMgr::FormTrustMgr()
28 {
29 FormRdbConfig formRdbConfig;
30 formRdbConfig.tableName = UNTRUST_LIST;
31 formRdbConfig.createTableSql =
32 "CREATE TABLE IF NOT EXISTS " + formRdbConfig.tableName + " (KEY TEXT NOT NULL PRIMARY KEY);";
33 rdbDataManager_ = std::make_shared<FormRdbDataMgr>(formRdbConfig);
34 rdbDataManager_->Init();
35 HILOG_INFO("FormTrustMgr is created");
36 }
37
~FormTrustMgr()38 FormTrustMgr::~FormTrustMgr()
39 {
40 HILOG_INFO("FormTrustMgr is deleted");
41 }
42
Start()43 void FormTrustMgr::Start()
44 {
45 std::lock_guard<std::mutex> lock(rdbStoreMutex_);
46 rdbDataManager_->QueryAllKeys(unTrustList_);
47 }
48
IsTrust(const std::string & bundleName)49 bool FormTrustMgr::IsTrust(const std::string &bundleName)
50 {
51 std::lock_guard<std::mutex> lock(rdbStoreMutex_);
52 return unTrustList_.find(bundleName) == unTrustList_.end();
53 }
54
MarkTrustFlag(const std::string & bundleName,bool isTrust)55 void FormTrustMgr::MarkTrustFlag(const std::string &bundleName, bool isTrust)
56 {
57 std::lock_guard<std::mutex> lock(rdbStoreMutex_);
58 auto iter = unTrustList_.find(bundleName);
59 if (isTrust && iter != unTrustList_.end()) {
60 auto ret = rdbDataManager_->DeleteData(bundleName);
61 if (ret != ERR_OK) {
62 HILOG_ERROR("DeleteData failed, key: %{public}s", bundleName.c_str());
63 return;
64 }
65
66 unTrustList_.erase(iter);
67 return;
68 }
69
70 if (!isTrust && iter == unTrustList_.end()) {
71 auto ret = rdbDataManager_->InsertData(bundleName);
72 if (ret != ERR_OK) {
73 HILOG_ERROR("InsertData failed, key: %{public}s", bundleName.c_str());
74 return;
75 }
76
77 unTrustList_.insert(bundleName);
78 }
79 }
80 } // namespace AppExecFwk
81 } // namespace OHOS
82