1 /* 2 * Copyright (c) 2021-2022 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 BASE_EVENTHANDLER_INTERFACES_INNER_API_FILE_DESCRIPTOR_LISTENER_H 17 #define BASE_EVENTHANDLER_INTERFACES_INNER_API_FILE_DESCRIPTOR_LISTENER_H 18 19 #include <cstdint> 20 #include <memory> 21 22 #include "nocopyable.h" 23 24 namespace OHOS { 25 namespace AppExecFwk { 26 // Listen input or output events 27 const uint32_t FILE_DESCRIPTOR_INPUT_EVENT = 1; 28 const uint32_t FILE_DESCRIPTOR_OUTPUT_EVENT = 2; 29 // Listen shutdown and exception events automatically, so not necessary to set. 30 const uint32_t FILE_DESCRIPTOR_SHUTDOWN_EVENT = 4; 31 const uint32_t FILE_DESCRIPTOR_EXCEPTION_EVENT = 8; 32 const uint32_t FILE_DESCRIPTOR_EVENTS_MASK = (FILE_DESCRIPTOR_INPUT_EVENT | FILE_DESCRIPTOR_OUTPUT_EVENT); 33 34 class EventHandler; 35 36 class FileDescriptorListener { 37 public: 38 enum ListenerType { 39 LTYPE_UNKNOW, 40 LTYPE_VSYNC, 41 LTYPE_UV, 42 LTYPE_MMI, 43 LTYPE_WEBVIEW, 44 }; 45 DISALLOW_COPY_AND_MOVE(FileDescriptorListener); 46 47 /** 48 * Called while file descriptor is readable. 49 * 50 * @param fileDescriptor File descriptor which is readable. 51 */ 52 virtual void OnReadable(int32_t fileDescriptor); 53 54 /** 55 * Called while file descriptor is writable. 56 * 57 * @param fileDescriptor File descriptor which is writable. 58 */ 59 virtual void OnWritable(int32_t fileDescriptor); 60 61 /** 62 * Called while shutting down this file descriptor. 63 * 64 * @param fileDescriptor File descriptor which is shutting down. 65 */ 66 virtual void OnShutdown(int32_t fileDescriptor); 67 68 /** 69 * Called while error happened on this file descriptor. 70 * 71 * @param fileDescriptor Error happened on this file descriptor. 72 */ 73 virtual void OnException(int32_t fileDescriptor); 74 75 /** 76 * Get owner of the event. 77 * 78 * @return Returns owner of the event after it has been sent. 79 */ GetOwner()80 inline std::shared_ptr<EventHandler> GetOwner() const 81 { 82 return owner_.lock(); 83 } 84 85 /** 86 * Set owner of the event. 87 * 88 * @param owner Owner for the event. 89 */ SetOwner(const std::shared_ptr<EventHandler> & owner)90 inline void SetOwner(const std::shared_ptr<EventHandler> &owner) 91 { 92 owner_ = owner; 93 } 94 95 /** 96 * Get isDeamonWaiter of the event. 97 * 98 * @return Returns deamon waiter flag. 99 */ GetIsDeamonWaiter()100 inline bool GetIsDeamonWaiter() 101 { 102 return isDeamonWaiter_; 103 } 104 105 /** 106 * Set Deamon Waiter of the event. 107 */ SetDeamonWaiter()108 inline void SetDeamonWaiter() 109 { 110 isDeamonWaiter_ = true; 111 } 112 113 /** 114 * Set the event type for the fd listener. 115 */ SetType(enum ListenerType type)116 inline void SetType(enum ListenerType type) 117 { 118 type_ = type; 119 } 120 121 /** 122 * Get the event type for the fd listener. 123 */ GetType()124 inline enum ListenerType GetType() 125 { 126 return type_; 127 } 128 129 /** 130 * Check the event type for the fd listener is vsync or not. 131 */ IsVsyncListener()132 inline bool IsVsyncListener() 133 { 134 return type_ == LTYPE_VSYNC; 135 } 136 protected: 137 FileDescriptorListener() = default; 138 virtual ~FileDescriptorListener() = default; 139 140 private: 141 std::weak_ptr<EventHandler> owner_; 142 bool isDeamonWaiter_{false}; 143 enum ListenerType type_ = LTYPE_UNKNOW; 144 }; 145 } // namespace AppExecFwk 146 } // namespace OHOS 147 148 #endif // #ifndef BASE_EVENTHANDLER_INTERFACES_INNER_API_FILE_DESCRIPTOR_LISTENER_H 149