• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_VIEW_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_VIEW_H
18 
19 #include <list>
20 
21 #include "core/pipeline/base/composed_component.h"
22 #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h"
23 #include "frameworks/bridge/declarative_frontend/jsview/js_view_abstract.h"
24 #include "frameworks/bridge/declarative_frontend/jsview/js_view_functions.h"
25 
26 namespace OHOS::Ace {
27 
28 class ComposedElement;
29 
30 } // namespace OHOS::Ace
31 
32 namespace OHOS::Ace::Framework {
33 
34 class JSView;
35 
36 class JSView : public JSViewAbstract, public Referenced {
37 public:
38     JSView(const std::string& viewId, JSRef<JSObject> jsObject, JSRef<JSFunc> jsRenderFunction);
39     ~JSView() override;
40 
41     RefPtr<OHOS::Ace::Component> InternalRender(const RefPtr<Component>& parent);
42     void Destroy(JSView* parentCustomView);
43     RefPtr<Component> CreateComponent();
44     RefPtr<PageTransitionComponent> BuildPageTransitionComponent();
45 
46     void MarkNeedUpdate();
47 
48     void SyncInstanceId();
49 
50     void RestoreInstanceId();
51 
NeedsUpdate()52     bool NeedsUpdate()
53     {
54         return needsUpdate_;
55     }
56 
57     /**
58      * Views which do not have a state can mark static.
59      * The element will be reused and re-render will be skipped.
60      */
MarkStatic()61     void MarkStatic()
62     {
63         isStatic_ = true;
64     }
65 
IsStatic()66     bool IsStatic()
67     {
68         return isStatic_;
69     }
70 
71     /**
72      * During render function execution, the child customview with same id will
73      * be recycled if they exist already in our child map. The ones which are not
74      * recycled needs to be cleaned. So After render function execution, clean the
75      * abandoned child customview.
76      */
77     void CleanUpAbandonedChild();
78 
79     /**
80      * Retries the customview child for recycling
81      * always use FindChildById to be certain before calling this method
82      */
83     JSRef<JSObject> GetChildById(const std::string& viewId);
84 
85     void FindChildById(const JSCallbackInfo& info);
86     void GetContext(const JSCallbackInfo& info);
87     void GetContentStorage(const JSCallbackInfo& info);
88 
FireOnShow()89     void FireOnShow()
90     {
91         if (jsViewFunction_) {
92             ACE_SCORING_EVENT("Component[" + viewId_ + "].OnShow");
93             jsViewFunction_->ExecuteShow();
94         }
95     }
96 
FireOnHide()97     void FireOnHide()
98     {
99         if (jsViewFunction_) {
100             ACE_SCORING_EVENT("Component[" + viewId_ + "].OnHide");
101             jsViewFunction_->ExecuteHide();
102         }
103     }
104 
FireOnBackPress()105     bool FireOnBackPress()
106     {
107         if (jsViewFunction_) {
108             ACE_SCORING_EVENT("Component[" + viewId_ + "].OnBackPress");
109             return jsViewFunction_->ExecuteOnBackPress();
110         }
111         return false;
112     }
113 
SetContext(const JSExecutionContext & context)114     void SetContext(const JSExecutionContext& context)
115     {
116         jsViewFunction_->SetContext(context);
117     }
118 
ExecuteUpdateWithValueParams(const std::string & jsonData)119     void ExecuteUpdateWithValueParams(const std::string& jsonData)
120     {
121         jsViewFunction_->ExecuteUpdateWithValueParams(jsonData);
122     }
123 
MarkLazyForEachProcess(const std::string & groudId)124     void MarkLazyForEachProcess(const std::string& groudId)
125     {
126         isLazyForEachProcessed_ = true;
127         lazyItemGroupId_ = groudId;
128     }
129 
ResetLazyForEachProcess()130     void ResetLazyForEachProcess()
131     {
132         isLazyForEachProcessed_ = false;
133         lazyItemGroupId_ = "";
134     }
135 
136     /**
137      * New CustomView child will be added to the map.
138      * and it can be reterieved for recycling in next render function
139      * In next render call if this child is not recycled, it will be destroyed.
140      */
141     std::string AddChildById(const std::string& viewId, const JSRef<JSObject>& obj);
142 
143     void RemoveChildGroupById(const std::string& viewId);
144 
145     static void Create(const JSCallbackInfo& info);
146     static void JSBind(BindingTarget globalObj);
147 
148     static void ConstructorCallback(const JSCallbackInfo& args);
149     static void DestructorCallback(JSView* instance);
150 
151 private:
GetElement()152     WeakPtr<OHOS::Ace::ComposedElement> GetElement()
153     {
154         return element_;
155     }
156 
157     void DestroyChild(JSView* parentCustomView);
158 
159     /**
160      * Takes care of the viewId wrt to foreach
161      */
162     std::string ProcessViewId(const std::string& viewId);
163     /**
164      * creates a set of valid viewids on a render function execution
165      * its cleared after cleaning up the abandoned child.
166      */
167     void ChildAccessedById(const std::string& viewId);
168 
169     // view id for custom view itself
170     std::string viewId_;
171 
172     // the unique id for recycle view.
173     std::string id_;
174 
175     int32_t instanceId_ = -1;
176     int32_t restoreInstanceId_ = -1;
177 
178     WeakPtr<OHOS::Ace::ComposedElement> element_ = nullptr;
179     bool needsUpdate_ = false;
180     bool isStatic_ = false;
181     bool isLazyForEachProcessed_ = false;
182     std::string lazyItemGroupId_;
183 
184     RefPtr<ViewFunctions> jsViewFunction_;
185 
186     // hold handle to the native and javascript object to keep them alive
187     // until they are abandoned
188     std::unordered_map<std::string, JSRef<JSObject>> customViewChildren_;
189 
190     // hold handle to the native and javascript object to keep them alive
191     // until they are abandoned used by lazyForEach
192     std::unordered_map<std::string, JSRef<JSObject>> customViewChildrenWithLazy_;
193 
194     // hold js view ids by lazy item ground.
195     // until they are abandoned used by lazyForEach
196     std::unordered_map<std::string, std::list<std::string>> lazyItemGroups_;
197 
198     // a set of valid viewids on a renderfuntion excution
199     // its cleared after cleaning up the abandoned child.
200     std::unordered_set<std::string> lastAccessedViewIds_;
201 };
202 
203 } // namespace OHOS::Ace::Framework
204 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_VIEW_H
205