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 "commonlibrary/ets_utils/js_concurrent_module/worker/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/common/container.h"
40 #include "core/common/container_scope.h"
41 #include "core/components_ng/base/view_abstract_model.h"
42 #include "core/components_ng/pattern/ui_extension/ui_extension_model.h"
43 #include "core/components_ng/pattern/ui_extension/ui_extension_model_ng.h"
44 
45 using namespace Commonlibrary::Concurrent::WorkerModule;
46 
47 namespace OHOS::Ace::Framework {
48 
JSBind(BindingTarget globalObj)49 void JSDynamicComponent::JSBind(BindingTarget globalObj)
50 {
51     JSClass<JSDynamicComponent>::Declare("DynamicComponent");
52     MethodOptions opt = MethodOptions::NONE;
53     JSClass<JSDynamicComponent>::StaticMethod("create", &JSDynamicComponent::Create, opt);
54     JSClass<JSDynamicComponent>::StaticMethod("onSizeChanged", &JSDynamicComponent::SetOnSizeChanged, opt);
55     JSClass<JSDynamicComponent>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
56     JSClass<JSDynamicComponent>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
57     JSClass<JSDynamicComponent>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
58     JSClass<JSDynamicComponent>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
59     JSClass<JSDynamicComponent>::StaticMethod("width", &JSDynamicComponent::Width, opt);
60     JSClass<JSDynamicComponent>::StaticMethod("height", &JSDynamicComponent::Height, opt);
61     JSClass<JSDynamicComponent>::InheritAndBind<JSViewAbstract>(globalObj);
62 }
63 
Create(const JSCallbackInfo & info)64 void JSDynamicComponent::Create(const JSCallbackInfo& info)
65 {
66     if (info.Length() < 1 || !info[0]->IsObject()) {
67         TAG_LOGW(AceLogTag::ACE_ISOLATED_COMPONENT, "DynamicComponent argument is invalid");
68         return;
69     }
70     auto dynamicComponentArg = JSRef<JSObject>::Cast(info[0]);
71     auto hapPathValue = dynamicComponentArg->GetProperty("hapPath");
72     auto abcPathValue = dynamicComponentArg->GetProperty("abcPath");
73     auto entryPointValue = dynamicComponentArg->GetProperty("entryPoint");
74     if (!hapPathValue->IsString() || !abcPathValue->IsString() || !entryPointValue->IsString()) {
75         TAG_LOGW(AceLogTag::ACE_ISOLATED_COMPONENT, "DynamicComponent argument type is invalid");
76         return;
77     }
78 
79     auto hostEngine = EngineHelper::GetCurrentEngine();
80     CHECK_NULL_VOID(hostEngine);
81     NativeEngine* hostNativeEngine = hostEngine->GetNativeEngine();
82     auto jsWorker = dynamicComponentArg->GetProperty("worker");
83     panda::Local<JsiValue> value = jsWorker.Get().GetLocalHandle();
84     JSValueWrapper valueWrapper = value;
85     napi_value nativeValue = hostNativeEngine->ValueToNapiValue(valueWrapper);
86     Worker* worker = nullptr;
87     napi_unwrap(reinterpret_cast<napi_env>(hostNativeEngine), nativeValue, reinterpret_cast<void**>(&worker));
88     if (worker == nullptr) {
89         TAG_LOGE(AceLogTag::ACE_ISOLATED_COMPONENT, "worker is nullptr");
90         return;
91     } else {
92         TAG_LOGD(AceLogTag::ACE_ISOLATED_COMPONENT, "worker running=%{public}d, worker name=%{public}s",
93             worker->IsRunning(), worker->GetName().c_str());
94     }
95     auto hapPath = hapPathValue->ToString();
96     auto abcPath = abcPathValue->ToString();
97     auto entryPoint = entryPointValue->ToString();
98 
99     UIExtensionModel::GetInstance()->Create();
100     auto frameNode = NG::ViewStackProcessor::GetInstance()->GetMainFrameNode();
101     CHECK_NULL_VOID(frameNode);
102     auto instanceId = Container::CurrentId();
103 
104     worker->RegisterCallbackForWorkerEnv([instanceId, weak = AceType::WeakClaim(frameNode), hapPath,
105                                              abcPath, entryPoint](napi_env env) {
106         ContainerScope scope(instanceId);
107         auto container = Container::Current();
108         container->GetTaskExecutor()->PostTask(
109             [weak, hapPath, abcPath, entryPoint, env]() {
110                 auto frameNode = weak.Upgrade();
111                 CHECK_NULL_VOID(frameNode);
112                 UIExtensionModel::GetInstance()->InitializeDynamicComponent(
113                     frameNode, hapPath, abcPath, entryPoint, env);
114             },
115             TaskExecutor::TaskType::UI, "ArkUIDynamicComponentInitialize");
116     });
117 }
118 
SetOnSizeChanged(const JSCallbackInfo & info)119 void JSDynamicComponent::SetOnSizeChanged(const JSCallbackInfo& info)
120 {
121 }
122 
Width(const JSCallbackInfo & info)123 void JSDynamicComponent::Width(const JSCallbackInfo& info)
124 {
125     JSViewAbstract::JsWidth(info);
126 
127     CalcDimension value;
128     bool parseResult = ParseJsDimensionVpNG(info[0], value);
129     if (NearEqual(value.Value(), 0)) {
130         ViewAbstractModel::GetInstance()->ClearWidthOrHeight(true);
131         UIExtensionModel::GetInstance()->SetAdaptiveWidth(true);
132         return;
133     }
134     UIExtensionModel::GetInstance()->SetAdaptiveWidth(!parseResult || value.Unit() == DimensionUnit::AUTO);
135 }
136 
Height(const JSCallbackInfo & info)137 void JSDynamicComponent::Height(const JSCallbackInfo& info)
138 {
139     JSViewAbstract::JsHeight(info);
140 
141     CalcDimension value;
142     bool parseResult = ParseJsDimensionVpNG(info[0], value);
143     if (NearEqual(value.Value(), 0)) {
144         ViewAbstractModel::GetInstance()->ClearWidthOrHeight(false);
145         UIExtensionModel::GetInstance()->SetAdaptiveHeight(true);
146         return;
147     }
148     UIExtensionModel::GetInstance()->SetAdaptiveHeight(!parseResult || value.Unit() == DimensionUnit::AUTO);
149 }
150 } // namespace OHOS::Ace::Framework
151