• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_XCOMPONENT_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_XCOMPONENT_H
18 
19 #include "core/common/container.h"
20 #include "frameworks/bridge/declarative_frontend/engine/functions/js_function.h"
21 #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h"
22 #include "frameworks/bridge/declarative_frontend/jsview/js_container_base.h"
23 #include "frameworks/bridge/declarative_frontend/jsview/js_utils.h"
24 #include "frameworks/bridge/declarative_frontend/jsview/js_xcomponent_controller.h"
25 
26 namespace OHOS::Ace::Framework {
27 
28 struct XComponentParams {
29     int32_t elmtId = -1;
30     int32_t xcomponentType = 0;
31     int32_t renderType = 0;
32     int32_t width = 0;
33     int32_t height = 0;
34     std::string xcomponentId;
35     std::string surfaceId;
36     std::string libraryName;
37 };
38 
39 class XComponentClient {
40 public:
41     using GetJSValCallback = std::function<bool(JSRef<JSVal>& param)>;
42     using DeleteCallback = std::function<void()>;
43     XComponentClient& operator=(const XComponentClient&) = delete;
44     XComponentClient(const XComponentClient&) = delete;
45     ~XComponentClient() = default;
46 
GetInstance()47     static XComponentClient& GetInstance()
48     {
49         static XComponentClient instance;
50         return instance;
51     }
52 
GetControllerFromJSXComponentControllersMap(const std::string & xcomponentId)53     RefPtr<JSXComponentController> GetControllerFromJSXComponentControllersMap(const std::string& xcomponentId)
54     {
55         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
56         auto iter = jsXComponentControllersMap_.find(idWithContainerId);
57         if (iter == jsXComponentControllersMap_.end()) {
58             return nullptr;
59         }
60         return iter->second;
61     }
62 
GetNativeXComponentFromXcomponentsMap(const std::string & xcomponentId)63     std::pair<RefPtr<OHOS::Ace::NativeXComponentImpl>, OH_NativeXComponent*> GetNativeXComponentFromXcomponentsMap(
64         const std::string& xcomponentId)
65     {
66         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
67         auto it = nativeXcomponentsMap_.find(idWithContainerId);
68         if (it != nativeXcomponentsMap_.end()) {
69             return it->second;
70         } else {
71             auto nativeXComponentImpl = AceType::MakeRefPtr<NativeXComponentImpl>();
72             auto nativeXComponent = new OH_NativeXComponent(AceType::RawPtr(nativeXComponentImpl));
73             nativeXcomponentsMap_[idWithContainerId] = std::make_pair(nativeXComponentImpl, nativeXComponent);
74             return nativeXcomponentsMap_[idWithContainerId];
75         }
76     }
77 
AddControllerToJSXComponentControllersMap(const std::string & xcomponentId,JSXComponentController * & controller)78     void AddControllerToJSXComponentControllersMap(const std::string& xcomponentId, JSXComponentController*& controller)
79     {
80         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
81         jsXComponentControllersMap_[idWithContainerId] = controller;
82     }
83 
DeleteControllerFromJSXComponentControllersMap(const std::string & xcomponentId)84     void DeleteControllerFromJSXComponentControllersMap(const std::string& xcomponentId)
85     {
86         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
87         jsXComponentControllersMap_.erase(idWithContainerId);
88     }
89 
DeleteFromNativeXcomponentsMapById(const std::string & xcomponentId)90     void DeleteFromNativeXcomponentsMapById(const std::string& xcomponentId)
91     {
92         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
93         auto it = nativeXcomponentsMap_.find(idWithContainerId);
94         if (it == nativeXcomponentsMap_.end()) {
95             return;
96         }
97         if (it->second.second) {
98             delete it->second.second;
99             it->second.second = nullptr;
100         }
101         nativeXcomponentsMap_.erase(it);
102     }
103 
AddJsValToJsValMap(const std::string & xcomponentId,const JSRef<JSVal> & jsVal)104     void AddJsValToJsValMap(const std::string& xcomponentId, const JSRef<JSVal>& jsVal)
105     {
106         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
107         auto result = jsValMap_.try_emplace(idWithContainerId, jsVal);
108         if (!result.second) {
109             result.first->second = jsVal;
110         }
111     }
112 
DeleteFromJsValMapById(const std::string & xcomponentId)113     void DeleteFromJsValMapById(const std::string& xcomponentId)
114     {
115         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
116         auto it = jsValMap_.find(idWithContainerId);
117         if (it == jsValMap_.end()) {
118             return;
119         }
120         jsValMap_.erase(it);
121     }
122 
GetJSVal(const std::string & xcomponentId,JSRef<JSVal> & jsVal)123     bool GetJSVal(const std::string& xcomponentId, JSRef<JSVal>& jsVal)
124     {
125         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
126         auto iter = jsValMap_.find(idWithContainerId);
127         if (iter != jsValMap_.end()) {
128             jsVal = iter->second;
129             jsValMap_.erase(iter);
130             return true;
131         }
132         return false;
133     }
134 
135 private:
136     XComponentClient() = default;
137     std::unordered_map<std::string, RefPtr<JSXComponentController>> jsXComponentControllersMap_;
138     std::unordered_map<std::string, std::pair<RefPtr<OHOS::Ace::NativeXComponentImpl>, OH_NativeXComponent*>>
139         nativeXcomponentsMap_;
140     std::unordered_map<std::string, JSRef<JSVal>> jsValMap_;
141 };
142 
143 class ACE_EXPORT JSXComponent : public JSContainerBase {
144 public:
145     static void JSBind(BindingTarget globalObj);
146     static void Create(const JSCallbackInfo& info);
147     static void JsOnLoad(const JSCallbackInfo& args);
148     static void JsOnDestroy(const JSCallbackInfo& args);
149     static void JsOnAppear(const JSCallbackInfo& args);
150     static void JsOnDisAppear(const JSCallbackInfo& args);
151 
152     static void JsOnTouch(const JSCallbackInfo& args);
153     static void JsOnClick(const JSCallbackInfo& args);
154     static void JsOnKeyEvent(const JSCallbackInfo& args);
155     static void JsOnMouse(const JSCallbackInfo& args);
156     static void JsOnHover(const JSCallbackInfo& args);
157     static void JsOnFocus(const JSCallbackInfo& args);
158     static void JsOnBlur(const JSCallbackInfo& args);
159 
160     static void JsBackgroundColor(const JSCallbackInfo& args);
161     static void JsBackgroundImage(const JSCallbackInfo& args);
162     static void JsBackgroundImageSize(const JSCallbackInfo& args);
163     static void JsBackgroundImagePosition(const JSCallbackInfo& args);
164     static void JsOpacity(const JSCallbackInfo& args);
165     static void JsBlur(const JSCallbackInfo& args);
166     static void JsBackdropBlur(const JSCallbackInfo& args);
167     static void JsGrayscale(const JSCallbackInfo& args);
168     static void JsBrightness(const JSCallbackInfo& args);
169     static void JsSaturate(const JSCallbackInfo& args);
170     static void JsContrast(const JSCallbackInfo& args);
171     static void JsInvert(const JSCallbackInfo& args);
172     static void JsSepia(const JSCallbackInfo& args);
173     static void JsHueRotate(const JSCallbackInfo& args);
174     static void JsColorBlend(const JSCallbackInfo& args);
175     static void JsSphericalEffect(const JSCallbackInfo& args);
176     static void JsLightUpEffect(const JSCallbackInfo& args);
177     static void JsPixelStretchEffect(const JSCallbackInfo& args);
178     static void JsLinearGradientBlur(const JSCallbackInfo& args);
179 
180     // For xcomponent node
181     static void* Create(const XComponentParams& params);
182 
183     void RegisterOnCreate(const JsiExecutionContext& execCtx, const Local<JSValueRef>& func);
184     void RegisterOnDestroy(const JsiExecutionContext& execCtx, const Local<JSValueRef>& func);
SetFrameNode(RefPtr<AceType> frameNode)185     void SetFrameNode(RefPtr<AceType> frameNode)
186     {
187         frameNode_ = frameNode;
188     }
GetFrameNode()189     RefPtr<AceType> GetFrameNode() const
190     {
191         return frameNode_;
192     }
193     bool ChangeRenderType(int32_t renderType);
194 
195 private:
196     RefPtr<AceType> frameNode_;
197 };
198 } // namespace OHOS::Ace::Framework
199 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_XCOMPONENT_H
200