1 /*
2  * Copyright (c) 2021 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_V2_COMMON_ELEMENT_PROXY_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_COMMON_ELEMENT_PROXY_H
18 
19 #include <limits>
20 #include <list>
21 
22 #include "base/memory/referenced.h"
23 #include "base/utils/noncopyable.h"
24 #include "core/common/layout_inspector.h"
25 #include "core/pipeline/base/component.h"
26 #include "core/pipeline/base/element.h"
27 #include "core/pipeline/base/element_register.h"
28 
29 namespace OHOS::Ace::V2 {
30 
31 class ElementProxyHost;
32 
33 class ElementProxy : public virtual AceType {
34     DECLARE_ACE_TYPE(ElementProxy, AceType);
35 
36 public:
37     static constexpr size_t INVALID_INDEX = std::numeric_limits<size_t>::max();
38 
39     static RefPtr<ElementProxy> Create(const WeakPtr<ElementProxyHost>& host, const RefPtr<Component>& component);
40 
ElementProxy(const WeakPtr<ElementProxyHost> & host)41     explicit ElementProxy(const WeakPtr<ElementProxyHost>& host) : Referenced(false), host_(host) {}
42     ~ElementProxy() override = default;
43 
44     virtual void Update(const RefPtr<Component>& component, size_t startIndex) = 0;
45     virtual void UpdateIndex(size_t startIndex) = 0;
46     virtual RefPtr<Component> GetComponentByIndex(size_t index) = 0;
47     virtual RefPtr<Element> GetElementByIndex(size_t index) = 0;
48     virtual void ReleaseElementByIndex(size_t index) = 0;
49     virtual void ReleaseElementById(const ComposeId& composeId) = 0;
50     virtual void RefreshActiveComposeIds() = 0;
51 
RenderCount()52     size_t RenderCount() const
53     {
54         return count_;
55     }
56 
GetId()57     const ComposeId& GetId() const
58     {
59         return composedId_;
60     }
61 
IndexInRange(size_t index)62     bool IndexInRange(size_t index) const
63     {
64         return (index >= startIndex_) && (index < startIndex_ + count_);
65     }
66 
67     virtual void Dump(const std::string& prefix) const;
68 
GetElementId()69     int32_t GetElementId()
70     {
71         return elmtId_;
72     }
SetElementId(int32_t id)73     void SetElementId(int32_t id)
74     {
75         elmtId_ = id;
76     }
77 
78     /**
79      * method needs to be called after localized update
80      * to the proxied Element
81      */
LocalizedUpdate(const RefPtr<Component> & newComponent,const RefPtr<Component> & outmostWrappingComponent)82     virtual void LocalizedUpdate(
83         const RefPtr<Component>& newComponent, const RefPtr<Component>& outmostWrappingComponent)
84     {}
85 
86 protected:
87     void AddSelfToElementRegistry();
88     void RemoveSelfFromElementRegistry();
89 
90     WeakPtr<ElementProxyHost> host_;
91 
92     ComposeId composedId_;
93     size_t startIndex_ = INVALID_INDEX;
94     size_t count_ = 0;
95 
96 private:
97     ElementIdType elmtId_ = ElementRegister::UndefinedElementId;
98 
99     ACE_DISALLOW_COPY_AND_MOVE(ElementProxy);
100 };
101 
102 class ElementProxyHost : virtual public AceType {
103     DECLARE_ACE_TYPE(ElementProxyHost, AceType);
104 
105 public:
106     size_t TotalCount() const;
107 
108     void UpdateChildren(const std::list<RefPtr<Component>>& components);
109     virtual void UpdateIndex();
110 
111     RefPtr<Component> GetComponentByIndex(size_t index);
112     RefPtr<Element> GetElementByIndex(size_t index);
113     void ReleaseElementByIndex(size_t index);
114     void ReleaseElementById(const ComposeId& id);
115     void AddComposeId(const ComposeId& id);
116     void AddActiveComposeId(ComposeId& id);
117     void ReleaseRedundantComposeIds();
118     void DumpProxy();
119 
120     virtual RefPtr<Element> OnUpdateElement(const RefPtr<Element>& element, const RefPtr<Component>& component) = 0;
121     virtual RefPtr<Component> OnMakeEmptyComponent() = 0;
OnDataSourceUpdated(size_t startIndex)122     virtual void OnDataSourceUpdated(size_t startIndex)
123     {
124 #if !defined(PREVIEW)
125     LayoutInspector::SupportInspector();
126 #endif
127     }
128     virtual size_t GetReloadedCheckNum();
129 
130 private:
131     RefPtr<ElementProxy> proxy_;
132     std::set<ComposeId> composeIds_;
133     std::set<ComposeId> activeComposeIds_;
134 };
135 
136 class ACE_EXPORT ForEachElementLookup {
137 public:
138     static std::list<std::string> GetIdArray(int32_t elmtId);
139 };
140 
141 } // namespace OHOS::Ace::V2
142 
143 
144 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_COMMON_ELEMENT_PROXY_H
145