• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "ark_interop_internal.h"
17 #include "ark_interop_napi.h"
18 #include "ark_interop_log.h"
19 
20 #include <unordered_map>
21 #include <mutex>
22 
23 using namespace panda::ecmascript;
24 
25 #ifdef __OHOS__
26 using namespace OHOS::AppExecFwk;
27 
28 namespace {
29 std::unordered_map<ARKTS_Env, ARKTS_Loop> g_eventHandlers_;
30 std::mutex g_eventHandlerMutex_;
31 
32 struct ARKTS_Loop_ {
33     enum Status {
34         IDLE,
35         REQUESTING,
36         STOPPING,
37         STOPPED,
38     };
39     uv_async_t asyncReq;
40     Status status;
41     std::vector<std::function<void ()>> callbacks;
42     std::mutex mutex;
43     std::condition_variable cv;
44     bool hasCleanupHook;
45 
46     explicit ARKTS_Loop_(napi_env env, uv_loop_t* loop);
47     ~ARKTS_Loop_() = default;
48 
49     void PostTask(std::function<void ()> task);
50     // only to be call by uv_queue_task
51     void DrainTasks();
52 };
53 
54 std::unordered_map<ARKTS_Env, ARKTS_Loop_*> g_uvLoops_;
55 std::mutex g_uvLoopMutex_;
56 }
57 #endif
58 
ARKTS_GetOrCreateEventHandler(ARKTS_Env env)59 ARKTS_Loop ARKTS_GetOrCreateEventHandler(ARKTS_Env env)
60 {
61     if (!env) {
62         return nullptr;
63     }
64 #ifdef __OHOS__
65     std::lock_guard lock(g_eventHandlerMutex_);
66     auto searchRet = g_eventHandlers_.find(env);
67     if (searchRet != g_eventHandlers_.end()) {
68         return searchRet->second;
69     }
70     auto handler = std::make_shared<EventHandler>(EventRunner::Current());
71     g_eventHandlers_[env] = handler;
72     return handler;
73 #else
74     return nullptr;
75 #endif
76 }
77 
ARKTSInner_GetUvLoop(ARKTS_Env env)78 static ARKTS_Loop_* ARKTSInner_GetUvLoop(ARKTS_Env env)
79 {
80     std::lock_guard lock(g_uvLoopMutex_);
81     auto searchRet = g_uvLoops_.find(env);
82     if (searchRet != g_uvLoops_.end()) {
83         return searchRet->second;
84     } else {
85         return nullptr;
86     }
87 }
88 
89 #ifdef __OHOS__
ARKTS_GetEventHandler(ARKTS_Env env)90 static ARKTS_Loop ARKTS_GetEventHandler(ARKTS_Env env)
91 {
92     if (!env) {
93         return nullptr;
94     }
95     std::lock_guard lock(g_eventHandlerMutex_);
96     auto searchRet = g_eventHandlers_.find(env);
97     if (searchRet != g_eventHandlers_.end()) {
98         return searchRet->second;
99     }
100     return nullptr;
101 }
102 #endif
103 
104 // @deprecated
ARKTS_InitEventHandle(ARKTS_Env env)105 void ARKTS_InitEventHandle(ARKTS_Env env)
106 {
107     ARKTS_ASSERT_V(env, "env is null");
108     void (ARKTS_GetOrCreateEventHandler(env));
109 }
110 
ARKTS_DisposeEventHandler(ARKTS_Env env)111 void ARKTS_DisposeEventHandler(ARKTS_Env env)
112 {
113     if (!env) {
114         return;
115     }
116 #ifdef __OHOS__
117     {
118         std::lock_guard lock(g_eventHandlerMutex_);
119         g_eventHandlers_.erase(env);
120     }
121 #endif
122     {
123         std::lock_guard lock(g_uvLoopMutex_);
124         g_uvLoops_.erase(env);
125     }
126 }
127 
ARKTSInner_CreateAsyncTask(ARKTS_Env env,ARKTS_AsyncCallback callback,void * data)128 void ARKTSInner_CreateAsyncTask(ARKTS_Env env, ARKTS_AsyncCallback callback, void* data)
129 {
130     // uv_loop first, secondary is for compatible with early version.
131     if (auto loop = ARKTSInner_GetUvLoop(env)) {
132         loop->PostTask([env, callback, data] {
133             callback(env, data);
134         });
135         return;
136     }
137 #ifdef __OHOS__
138     auto handler = ARKTS_GetEventHandler(env);
139     if (!handler) {
140         LOGE("event handler not initialized");
141         return;
142     }
143     handler->PostTask([env, callback, data] {
144         callback(env, data);
145     });
146 #endif
147 }
148 
ARKTS_CreateAsyncTask(ARKTS_Env env,int64_t callbackId)149 void ARKTS_CreateAsyncTask(ARKTS_Env env, int64_t callbackId)
150 {
151     ARKTSInner_CreateAsyncTask(env, ARKTSInner_CJAsyncCallback, reinterpret_cast<void*>(callbackId));
152 }
153 
ARKTS_Loop_(napi_env env,uv_loop_t * loop)154 ARKTS_Loop_::ARKTS_Loop_(napi_env env, uv_loop_t* loop) : asyncReq(), status(IDLE)
155 {
156     auto error = uv_async_init(loop, &asyncReq, [](uv_async_t* work) {
157         auto loop = static_cast<ARKTS_Loop_*>(work->data);
158         if (loop) {
159             loop->DrainTasks();
160         }
161     });
162     if (error != 0) {
163         LOGE("uv_async_init failed, status: %{public}d", error);
164         hasCleanupHook = false;
165         return;
166     }
167     asyncReq.data = this;
168     hasCleanupHook = napi_add_cleanup_finalizer(env, [](void* data) {
169         if (!data) {
170             return;
171         }
172         auto loop = reinterpret_cast<ARKTS_Loop_*>(data);
173         delete loop;
174     }, this) == napi_ok;
175 }
176 
PostTask(std::function<void ()> task)177 void ARKTS_Loop_::PostTask(std::function<void()> task)
178 {
179     std::lock_guard lock(mutex);
180     if (status >= STOPPING) {
181         return;
182     }
183     callbacks.push_back(std::move(task));
184     if (status == IDLE) {
185         int tryTimes = 3;
186         while (tryTimes--) {
187             auto ret = uv_async_send(&asyncReq);
188             if (ret == 0) {
189                 status = REQUESTING;
190                 break;
191             }
192         }
193     }
194 }
195 
DrainTasks()196 void ARKTS_Loop_::DrainTasks()
197 {
198     std::vector<std::function<void ()>> pendingTasks;
199     {
200         std::lock_guard lock(mutex);
201         // drop all callbacks
202         if (status >= STOPPING) {
203             if (status == STOPPING) {
204                 status = STOPPED;
205                 cv.notify_all();
206             }
207             return;
208         }
209         // status should be REQUESTING, skipping check.
210         std::swap(callbacks, pendingTasks);
211         status = IDLE;
212     }
213     for (const auto& task : pendingTasks) {
214         task();
215     }
216 }
217 
218 // napi_add_env_cleanup_hook(napi_env, callback, hint) holds a (non-milt) map of all 'callbacks' by keyof 'hint'
219 // if others pass env as 'hint' may cause failure, so new CleanupTask as 'hint' to prevent collision.
220 struct CleanupTask {
221     ARKTS_Env env;
222 };
223 
ARKTSInner_InitLoop(napi_env env,ARKTS_Env vm,uv_loop_t * loop)224 bool ARKTSInner_InitLoop(napi_env env, ARKTS_Env vm, uv_loop_t* loop)
225 {
226     std::lock_guard lock(g_uvLoopMutex_);
227     if (g_uvLoops_.find(vm) != g_uvLoops_.end()) {
228         return true;
229     }
230     auto arktsLoop = new (std::nothrow) ARKTS_Loop_(env, loop);
231     if (!arktsLoop) {
232         LOGE("new ARKTS_Loop_ failed.");
233         return false;
234     }
235     if (!arktsLoop->hasCleanupHook) {
236         LOGE("ARKTSLoop has no cleanup hook.");
237         delete arktsLoop;
238         return false;
239     }
240 
241     auto cleanupTask = new (std::nothrow) CleanupTask {vm};
242     if (!cleanupTask) {
243         LOGE("new CleanupTask failed.");
244         delete arktsLoop;
245         return false;
246     }
247     auto status = napi_add_env_cleanup_hook(env, [](void* data) {
248         if (!data) {
249             return;
250         }
251         auto task = reinterpret_cast<CleanupTask*>(data);
252         ARKTS_DisposeJSContext(task->env);
253         ARKTS_DisposeEventHandler(task->env);
254         ARKTS_RemoveGlobalManager(task->env);
255         delete task;
256     }, cleanupTask);
257     if (status != napi_ok) {
258         LOGE("add env cleanup hook failed: %{public}d", status);
259         delete arktsLoop;
260         delete cleanupTask;
261         return false;
262     }
263     ARKTS_InitGlobalManager(vm);
264     g_uvLoops_[vm] = arktsLoop;
265     return true;
266 }