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 #include "app_manager_death_callback.h" 27 #include "app_status_change_callback.h" 28 #include "permission_manager.h" 29 30 namespace OHOS { 31 namespace Security { 32 namespace AccessToken { 33 34 using AccessTokenID = uint32_t; 35 36 typedef struct { 37 AccessTokenID tokenID; 38 std::string permissionName; 39 uint32_t firstGrantTimes; 40 uint32_t revokeTimes; 41 } PermTimerData; 42 43 class ShortPermAppStateObserver : public ApplicationStateObserverStub { 44 public: 45 ShortPermAppStateObserver() = default; 46 ~ShortPermAppStateObserver() = default; 47 48 void OnAppStopped(const AppStateData &appStateData) override; 49 50 DISALLOW_COPY_AND_MOVE(ShortPermAppStateObserver); 51 }; 52 53 class ShortPermAppManagerDeathCallback : public AppManagerDeathCallback { 54 public: 55 ShortPermAppManagerDeathCallback() = default; 56 ~ShortPermAppManagerDeathCallback() = default; 57 58 void NotifyAppManagerDeath() override; 59 DISALLOW_COPY_AND_MOVE(ShortPermAppManagerDeathCallback); 60 }; 61 62 class ShortGrantManager { 63 public: 64 static ShortGrantManager& GetInstance(); 65 66 void OnAppMgrRemoteDiedHandle(); 67 68 #ifdef EVENTHANDLER_ENABLE 69 void InitEventHandler(); 70 #endif 71 72 int RefreshPermission(AccessTokenID tokenID, const std::string& permissionName, uint32_t onceTime); 73 74 bool IsShortGrantPermission(const std::string& permissionName); 75 76 void ClearShortPermissionByTokenID(AccessTokenID tokenID); 77 78 void RegisterAppStopListener(); 79 80 void UnRegisterAppStopListener(); 81 private: 82 ShortGrantManager(); 83 ~ShortGrantManager(); 84 uint32_t GetCurrentTime(); 85 void ScheduleRevokeTask(AccessTokenID tokenID, const std::string& permission, 86 const std::string& taskName, uint32_t cancelTimes); 87 void ClearShortPermissionData(AccessTokenID tokenID, const std::string& permission); 88 bool CancelTaskOfPermissionRevoking(const std::string& taskName); 89 uint32_t maxTime_; 90 std::vector<PermTimerData> shortGrantData_; 91 std::mutex shortGrantDataMutex_; 92 93 #ifdef EVENTHANDLER_ENABLE 94 std::shared_ptr<AccessEventHandler> GetEventHandler(); 95 std::shared_ptr<AccessEventHandler> eventHandler_; 96 std::mutex eventHandlerLock_; 97 #endif 98 sptr<ShortPermAppStateObserver> appStopCallBack_; 99 std::mutex appStopCallbackMutex_; 100 101 std::mutex appManagerDeathMutex_; 102 std::shared_ptr<ShortPermAppManagerDeathCallback> appManagerDeathCallback_ = nullptr; 103 }; 104 } // namespace AccessToken 105 } // namespace Security 106 } // namespace OHOS 107 #endif // SHORT_GRANT_MANAGER_H 108