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