• 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 <string>
20 #include <set>
21 #include <vector>
22 #include "access_token.h"
23 #include "nocopyable.h"
24 #include "permission_record.h"
25 #include "permission_record_node.h"
26 #include "rwlock.h"
27 #include "thread_pool.h"
28 namespace OHOS {
29 namespace Security {
30 namespace AccessToken {
31 class PermissionUsedRecordCache {
32 public:
33     static PermissionUsedRecordCache& GetInstance();
34     ~PermissionUsedRecordCache();
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, const GenericValues& andConditionValues,
43         std::vector<GenericValues>& findRecordsValues, int32_t cache1QueryCount);
44     void GetFromPersistQueueAndDatabase(const std::set<int32_t>& opCodeList, const GenericValues& andConditionValues,
45         std::vector<GenericValues>& findRecordsValues, int32_t cache2QueryCount);
46     bool RecordCompare(const AccessTokenID tokenId, const std::set<int32_t>& opCodeList,
47         const GenericValues& andConditionValues, const PermissionRecord& record);
48     void FindTokenIdList(std::set<AccessTokenID>& tokenIdList);
49     void TransferToOpcode(std::set<int32_t>& opCodeList,
50         const std::vector<std::string>& permissionList);
51     void ResetRecordBuffer(const int32_t remainCount,
52         std::shared_ptr<PermissionUsedRecordNode>& persistPendingBufferEnd);
53     void ResetRecordBufferWhenAdd(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 #ifdef POWER_MANAGER_ENABLE
58     void PersistPendingRecordsImmediately();
59 #endif
60 
61 private:
62     PermissionUsedRecordCache();
63     DISALLOW_COPY_AND_MOVE(PermissionUsedRecordCache);
64     bool RecordMergeCheck(const PermissionRecord& record1, const PermissionRecord& record2);
65     void DeepCopyFromHead(const std::shared_ptr<PermissionUsedRecordNode>& oriHeadNode,
66         std::shared_ptr<PermissionUsedRecordNode>& copyHeadNode, int32_t copyCount);
67     bool hasInited_;
68     OHOS::Utils::RWLock initLock_;
69     int32_t readableSize_ = 0;
70     std::shared_ptr<PermissionUsedRecordNode> recordBufferHead_ = std::make_shared<PermissionUsedRecordNode>();
71     std::shared_ptr<PermissionUsedRecordNode> curRecordBufferPos_ = recordBufferHead_;
72     std::vector<std::shared_ptr<PermissionUsedRecordNode>> persistPendingBufferQueue_;
73     const static int64_t INTERVAL = 15 * 60 * 1000; // 1s = 1000ms
74     const static int32_t MAX_PERSIST_SIZE = 100;
75     bool persistIsRunning_ = false;
76     // cacheLock1_ is used for locking recordBufferHead_ and curRecordBufferPos_
77     OHOS::Utils::RWLock cacheLock1_;
78     // cacheLock2_ is used for locking persistPendingBufferQueue_ and persistIsRunning_
79     OHOS::Utils::RWLock cacheLock2_;
80     OHOS::ThreadPool readRecordBufferTaskWorker_;
81 };
82 } // namespace AccessToken
83 } // namespace Security
84 } // namespace OHOS
85 #endif // PERMISSION_USED_RECORD_CACHE_H
86