1 /* 2 * Copyright (c) 2022 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 #include "i_permission_state_callback.h" 25 #include "permission_state_change_info.h" 26 #include "permission_state_change_callback_proxy.h" 27 28 namespace OHOS { 29 namespace Security { 30 namespace AccessToken { 31 enum PermStateChangeType { 32 REVOKED = 0, 33 GRANTED = 1, 34 }; 35 struct CallbackRecord { CallbackRecordCallbackRecord36 CallbackRecord() : scopePtr_(nullptr), callbackObject_(nullptr) 37 {} CallbackRecordCallbackRecord38 CallbackRecord(std::shared_ptr<PermStateChangeScope> callbackScopePtr, sptr<IRemoteObject> callback) 39 : scopePtr_(callbackScopePtr), callbackObject_(callback) 40 {} 41 42 std::shared_ptr<PermStateChangeScope> scopePtr_; 43 sptr<IRemoteObject> callbackObject_; 44 }; 45 46 class CallbackManager { 47 public: 48 virtual ~CallbackManager(); 49 CallbackManager(); 50 static CallbackManager& GetInstance(); 51 52 int32_t AddCallback(const PermStateChangeScope& scope, const sptr<IRemoteObject>& callback); 53 int32_t RemoveCallback(const sptr<IRemoteObject>& callback); 54 bool CalledAccordingToTokenIdLlist(const std::vector<AccessTokenID>& tokenIDList, AccessTokenID tokenID); 55 bool CalledAccordingToPermLlist(const std::vector<std::string>& permList, const std::string& permName); 56 void ExecuteCallbackAsync(AccessTokenID tokenID, const std::string& permName, int32_t changeType); 57 58 private: 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