• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DEBUG("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     CHECK_POINTER_AND_RETURN(engine, false);
90     auto uvLoop = engine->GetUVLoop();
91     auto fd = uvLoop != nullptr ? uv_backend_fd(uvLoop) : -1;
92     if (fd < 0) {
93         HILOG_ERROR("Failed to get backend fd from uv loop");
94         return false;
95     }
96     uv_run(uvLoop, UV_RUN_NOWAIT);
97 
98     if (eventHandler_ != nullptr) {
99         uint32_t events = AppExecFwk::FILE_DESCRIPTOR_INPUT_EVENT | AppExecFwk::FILE_DESCRIPTOR_OUTPUT_EVENT;
100         eventHandler_->AddFileDescriptorListener(fd, events, std::make_shared<OHOSLoopHandler>(uvLoop), "uvLoopTask");
101     }
102 
103     return true;
104 }
105 
DeInitLoop(NativeEngine * engine)106 void OHOSJsEnvironmentImpl::DeInitLoop(NativeEngine* engine)
107 {
108     CHECK_POINTER(engine);
109     auto uvLoop = engine->GetUVLoop();
110     auto fd = uvLoop != nullptr ? uv_backend_fd(uvLoop) : -1;
111     if (fd >= 0 && eventHandler_ != nullptr) {
112         eventHandler_->RemoveFileDescriptorListener(fd);
113     }
114     RemoveTask(TIMER_TASK);
115 }
116 
InitWorkerModule(NativeEngine * engine,std::shared_ptr<JsEnv::WorkerInfo> workerInfo)117 void OHOSJsEnvironmentImpl::InitWorkerModule(NativeEngine* engine, std::shared_ptr<JsEnv::WorkerInfo> workerInfo)
118 {
119     HILOG_DEBUG("called");
120     CHECK_POINTER(engine);
121     engine->SetInitWorkerFunc(InitWorkerFunc);
122     engine->SetOffWorkerFunc(OffWorkerFunc);
123     engine->SetGetAssetFunc(AssetHelper(workerInfo));
124 
125     engine->SetGetContainerScopeIdFunc(GetContainerId);
126     engine->SetInitContainerScopeFunc(UpdateContainerScope);
127     engine->SetFinishContainerScopeFunc(RestoreContainerScope);
128 }
129 
InitSyscapModule()130 void OHOSJsEnvironmentImpl::InitSyscapModule()
131 {
132     HILOG_DEBUG("called");
133 }
134 } // namespace AbilityRuntime
135 } // namespace OHOS
136