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_EVENT_MANAGER_ADAPT_H 17 #define OHOS_EVENT_MANAGER_ADAPT_H 18 19 #include <functional> 20 #include <map> 21 #include <mutex> 22 23 #include "common_event.h" 24 #include "common_event_data.h" 25 #include "common_event_manager.h" 26 #include "common_event_subscribe_info.h" 27 #include "common_event_subscriber.h" 28 #include "dm_log.h" 29 #include "matching_skills.h" 30 #include "single_instance.h" 31 32 namespace OHOS { 33 namespace DistributedHardware { 34 using CommomEventCallback = std::function<void(int32_t)>; 35 36 struct CommomEventCallbackNode { 37 int32_t input_; 38 CommomEventCallback callback_; 39 }; 40 41 class DmCommonEventManager { 42 DECLARE_SINGLE_INSTANCE_BASE(DmCommonEventManager); 43 public: 44 bool SubscribeServiceEvent(const std::string &event, const CommomEventCallback callback); 45 bool UnsubscribeServiceEvent(const std::string &event); 46 class EventSubscriber : public EventFwk::CommonEventSubscriber { 47 public: EventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo,const CommomEventCallback & callback,const std::string & event)48 EventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo, const CommomEventCallback &callback, 49 const std::string &event) : EventFwk::CommonEventSubscriber(subscribeInfo), 50 callback_(callback), event_(event) {} 51 void OnReceiveEvent(const EventFwk::CommonEventData &data); 52 private: 53 CommomEventCallback callback_; 54 std::string event_; 55 }; 56 private: 57 DmCommonEventManager(); 58 ~DmCommonEventManager(); 59 private: 60 static void DealCallback(void); 61 private: 62 static std::mutex callbackQueueMutex_; 63 static std::mutex eventSubscriberMutex_; 64 static std::condition_variable notEmpty_; 65 static std::list<CommomEventCallbackNode> callbackQueue_; 66 std::map<std::string, std::shared_ptr<EventSubscriber>> dmEventSubscriber_; 67 }; 68 } // namespace DistributedHardware 69 } // namespace OHOS 70 #endif // OHOS_EVENT_MANAGER_ADAPT_H