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/plugin/plugin_node.h"
17 
18 #include "base/utils/utils.h"
19 #include "core/components/plugin/plugin_sub_container.h"
20 #include "core/components_ng/pattern/plugin/plugin_pattern.h"
21 #include "core/pipeline_ng/pipeline_context.h"
22 #include "core/pipeline_ng/ui_task_scheduler.h"
23 
24 namespace OHOS::Ace::NG {
25 
GetOrCreatePluginNode(const std::string & tag,int32_t nodeId,const std::function<RefPtr<Pattern> (void)> & patternCreator)26 RefPtr<PluginNode> PluginNode::GetOrCreatePluginNode(
27     const std::string& tag, int32_t nodeId, const std::function<RefPtr<Pattern>(void)>& patternCreator)
28 {
29     auto pluginNode = ElementRegister::GetInstance()->GetSpecificItemById<PluginNode>(nodeId);
30     if (pluginNode) {
31         if (pluginNode->GetTag() == tag) {
32             return pluginNode;
33         }
34         ElementRegister::GetInstance()->RemoveItemSilently(nodeId);
35         auto parent = pluginNode->GetParent();
36         if (parent) {
37             parent->RemoveChild(pluginNode);
38         }
39     }
40 
41     auto pattern = patternCreator ? patternCreator() : AceType::MakeRefPtr<Pattern>();
42     pluginNode = AceType::MakeRefPtr<PluginNode>(tag, nodeId, pattern, false);
43     pluginNode->InitializePatternAndContext();
44     ElementRegister::GetInstance()->AddUINode(pluginNode);
45     return pluginNode;
46 }
47 
48 } // namespace OHOS::Ace::NG
49