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 ACTIVE_STATUS_CHANGE_CALLBACK_MANAGER_H
17 #define ACTIVE_STATUS_CHANGE_CALLBACK_MANAGER_H
18 
19 #include <mutex>
20 #include <vector>
21 
22 #ifdef EVENTHANDLER_ENABLE
23 #include "access_event_handler.h"
24 #endif
25 #include "access_token.h"
26 #include "accesstoken_log.h"
27 #include "perm_active_status_callback_death_recipient.h"
28 #include "perm_active_status_change_callback_proxy.h"
29 
30 namespace OHOS {
31 namespace Security {
32 namespace AccessToken {
33 struct CallbackData {
CallbackDataCallbackData34     CallbackData() : permList_(), callbackObject_(nullptr)
35     {}
CallbackDataCallbackData36     CallbackData(const std::vector<std::string>& permList, sptr<IRemoteObject> callback)
37         : permList_(permList), callbackObject_(callback)
38     {}
39 
40     AccessTokenID registerTokenId {0};
41     std::vector<std::string> permList_;
42     sptr<IRemoteObject> callbackObject_;
43 };
44 
45 class ActiveStatusCallbackManager {
46 public:
47     virtual ~ActiveStatusCallbackManager();
48     ActiveStatusCallbackManager();
49     static ActiveStatusCallbackManager& GetInstance();
50 
51     int32_t AddCallback(
52         AccessTokenID regiterTokenId, const std::vector<std::string>& permList, const sptr<IRemoteObject>& callback);
53     int32_t RemoveCallback(const sptr<IRemoteObject>& callback);
54     bool NeedCalled(const std::vector<std::string>& permList, const std::string& permName);
55     void ExecuteCallbackAsync(
56         AccessTokenID tokenId, const std::string& permName, const std::string& deviceId, ActiveChangeType changeType);
57 #ifdef EVENTHANDLER_ENABLE
58     void InitEventHandler(const std::shared_ptr<AccessEventHandler>& eventHandler);
59 #endif
60     void ActiveStatusChange(AccessTokenID tokenId, const std::string& permName,
61         const std::string& deviceId, ActiveChangeType changeType);
62 private:
63     std::mutex mutex_;
64     std::vector<CallbackData> callbackDataList_;
65     sptr<IRemoteObject::DeathRecipient> callbackDeathRecipient_;
66 #ifdef EVENTHANDLER_ENABLE
67     std::shared_ptr<AccessEventHandler> eventHandler_;
68 #endif
69 };
70 } // namespace AccessToken
71 } // namespace Security
72 } // namespace OHOS
73 #endif // ACTIVE_STATUS_CHANGE_CALLBACK_MANAGER_H
74