1 /*
2  * Copyright (c) 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_isolated_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/pattern/ui_extension/ui_extension_model.h"
42 #include "core/components_ng/pattern/ui_extension/ui_extension_model_ng.h"
43 
44 using namespace Commonlibrary::Concurrent::WorkerModule;
45 
46 namespace OHOS::Ace::Framework {
47 
ParseWorker(const JSRef<JSVal> & jsWorker)48 static Worker* ParseWorker(const JSRef<JSVal>& jsWorker)
49 {
50     auto hostEngine = EngineHelper::GetCurrentEngine();
51     CHECK_NULL_RETURN(hostEngine, nullptr);
52     NativeEngine* hostNativeEngine = hostEngine->GetNativeEngine();
53     CHECK_NULL_RETURN(hostNativeEngine, nullptr);
54     panda::Local<JsiValue> value = jsWorker.Get().GetLocalHandle();
55     JSValueWrapper valueWrapper = value;
56     napi_value nativeValue = hostNativeEngine->ValueToNapiValue(valueWrapper);
57     Worker* worker = nullptr;
58     napi_unwrap(reinterpret_cast<napi_env>(hostNativeEngine),
59         nativeValue, reinterpret_cast<void**>(&worker));
60     return worker;
61 }
62 
JSBind(BindingTarget globalObj)63 void JSIsolatedComponent::JSBind(BindingTarget globalObj)
64 {
65     JSClass<JSIsolatedComponent>::Declare("IsolatedComponent");
66     MethodOptions opt = MethodOptions::NONE;
67     JSClass<JSIsolatedComponent>::StaticMethod("create", &JSIsolatedComponent::Create, opt);
68     JSClass<JSIsolatedComponent>::StaticMethod("onError", &JSIsolatedComponent::JsOnError, opt);
69     JSClass<JSIsolatedComponent>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
70     JSClass<JSIsolatedComponent>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
71     JSClass<JSIsolatedComponent>::StaticMethod("width", &JSIsolatedComponent::Width, opt);
72     JSClass<JSIsolatedComponent>::StaticMethod("height", &JSIsolatedComponent::Height, opt);
73     JSClass<JSIsolatedComponent>::InheritAndBind<JSViewAbstract>(globalObj);
74 }
75 
Create(const JSCallbackInfo & info)76 void JSIsolatedComponent::Create(const JSCallbackInfo& info)
77 {
78     if (info.Length() < 1 || !info[0]->IsObject()) {
79         TAG_LOGW(AceLogTag::ACE_ISOLATED_COMPONENT, "IsolatedComponent argument is invalid");
80         return;
81     }
82 
83     auto obj = JSRef<JSObject>::Cast(info[0]);
84     JSRef<JSVal> wantObj = obj->GetProperty("want");
85     if (!wantObj->IsObject()) {
86         TAG_LOGW(AceLogTag::ACE_ISOLATED_COMPONENT, "IsolatedComponent want is invalid");
87         return;
88     }
89 
90     RefPtr<OHOS::Ace::WantWrap> want = CreateWantWrapFromNapiValue(wantObj);
91     CHECK_NULL_VOID(want);
92     Worker* worker = ParseWorker(obj->GetProperty("worker"));
93     if (worker == nullptr) {
94         TAG_LOGW(AceLogTag::ACE_ISOLATED_COMPONENT, "worker is null");
95         return;
96     }
97 
98     TAG_LOGI(AceLogTag::ACE_ISOLATED_COMPONENT, "worker running=%{public}d,  worker name=%{public}s",
99         worker->IsRunning(), worker->GetName().c_str());
100     UIExtensionModel::GetInstance()->Create();
101     auto frameNode = NG::ViewStackProcessor::GetInstance()->GetMainFrameNode();
102     CHECK_NULL_VOID(frameNode);
103     auto instanceId = Container::CurrentId();
104     auto weak = AceType::WeakClaim(frameNode);
105     worker->RegisterCallbackForWorkerEnv([instanceId, weak, want](napi_env env) {
106         ContainerScope scope(instanceId);
107         auto container = Container::Current();
108         container->GetTaskExecutor()->PostTask(
109             [weak, want, env]() {
110                 auto frameNode = weak.Upgrade();
111                 CHECK_NULL_VOID(frameNode);
112                 UIExtensionModel::GetInstance()->InitializeIsolatedComponent(
113                     frameNode, want, env);
114             },
115             TaskExecutor::TaskType::UI, "ArkUIIsolatedComponentInitialize");
116     });
117 }
118 
JsOnError(const JSCallbackInfo & info)119 void JSIsolatedComponent::JsOnError(const JSCallbackInfo& info)
120 {
121     if (info.Length() < 1 || !info[0]->IsFunction()) {
122         TAG_LOGW(AceLogTag::ACE_ISOLATED_COMPONENT, "onError argument is invalid");
123         return;
124     }
125 
126     WeakPtr<NG::FrameNode> frameNode =
127         AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
128     auto execCtx = info.GetExecutionContext();
129     auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
130     auto instanceId = Container::CurrentId();
131     auto onError = [execCtx, func = std::move(jsFunc), instanceId, node = frameNode]
132         (int32_t code, const std::string& name, const std::string& message) {
133             ContainerScope scope(instanceId);
134             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
135             ACE_SCORING_EVENT("IsolatedComponent.onError");
136             auto pipelineContext = PipelineContext::GetCurrentContext();
137             CHECK_NULL_VOID(pipelineContext);
138             pipelineContext->UpdateCurrentActiveNode(node);
139             JSRef<JSObject> obj = JSRef<JSObject>::New();
140             obj->SetProperty<int32_t>("code", code);
141             obj->SetProperty<std::string>("name", name);
142             obj->SetProperty<std::string>("message", message);
143             auto returnValue = JSRef<JSVal>::Cast(obj);
144             func->ExecuteJS(1, &returnValue);
145         };
146     UIExtensionModel::GetInstance()->SetPlatformOnError(std::move(onError));
147 }
148 
Width(const JSCallbackInfo & info)149 void JSIsolatedComponent::Width(const JSCallbackInfo& info)
150 {
151     JSViewAbstract::JsWidth(info);
152 
153     CalcDimension value;
154     bool parseResult = ParseJsDimensionVpNG(info[0], value);
155     UIExtensionModel::GetInstance()->SetAdaptiveWidth(!parseResult || value.Unit() == DimensionUnit::AUTO);
156 }
157 
Height(const JSCallbackInfo & info)158 void JSIsolatedComponent::Height(const JSCallbackInfo& info)
159 {
160     JSViewAbstract::JsHeight(info);
161 
162     CalcDimension value;
163     bool parseResult = ParseJsDimensionVpNG(info[0], value);
164     UIExtensionModel::GetInstance()->SetAdaptiveHeight(!parseResult || value.Unit() == DimensionUnit::AUTO);
165 }
166 } // namespace OHOS::Ace::Framework
167