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 "feature/bundle_forbidden/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 if (!IsBundleForbidMgrInit()) {
61 return false;
62 }
63
64 std::shared_lock<std::shared_mutex> lock(bundleForbiddenSetMutex_);
65 auto iter = formBundleForbiddenSet_.find(bundleName);
66 return iter != formBundleForbiddenSet_.end();
67 }
68
SetBundleForbiddenStatus(const std::string & bundleName,bool isForbidden)69 void FormBundleForbidMgr::SetBundleForbiddenStatus(const std::string &bundleName, bool isForbidden)
70 {
71 if (bundleName.empty()) {
72 HILOG_ERROR("invalid bundleName");
73 return;
74 }
75
76 std::unique_lock<std::shared_mutex> lock(bundleForbiddenSetMutex_);
77 if (!isInitialized_ && !Init()) {
78 HILOG_ERROR("Form bundle forbid mgr not init");
79 return;
80 }
81
82 auto iter = formBundleForbiddenSet_.find(bundleName);
83 if (isForbidden && iter == formBundleForbiddenSet_.end()) {
84 formBundleForbiddenSet_.insert(bundleName);
85 FormRdbDataMgr::GetInstance().InsertData(FORBIDDEN_FORM_BUNDLE_TABLE, bundleName);
86 } else if (!isForbidden && iter != formBundleForbiddenSet_.end()) {
87 formBundleForbiddenSet_.erase(bundleName);
88 FormRdbDataMgr::GetInstance().DeleteData(FORBIDDEN_FORM_BUNDLE_TABLE, bundleName);
89 }
90 }
91
IsBundleForbidMgrInit()92 bool FormBundleForbidMgr::IsBundleForbidMgrInit()
93 {
94 std::unique_lock<std::shared_mutex> lock(bundleForbiddenSetMutex_);
95 if (!isInitialized_ && !Init()) {
96 HILOG_ERROR("Form bundle forbid mgr not init");
97 return false;
98 }
99 return true;
100 }
101 } // namespace AppExecFwk
102 } // namespace OHOS
103