1 /*
2  * Copyright (c) 2024-2024 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 #include "form_bundle_forbid_mgr.h"
16 
17 #include "fms_log_wrapper.h"
18 #include "form_mgr_errors.h"
19 
20 namespace OHOS {
21 namespace AppExecFwk {
22 namespace {
23     const std::string FORBIDDEN_FORM_BUNDLE_TABLE = "forbidden_form_bundle_table";
24 }
25 
FormBundleForbidMgr()26 FormBundleForbidMgr::FormBundleForbidMgr()
27 {
28     HILOG_INFO("Create");
29 }
30 
~FormBundleForbidMgr()31 FormBundleForbidMgr::~FormBundleForbidMgr()
32 {
33     HILOG_INFO("Destroy");
34 }
35 
Init()36 bool FormBundleForbidMgr::Init()
37 {
38     FormRdbTableConfig formRdbTableConfig;
39     formRdbTableConfig.tableName = FORBIDDEN_FORM_BUNDLE_TABLE;
40     formRdbTableConfig.createTableSql = "CREATE TABLE IF NOT EXISTS " +
41         formRdbTableConfig.tableName + " (KEY TEXT NOT NULL PRIMARY KEY);";
42     if (FormRdbDataMgr::GetInstance().InitFormRdbTable(formRdbTableConfig) != ERR_OK) {
43         HILOG_ERROR("Form bundle forbid mgr init form rdb table fail");
44         return false;
45     }
46 
47     FormRdbDataMgr::GetInstance().QueryAllKeys(FORBIDDEN_FORM_BUNDLE_TABLE, formBundleForbiddenSet_);
48     isInitialized_ = true;
49     HILOG_INFO("initialized");
50     return true;
51 }
52 
IsBundleForbidden(const std::string & bundleName)53 bool FormBundleForbidMgr::IsBundleForbidden(const std::string &bundleName)
54 {
55     if (bundleName.empty()) {
56         HILOG_ERROR("invalid bundleName");
57         return false;
58     }
59 
60     {
61         std::unique_lock<std::shared_mutex> lock(bundleForbiddenSetMutex_);
62         if (!isInitialized_ && !Init()) {
63             HILOG_ERROR("Form bundle forbid mgr not init");
64             return false;
65         }
66     }
67 
68     std::shared_lock<std::shared_mutex> lock(bundleForbiddenSetMutex_);
69     auto iter = formBundleForbiddenSet_.find(bundleName);
70     return iter != formBundleForbiddenSet_.end();
71 }
72 
SetBundleForbiddenStatus(const std::string & bundleName,bool isForbidden)73 void FormBundleForbidMgr::SetBundleForbiddenStatus(const std::string &bundleName, bool isForbidden)
74 {
75     if (bundleName.empty()) {
76         HILOG_ERROR("invalid bundleName");
77         return;
78     }
79 
80     std::unique_lock<std::shared_mutex> lock(bundleForbiddenSetMutex_);
81     if (!isInitialized_ && !Init()) {
82         HILOG_ERROR("Form bundle forbid mgr not init");
83         return;
84     }
85 
86     auto iter = formBundleForbiddenSet_.find(bundleName);
87     if (isForbidden && iter == formBundleForbiddenSet_.end()) {
88         formBundleForbiddenSet_.insert(bundleName);
89         FormRdbDataMgr::GetInstance().InsertData(FORBIDDEN_FORM_BUNDLE_TABLE, bundleName);
90     } else if (!isForbidden && iter != formBundleForbiddenSet_.end()) {
91         formBundleForbiddenSet_.erase(bundleName);
92         FormRdbDataMgr::GetInstance().DeleteData(FORBIDDEN_FORM_BUNDLE_TABLE, bundleName);
93     }
94 }
95 }  // namespace AppExecFwk
96 }  // namespace OHOS
97