• 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 "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 
JSBind(BindingTarget globalObj)48 void JSDynamicComponent::JSBind(BindingTarget globalObj)
49 {
50     JSClass<JSDynamicComponent>::Declare("DynamicComponent");
51     MethodOptions opt = MethodOptions::NONE;
52     JSClass<JSDynamicComponent>::StaticMethod("create", &JSDynamicComponent::Create, opt);
53     JSClass<JSDynamicComponent>::StaticMethod("onSizeChanged", &JSDynamicComponent::SetOnSizeChanged, opt);
54     JSClass<JSDynamicComponent>::InheritAndBind<JSViewAbstract>(globalObj);
55 }
56 
Create(const JSCallbackInfo & info)57 void JSDynamicComponent::Create(const JSCallbackInfo& info)
58 {
59     if (info.Length() < 1 || !info[0]->IsObject()) {
60         TAG_LOGW(AceLogTag::ACE_DYNAMIC_COMPONENT, "DynamicComponent argument is invalid");
61         return;
62     }
63     auto dynamicComponentArg = JSRef<JSObject>::Cast(info[0]);
64     auto hapPathValue = dynamicComponentArg->GetProperty("hapPath");
65     auto abcPathValue = dynamicComponentArg->GetProperty("abcPath");
66     auto entryPointValue = dynamicComponentArg->GetProperty("entryPoint");
67     if (!hapPathValue->IsString() || !abcPathValue->IsString() || !entryPointValue->IsString()) {
68         TAG_LOGW(AceLogTag::ACE_DYNAMIC_COMPONENT, "DynamicComponent argument type is invalid");
69         return;
70     }
71 
72     auto hostEngine = EngineHelper::GetCurrentEngine();
73     CHECK_NULL_VOID(hostEngine);
74     NativeEngine* hostNativeEngine = hostEngine->GetNativeEngine();
75     auto jsWorker = dynamicComponentArg->GetProperty("worker");
76     panda::Local<JsiValue> value = jsWorker.Get().GetLocalHandle();
77     JSValueWrapper valueWrapper = value;
78     napi_value nativeValue = hostNativeEngine->ValueToNapiValue(valueWrapper);
79     Worker* worker = nullptr;
80     napi_unwrap(reinterpret_cast<napi_env>(hostNativeEngine), nativeValue, reinterpret_cast<void**>(&worker));
81     TAG_LOGD(AceLogTag::ACE_DYNAMIC_COMPONENT, "worker running=%{public}d,  worker name=%{public}s",
82         worker->IsRunning(), worker->GetName().c_str());
83     auto hapPath = hapPathValue->ToString();
84     auto abcPath = abcPathValue->ToString();
85     auto entryPoint = entryPointValue->ToString();
86 
87     UIExtensionModel::GetInstance()->Create();
88     auto frameNode = NG::ViewStackProcessor::GetInstance()->GetMainFrameNode();
89     CHECK_NULL_VOID(frameNode);
90     auto instanceId = Container::CurrentId();
91 
92     worker->RegisterCallbackForWorkerEnv([instanceId, weak = AceType::WeakClaim(AceType::RawPtr(frameNode)), hapPath,
93                                              abcPath, entryPoint](napi_env env) {
94         ContainerScope scope(instanceId);
95         auto container = Container::Current();
96         container->GetTaskExecutor()->PostTask(
97             [weak, hapPath, abcPath, entryPoint, env]() {
98                 auto frameNode = weak.Upgrade();
99                 CHECK_NULL_VOID(frameNode);
100                 UIExtensionModel::GetInstance()->InitializeDynamicComponent(
101                     frameNode, hapPath, abcPath, entryPoint, env);
102             },
103             TaskExecutor::TaskType::UI);
104     });
105 }
106 
SetOnSizeChanged(const JSCallbackInfo & info)107 void JSDynamicComponent::SetOnSizeChanged(const JSCallbackInfo& info)
108 {
109     if (info.Length() < 1 || !info[0]->IsFunction()) {
110         TAG_LOGW(AceLogTag::ACE_DYNAMIC_COMPONENT, "OnSizeChanged argument is invalid");
111         return;
112     }
113     auto execCtx = info.GetExecutionContext();
114     auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
115     auto onCardSizeChanged = [execCtx, func = std::move(jsFunc)](int32_t width, int32_t height) {
116         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
117         ACE_SCORING_EVENT("DynamicComponent.onSizeChanged");
118         JSRef<JSObject> obj = JSRef<JSObject>::New();
119         obj->SetProperty<int32_t>("width", width);
120         obj->SetProperty<int32_t>("height", height);
121         auto returnValue = JSRef<JSVal>::Cast(obj);
122         func->ExecuteJS(1, &returnValue);
123     };
124     UIExtensionModel::GetInstance()->SetOnSizeChanged(std::move(onCardSizeChanged));
125 }
126 } // namespace OHOS::Ace::Framework
127