1 /*
2 * Copyright (c) 2023 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_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 {
OHOSJsEnvironmentImpl()27 OHOSJsEnvironmentImpl::OHOSJsEnvironmentImpl()
28 {
29 HILOG_DEBUG("called");
30 }
31
OHOSJsEnvironmentImpl(const std::shared_ptr<AppExecFwk::EventRunner> & eventRunner)32 OHOSJsEnvironmentImpl::OHOSJsEnvironmentImpl(const std::shared_ptr<AppExecFwk::EventRunner>& eventRunner)
33 {
34 HILOG_INFO("called");
35 if (eventRunner != nullptr) {
36 HILOG_DEBUG("Create event handler.");
37 eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(eventRunner);
38 }
39 }
40
~OHOSJsEnvironmentImpl()41 OHOSJsEnvironmentImpl::~OHOSJsEnvironmentImpl()
42 {
43 HILOG_DEBUG("called");
44 }
45
PostTask(const std::function<void ()> & task,const std::string & name,int64_t delayTime)46 void OHOSJsEnvironmentImpl::PostTask(const std::function<void()>& task, const std::string& name, int64_t delayTime)
47 {
48 HILOG_DEBUG("called");
49 if (eventHandler_ != nullptr) {
50 eventHandler_->PostTask(task, name, delayTime);
51 }
52 }
53
PostSyncTask(const std::function<void ()> & task,const std::string & name)54 void OHOSJsEnvironmentImpl::PostSyncTask(const std::function<void()>& task, const std::string& name)
55 {
56 HILOG_DEBUG("Post sync task");
57 if (eventHandler_ != nullptr) {
58 eventHandler_->PostSyncTask(task, name);
59 }
60 }
61
RemoveTask(const std::string & name)62 void OHOSJsEnvironmentImpl::RemoveTask(const std::string& name)
63 {
64 HILOG_DEBUG("called");
65 if (eventHandler_ != nullptr) {
66 eventHandler_->RemoveTask(name);
67 }
68 }
69
InitTimerModule(NativeEngine * engine)70 void OHOSJsEnvironmentImpl::InitTimerModule(NativeEngine* engine)
71 {
72 HILOG_DEBUG("Init timer.");
73 CHECK_POINTER(engine);
74 auto ret = JsSysModule::Timer::RegisterTime(reinterpret_cast<napi_env>(engine));
75 if (!ret) {
76 HILOG_ERROR("Register timer failed");
77 }
78 }
79
InitConsoleModule(NativeEngine * engine)80 void OHOSJsEnvironmentImpl::InitConsoleModule(NativeEngine* engine)
81 {
82 HILOG_DEBUG("called");
83 JsSysModule::Console::InitConsoleModule(reinterpret_cast<napi_env>(engine));
84 }
85
InitLoop(NativeEngine * engine)86 bool OHOSJsEnvironmentImpl::InitLoop(NativeEngine* engine)
87 {
88 HILOG_DEBUG("called");
89 auto uvLoop = engine->GetUVLoop();
90 auto fd = uvLoop != nullptr ? uv_backend_fd(uvLoop) : -1;
91 if (fd < 0) {
92 HILOG_ERROR("Failed to get backend fd from uv loop");
93 return false;
94 }
95 uv_run(uvLoop, UV_RUN_NOWAIT);
96
97 if (eventHandler_ != nullptr) {
98 uint32_t events = AppExecFwk::FILE_DESCRIPTOR_INPUT_EVENT | AppExecFwk::FILE_DESCRIPTOR_OUTPUT_EVENT;
99 eventHandler_->AddFileDescriptorListener(fd, events, std::make_shared<OHOSLoopHandler>(uvLoop));
100 }
101
102 return true;
103 }
104
DeInitLoop(NativeEngine * engine)105 void OHOSJsEnvironmentImpl::DeInitLoop(NativeEngine* engine)
106 {
107 auto uvLoop = engine->GetUVLoop();
108 auto fd = uvLoop != nullptr ? uv_backend_fd(uvLoop) : -1;
109 if (fd >= 0 && eventHandler_ != nullptr) {
110 eventHandler_->RemoveFileDescriptorListener(fd);
111 }
112 RemoveTask(TIMER_TASK);
113 }
114
InitWorkerModule(NativeEngine & engine,std::shared_ptr<JsEnv::WorkerInfo> workerInfo)115 void OHOSJsEnvironmentImpl::InitWorkerModule(NativeEngine& engine, std::shared_ptr<JsEnv::WorkerInfo> workerInfo)
116 {
117 HILOG_DEBUG("called");
118 engine.SetInitWorkerFunc(InitWorkerFunc);
119 engine.SetOffWorkerFunc(OffWorkerFunc);
120 engine.SetGetAssetFunc(AssetHelper(workerInfo));
121
122 engine.SetGetContainerScopeIdFunc(GetContainerId);
123 engine.SetInitContainerScopeFunc(UpdateContainerScope);
124 engine.SetFinishContainerScopeFunc(RestoreContainerScope);
125 }
126
InitSyscapModule()127 void OHOSJsEnvironmentImpl::InitSyscapModule()
128 {
129 HILOG_DEBUG("called");
130 }
131 } // namespace AbilityRuntime
132 } // namespace OHOS
133