1 /*
2  * Copyright (c) 2021 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 OHOS_DEVICE_PROFILE_PROFILE_EVENT_HANDLER_H
17 #define OHOS_DEVICE_PROFILE_PROFILE_EVENT_HANDLER_H
18 
19 #include <map>
20 #include <unordered_set>
21 #include <list>
22 #include <atomic>
23 
24 #include "event_handler.h"
25 #include "iremote_object.h"
26 
27 #include "iprofile_event_notifier.h"
28 #include "profile_event.h"
29 #include "profile_change_notification.h"
30 #include "subscribe_info.h"
31 #include "single_instance.h"
32 
33 namespace OHOS {
34 namespace DeviceProfile {
35 struct FilterInfo {
36     bool filtered {false};
37     std::unordered_set<uint32_t> indexes;
38 };
39 
40 class ProfileEventHandler {
41     friend class SubscriberDeathRecipient;
42 public:
ProfileEventHandler(const std::string & handlerName)43     explicit ProfileEventHandler(const std::string& handlerName)
44         : handlerName_(handlerName) {}
45     virtual ~ProfileEventHandler() = default;
46 
47     bool Init();
48     bool IsRegistered() const;
49 
50     virtual int32_t Subscribe(const SubscribeInfo& subscribeInfo,
51         const sptr<IRemoteObject>& profileEventNotifier);
52     int32_t Unsubscribe(const sptr<IRemoteObject>& profileEventNotifier);
53     void OnSubscriberDied(const sptr<IRemoteObject>& profileEventNotifier);
54     std::string AnonymizeSubscribeInfo(const SubscribeInfo& subscribeInfo);
55 
56 protected:
57     std::atomic<bool> isRegistered_ {false};
58     std::mutex notifierLock_;
59     std::map<sptr<IRemoteObject>, SubscribeInfo> profileEventSubscribeInfos_;
60     std::string handlerName_;
61     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_;
62 
63 private:
64     virtual int32_t Register() = 0;
65     virtual int32_t Unregister() = 0;
66 };
67 }  // namespace DeviceProfile
68 }  // namespace OHOS
69 
70 #endif  // OHOS_DEVICE_PROFILE_I_PROFILE_EVENT_H
71