• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 RESSCHED_SERVICES_RESSCHEDMGR_RESSCHEDFWK_INCLUDE_EVENT_LISTENER_MGR_H
17 #define RESSCHED_SERVICES_RESSCHEDMGR_RESSCHEDFWK_INCLUDE_EVENT_LISTENER_MGR_H
18 
19 #include <list>
20 #include <map>
21 #include <mutex>
22 #include <string>
23 #include <unordered_set>
24 #include <vector>
25 
26 #include "ffrt.h"
27 #include "iremote_object.h"
28 #include "refbase.h"
29 #include "res_type.h"
30 #include "nlohmann/json.hpp"
31 #include "single_instance.h"
32 
33 namespace OHOS {
34 namespace ResourceSchedule {
35 class EventListenerMgr {
36     DECLARE_SINGLE_INSTANCE_BASE(EventListenerMgr);
37 public:
38     struct EventListenerInfo {
39         sptr<IRemoteObject> listener = nullptr;
40         pid_t pid;
41         std::unordered_set<uint32_t> groups;
42     };
43     using EventListenerMap = std::map<uint32_t, std::map<pid_t, EventListenerInfo>>;
44     void Init();
45     void Deinit();
46     void RegisterEventListener(int32_t callingPid, const sptr<IRemoteObject>& listener, uint32_t eventType,
47         uint32_t listenerGroup);
48     void UnRegisterEventListener(int32_t callingPid, uint32_t eventType, uint32_t listenerGroup);
49     void OnRemoteNotifierDied(const sptr<IRemoteObject>& notifier);
50     void SendEvent(uint32_t eventType, uint32_t eventValue, const nlohmann::json &extInfo,
51         uint32_t listenerGroup = ResType::EventListenerGroup::LISTENER_GROUP_COMMON);
52     std::unordered_map<int32_t, std::vector<pid_t>> DumpRegisterInfo();
53 private:
54     EventListenerMgr() = default;
55     ~EventListenerMgr();
56     void RemoveListenerLock(const sptr<IRemoteObject>& listener);
57     void SendEventLock(uint32_t eventType, uint32_t eventValue, const nlohmann::json &extInfo,
58         uint32_t listenerGroup);
59     void HandleSendEvent(std::vector<sptr<IRemoteObject>>& listenerVec,
60         uint32_t eventType, uint32_t eventValue, const nlohmann::json &extInfo, uint32_t listenerGroup);
61     void OnRemoteListenerDied(const sptr<IRemoteObject>& listener);
62 
63     bool initialized_ = false;
64     std::mutex mutex_;
65     EventListenerMap eventListenerMap_;
66     sptr<IRemoteObject::DeathRecipient> eventListenerDeathRecipient_ = nullptr;
67     std::shared_ptr<ffrt::queue> eventSenderQueue_ = nullptr;
68 };
69 } // ResourceSchedule
70 } // OHOS
71 #endif // RESSCHED_SERVICES_RESSCHEDMGR_RESSCHEDFWK_INCLUDE_NOTIFIER_MGR_H