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