• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 #ifndef PERMISSION_USED_RECORD_CACHE_H
17 #define PERMISSION_USED_RECORD_CACHE_H
18 
19 #include <map>
20 #include <string>
21 #include <set>
22 #include <vector>
23 #include "accesstoken_kit.h"
24 #include "nocopyable.h"
25 #include "permission_record.h"
26 #include "permission_record_node.h"
27 #include "rwlock.h"
28 #include "thread_pool.h"
29 namespace OHOS {
30 namespace Security {
31 namespace AccessToken {
32 class PermissionUsedRecordCache {
33 public:
34     static PermissionUsedRecordCache& GetInstance();
35     void AddRecordToBuffer(const PermissionRecord& record);
36     void MergeRecord(PermissionRecord& record, std::shared_ptr<PermissionUsedRecordNode> curFindMergePos);
37     void AddToPersistQueue(const std::shared_ptr<PermissionUsedRecordNode> persistPendingBufferHead);
38     void ExecuteReadRecordBufferTask();
39     int32_t PersistPendingRecords();
40     int32_t RemoveRecords(const AccessTokenID tokenId);
41     void RemoveFromPersistQueueAndDatabase(const AccessTokenID tokenId);
42     void GetRecords(const std::vector<std::string>& permissionList,
43         const GenericValues& andConditionValues, const GenericValues& orConditionValues,
44         std::vector<GenericValues>& findRecordsValues);
45     void GetFromPersistQueueAndDatabase(const std::set<int32_t>& opCodeList,
46         const GenericValues& andConditionValues, const GenericValues& orConditionValues,
47         std::vector<GenericValues>& findRecordsValues);
48     bool RecordCompare(const AccessTokenID tokenId, const std::set<int32_t>& opCodeList,
49         const GenericValues& andConditionValues, const PermissionRecord& record);
50     void FindTokenIdList(std::set<AccessTokenID>& tokenIdList);
51     void TransferToOpcode(std::set<int32_t>& opCodeList,
52         const std::vector<std::string>& permissionList);
53     void ResetRecordBuffer(const int32_t remainCount,
54         std::shared_ptr<PermissionUsedRecordNode>& persistPendingBufferEnd);
55     void AddRecordNode(const PermissionRecord& record);
56     void DeleteRecordNode(std::shared_ptr<PermissionUsedRecordNode> deleteRecordNode);
57 
58 private:
59     int32_t readableSize_ = 0;
60     std::shared_ptr<PermissionUsedRecordNode> recordBufferHead_ = std::make_shared<PermissionUsedRecordNode>();
61     std::shared_ptr<PermissionUsedRecordNode> curRecordBufferPos_ = recordBufferHead_;
62     std::vector<std::shared_ptr<PermissionUsedRecordNode>> persistPendingBufferQueue_;
63     int64_t nextPersistTimestamp_ = 0L;
64     const static int64_t INTERVAL = 15 * 60 * 1000; // 1s = 1000ms
65     const static int32_t MAX_PERSIST_SIZE = 100;
66     bool persistIsRunning_ = false;
67     // cacheLock1_ is used for locking recordBufferHead_ and curRecordBufferPos_
68     OHOS::Utils::RWLock cacheLock1_;
69     // cacheLock2_ is used for locking persistPendingBufferQueue_ and persistIsRunning_
70     OHOS::Utils::RWLock cacheLock2_;
71     OHOS::ThreadPool readRecordBufferTaskWorker_;
72 };
73 } // namespace AccessToken
74 } // namespace Security
75 } // namespace OHOS
76 #endif // PERMISSION_USED_RECORD_CACHE_H
77