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