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 MEDIA_ACCOUNT_SUBSCRIBER_H
17 #define MEDIA_ACCOUNT_SUBSCRIBER_H
18 
19 #include "common_event_subscriber.h"
20 #include "common_event_support.h"
21 #include <vector>
22 #include <mutex>
23 
24 namespace OHOS {
25 namespace Media {
26 class CommonEventReceiver {
27 public:
OnCommonEventReceived(const std::string & event)28     virtual void OnCommonEventReceived(const std::string &event) {};
29     virtual ~CommonEventReceiver() = default;
30 };
31 
32 class AccountSubscriber : public EventFwk::CommonEventSubscriber,
33                           public std::enable_shared_from_this<AccountSubscriber> {
34 public:
35     explicit AccountSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo);
36     virtual ~AccountSubscriber();
37     void OnReceiveEvent(const EventFwk::CommonEventData &eventData) override;
38     void RegisterCommonEventReceiver(int32_t userId, const std::shared_ptr<CommonEventReceiver> &receiver);
39     void UnregisterCommonEventReceiver(int32_t userId, const std::shared_ptr<CommonEventReceiver> &receiver);
40 
41     static std::shared_ptr<AccountSubscriber> GetInstance();
42 private:
43     AccountSubscriber() = default;
44     void DispatchEvent(int32_t userId, const std::string &action);
45 
46     static std::shared_ptr<AccountSubscriber> instance_;
47     std::mutex userMutex_;
48     std::map<int32_t, std::vector<std::shared_ptr<CommonEventReceiver>>> userMap_;
49 };
50 } // namespace Media
51 } // namespace OHOS
52 #endif // MEDIA_ACCOUNT_SUBSCRIBER_H