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 AAFWK_APPEXECFWK_NATIVE_IMPLEMENT_EVENTHANDLER_H
17 #define AAFWK_APPEXECFWK_NATIVE_IMPLEMENT_EVENTHANDLER_H
18 
19 #include "event_runner.h"
20 
21 using OHOS::ErrCode;
22 using OHOS::AppExecFwk::EventHandler;
23 using OHOS::AppExecFwk::EventRunner;
24 
25 typedef void (*FileFDCallback)(int32_t filedescriptor);
26 
27 struct FileDescriptorCallbacks;
28 struct EventRunnerNativeImplement {
29 public:
30     explicit EventRunnerNativeImplement(bool current);
31     ~EventRunnerNativeImplement();
32 
33     /**
34      * Get current thread 'EventRunnerNativeImplement'.
35      *
36      * @return Returns pointer of the new 'EventRunnerNativeImplement'.
37      */
38     static const EventRunnerNativeImplement *GetEventRunnerNativeObj();
39 
40     /**
41      * Create new 'EventRunnerNativeImplement'.
42      *
43      * @return Returns pointer of the new 'EventRunnerNativeImplement'.
44      */
45     static const EventRunnerNativeImplement *CreateEventRunnerNativeObj();
46 
47     /**
48      * Start to run the 'EventRunnerNativeImplement'. Only running on single thread.
49      *
50      * @return Returns 'ERR_OK' on success.
51      */
52     ErrCode RunEventRunnerNativeObj() const;
53 
54     /**
55      * Stop to run the 'EventRunnerNativeImplement'.
56      *
57      * @return Returns 'ERR_OK' on success.
58      */
59     ErrCode StopEventRunnerNativeObj() const;
60 
61     /**
62      * Add file descriptor listener for a file descriptor.
63      *
64      * @param fileDescriptor File descriptor.
65      * @param events Events from file descriptor, such as input, output, error
66      * @param onReadableCallback Called while file descriptor is readable.
67      * @param onWritableCallback Called while file descriptor is writable.
68      * @param onShutdownCallback Called while shutting down this file descriptor.
69      * @param onExceptionCallback Called while error happened on this file descriptor.
70      * @return Return 'ERR_OK' on success.
71      */
72     ErrCode AddFileDescriptorListener(
73         int32_t fileDescriptor, uint32_t events, const FileDescriptorCallbacks *fdCallbacks) const;
74 
75     /**
76      * Remove file descriptor listener for a file descriptor.
77      *
78      * @param fileDescriptor File descriptor.
79      */
80     void RemoveFileDescriptorListener(int32_t fileDescriptor) const;
81 
82 private:
83     std::shared_ptr<EventRunner> eventRunner_ = nullptr;
84 };
85 
86 #endif  // AAFWK_APPEXECFWK_NATIVE_IMPLEMENT_EVENTHANDLER_H
87