1 /*
2  * Copyright (c) 2022 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 "core/components_ng/pattern/custom/custom_node_base.h"
17 
18 #include "base/memory/ace_type.h"
19 #include "base/utils/utils.h"
20 #include "core/components_ng/base/ui_node.h"
21 #include "core/components_ng/pattern/image/image_pattern.h"
22 #include "core/pipeline_ng/pipeline_context.h"
23 
24 namespace OHOS::Ace::NG {
25 
~CustomNodeBase()26 CustomNodeBase::~CustomNodeBase()
27 {
28     RecycleManager::Erase(recycleInfo_.elemtId);
29     // appearFunc_ & destroyFunc_ should be executed in pairs
30     if (!executeFireOnAppear_ && appearFunc_) {
31         appearFunc_();
32         if (didBuildFunc_) {
33             didBuildFunc_();
34         }
35     }
36     if (destroyFunc_) {
37         ACE_SCOPED_TRACE("CustomNodeBase:Destroy [%s]", GetJSViewName().c_str());
38         destroyFunc_();
39     }
40 }
41 
Update()42 void CustomNodeBase::Update()
43 {
44     needRebuild_ = false;
45     if (updateFunc_) {
46         updateFunc_();
47     }
48 }
49 
MarkNeedUpdate()50 void CustomNodeBase::MarkNeedUpdate()
51 {
52     if (recycleRenderFunc_) {
53         return;
54     }
55     auto context = PipelineContext::GetCurrentContext();
56     CHECK_NULL_VOID(context);
57     if (needRebuild_) {
58         return;
59     }
60     needRebuild_ = true;
61     context->AddDirtyCustomNode(AceType::DynamicCast<UINode>(Claim(this)));
62 }
63 
FireRecycleSelf()64 void CustomNodeBase::FireRecycleSelf()
65 {
66     auto uiNode = AceType::DynamicCast<UINode>(Claim(this));
67     uiNode->OnRecycle();
68     if (recycleCustomNodeFunc_) {
69         recycleInfo_.Recycle(uiNode->GetId());
70         RecycleManager::Push(uiNode->GetId(), AceType::WeakClaim(this));
71         recycleCustomNodeFunc_(AceType::Claim<CustomNodeBase>(this));
72     }
73 }
74 
FireRecycleRenderFunc()75 void CustomNodeBase::FireRecycleRenderFunc()
76 {
77     if (recycleRenderFunc_) {
78         ACE_SCOPED_TRACE("CustomNode:BuildRecycle %s", GetJSViewName().c_str());
79         auto node = AceType::DynamicCast<UINode>(Claim(this));
80         recycleInfo_.Reuse();
81         RecycleManager::Pop(node->GetId());
82         {
83             ScopedViewStackProcessor scopedViewStackProcessor;
84             recycleRenderFunc_();
85         }
86         node->OnReuse();
87         node->SetJSViewActive(true);
88         recycleRenderFunc_ = nullptr;
89     }
90 }
91 
SetOnDumpInfoFunc(std::function<void (const std::vector<std::string> &)> && func)92 void CustomNodeBase::SetOnDumpInfoFunc(std::function<void(const std::vector<std::string>&)>&& func)
93 {
94     onDumpInfoFunc_ = func;
95 }
96 
SetOnDumpInspectorFunc(std::function<std::string ()> && func)97 void CustomNodeBase::SetOnDumpInspectorFunc(std::function<std::string()>&& func)
98 {
99     onDumpInspectorFunc_ = func;
100 }
101 } // namespace OHOS::Ace::NG
102