• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "native_implement_eventhandler.h"
17 #include "event_runner.h"
18 #include "file_descriptor_listener.h"
19 
20 struct FileDescriptorCallbacks {
21     const FileFDCallback readableCallback_;
22     const FileFDCallback writableCallback_;
23     const FileFDCallback shutdownCallback_;
24     const FileFDCallback exceptionCallback_;
25 };
26 
27 class NativeFileDescriptorListener : public OHOS::AppExecFwk::FileDescriptorListener {
28 public:
NativeFileDescriptorListener(const struct FileDescriptorCallbacks * fileDescriptorCallbacks)29     explicit NativeFileDescriptorListener(const struct FileDescriptorCallbacks *fileDescriptorCallbacks)
30     {
31         if (fileDescriptorCallbacks->readableCallback_ != nullptr) {
32             onReadableCallback_ = fileDescriptorCallbacks->readableCallback_;
33         }
34         if (fileDescriptorCallbacks->writableCallback_ != nullptr) {
35             onWritableCallback_ = fileDescriptorCallbacks->writableCallback_;
36         }
37         if (fileDescriptorCallbacks->shutdownCallback_ != nullptr) {
38             onShutdownCallback_ = fileDescriptorCallbacks->shutdownCallback_;
39         }
40         if (fileDescriptorCallbacks->exceptionCallback_ != nullptr) {
41             onExceptionCallback_ = fileDescriptorCallbacks->exceptionCallback_;
42         }
43     }
44 
~NativeFileDescriptorListener()45     ~NativeFileDescriptorListener()
46     {}
47 
48     /**
49      * Called while file descriptor is readable.
50      *
51      * @param fileDescriptor File descriptor which is readable.
52      */
OnReadable(int32_t filedescriptor)53     void OnReadable(int32_t filedescriptor)
54     {
55         if (onReadableCallback_ != nullptr) {
56             onReadableCallback_(filedescriptor);
57         }
58     }
59 
60     /**
61      * Called while file descriptor is writable.
62      *
63      * @param fileDescriptor File descriptor which is writable.
64      */
OnWritable(int32_t filedescriptor)65     void OnWritable(int32_t filedescriptor)
66     {
67         if (onWritableCallback_ != nullptr) {
68             onWritableCallback_(filedescriptor);
69         }
70     }
71 
72     /**
73      * Called while shutting down this file descriptor.
74      *
75      * @param fileDescriptor File descriptor which is shutting down.
76      */
OnShutdown(int32_t filedescriptor)77     void OnShutdown(int32_t filedescriptor)
78     {
79         if (onShutdownCallback_ != nullptr) {
80             onShutdownCallback_(filedescriptor);
81         }
82     }
83 
84     /**
85      * Called while error happened on this file descriptor.
86      *
87      * @param fileDescriptor Error happened on this file descriptor.
88      */
OnException(int32_t filedescriptor)89     void OnException(int32_t filedescriptor)
90     {
91         if (onExceptionCallback_ != nullptr) {
92             onExceptionCallback_(filedescriptor);
93         }
94     }
95 
96     NativeFileDescriptorListener(const NativeFileDescriptorListener &) = delete;
97     NativeFileDescriptorListener &operator=(const NativeFileDescriptorListener &) = delete;
98     NativeFileDescriptorListener(NativeFileDescriptorListener &&) = delete;
99     NativeFileDescriptorListener &operator=(NativeFileDescriptorListener &&) = delete;
100 
101 private:
102     FileFDCallback onReadableCallback_ = nullptr;
103     FileFDCallback onWritableCallback_ = nullptr;
104     FileFDCallback onShutdownCallback_ = nullptr;
105     FileFDCallback onExceptionCallback_ = nullptr;
106 };
107 
EventRunnerNativeImplement(bool current)108 EventRunnerNativeImplement::EventRunnerNativeImplement(bool current)
109 {
110     if (current) {
111         eventRunner_ = EventRunner::Current();
112     } else {
113         eventRunner_ = EventRunner::Create(false);
114     }
115 }
116 
~EventRunnerNativeImplement()117 EventRunnerNativeImplement::~EventRunnerNativeImplement()
118 {
119     eventRunner_ = nullptr;
120 }
121 
GetEventRunnerNativeObj()122 const EventRunnerNativeImplement *EventRunnerNativeImplement::GetEventRunnerNativeObj()
123 {
124     return new EventRunnerNativeImplement(true);
125 }
126 
CreateEventRunnerNativeObj()127 const EventRunnerNativeImplement *EventRunnerNativeImplement::CreateEventRunnerNativeObj()
128 {
129     return new EventRunnerNativeImplement(false);
130 }
131 
RunEventRunnerNativeObj() const132 ErrCode EventRunnerNativeImplement::RunEventRunnerNativeObj() const
133 {
134     if (eventRunner_ != nullptr) {
135         return eventRunner_->Run();
136     }
137     return OHOS::AppExecFwk::EVENT_HANDLER_ERR_NO_EVENT_RUNNER;
138 }
139 
StopEventRunnerNativeObj() const140 ErrCode EventRunnerNativeImplement::StopEventRunnerNativeObj() const
141 {
142     if (eventRunner_ != nullptr) {
143         return eventRunner_->Stop();
144     }
145     return OHOS::AppExecFwk::EVENT_HANDLER_ERR_NO_EVENT_RUNNER;
146 }
147 
AddFileDescriptorListener(int32_t fileDescriptor,uint32_t events,const FileDescriptorCallbacks * fdCallbacks) const148 ErrCode EventRunnerNativeImplement::AddFileDescriptorListener(
149     int32_t fileDescriptor, uint32_t events, const FileDescriptorCallbacks *fdCallbacks) const
150 {
151     auto nativeFileDescriptorListener = std::make_shared<NativeFileDescriptorListener>(fdCallbacks);
152     return eventRunner_->GetEventQueue()->AddFileDescriptorListener(
153         fileDescriptor, events, nativeFileDescriptorListener);
154 }
155 
RemoveFileDescriptorListener(int32_t fileDescriptor) const156 void EventRunnerNativeImplement::RemoveFileDescriptorListener(int32_t fileDescriptor) const
157 {
158     eventRunner_->GetEventQueue()->RemoveFileDescriptorListener(fileDescriptor);
159 }