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