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 struct CallbackRecord {
CallbackRecordCallbackRecord35     CallbackRecord() : scopePtr_(nullptr), callbackObject_(nullptr)
36     {}
CallbackRecordCallbackRecord37     CallbackRecord(std::shared_ptr<PermStateChangeScope> callbackScopePtr, sptr<IRemoteObject> callback)
38         : scopePtr_(callbackScopePtr), callbackObject_(callback)
39     {}
40 
41     std::shared_ptr<PermStateChangeScope> scopePtr_;
42     sptr<IRemoteObject> callbackObject_;
43 };
44 
45 class CallbackManager {
46 public:
47     virtual ~CallbackManager();
48     CallbackManager();
49     static CallbackManager& GetInstance();
50 
51     int32_t AddCallback(const PermStateChangeScope& scope, const sptr<IRemoteObject>& callback);
52     int32_t RemoveCallback(const sptr<IRemoteObject>& callback);
53     bool CalledAccordingToTokenIdLlist(const std::vector<AccessTokenID>& tokenIDList, AccessTokenID tokenID);
54     bool CalledAccordingToPermLlist(const std::vector<std::string>& permList, const std::string& permName);
55     void ExecuteCallbackAsync(AccessTokenID tokenID, const std::string& permName, int32_t changeType);
56 
57 private:
58     void ExcuteAllCallback(std::vector<sptr<IRemoteObject>>& list, AccessTokenID tokenID, const std::string& permName,
59         int32_t changeType);
60     void GetCallbackObjectList(AccessTokenID tokenID, const std::string& permName,
61         std::vector<sptr<IRemoteObject>>& list);
62 #ifdef RESOURCESCHEDULE_FFRT_ENABLE
63     ffrt::mutex mutex_;
64 #else
65     std::mutex mutex_;
66 #endif
67     std::vector<CallbackRecord> callbackInfoList_;
68     sptr<IRemoteObject::DeathRecipient> callbackDeathRecipient_;
69 };
70 } // namespace AccessToken
71 } // namespace Security
72 } // namespace OHOS
73 #endif // ACCESS_TOKEN_PERMISSION_CALLBACK_MANAGER_H
74