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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_XCOMPONENT_XCOMPONENT_COMPONENT_CLIENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_XCOMPONENT_XCOMPONENT_COMPONENT_CLIENT_H
18 
19 #include <string>
20 
21 #include "core/common/container.h"
22 #include "core/components/xcomponent/xcomponent_component.h"
23 
24 namespace OHOS::Ace::Framework {
25 class XComponentComponentClient {
26 public:
27     XComponentComponentClient& operator=(const XComponentComponentClient&) = delete;
28     XComponentComponentClient(const XComponentComponentClient&) = delete;
29     ~XComponentComponentClient() = default;
30 
GetInstance()31     static XComponentComponentClient& GetInstance()
32     {
33         static XComponentComponentClient instance;
34         return instance;
35     }
36 
GetXComponentFromXcomponentsMap(const std::string & xcomponentId)37     RefPtr<XComponentComponent> GetXComponentFromXcomponentsMap(const std::string& xcomponentId)
38     {
39         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
40         auto iter = xcomponentsMap_.find(idWithContainerId);
41         if (iter == xcomponentsMap_.end()) {
42             LOGE("xcomponent: %s not exists", xcomponentId.c_str());
43             return nullptr;
44         }
45         return AceType::DynamicCast<XComponentComponent>(iter->second.Upgrade());
46     }
47 
AddXComponentToXcomponentsMap(const std::string & xcomponentId,const RefPtr<XComponentComponent> & component)48     void AddXComponentToXcomponentsMap(const std::string& xcomponentId, const RefPtr<XComponentComponent>& component)
49     {
50         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
51         auto result = xcomponentsMap_.try_emplace(idWithContainerId, component);
52         if (!result.second) {
53             auto oldXcomponent = result.first->second.Upgrade();
54             if (oldXcomponent) {
55                 oldXcomponent->SetDeleteCallbackToNull();
56             }
57             result.first->second = component;
58         }
59     }
60 
DeleteFromXcomponentsMapById(const std::string & xcomponentId)61     void DeleteFromXcomponentsMapById(const std::string& xcomponentId)
62     {
63         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
64         auto it = xcomponentsMap_.find(idWithContainerId);
65         if (it == xcomponentsMap_.end()) {
66             return;
67         }
68         auto xcomponent = it->second.Upgrade();
69         if (xcomponent) {
70             xcomponent->SetDeleteCallbackToNull();
71         }
72         xcomponentsMap_.erase(it);
73     }
74 
75 private:
76     XComponentComponentClient() = default;
77     std::unordered_map<std::string, WeakPtr<XComponentComponent>> xcomponentsMap_;
78 };
79 
80 } // namespace OHOS::Ace::Framework
81 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_XCOMPONENT_XCOMPONENT_COMPONENT_CLIENT_H