1 /* 2 * Copyright (c) 2022-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 #ifndef ACCESS_TOKEN_PERMISSION_CALLBACK_MANAGER_H 17 #define ACCESS_TOKEN_PERMISSION_CALLBACK_MANAGER_H 18 19 #include <mutex> 20 #include <vector> 21 22 #include "access_token.h" 23 #include "accesstoken_log.h" 24 #ifdef RESOURCESCHEDULE_FFRT_ENABLE 25 #include "ffrt.h" 26 #endif 27 #include "i_permission_state_callback.h" 28 #include "permission_state_change_info.h" 29 #include "accesstoken_callback_proxys.h" 30 31 namespace OHOS { 32 namespace Security { 33 namespace AccessToken { 34 enum PermStateChangeType { 35 REVOKED = 0, 36 GRANTED = 1, 37 }; 38 struct CallbackRecord { CallbackRecordCallbackRecord39 CallbackRecord() : scopePtr_(nullptr), callbackObject_(nullptr) 40 {} CallbackRecordCallbackRecord41 CallbackRecord(std::shared_ptr<PermStateChangeScope> callbackScopePtr, sptr<IRemoteObject> callback) 42 : scopePtr_(callbackScopePtr), callbackObject_(callback) 43 {} 44 45 std::shared_ptr<PermStateChangeScope> scopePtr_; 46 sptr<IRemoteObject> callbackObject_; 47 }; 48 49 class CallbackManager { 50 public: 51 virtual ~CallbackManager(); 52 CallbackManager(); 53 static CallbackManager& GetInstance(); 54 55 int32_t AddCallback(const PermStateChangeScope& scope, const sptr<IRemoteObject>& callback); 56 int32_t RemoveCallback(const sptr<IRemoteObject>& callback); 57 bool CalledAccordingToTokenIdLlist(const std::vector<AccessTokenID>& tokenIDList, AccessTokenID tokenID); 58 bool CalledAccordingToPermLlist(const std::vector<std::string>& permList, const std::string& permName); 59 void ExecuteCallbackAsync(AccessTokenID tokenID, const std::string& permName, int32_t changeType); 60 61 private: 62 void ExcuteAllCallback(std::vector<sptr<IRemoteObject>>& list, AccessTokenID tokenID, const std::string& permName, 63 int32_t changeType); 64 void GetCallbackObjectList(AccessTokenID tokenID, const std::string& permName, 65 std::vector<sptr<IRemoteObject>>& list); 66 #ifdef RESOURCESCHEDULE_FFRT_ENABLE 67 ffrt::mutex mutex_; 68 #else 69 std::mutex mutex_; 70 #endif 71 std::vector<CallbackRecord> callbackInfoList_; 72 sptr<IRemoteObject::DeathRecipient> callbackDeathRecipient_; 73 }; 74 } // namespace AccessToken 75 } // namespace Security 76 } // namespace OHOS 77 #endif // ACCESS_TOKEN_PERMISSION_CALLBACK_MANAGER_H 78