• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_lock/form_bundle_lock_mgr.h"
16 
17 #include "fms_log_wrapper.h"
18 #include "form_mgr_errors.h"
19 #include "data_center/form_data_mgr.h"
20 #include "common/util/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     if (!IsBundleLockMgrInit()) {
74         return false;
75     }
76 
77     std::shared_lock<std::shared_mutex> lock(bundleLockSetMutex_);
78     auto iter = formBundleLockSet_.find(bundleName);
79     return iter != formBundleLockSet_.end();
80 }
81 
SetBundleLockStatus(const std::string & bundleName,bool isLock)82 void FormBundleLockMgr::SetBundleLockStatus(const std::string &bundleName, bool isLock)
83 {
84     if (bundleName.empty()) {
85         HILOG_ERROR("invalid bundleName");
86         return;
87     }
88 
89     std::unique_lock<std::shared_mutex> lock(bundleLockSetMutex_);
90     if (!isInitialized_ && !Init()) {
91         HILOG_ERROR("Form bundle lock mgr not init");
92         return;
93     }
94 
95     auto iter = formBundleLockSet_.find(bundleName);
96     if (isLock && iter == formBundleLockSet_.end()) {
97         formBundleLockSet_.insert(bundleName);
98         FormRdbDataMgr::GetInstance().InsertData(LOCK_FORM_BUNDLE_TABLE, bundleName);
99     } else if (!isLock && iter != formBundleLockSet_.end()) {
100         formBundleLockSet_.erase(bundleName);
101         FormRdbDataMgr::GetInstance().DeleteData(LOCK_FORM_BUNDLE_TABLE, bundleName);
102     }
103 }
104 
IsBundleProtect(const std::string & bundleName,int64_t formId)105 bool FormBundleLockMgr::IsBundleProtect(const std::string &bundleName, int64_t formId)
106 {
107     if (FormUtil::GetCurrentAccountId() != DEFAULT_USER_ID) {
108         return false;
109     }
110 
111     if (formId != 0) {
112         bool protectStatus = false;
113         FormDataMgr::GetInstance().GetFormProtect(formId, protectStatus);
114         return protectStatus;
115     }
116 
117     if (bundleName.empty()) {
118         HILOG_ERROR("invalid bundleName");
119         return false;
120     }
121 
122     std::shared_lock<std::shared_mutex> lock(bundleProtectSetMutex_);
123     auto iter = formBundleProtectSet_.find(bundleName);
124     return iter != formBundleProtectSet_.end();
125 }
126 
SetBundleProtectStatus(const std::string & bundleName,bool isProtect)127 void FormBundleLockMgr::SetBundleProtectStatus(const std::string &bundleName, bool isProtect)
128 {
129     if (bundleName.empty()) {
130         HILOG_ERROR("invalid bundleName");
131         return;
132     }
133 
134     std::unique_lock<std::shared_mutex> lock(bundleProtectSetMutex_);
135     auto iter = formBundleProtectSet_.find(bundleName);
136     if (isProtect && iter == formBundleProtectSet_.end()) {
137         formBundleProtectSet_.insert(bundleName);
138     } else if (!isProtect && iter != formBundleProtectSet_.end()) {
139         formBundleProtectSet_.erase(bundleName);
140     } else {
141         HILOG_ERROR("set bundle protect status failed");
142     }
143 }
144 
IsBundleLockMgrInit()145 bool FormBundleLockMgr::IsBundleLockMgrInit()
146 {
147     std::unique_lock<std::shared_mutex> lock(bundleLockSetMutex_);
148     if (!isInitialized_ && !Init()) {
149         HILOG_ERROR("Form bundle lock mgr not init");
150         return false;
151     }
152     return true;
153 }
154 }  // namespace AppExecFwk
155 }  // namespace OHOS
156