• 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     JSXComponentController* controller = nullptr;
38 };
39 
40 struct XComponentOptions {
41     std::optional<std::string> id = std::nullopt;
42     XComponentType xcomponentType = XComponentType::SURFACE;
43     std::optional<std::string> libraryName = std::nullopt;
44     std::shared_ptr<InnerXComponentController> xcomponentController = nullptr;
45     std::optional<std::uint64_t> screenId = std::nullopt;
46 };
47 
48 class XComponentClient {
49 public:
50     using GetJSValCallback = std::function<bool(JSRef<JSVal>& param)>;
51     using DeleteCallback = std::function<void()>;
52     XComponentClient& operator=(const XComponentClient&) = delete;
53     XComponentClient(const XComponentClient&) = delete;
54     ~XComponentClient() = default;
55 
GetInstance()56     static XComponentClient& GetInstance()
57     {
58         static XComponentClient instance;
59         return instance;
60     }
61 
GetControllerFromJSXComponentControllersMap(const std::string & xcomponentId)62     RefPtr<JSXComponentController> GetControllerFromJSXComponentControllersMap(const std::string& xcomponentId)
63     {
64         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
65         auto iter = jsXComponentControllersMap_.find(idWithContainerId);
66         if (iter == jsXComponentControllersMap_.end()) {
67             return nullptr;
68         }
69         return iter->second;
70     }
71 
GetNativeXComponentFromXcomponentsMap(const std::string & xcomponentId)72     std::pair<RefPtr<OHOS::Ace::NativeXComponentImpl>, OH_NativeXComponent*> GetNativeXComponentFromXcomponentsMap(
73         const std::string& xcomponentId)
74     {
75         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
76         auto it = nativeXcomponentsMap_.find(idWithContainerId);
77         if (it != nativeXcomponentsMap_.end()) {
78             return it->second;
79         } else {
80             auto nativeXComponentImpl = AceType::MakeRefPtr<NativeXComponentImpl>();
81             auto nativeXComponent = new OH_NativeXComponent(AceType::RawPtr(nativeXComponentImpl));
82             nativeXcomponentsMap_[idWithContainerId] = std::make_pair(nativeXComponentImpl, nativeXComponent);
83             return nativeXcomponentsMap_[idWithContainerId];
84         }
85     }
86 
AddControllerToJSXComponentControllersMap(const std::string & xcomponentId,JSXComponentController * & controller)87     void AddControllerToJSXComponentControllersMap(const std::string& xcomponentId, JSXComponentController*& controller)
88     {
89         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
90         jsXComponentControllersMap_[idWithContainerId] = controller;
91     }
92 
DeleteControllerFromJSXComponentControllersMap(const std::string & xcomponentId)93     void DeleteControllerFromJSXComponentControllersMap(const std::string& xcomponentId)
94     {
95         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
96         jsXComponentControllersMap_.erase(idWithContainerId);
97     }
98 
DeleteFromNativeXcomponentsMapById(const std::string & xcomponentId)99     void DeleteFromNativeXcomponentsMapById(const std::string& xcomponentId)
100     {
101         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
102         auto it = nativeXcomponentsMap_.find(idWithContainerId);
103         if (it == nativeXcomponentsMap_.end()) {
104             return;
105         }
106         if (it->second.second) {
107             delete it->second.second;
108             it->second.second = nullptr;
109         }
110         nativeXcomponentsMap_.erase(it);
111     }
112 
AddJsValToJsValMap(const std::string & xcomponentId,const JSRef<JSVal> & jsVal)113     void AddJsValToJsValMap(const std::string& xcomponentId, const JSRef<JSVal>& jsVal)
114     {
115         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
116         auto result = jsValMap_.try_emplace(idWithContainerId, jsVal);
117         if (!result.second) {
118             result.first->second = jsVal;
119         }
120     }
121 
DeleteFromJsValMapById(const std::string & xcomponentId)122     void DeleteFromJsValMapById(const std::string& xcomponentId)
123     {
124         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
125         auto it = jsValMap_.find(idWithContainerId);
126         if (it == jsValMap_.end()) {
127             return;
128         }
129         jsValMap_.erase(it);
130     }
131 
GetJSVal(const std::string & xcomponentId,JSRef<JSVal> & jsVal)132     bool GetJSVal(const std::string& xcomponentId, JSRef<JSVal>& jsVal)
133     {
134         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
135         auto iter = jsValMap_.find(idWithContainerId);
136         if (iter != jsValMap_.end()) {
137             jsVal = iter->second;
138             jsValMap_.erase(iter);
139             return true;
140         }
141         return false;
142     }
143 
144 private:
145     XComponentClient() = default;
146     std::unordered_map<std::string, RefPtr<JSXComponentController>> jsXComponentControllersMap_;
147     std::unordered_map<std::string, std::pair<RefPtr<OHOS::Ace::NativeXComponentImpl>, OH_NativeXComponent*>>
148         nativeXcomponentsMap_;
149     std::unordered_map<std::string, JSRef<JSVal>> jsValMap_;
150 };
151 
152 class ACE_EXPORT JSXComponent : public JSContainerBase {
153 public:
154     static void JSBind(BindingTarget globalObj);
155     static void Create(const JSCallbackInfo& info);
156     static void JsOnLoad(const JSCallbackInfo& args);
157     static void JsOnDestroy(const JSCallbackInfo& args);
158     static void JsOnAppear(const JSCallbackInfo& args);
159     static void JsOnDisAppear(const JSCallbackInfo& args);
160     static void JsOnAttach(const JSCallbackInfo& args);
161     static void JsOnDetach(const JSCallbackInfo& args);
162 
163     static void JsOnTouch(const JSCallbackInfo& args);
164     static void JsOnClick(const JSCallbackInfo& args);
165     static void JsOnKeyEvent(const JSCallbackInfo& args);
166     static void JsOnMouse(const JSCallbackInfo& args);
167     static void JsOnHover(const JSCallbackInfo& args);
168     static void JsOnFocus(const JSCallbackInfo& args);
169     static void JsOnBlur(const JSCallbackInfo& args);
170 
171     static void JsBackgroundColor(const JSCallbackInfo& args);
172     static void JsBackgroundImage(const JSCallbackInfo& args);
173     static void JsBackgroundImageSize(const JSCallbackInfo& args);
174     static void JsBackgroundImagePosition(const JSCallbackInfo& args);
175     static void JsOpacity(const JSCallbackInfo& args);
176     static void JsBlur(const JSCallbackInfo& args);
177     static void JsBackdropBlur(const JSCallbackInfo& args);
178     static void JsGrayscale(const JSCallbackInfo& args);
179     static void JsBrightness(const JSCallbackInfo& args);
180     static void JsSaturate(const JSCallbackInfo& args);
181     static void JsContrast(const JSCallbackInfo& args);
182     static void JsInvert(const JSCallbackInfo& args);
183     static void JsSepia(const JSCallbackInfo& args);
184     static void JsHueRotate(const JSCallbackInfo& args);
185     static void JsColorBlend(const JSCallbackInfo& args);
186     static void JsSphericalEffect(const JSCallbackInfo& args);
187     static void JsLightUpEffect(const JSCallbackInfo& args);
188     static void JsPixelStretchEffect(const JSCallbackInfo& args);
189     static void JsLinearGradientBlur(const JSCallbackInfo& args);
190     static void JsEnableAnalyzer(bool enable);
191     static void JsRenderFit(const JSCallbackInfo& args);
192     static void JsEnableSecure(const JSCallbackInfo& args);
193     static void JsHdrBrightness(const JSCallbackInfo& args);
194     static void JsBlendMode(const JSCallbackInfo& args);
195     static void JsEnableTransparentLayer(const JSCallbackInfo& args);
196 
197     // For xcomponent node
198     static void* Create(const XComponentParams& params);
199 
200     void RegisterOnCreate(const JsiExecutionContext& execCtx, const Local<JSValueRef>& func);
201     void RegisterOnDestroy(const JsiExecutionContext& execCtx, const Local<JSValueRef>& func);
SetFrameNode(RefPtr<AceType> frameNode)202     void SetFrameNode(RefPtr<AceType> frameNode)
203     {
204         frameNode_ = frameNode;
205     }
GetFrameNode()206     RefPtr<AceType> GetFrameNode() const
207     {
208         return frameNode_;
209     }
210     bool ChangeRenderType(int32_t renderType);
211 
212 private:
213     static void ParseImageAIOptions(const JSRef<JSVal>& jsValue);
214     static void ExtractInfoToXComponentOptions(
215         XComponentOptions& options, JSRef<JSObject>& controllerObj,
216         const JSRef<JSObject>& paramObject, const JSCallbackInfo& info);
217 
218     RefPtr<AceType> frameNode_;
219 };
220 } // namespace OHOS::Ace::Framework
221 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_XCOMPONENT_H
222