1 /*
2 * Copyright (c) 2023-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 "ohos_js_environment_impl.h"
17
18 #include "console.h"
19 #include "hilog_tag_wrapper.h"
20 #include "js_utils.h"
21 #include "js_worker.h"
22 #include "ohos_loop_handler.h"
23 #include "sys_timer.h"
24
25 namespace OHOS {
26 namespace AbilityRuntime {
27 namespace {
28 std::shared_ptr<AppExecFwk::EventHandler> g_eventHandler = nullptr;
29 }
PostTaskToHandler(void * handler,uv_io_cb func,void * work,int status,int priority)30 void OHOSJsEnvironmentImpl::PostTaskToHandler(void* handler, uv_io_cb func, void* work, int status, int priority)
31 {
32 TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
33 if (!func || !work) {
34 TAG_LOGE(AAFwkTag::JSRUNTIME, "Invalid parameters");
35 return;
36 }
37
38 auto task = [func, work, status]() {
39 TAG_LOGD(AAFwkTag::JSRUNTIME, "Do uv work");
40 func(work, status);
41 TAG_LOGD(AAFwkTag::JSRUNTIME, "Do uv work end");
42 };
43
44 AppExecFwk::EventQueue::Priority prio = AppExecFwk::EventQueue::Priority::IMMEDIATE;
45 switch (priority) {
46 case uv_qos_t::uv_qos_user_interactive:
47 prio = AppExecFwk::EventQueue::Priority::VIP;
48 break;
49 case uv_qos_t::uv_qos_user_initiated:
50 prio = AppExecFwk::EventQueue::Priority::IMMEDIATE;
51 break;
52 case uv_qos_t::uv_qos_utility:
53 prio = AppExecFwk::EventQueue::Priority::LOW;
54 break;
55 case uv_qos_t::uv_qos_background:
56 prio = AppExecFwk::EventQueue::Priority::IDLE;
57 break;
58 default:
59 prio = AppExecFwk::EventQueue::Priority::HIGH;
60 break;
61 }
62
63 if (g_eventHandler == nullptr) {
64 TAG_LOGE(AAFwkTag::JSRUNTIME, "Invalid parameters");
65 return;
66 }
67 g_eventHandler->PostTask(task, "uv_io_cb", 0, prio);
68 }
OHOSJsEnvironmentImpl()69 OHOSJsEnvironmentImpl::OHOSJsEnvironmentImpl()
70 {
71 TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
72 }
73
OHOSJsEnvironmentImpl(const std::shared_ptr<AppExecFwk::EventRunner> & eventRunner)74 OHOSJsEnvironmentImpl::OHOSJsEnvironmentImpl(const std::shared_ptr<AppExecFwk::EventRunner>& eventRunner)
75 {
76 TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
77 if (eventRunner != nullptr) {
78 TAG_LOGD(AAFwkTag::JSRUNTIME, "Create event handler");
79 eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(eventRunner);
80 if (eventRunner.get() == AppExecFwk::EventRunner::GetMainEventRunner().get()) {
81 g_eventHandler = std::make_shared<AppExecFwk::EventHandler>(eventRunner);
82 }
83 }
84 }
85
~OHOSJsEnvironmentImpl()86 OHOSJsEnvironmentImpl::~OHOSJsEnvironmentImpl()
87 {
88 TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
89 }
90
PostTask(const std::function<void ()> & task,const std::string & name,int64_t delayTime)91 void OHOSJsEnvironmentImpl::PostTask(const std::function<void()>& task, const std::string& name, int64_t delayTime)
92 {
93 TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
94 if (eventHandler_ != nullptr) {
95 eventHandler_->PostTask(task, name, delayTime);
96 }
97 }
98
PostSyncTask(const std::function<void ()> & task,const std::string & name)99 void OHOSJsEnvironmentImpl::PostSyncTask(const std::function<void()>& task, const std::string& name)
100 {
101 TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
102 if (eventHandler_ != nullptr) {
103 eventHandler_->PostSyncTask(task, name);
104 }
105 }
106
RemoveTask(const std::string & name)107 void OHOSJsEnvironmentImpl::RemoveTask(const std::string& name)
108 {
109 TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
110 if (eventHandler_ != nullptr) {
111 eventHandler_->RemoveTask(name);
112 }
113 }
114
InitTimerModule(NativeEngine * engine)115 void OHOSJsEnvironmentImpl::InitTimerModule(NativeEngine* engine)
116 {
117 TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
118 CHECK_POINTER(engine);
119 auto ret = JsSysModule::Timer::RegisterTime(reinterpret_cast<napi_env>(engine));
120 if (!ret) {
121 TAG_LOGE(AAFwkTag::JSRUNTIME, "Register timer failed");
122 }
123 }
124
InitConsoleModule(NativeEngine * engine)125 void OHOSJsEnvironmentImpl::InitConsoleModule(NativeEngine* engine)
126 {
127 TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
128 JsSysModule::Console::InitConsoleModule(reinterpret_cast<napi_env>(engine));
129 }
130
InitLoop(NativeEngine * engine,bool isStage)131 bool OHOSJsEnvironmentImpl::InitLoop(NativeEngine* engine, bool isStage)
132 {
133 TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
134 CHECK_POINTER_AND_RETURN(engine, false);
135 auto uvLoop = engine->GetUVLoop();
136 auto fd = uvLoop != nullptr ? uv_backend_fd(uvLoop) : -1;
137 if (fd < 0) {
138 TAG_LOGE(AAFwkTag::JSRUNTIME, "get fd failed");
139 return false;
140 }
141 uv_run(uvLoop, UV_RUN_NOWAIT);
142
143 if (eventHandler_ != nullptr) {
144 uint32_t events = AppExecFwk::FILE_DESCRIPTOR_INPUT_EVENT | AppExecFwk::FILE_DESCRIPTOR_OUTPUT_EVENT;
145 eventHandler_->AddFileDescriptorListener(fd, events, std::make_shared<OHOSLoopHandler>(uvLoop), "uvLoopTask");
146 TAG_LOGD(AAFwkTag::JSRUNTIME, "uv_register_task_to_event, isStage: %{public}d", isStage);
147 if (isStage && (eventHandler_->GetEventRunner()).get() == AppExecFwk::EventRunner::GetMainEventRunner().get()) {
148 uv_register_task_to_event(uvLoop, PostTaskToHandler, nullptr);
149 // send signal here to trigger uv tasks generated during initialization.
150 uv_async_send(&uvLoop->wq_async);
151 }
152 }
153
154 return true;
155 }
156
DeInitLoop(NativeEngine * engine)157 void OHOSJsEnvironmentImpl::DeInitLoop(NativeEngine* engine)
158 {
159 CHECK_POINTER(engine);
160 auto uvLoop = engine->GetUVLoop();
161 auto fd = uvLoop != nullptr ? uv_backend_fd(uvLoop) : -1;
162 if (fd >= 0 && eventHandler_ != nullptr) {
163 eventHandler_->RemoveFileDescriptorListener(fd);
164 }
165 uv_unregister_task_to_event(uvLoop);
166 RemoveTask(TIMER_TASK);
167 }
168
InitWorkerModule(NativeEngine * engine,std::shared_ptr<JsEnv::WorkerInfo> workerInfo)169 void OHOSJsEnvironmentImpl::InitWorkerModule(NativeEngine* engine, std::shared_ptr<JsEnv::WorkerInfo> workerInfo)
170 {
171 TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
172 CHECK_POINTER(engine);
173 CHECK_POINTER(workerInfo);
174 engine->SetInitWorkerFunc(InitWorkerFunc);
175 engine->SetOffWorkerFunc(OffWorkerFunc);
176 engine->SetGetAssetFunc(AssetHelper(workerInfo));
177 engine->SetApiVersion(static_cast<int32_t>(workerInfo->apiTargetVersion.GetOriginPointer()));
178
179 engine->SetGetContainerScopeIdFunc(GetContainerId);
180 engine->SetInitContainerScopeFunc(UpdateContainerScope);
181 engine->SetFinishContainerScopeFunc(RestoreContainerScope);
182 }
183
InitSyscapModule()184 void OHOSJsEnvironmentImpl::InitSyscapModule()
185 {
186 TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
187 }
188 } // namespace AbilityRuntime
189 } // namespace OHOS
190