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_lock_mgr.h"
16
17 #include "fms_log_wrapper.h"
18 #include "form_mgr_errors.h"
19 #include "form_data_mgr.h"
20 #include "form_util.h"
21
22 namespace OHOS {
23 namespace AppExecFwk {
24 namespace {
25 const std::string LOCK_FORM_BUNDLE_TABLE = "lock_form_bundle_table";
26 constexpr int32_t DEFAULT_USER_ID = 100;
27 }
28
FormBundleLockMgr()29 FormBundleLockMgr::FormBundleLockMgr()
30 {
31 HILOG_INFO("Create");
32 }
33
~FormBundleLockMgr()34 FormBundleLockMgr::~FormBundleLockMgr()
35 {
36 HILOG_INFO("Destroy");
37 }
38
Init()39 bool FormBundleLockMgr::Init()
40 {
41 FormRdbTableConfig formRdbTableConfig;
42 formRdbTableConfig.tableName = LOCK_FORM_BUNDLE_TABLE;
43 formRdbTableConfig.createTableSql = "CREATE TABLE IF NOT EXISTS " +
44 formRdbTableConfig.tableName + " (KEY TEXT NOT NULL PRIMARY KEY);";
45 if (FormRdbDataMgr::GetInstance().InitFormRdbTable(formRdbTableConfig) != ERR_OK) {
46 HILOG_ERROR("Form bundle lock mgr init form rdb table fail");
47 return false;
48 }
49
50 FormRdbDataMgr::GetInstance().QueryAllKeys(LOCK_FORM_BUNDLE_TABLE, formBundleLockSet_);
51 isInitialized_ = true;
52 HILOG_INFO("initialized");
53 return true;
54 }
55
IsBundleLock(const std::string & bundleName,int64_t formId)56 bool FormBundleLockMgr::IsBundleLock(const std::string &bundleName, int64_t formId)
57 {
58 if (FormUtil::GetCurrentAccountId() != DEFAULT_USER_ID) {
59 return false;
60 }
61
62 if (formId != 0) {
63 bool lockStatus = false;
64 FormDataMgr::GetInstance().GetFormLock(formId, lockStatus);
65 return lockStatus;
66 }
67
68 if (bundleName.empty()) {
69 HILOG_ERROR("invalid bundleName");
70 return false;
71 }
72
73 {
74 std::unique_lock<std::shared_mutex> lock(bundleLockSetMutex_);
75 if (!isInitialized_ && !Init()) {
76 HILOG_ERROR("Form bundle lock mgr not init");
77 return false;
78 }
79 }
80
81 std::shared_lock<std::shared_mutex> lock(bundleLockSetMutex_);
82 auto iter = formBundleLockSet_.find(bundleName);
83 return iter != formBundleLockSet_.end();
84 }
85
SetBundleLockStatus(const std::string & bundleName,bool isLock)86 void FormBundleLockMgr::SetBundleLockStatus(const std::string &bundleName, bool isLock)
87 {
88 if (bundleName.empty()) {
89 HILOG_ERROR("invalid bundleName");
90 return;
91 }
92
93 std::unique_lock<std::shared_mutex> lock(bundleLockSetMutex_);
94 if (!isInitialized_ && !Init()) {
95 HILOG_ERROR("Form bundle lock mgr not init");
96 return;
97 }
98
99 auto iter = formBundleLockSet_.find(bundleName);
100 if (isLock && iter == formBundleLockSet_.end()) {
101 formBundleLockSet_.insert(bundleName);
102 FormRdbDataMgr::GetInstance().InsertData(LOCK_FORM_BUNDLE_TABLE, bundleName);
103 } else if (!isLock && iter != formBundleLockSet_.end()) {
104 formBundleLockSet_.erase(bundleName);
105 FormRdbDataMgr::GetInstance().DeleteData(LOCK_FORM_BUNDLE_TABLE, bundleName);
106 }
107 }
108
IsBundleProtect(const std::string & bundleName,int64_t formId)109 bool FormBundleLockMgr::IsBundleProtect(const std::string &bundleName, int64_t formId)
110 {
111 if (FormUtil::GetCurrentAccountId() != DEFAULT_USER_ID) {
112 return false;
113 }
114
115 if (formId != 0) {
116 bool protectStatus = false;
117 FormDataMgr::GetInstance().GetFormProtect(formId, protectStatus);
118 return protectStatus;
119 }
120
121 if (bundleName.empty()) {
122 HILOG_ERROR("invalid bundleName");
123 return false;
124 }
125
126 std::shared_lock<std::shared_mutex> lock(bundleProtectSetMutex_);
127 auto iter = formBundleProtectSet_.find(bundleName);
128 return iter != formBundleProtectSet_.end();
129 }
130
SetBundleProtectStatus(const std::string & bundleName,bool isProtect)131 void FormBundleLockMgr::SetBundleProtectStatus(const std::string &bundleName, bool isProtect)
132 {
133 if (bundleName.empty()) {
134 HILOG_ERROR("invalid bundleName");
135 return;
136 }
137
138 std::unique_lock<std::shared_mutex> lock(bundleProtectSetMutex_);
139 auto iter = formBundleProtectSet_.find(bundleName);
140 if (isProtect && iter == formBundleProtectSet_.end()) {
141 formBundleProtectSet_.insert(bundleName);
142 } else if (!isProtect && iter != formBundleProtectSet_.end()) {
143 formBundleProtectSet_.erase(bundleName);
144 } else {
145 HILOG_ERROR("set bundle protect status failed");
146 }
147 }
148 } // namespace AppExecFwk
149 } // namespace OHOS
150