• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025-2025 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_exempt_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_EXEMPT_TABLE = "lock_form_exempt_table";
26 }
27 
FormExemptLockMgr()28 FormExemptLockMgr::FormExemptLockMgr()
29 {
30     HILOG_INFO("Create");
31 }
32 
~FormExemptLockMgr()33 FormExemptLockMgr::~FormExemptLockMgr()
34 {
35     HILOG_INFO("Destroy");
36 }
37 
Init()38 bool FormExemptLockMgr::Init()
39 {
40     FormRdbTableConfig formRdbTableConfig;
41     formRdbTableConfig.tableName = LOCK_FORM_EXEMPT_TABLE;
42     formRdbTableConfig.createTableSql = "CREATE TABLE IF NOT EXISTS " +
43         formRdbTableConfig.tableName + " (KEY TEXT NOT NULL PRIMARY KEY);";
44     if (FormRdbDataMgr::GetInstance().InitFormRdbTable(formRdbTableConfig) != ERR_OK) {
45         HILOG_ERROR("Form exempt lock mgr init form rdb table fail");
46         return false;
47     }
48 
49     FormRdbDataMgr::GetInstance().QueryAllKeys(LOCK_FORM_EXEMPT_TABLE, formExemptLockSet_);
50     isInitialized_ = true;
51     HILOG_INFO("initialized");
52     return true;
53 }
54 
IsExemptLock(int64_t formId)55 bool FormExemptLockMgr::IsExemptLock(int64_t formId)
56 {
57     std::string formId_s = std::to_string(formId);
58     if (!IsExemptLockMgrInit()) {
59         HILOG_ERROR("Form exempt lock mgr not init");
60         return false;
61     }
62 
63     std::shared_lock<std::shared_mutex> lock(exemptLockSetMutex_);
64     auto iter = formExemptLockSet_.find(formId_s);
65     return iter != formExemptLockSet_.end();
66 }
67 
SetExemptLockStatus(int64_t formId,bool isExempt)68 void FormExemptLockMgr::SetExemptLockStatus(int64_t formId, bool isExempt)
69 {
70     std::string formId_s = std::to_string(formId);
71     std::unique_lock<std::shared_mutex> lock(exemptLockSetMutex_);
72     if (!isInitialized_ && !Init()) {
73         HILOG_ERROR("Form bundle lock mgr not init");
74         return;
75     }
76 
77     auto iter = formExemptLockSet_.find(formId_s);
78     if (isExempt && iter == formExemptLockSet_.end()) {
79         formExemptLockSet_.insert(formId_s);
80         FormRdbDataMgr::GetInstance().InsertData(LOCK_FORM_EXEMPT_TABLE, formId_s);
81     } else if (!isExempt && iter != formExemptLockSet_.end()) {
82         formExemptLockSet_.erase(formId_s);
83         FormRdbDataMgr::GetInstance().DeleteData(LOCK_FORM_EXEMPT_TABLE, formId_s);
84     }
85 }
86 
IsExemptLockMgrInit()87 bool FormExemptLockMgr::IsExemptLockMgrInit()
88 {
89     std::unique_lock<std::shared_mutex> lock(exemptLockSetMutex_);
90     if (!isInitialized_ && !Init()) {
91         HILOG_ERROR("Form bundle lock mgr not init");
92         return false;
93     }
94     return true;
95 }
96 }  // namespace AppExecFwk
97 }  // namespace OHOS
98