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 "bridge/declarative_frontend/jsview/js_dynamic_component.h"
17
18 #include <cstdint>
19 #include <functional>
20 #include <string>
21
22 #include "worker.h"
23 #include "jsnapi.h"
24 #include "native_engine.h"
25
26 #include "base/log/ace_scoring_log.h"
27 #include "base/log/log_wrapper.h"
28 #include "base/memory/ace_type.h"
29 #include "base/thread/task_executor.h"
30 #include "base/utils/utils.h"
31 #include "bridge/common/utils/engine_helper.h"
32 #include "bridge/declarative_frontend/engine/js_converter.h"
33 #include "bridge/declarative_frontend/engine/js_ref_ptr.h"
34 #include "bridge/declarative_frontend/engine/js_types.h"
35 #include "bridge/declarative_frontend/engine/jsi/jsi_ref.h"
36 #include "bridge/declarative_frontend/engine/jsi/jsi_types.h"
37 #include "bridge/declarative_frontend/jsview/js_utils.h"
38 #include "bridge/js_frontend/engine/jsi/js_value.h"
39 #include "core/components_ng/base/view_abstract_model.h"
40 #include "core/components_ng/pattern/ui_extension/dynamic_component/dynamic_model.h"
41 #include "core/components_ng/pattern/ui_extension/dynamic_component/dynamic_pattern.h"
42
43 using namespace Commonlibrary::Concurrent::WorkerModule;
44 namespace OHOS::Ace {
GetInstance()45 NG::DynamicModelNG* NG::DynamicModelNG::GetInstance()
46 {
47 if (!Container::IsCurrentUseNewPipeline()) {
48 LOGE("Get DynamicModelNG in non NewPipeline.");
49 }
50 static NG::DynamicModelNG instance;
51 return &instance;
52 }
53 } // namespace OHOS::Ace
54 namespace OHOS::Ace::Framework {
55 const CalcDimension DYNAMIC_COMPONENT_MIN_WIDTH(10.0f, DimensionUnit::VP);
56 const CalcDimension DYNAMIC_COMPONENT_MIN_HEIGHT(10.0f, DimensionUnit::VP);
57
JSBind(BindingTarget globalObj)58 void JSDynamicComponent::JSBind(BindingTarget globalObj)
59 {
60 JSClass<JSDynamicComponent>::Declare("DynamicComponent");
61 MethodOptions opt = MethodOptions::NONE;
62 JSClass<JSDynamicComponent>::StaticMethod("create", &JSDynamicComponent::Create, opt);
63 JSClass<JSDynamicComponent>::StaticMethod("onSizeChanged", &JSDynamicComponent::SetOnSizeChanged, opt);
64 JSClass<JSDynamicComponent>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
65 JSClass<JSDynamicComponent>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
66 JSClass<JSDynamicComponent>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
67 JSClass<JSDynamicComponent>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
68 JSClass<JSDynamicComponent>::StaticMethod("width", &JSDynamicComponent::Width, opt);
69 JSClass<JSDynamicComponent>::StaticMethod("height", &JSDynamicComponent::Height, opt);
70 JSClass<JSDynamicComponent>::StaticMethod("onError", &JSDynamicComponent::JsOnError, opt);
71 JSClass<JSDynamicComponent>::StaticMethod("isReportFrameEvent",
72 &JSDynamicComponent::SetIsReportFrameEvent, opt);
73 JSClass<JSDynamicComponent>::InheritAndBind<JSViewAbstract>(globalObj);
74 }
75
Create(const JSCallbackInfo & info)76 void JSDynamicComponent::Create(const JSCallbackInfo& info)
77 {
78 if (info.Length() < 1 || !info[0]->IsObject()) {
79 TAG_LOGW(AceLogTag::ACE_DYNAMIC_COMPONENT, "DynamicComponent argument is invalid");
80 return;
81 }
82 auto dynamicComponentArg = JSRef<JSObject>::Cast(info[0]);
83 auto hapPathValue = dynamicComponentArg->GetProperty("hapPath");
84 auto abcPathValue = dynamicComponentArg->GetProperty("abcPath");
85 auto entryPointValue = dynamicComponentArg->GetProperty("entryPoint");
86 auto backgroundTransparentValue = dynamicComponentArg->GetProperty("backgroundTransparent");
87 if (!entryPointValue->IsString()) {
88 TAG_LOGW(AceLogTag::ACE_DYNAMIC_COMPONENT, "DynamicComponent argument type is invalid");
89 return;
90 }
91 auto entryPoint = entryPointValue->ToString();
92 bool backgroundTransparent = true;
93 if (backgroundTransparentValue->IsBoolean()) {
94 backgroundTransparent = backgroundTransparentValue->ToBoolean();
95 }
96 NG::UIExtensionConfig config;
97 config.sessionType = NG::SessionType::DYNAMIC_COMPONENT;
98 config.backgroundTransparent = backgroundTransparent;
99 NG::DynamicModelNG::GetInstance()->Create(config);
100 ViewAbstractModel::GetInstance()->SetWidth(DYNAMIC_COMPONENT_MIN_WIDTH);
101 ViewAbstractModel::GetInstance()->SetHeight(DYNAMIC_COMPONENT_MIN_HEIGHT);
102 ViewAbstractModel::GetInstance()->SetMinWidth(DYNAMIC_COMPONENT_MIN_WIDTH);
103 ViewAbstractModel::GetInstance()->SetMinHeight(DYNAMIC_COMPONENT_MIN_HEIGHT);
104 auto frameNode = NG::ViewStackProcessor::GetInstance()->GetMainFrameNode();
105 CHECK_NULL_VOID(frameNode);
106 auto pattern = frameNode->GetPattern<NG::DynamicPattern>();
107 if (pattern && pattern->HasDynamicRenderer()) {
108 TAG_LOGI(AceLogTag::ACE_DYNAMIC_COMPONENT, "dynamic renderer already exists");
109 return;
110 }
111 auto hostEngine = EngineHelper::GetCurrentEngine();
112 CHECK_NULL_VOID(hostEngine);
113 NativeEngine* hostNativeEngine = hostEngine->GetNativeEngine();
114 auto jsWorker = dynamicComponentArg->GetProperty("worker");
115 panda::Local<JsiValue> value = jsWorker.Get().GetLocalHandle();
116 JSValueWrapper valueWrapper = value;
117 napi_value nativeValue = hostNativeEngine->ValueToNapiValue(valueWrapper);
118 Worker* worker = nullptr;
119 napi_unwrap(reinterpret_cast<napi_env>(hostNativeEngine), nativeValue, reinterpret_cast<void**>(&worker));
120 if (worker == nullptr || entryPoint.empty()) {
121 TAG_LOGE(AceLogTag::ACE_DYNAMIC_COMPONENT, "worker is nullptr or entryPoint is empty");
122 NG::DynamicModelNG::GetInstance()->InitializeDynamicComponent(
123 AceType::Claim(frameNode), "", "", entryPoint, nullptr);
124 return;
125 }
126
127 TAG_LOGI(AceLogTag::ACE_DYNAMIC_COMPONENT, "worker running=%{public}d, worker name=%{public}s",
128 worker->IsRunning(), worker->GetName().c_str());
129 auto instanceId = Container::CurrentId();
130 worker->RegisterCallbackForWorkerEnv([instanceId,
131 weak = AceType::WeakClaim(frameNode), entryPoint](napi_env env) {
132 ContainerScope scope(instanceId);
133 auto container = Container::Current();
134 container->GetTaskExecutor()->PostTask(
135 [weak, entryPoint, env]() {
136 auto frameNode = weak.Upgrade();
137 CHECK_NULL_VOID(frameNode);
138 NG::DynamicModelNG::GetInstance()->InitializeDynamicComponent(
139 frameNode, "", "", entryPoint, env);
140 },
141 TaskExecutor::TaskType::UI, "ArkUIDynamicComponentInitialize");
142 });
143 }
144
SetIsReportFrameEvent(const JSCallbackInfo & info)145 void JSDynamicComponent::SetIsReportFrameEvent(const JSCallbackInfo& info)
146 {
147 bool isReportFrameEvent = false;
148 if (info[0]->IsBoolean()) {
149 isReportFrameEvent = info[0]->ToBoolean();
150 }
151
152 NG::DynamicModelNG::GetInstance()->SetIsReportFrameEvent(isReportFrameEvent);
153 }
154
JsOnError(const JSCallbackInfo & info)155 void JSDynamicComponent::JsOnError(const JSCallbackInfo& info)
156 {
157 if (info.Length() < 1 || !info[0]->IsFunction()) {
158 TAG_LOGW(AceLogTag::ACE_ISOLATED_COMPONENT, "onError argument is invalid");
159 return;
160 }
161
162 WeakPtr<NG::FrameNode> frameNode =
163 AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
164 auto execCtx = info.GetExecutionContext();
165 auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
166 auto instanceId = Container::CurrentId();
167 auto onError = [execCtx, func = std::move(jsFunc), instanceId, node = frameNode]
168 (int32_t code, const std::string& name, const std::string& message) {
169 ContainerScope scope(instanceId);
170 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
171 ACE_SCORING_EVENT("DynamicComponent.onError");
172 auto pipelineContext = PipelineContext::GetCurrentContext();
173 CHECK_NULL_VOID(pipelineContext);
174 pipelineContext->UpdateCurrentActiveNode(node);
175 JSRef<JSObject> obj = JSRef<JSObject>::New();
176 obj->SetProperty<int32_t>("code", code);
177 obj->SetProperty<std::string>("name", name);
178 obj->SetProperty<std::string>("message", message);
179 auto returnValue = JSRef<JSVal>::Cast(obj);
180 func->ExecuteJS(1, &returnValue);
181 };
182 NG::DynamicModelNG::GetInstance()->SetPlatformOnError(std::move(onError));
183 }
184
SetOnSizeChanged(const JSCallbackInfo & info)185 void JSDynamicComponent::SetOnSizeChanged(const JSCallbackInfo& info)
186 {
187 }
188
Width(const JSCallbackInfo & info)189 void JSDynamicComponent::Width(const JSCallbackInfo& info)
190 {
191 if (info[0]->IsUndefined()) {
192 return;
193 }
194
195 CalcDimension value;
196 if (JSViewAbstract::ParseJsDimensionVpNG(info[0], value)) {
197 ViewAbstractModel::GetInstance()->SetWidth(value);
198 }
199 }
200
Height(const JSCallbackInfo & info)201 void JSDynamicComponent::Height(const JSCallbackInfo& info)
202 {
203 if (info[0]->IsUndefined()) {
204 return;
205 }
206
207 CalcDimension value;
208 if (JSViewAbstract::ParseJsDimensionVpNG(info[0], value)) {
209 ViewAbstractModel::GetInstance()->SetHeight(value);
210 }
211 }
212 } // namespace OHOS::Ace::Framework
213