• 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 "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     FormRdbConfig formRdbConfig;
31     formRdbConfig.tableName = UNTRUST_LIST;
32     formRdbConfig.createTableSql =
33         "CREATE TABLE IF NOT EXISTS " + formRdbConfig.tableName + " (KEY TEXT NOT NULL PRIMARY KEY);";
34     rdbDataManager_ = std::make_shared<FormRdbDataMgr>(formRdbConfig);
35     rdbDataManager_->Init();
36     HILOG_INFO("FormTrustMgr is created");
37 }
38 
~FormTrustMgr()39 FormTrustMgr::~FormTrustMgr()
40 {
41     HILOG_INFO("FormTrustMgr is deleted");
42 }
43 
IsTrust(const std::string & bundleName)44 bool FormTrustMgr::IsTrust(const std::string &bundleName)
45 {
46     std::lock_guard<std::mutex> lock(rdbStoreMutex_);
47     auto iter = unTrustList_.find(bundleName);
48     if (iter == unTrustList_.end()) {
49         return true;
50     }
51 
52     return iter->second <= UNTRUST_THRESHOLD;
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         }
64 
65         unTrustList_.erase(iter);
66         return;
67     }
68 
69     if (!isTrust) {
70         if (iter == unTrustList_.end()) {
71             unTrustList_[bundleName] = 1;
72             return;
73         }
74 
75         int32_t trustNum = iter->second;
76         trustNum++;
77         unTrustList_[bundleName] = trustNum;
78         if (trustNum > UNTRUST_THRESHOLD) {
79             auto ret = rdbDataManager_->InsertData(bundleName);
80             if (ret != ERR_OK) {
81                 HILOG_ERROR("InsertData failed, key: %{public}s", bundleName.c_str());
82             }
83         }
84     }
85 }
86 } // namespace AppExecFwk
87 } // namespace OHOS
88