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 TOKEN_MODIFY_NOTIFIER_H
17 #define TOKEN_MODIFY_NOTIFIER_H
18 
19 #include <atomic>
20 #include <set>
21 #include <vector>
22 
23 #include "access_token.h"
24 #include "i_token_sync_callback.h"
25 #include "nocopyable.h"
26 #include "rwlock.h"
27 #ifndef RESOURCESCHEDULE_FFRT_ENABLE
28 #include "thread_pool.h"
29 #endif
30 #include "callback_death_recipients.h"
31 
32 namespace OHOS {
33 namespace Security {
34 namespace AccessToken {
35 class TokenModifyNotifier final {
36 public:
37     static TokenModifyNotifier& GetInstance();
38     ~TokenModifyNotifier();
39     void AddHapTokenObservation(AccessTokenID tokenID);
40     void NotifyTokenDelete(AccessTokenID tokenID);
41     void NotifyTokenModify(AccessTokenID tokenID);
42     void NotifyTokenChangedIfNeed();
43     void NotifyTokenSyncTask();
44     int32_t GetRemoteHapTokenInfo(const std::string& deviceID, AccessTokenID tokenID);
45     int32_t RegisterTokenSyncCallback(const sptr<IRemoteObject>& callback);
46     int32_t UnRegisterTokenSyncCallback();
47 #ifdef RESOURCESCHEDULE_FFRT_ENABLE
48     int32_t GetCurTaskNum();
49     void AddCurTaskNum();
50     void ReduceCurTaskNum();
51 #endif
52 
53 private:
54     TokenModifyNotifier();
55     DISALLOW_COPY_AND_MOVE(TokenModifyNotifier);
56 
57     bool hasInited_;
58     OHOS::Utils::RWLock initLock_;
59     OHOS::Utils::RWLock Notifylock_;
60 #ifdef RESOURCESCHEDULE_FFRT_ENABLE
61     std::atomic_int32_t curTaskNum_;
62 #else
63     OHOS::ThreadPool notifyTokenWorker_;
64 #endif
65     std::set<AccessTokenID> observationSet_;
66     std::vector<AccessTokenID> deleteTokenList_;
67     std::vector<AccessTokenID> modifiedTokenList_;
68     sptr<ITokenSyncCallback> tokenSyncCallbackObject_ = nullptr;
69 #ifdef TOKEN_SYNC_ENABLE
70     sptr<TokenSyncCallbackDeathRecipient> tokenSyncCallbackDeathRecipient_ = nullptr;
71 #endif
72 };
73 } // namespace AccessToken
74 } // namespace Security
75 } // namespace OHOS
76 #endif // TOKEN_MODIFY_NOTIFIER_H
77