1 /*
2 * Copyright (c) 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
16 #include "access_token_db_loader.h"
17
18 #include "accesstoken_common_log.h"
19 #include "access_token_db.h"
20 #include "rdb_helper.h"
21
22 namespace OHOS {
23 namespace Security {
24 namespace AccessToken {
25 namespace {
26 static constexpr int32_t DESTROY_REPEAT_TIMES = 10;
27 static constexpr int32_t DESTROY_WAIT_TIME_MILLISECONDS = 100; // 0.1s
28 }
29
AccessTokenDbLoader()30 AccessTokenDbLoader::AccessTokenDbLoader()
31 {
32 LOGI(ATM_DOMAIN, ATM_TAG, "AccessTokenDbLoader");
33 }
34
~AccessTokenDbLoader()35 AccessTokenDbLoader::~AccessTokenDbLoader()
36 {
37 AccessTokenDb::DestroyInstance();
38 LOGI(ATM_DOMAIN, ATM_TAG, "~AccessTokenDbLoader");
39 }
40
InitRdbHelper()41 void AccessTokenDbLoader::InitRdbHelper()
42 {
43 (void)NativeRdb::RdbHelper::Init();
44 }
45
Modify(const AtmDataType type,const GenericValues & modifyValue,const GenericValues & conditionValue)46 int32_t AccessTokenDbLoader::Modify(const AtmDataType type, const GenericValues& modifyValue,
47 const GenericValues& conditionValue)
48 {
49 return AccessTokenDb::GetInstance()->Modify(type, modifyValue, conditionValue);
50 }
51
Find(AtmDataType type,const GenericValues & conditionValue,std::vector<GenericValues> & results)52 int32_t AccessTokenDbLoader::Find(AtmDataType type, const GenericValues& conditionValue,
53 std::vector<GenericValues>& results)
54 {
55 return AccessTokenDb::GetInstance()->Find(type, conditionValue, results);
56 }
57
DeleteAndInsertValues(const std::vector<DelInfo> & delInfoVec,const std::vector<AddInfo> & addInfoVec)58 int32_t AccessTokenDbLoader::DeleteAndInsertValues(const std::vector<DelInfo>& delInfoVec,
59 const std::vector<AddInfo>& addInfoVec)
60 {
61 return AccessTokenDb::GetInstance()->DeleteAndInsertValues(delInfoVec, addInfoVec);
62 }
63
DestroyRdbHelper()64 bool AccessTokenDbLoader::DestroyRdbHelper()
65 {
66 AccessTokenDb::DestroyInstance();
67
68 LOGI(ATM_DOMAIN, ATM_TAG, "Clean up rdb resource.");
69
70 // rdb release ipc resource may delay in watch, repeat ten times and wait 0.1s after each attempt
71 auto sleepTime = std::chrono::milliseconds(DESTROY_WAIT_TIME_MILLISECONDS);
72 bool isDestroy = false;
73 for (int32_t i = 0; i < DESTROY_REPEAT_TIMES; ++i) {
74 isDestroy = NativeRdb::RdbHelper::Destroy();
75 if (isDestroy) {
76 LOGI(ATM_DOMAIN, ATM_TAG, "Rdb resource clean up success!");
77 break;
78 } else {
79 std::this_thread::sleep_for(sleepTime);
80 }
81 }
82
83 return isDestroy;
84 }
85
86 extern "C" {
Create()87 void* Create()
88 {
89 return reinterpret_cast<void*>(new AccessTokenDbLoader);
90 }
91
Destroy(void * loaderPtr)92 void Destroy(void* loaderPtr)
93 {
94 AccessTokenDbLoaderInterface* loader = reinterpret_cast<AccessTokenDbLoaderInterface*>(loaderPtr);
95 if (loader != nullptr) {
96 delete loader;
97 loader = nullptr;
98 }
99 loaderPtr = nullptr;
100 }
101 }
102 } // namespace AccessToken
103 } // namespace Security
104 } // namespace OHOS
105