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 const int32_t UNTRUST_THRESHOLD = 3;
26 } // namespace
27 
FormTrustMgr()28 FormTrustMgr::FormTrustMgr()
29 {
30     HILOG_INFO("create");
31     FormRdbTableConfig formRdbTableConfig;
32     formRdbTableConfig.tableName = UNTRUST_LIST;
33     formRdbTableConfig.createTableSql = "CREATE TABLE IF NOT EXISTS " +
34         formRdbTableConfig.tableName + " (KEY TEXT NOT NULL PRIMARY KEY);";
35     if (FormRdbDataMgr::GetInstance().InitFormRdbTable(formRdbTableConfig) != ERR_OK) {
36         HILOG_ERROR("Form trust mgr init form rdb table fail");
37     }
38 }
39 
~FormTrustMgr()40 FormTrustMgr::~FormTrustMgr()
41 {
42     HILOG_INFO("delete");
43 }
44 
IsTrust(const std::string & bundleName)45 bool FormTrustMgr::IsTrust(const std::string &bundleName)
46 {
47     std::lock_guard<std::mutex> lock(rdbStoreMutex_);
48     auto iter = unTrustList_.find(bundleName);
49     if (iter == unTrustList_.end()) {
50         return true;
51     }
52 
53     return iter->second <= UNTRUST_THRESHOLD;
54 }
55 
GetUntrustAppNameList(std::string & result)56 void FormTrustMgr::GetUntrustAppNameList(std::string &result)
57 {
58     std::map<std::string, int32_t>::iterator it = unTrustList_.begin();
59     for (; it != unTrustList_.end(); it++) {
60         if (it->second > UNTRUST_THRESHOLD) {
61             result += it->first + " untrusty\n";
62         } else {
63             result += it->first + " trusty\n";
64         }
65     }
66 }
67 
MarkTrustFlag(const std::string & bundleName,bool isTrust)68 void FormTrustMgr::MarkTrustFlag(const std::string &bundleName, bool isTrust)
69 {
70     std::lock_guard<std::mutex> lock(rdbStoreMutex_);
71     auto iter = unTrustList_.find(bundleName);
72     if (isTrust && iter != unTrustList_.end()) {
73         auto ret = FormRdbDataMgr::GetInstance().DeleteData(UNTRUST_LIST, bundleName);
74         if (ret != ERR_OK) {
75             HILOG_ERROR("DeleteData failed, key:%{public}s", bundleName.c_str());
76         }
77 
78         unTrustList_.erase(iter);
79         return;
80     }
81 
82     if (!isTrust) {
83         if (iter == unTrustList_.end()) {
84             unTrustList_[bundleName] = 1;
85             return;
86         }
87 
88         int32_t trustNum = iter->second;
89         trustNum++;
90         unTrustList_[bundleName] = trustNum;
91         if (trustNum > UNTRUST_THRESHOLD) {
92             auto ret = FormRdbDataMgr::GetInstance().InsertData(UNTRUST_LIST, bundleName);
93             if (ret != ERR_OK) {
94                 HILOG_ERROR("InsertData failed, key:%{public}s", bundleName.c_str());
95             }
96         }
97     }
98 }
99 } // namespace AppExecFwk
100 } // namespace OHOS
101