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_common_log.h" 24 #include "i_permission_state_callback.h" 25 #include "permission_state_change_info.h" 26 #include "accesstoken_callback_proxys.h" 27 28 namespace OHOS { 29 namespace Security { 30 namespace AccessToken { 31 struct CallbackRecord { CallbackRecordCallbackRecord32 CallbackRecord() : scopePtr_(nullptr), callbackObject_(nullptr) 33 {} CallbackRecordCallbackRecord34 CallbackRecord(std::shared_ptr<PermStateChangeScope> callbackScopePtr, sptr<IRemoteObject> callback) 35 : scopePtr_(callbackScopePtr), callbackObject_(callback) 36 {} 37 38 std::shared_ptr<PermStateChangeScope> scopePtr_; 39 sptr<IRemoteObject> callbackObject_; 40 }; 41 42 class CallbackManager { 43 public: 44 virtual ~CallbackManager(); 45 CallbackManager(); 46 static CallbackManager& GetInstance(); 47 48 int32_t AddCallback(const PermStateChangeScope& scope, const sptr<IRemoteObject>& callback); 49 int32_t RemoveCallback(const sptr<IRemoteObject>& callback); 50 bool CalledAccordingToTokenIdLlist(const std::vector<AccessTokenID>& tokenIDList, AccessTokenID tokenID); 51 bool CalledAccordingToPermLlist(const std::vector<std::string>& permList, const std::string& permName); 52 void ExecuteCallbackAsync(AccessTokenID tokenID, const std::string& permName, int32_t changeType); 53 54 private: 55 void ExecuteAllCallback(std::vector<sptr<IRemoteObject>>& list, AccessTokenID tokenID, const std::string& permName, 56 int32_t changeType); 57 void GetCallbackObjectList(AccessTokenID tokenID, const std::string& permName, 58 std::vector<sptr<IRemoteObject>>& list); 59 std::mutex mutex_; 60 std::vector<CallbackRecord> callbackInfoList_; 61 sptr<IRemoteObject::DeathRecipient> callbackDeathRecipient_; 62 }; 63 } // namespace AccessToken 64 } // namespace Security 65 } // namespace OHOS 66 #endif // ACCESS_TOKEN_PERMISSION_CALLBACK_MANAGER_H 67