1 /*
2  * Copyright (c) 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 
16 
17 #ifndef SHORT_GRANT_MANAGER_H
18 #define SHORT_GRANT_MANAGER_H
19 
20 #include <memory>
21 #include <mutex>
22 #include <unordered_map>
23 #include <string>
24 
25 #include "access_event_handler.h"
26 
27 namespace OHOS {
28 namespace Security {
29 namespace AccessToken {
30 
31 using AccessTokenID = uint32_t;
32 
33 typedef struct {
34     AccessTokenID tokenID;
35     std::string permissionName;
36     uint32_t firstGrantTimes;
37     uint32_t revokeTimes;
38 } PermTimerData;
39 
40 class ShortGrantManager {
41 public:
42     static ShortGrantManager& GetInstance();
43 
44     void InitEventHandler(const std::shared_ptr<AccessEventHandler>& eventHandler);
45 
46     int RefreshPermission(AccessTokenID tokenID, const std::string& permissionName, uint32_t onceTime);
47 
48     bool IsShortGrantPermission(const std::string& permissionName);
49 
50 private:
51     ShortGrantManager();
52     ~ShortGrantManager() = default;
53     uint32_t GetCurrentTime();
54     void ScheduleRevokeTask(AccessTokenID tokenID, const std::string& permission,
55         const std::string& taskName, uint32_t cancelTimes);
56     void ClearShortPermissionData(AccessTokenID tokenID, const std::string& permission);
57     bool CancelTaskOfPermissionRevoking(const std::string& taskName);
58     uint32_t maxTime_;
59     std::vector<PermTimerData> shortGrantData_;
60     std::mutex shortGrantDataMutex_;
61     std::shared_ptr<AccessEventHandler> eventHandler_;
62 };
63 } // namespace AccessToken
64 } // namespace Security
65 } // namespace OHOS
66 #endif // SHORT_GRANT_MANAGER_H
67