• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-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 #include "camera_common_event_manager.h"
17 
18 namespace OHOS {
19 namespace CameraStandard {
20 
21 sptr<CameraCommonEventManager> CameraCommonEventManager::instance_ = nullptr;
22 std::mutex CameraCommonEventManager::instanceMutex_;
23 std::mutex CameraCommonEventManager::mapMutex_;
CameraCommonEventManager()24 CameraCommonEventManager::CameraCommonEventManager()
25 {}
26 
~CameraCommonEventManager()27 CameraCommonEventManager::~CameraCommonEventManager()
28 {}
29 
GetInstance()30 sptr<CameraCommonEventManager> CameraCommonEventManager::GetInstance()
31 {
32     if (instance_ != nullptr) {
33         return instance_;
34     }
35     std::lock_guard<std::mutex> lock(instanceMutex_);
36     if (instance_ != nullptr) {
37         return instance_;
38     }
39     instance_ = new (std::nothrow) CameraCommonEventManager();
40     return instance_;
41 }
42 
SubscribeCommonEvent(const std::string & eventName,EventReceiver receiver)43 void CameraCommonEventManager::SubscribeCommonEvent(const std::string &eventName, EventReceiver receiver)
44 {
45     MEDIA_INFO_LOG("SubscribeCommonEvent eventName=%{public}s", eventName.c_str());
46     EventFwk::MatchingSkills matchingSkills;
47     matchingSkills.AddEvent(eventName);
48     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
49     auto subscriberPtr = std::make_shared<CameraCommonEventSubscriber>(subscribeInfo, receiver);
50     if (subscriberPtr == nullptr) {
51         MEDIA_INFO_LOG("subscriberPtr is nullptr");
52         return;
53     }
54     if (EventFwk::CommonEventManager::SubscribeCommonEvent(subscriberPtr)) {
55         MEDIA_INFO_LOG("SubscribeCommonEvent success");
56         {
57             std::lock_guard<std::mutex> lock(mapMutex_);
58             commonEventSubscriberMap_.insert(std::make_pair(eventName, subscriberPtr));
59         }
60     }
61 }
62 
UnSubscribeCommonEvent(const std::string & eventName)63 void CameraCommonEventManager::UnSubscribeCommonEvent(const std::string &eventName)
64 {
65     MEDIA_INFO_LOG("UnSubscribeCommonEvent eventName=%{public}s", eventName.c_str());
66     std::lock_guard<std::mutex> lock(mapMutex_);
67     auto subscriber = commonEventSubscriberMap_.find(eventName);
68     if (subscriber == commonEventSubscriberMap_.end()) {
69         MEDIA_INFO_LOG("UnSubscribeCommonEvent event:%{public}s is not subscribed", eventName.c_str());
70         return;
71     }
72     auto commonEventSubscriber = subscriber->second;
73     if (commonEventSubscriber != nullptr) {
74         EventFwk::CommonEventManager::UnSubscribeCommonEvent(commonEventSubscriber);
75     }
76     commonEventSubscriberMap_.erase(subscriber);
77 }
78 
CameraCommonEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo,EventReceiver receiver)79 CameraCommonEventManager::CameraCommonEventSubscriber::CameraCommonEventSubscriber(
80     const EventFwk::CommonEventSubscribeInfo &subscribeInfo, EventReceiver receiver)
81     : EventFwk::CommonEventSubscriber(subscribeInfo), eventReceiver_(receiver) {}
82 
OnReceiveEvent(const EventFwk::CommonEventData & data)83 void CameraCommonEventManager::CameraCommonEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
84 {
85     if (eventReceiver_ == nullptr) {
86         MEDIA_INFO_LOG("eventReceiver_ is nullptr");
87         return;
88     }
89     eventReceiver_(data);
90 }
91 
92 } // namespace CameraStandard
93 } // namespace OHOS
94