1 /*
2  * Copyright (c) 2020 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 OHOS_ACELITE_JS_ASYNC_WORK_H
17 #define OHOS_ACELITE_JS_ASYNC_WORK_H
18 
19 #include "message_queue_utils.h"
20 #include "memory_heap.h"
21 #if (defined(__LINUX__) || defined(__LITEOS_A__))
22 #include <functional>
23 #endif
24 
25 namespace OHOS {
26 namespace ACELite {
27 /**
28  * Function pointer type used for async work.
29  * Note: this type is deprecated, use AsyncHandler alternatively
30  */
31 typedef void (*AsyncWorkHandler)(void* data);
32 
33 /**
34  * Function pointer type used for async work.
35  */
36 typedef void (*AsyncHandler)(void* data, int8_t statusCode);
37 
38 typedef bool (*FatalHandleFunc)();
39 
40 #if (defined(__LINUX__) || defined(__LITEOS_A__))
41 typedef bool (*PostUITaskFunc)(std::function<void()> task);
42 #endif
43 
44 struct AsyncWork : public MemoryHeap {
45     AsyncWorkHandler workHandler;
46     AsyncHandler handler;
47     void* data;
AsyncWorkAsyncWork48     AsyncWork() : workHandler(nullptr), handler(nullptr), data(nullptr) {}
49     AsyncWork(const AsyncWork &) = delete;
50     AsyncWork &operator=(const AsyncWork &) = delete;
51     AsyncWork(AsyncWork &&) = delete;
52     AsyncWork &operator=(AsyncWork &&) = delete;
53 };
54 
55 /**
56  * @class JsASyncWork
57  *
58  * @brief JsAsyncWork is used to send asynchronous tasks to the system for execution
59  *
60  * @see MemoryHeap
61  */
62 class JsAsyncWork final : public MemoryHeap {
63 public:
64     JsAsyncWork(const JsAsyncWork &) = delete;
65     JsAsyncWork &operator=(const JsAsyncWork &) = delete;
66     JsAsyncWork(JsAsyncWork &&) = delete;
67     JsAsyncWork &operator=(JsAsyncWork &&) = delete;
~JsAsyncWork()68     ~JsAsyncWork() {}
69 
70     /**
71      * @brief Set the queue handler of the app main queue
72      *
73      * @param [in] handler: QueueHandler object to set
74      */
75     static void SetAppQueueHandler(const QueueHandler handler);
76 
77     /**
78      * @brief Create an async work from given parameters, and dispatch it to main app task handler
79      *
80      * @param [in] workHandler: the entry handler of the async work
81      * @param [in] data: pointer to the data the async work needed
82      * @return true: operation succeed
83      *         false: operation failed
84      * Note: this function is deprecated, use the other one alternatively
85      */
86     static bool DispatchAsyncWork(AsyncWorkHandler workHandler, void *data);
87 
88     /**
89      * @brief Create an async work from given parameters, and dispatch it to main app task handler
90      *
91      * @param[in] handler: the entry handler of the async work
92      * @param[in] data: pointer to the data the async work needed
93      * @return true: operation succeed
94      *         false: operation failed
95      */
96     static bool DispatchAsyncWork(AsyncHandler handler, void *data);
97 
98     /**
99      * @brief Common new message dispatching method.
100      *
101      * @param [in] msgId: the new message ID
102      * @param [in] data: pointer to the data
103      * @return true: operation succeed
104      *         false: operation failed
105      */
106     static bool DispatchToLoop(AbilityMsgId msgId, void *data);
107 
108     /**
109      * @brief Execute the given async work
110      *
111      * @param [in] asyncWork: reference to the async work to be executed
112      */
113     static void ExecuteAsyncWork(AsyncWork *&asyncWork, int8_t statusCode = ERR_OK);
114 
115     static void SetFatalHandleFunc(FatalHandleFunc isFatalErrorHitted, FatalHandleFunc isAppExiting);
116 
117     static void SetEnvStatus(bool envInitialized);
118 #if (defined(__LINUX__) || defined(__LITEOS_A__))
119     static void SetPostUITaskFunc(PostUITaskFunc postUITask);
120 #endif
121 
122     // Error code used for async work processing
123     static const int8_t ERR_OK = 0;
124     static const int8_t ERR_FAIL = -1;
125 
126 private:
127     static bool DispatchAsyncWorkInner(AsyncWorkHandler workHandler, AsyncHandler handler, void *data);
128     static QueueHandler appQueuehandler_;
129     static FatalHandleFunc isFatalErrorHitted_;
130     static FatalHandleFunc isAppExiting_;
131 #if (defined(__LINUX__) || defined(__LITEOS_A__))
132     static PostUITaskFunc postUITask_;
133 #endif
134     static bool isEnvInitialized_;
135 };
136 } // namespace ACELite
137 } // namespace OHOS
138 #endif // OHOS_ACELITE_JS_ASYNC_WORK_H
139