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 "frameworks/bridge/declarative_frontend/jsview/js_content_slot.h"
17 
18 #include "bridge/declarative_frontend/engine/js_types.h"
19 #include "bridge/declarative_frontend/engine/jsi/jsi_bindings.h"
20 #include "core/components_ng/syntax/content_slot_model_ng.h"
21 #include "core/components_ng/syntax/node_content.h"
22 
23 namespace OHOS::Ace::Framework {
24 
JSBind(BindingTarget globalObj)25 void JSContentSlot::JSBind(BindingTarget globalObj)
26 {
27     JSClass<JSContentSlot>::Declare("ContentSlot");
28     JSClass<JSContentSlot>::StaticMethod("create", &JSContentSlot::Create);
29     JSClass<JSContentSlot>::StaticMethod("pop", &JSContentSlot::Pop);
30 
31     JSClass<JSContentSlot>::Bind<>(globalObj);
32 }
33 
Create(const JSCallbackInfo & info)34 void JSContentSlot::Create(const JSCallbackInfo& info)
35 {
36     NG::NodeContent* content = nullptr;
37     if (info.Length() > 0 && info[0]->IsObject()) {
38         auto hasNativePtr = JSRef<JSObject>::Cast(info[0])->HasProperty("nativePtr_");
39         if (hasNativePtr) {
40             auto nodeContent = JSRef<JSObject>::Cast(info[0])->GetProperty("nativePtr_");
41             auto contentHandle = nodeContent.Get().GetLocalHandle();
42             content = reinterpret_cast<NG::NodeContent*>(contentHandle->ToNativePointer(info.GetVm())->Value());
43         }
44     }
45     NG::ContentSlotModel::Create(content);
46 }
47 
Pop()48 void JSContentSlot::Pop() {}
49 
Constructor(const JSCallbackInfo & info)50 void JSNodeContent::Constructor(const JSCallbackInfo& info)
51 {
52     auto content = Referenced::MakeRefPtr<NG::NodeContent>();
53     content->IncRefCount();
54     info.SetReturnValue(Referenced::RawPtr(content));
55 }
56 
Destructor(NG::NodeContent * nodeContent)57 void JSNodeContent::Destructor(NG::NodeContent* nodeContent)
58 {
59     if (nodeContent != nullptr) {
60         nodeContent->DecRefCount();
61     }
62 }
63 
GetNativeContent(const JSCallbackInfo & info)64 void JSNodeContent::GetNativeContent(const JSCallbackInfo& info)
65 {
66     if (info.Length() != 1 && !info[0]->IsObject()) {
67         return;
68     }
69     auto* nodeContent = JSRef<JSObject>::Cast(info[0])->Unwrap<NG::NodeContent>();
70     EcmaVM* vm = info.GetVm();
71     info.SetReturnValue(JSRef<JSVal>::Make(panda::NativePointerRef::New(vm, nodeContent)));
72 }
73 
JSBind(BindingTarget globalObj)74 void JSNodeContent::JSBind(BindingTarget globalObj)
75 {
76     JSClass<NG::NodeContent>::Declare("ArkUINativeNodeContent");
77     JSClass<NG::NodeContent>::StaticMethod("getNativeContent", &JSNodeContent::GetNativeContent);
78     JSClass<NG::NodeContent>::Bind(globalObj, JSNodeContent::Constructor, JSNodeContent::Destructor);
79 }
80 
81 } // namespace OHOS::Ace::Framework
82