1 /*
2 * Copyright (c) 2022-2023 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/models/xcomponent_model_impl.h"
17
18 #include "bridge/declarative_frontend/view_stack_processor.h"
19 #include "core/components/xcomponent/xcomponent_component_client.h"
20 #include "bridge/declarative_frontend/jsview/js_xcomponent.h"
21
22 namespace OHOS::Ace::Framework {
23
Create(const std::optional<std::string> & id,XComponentType,const std::optional<std::string> & libraryname,const std::shared_ptr<InnerXComponentController> & xcomponentController)24 void XComponentModelImpl::Create(const std::optional<std::string>& id, XComponentType /* type */,
25 const std::optional<std::string>& libraryname,
26 const std::shared_ptr<InnerXComponentController>& xcomponentController)
27 {
28 auto xcomponentComponent = AceType::MakeRefPtr<OHOS::Ace::XComponentComponent>("xcomponent");
29 xcomponentComponent->SetId(id.value_or(""));
30 xcomponentComponent->SetXComponentType("surface");
31 xcomponentComponent->SetLibraryName(libraryname.value_or(""));
32 if (xcomponentController) {
33 xcomponentComponent->SetXComponentController(xcomponentController);
34 }
35
36 XComponentComponentClient::GetInstance().AddXComponentToXcomponentsMap(
37 xcomponentComponent->GetId(), xcomponentComponent);
38 auto deleteCallback = [xcId = id.value_or("")]() {
39 XComponentComponentClient::GetInstance().DeleteFromXcomponentsMapById(xcId);
40 XComponentClient::GetInstance().DeleteControllerFromJSXComponentControllersMap(xcId);
41 };
42 xcomponentComponent->RegisterDeleteCallback(std::move(deleteCallback));
43 ViewStackProcessor::GetInstance()->Push(xcomponentComponent);
44 }
45
SetSoPath(const std::string & soPath)46 void XComponentModelImpl::SetSoPath(const std::string& soPath)
47 {
48 auto xcomponentComponent =
49 AceType::DynamicCast<XComponentComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
50 CHECK_NULL_VOID(xcomponentComponent);
51 xcomponentComponent->SetSoPath(soPath);
52 }
53
SetOnLoad(LoadEvent && onLoad)54 void XComponentModelImpl::SetOnLoad(LoadEvent&& onLoad)
55 {
56 auto* stack = ViewStackProcessor::GetInstance();
57 auto xcomponentComponent = AceType::DynamicCast<XComponentComponent>(stack->GetMainComponent());
58 if (!xcomponentComponent) {
59 LOGE("JSXComponent::JsOnLoad xcomponentComponent is null.");
60 return;
61 }
62 auto xcomponentId = xcomponentComponent->GetId();
63 xcomponentComponent->SetXComponentInitEventId(
64 EventMarker([func = std::move(onLoad), xcomponentId](const std::string& param) { func(xcomponentId); }));
65 }
66
SetOnDestroy(DestroyEvent && onDestroy)67 void XComponentModelImpl::SetOnDestroy(DestroyEvent&& onDestroy)
68 {
69 auto* stack = ViewStackProcessor::GetInstance();
70 auto xcomponentComponent = AceType::DynamicCast<XComponentComponent>(stack->GetMainComponent());
71 if (!xcomponentComponent) {
72 LOGE("JSXComponent::JsOnDestroy xcomponentComponent is null.");
73 return;
74 }
75 xcomponentComponent->SetXComponentDestroyEventId(
76 EventMarker([func = std::move(onDestroy)](const std::string& param) { func(""); }));
77 }
78 } // namespace OHOS::Ace::Framework
79