• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     DISALLOW_COPY_AND_MOVE(FileDescriptorListener);
39 
40     /**
41      * Called while file descriptor is readable.
42      *
43      * @param fileDescriptor File descriptor which is readable.
44      */
45     virtual void OnReadable(int32_t fileDescriptor);
46 
47     /**
48      * Called while file descriptor is writable.
49      *
50      * @param fileDescriptor File descriptor which is writable.
51      */
52     virtual void OnWritable(int32_t fileDescriptor);
53 
54     /**
55      * Called while shutting down this file descriptor.
56      *
57      * @param fileDescriptor File descriptor which is shutting down.
58      */
59     virtual void OnShutdown(int32_t fileDescriptor);
60 
61     /**
62      * Called while error happened on this file descriptor.
63      *
64      * @param fileDescriptor Error happened on this file descriptor.
65      */
66     virtual void OnException(int32_t fileDescriptor);
67 
68     /**
69      * Get owner of the event.
70      *
71      * @return Returns owner of the event after it has been sent.
72      */
GetOwner()73     inline std::shared_ptr<EventHandler> GetOwner() const
74     {
75         return owner_.lock();
76     }
77 
78     /**
79      * Set owner of the event.
80      *
81      * @param owner Owner for the event.
82      */
SetOwner(const std::shared_ptr<EventHandler> & owner)83     inline void SetOwner(const std::shared_ptr<EventHandler> &owner)
84     {
85         owner_ = owner;
86     }
87 
88 protected:
89     FileDescriptorListener() = default;
90     virtual ~FileDescriptorListener() = default;
91 
92 private:
93     std::weak_ptr<EventHandler> owner_;
94 };
95 }  // namespace AppExecFwk
96 }  // namespace OHOS
97 
98 #endif  // #ifndef BASE_EVENTHANDLER_INTERFACES_INNER_API_FILE_DESCRIPTOR_LISTENER_H
99