1 /* 2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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_SHARING_EVENT_HANDLER_MANAGER_H 17 #define OHOS_SHARING_EVENT_HANDLER_MANAGER_H 18 19 #include <memory> 20 #include <mutex> 21 #include <singleton.h> 22 #include "event_handler.h" 23 #include "event_runner.h" 24 #include "file_descriptor_listener.h" 25 26 namespace OHOS { 27 namespace Sharing { 28 using namespace OHOS::AppExecFwk; 29 30 class EventHandlerManager : public Singleton<EventHandlerManager> { 31 friend class Singleton<EventHandlerManager>; 32 33 public: 34 virtual ~EventHandlerManager(); 35 36 int32_t DelFdListener(int32_t fd); 37 int32_t AddFdListener(int32_t fd, const std::shared_ptr<FileDescriptorListener> &listener); 38 39 int32_t DelEventHandler(int32_t fd); 40 int32_t AddEventHandler(int32_t fd, const std::shared_ptr<OHOS::AppExecFwk::EventHandler> &handler); 41 42 std::shared_ptr<OHOS::AppExecFwk::EventRunner> GetEventRunner(); 43 std::shared_ptr<OHOS::AppExecFwk::EventHandler> GetEventHandler(int32_t fd); 44 45 private: 46 EventHandlerManager(); 47 48 void Init(); 49 50 private: 51 std::mutex mutex; 52 std::shared_ptr<OHOS::AppExecFwk::EventRunner> eventRunner_ = nullptr; 53 std::map<int32_t, std::weak_ptr<FileDescriptorListener>> listenerMap_; 54 std::map<int32_t, std::weak_ptr<OHOS::AppExecFwk::EventHandler>> handlerMap_; 55 }; 56 57 } // namespace Sharing 58 } // namespace OHOS 59 #endif