• 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 class XComponentClient {
28 public:
29     using GetJSValCallback = std::function<bool(JSRef<JSVal>& param)>;
30     using DeleteCallback = std::function<void()>;
31     XComponentClient& operator=(const XComponentClient&) = delete;
32     XComponentClient(const XComponentClient&) = delete;
33     ~XComponentClient() = default;
34 
GetInstance()35     static XComponentClient& GetInstance()
36     {
37         static XComponentClient instance;
38         return instance;
39     }
40 
GetControllerFromJSXComponentControllersMap(const std::string & xcomponentId)41     RefPtr<JSXComponentController> GetControllerFromJSXComponentControllersMap(const std::string& xcomponentId)
42     {
43         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
44         auto iter = jsXComponentControllersMap_.find(idWithContainerId);
45         if (iter == jsXComponentControllersMap_.end()) {
46             return nullptr;
47         }
48         return iter->second;
49     }
50 
GetNativeXComponentFromXcomponentsMap(const std::string & xcomponentId)51     std::pair<RefPtr<OHOS::Ace::NativeXComponentImpl>, OH_NativeXComponent*> GetNativeXComponentFromXcomponentsMap(
52         const std::string& xcomponentId)
53     {
54         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
55         auto it = nativeXcomponentsMap_.find(idWithContainerId);
56         if (it != nativeXcomponentsMap_.end()) {
57             return it->second;
58         } else {
59             auto nativeXComponentImpl = AceType::MakeRefPtr<NativeXComponentImpl>();
60             auto nativeXComponent = new OH_NativeXComponent(AceType::RawPtr(nativeXComponentImpl));
61             nativeXcomponentsMap_[idWithContainerId] = std::make_pair(nativeXComponentImpl, nativeXComponent);
62             return nativeXcomponentsMap_[idWithContainerId];
63         }
64     }
65 
AddControllerToJSXComponentControllersMap(const std::string & xcomponentId,JSXComponentController * & controller)66     void AddControllerToJSXComponentControllersMap(const std::string& xcomponentId, JSXComponentController*& controller)
67     {
68         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
69         jsXComponentControllersMap_[idWithContainerId] = controller;
70     }
71 
DeleteControllerFromJSXComponentControllersMap(const std::string & xcomponentId)72     void DeleteControllerFromJSXComponentControllersMap(const std::string& xcomponentId)
73     {
74         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
75         jsXComponentControllersMap_.erase(idWithContainerId);
76     }
77 
DeleteFromNativeXcomponentsMapById(const std::string & xcomponentId)78     void DeleteFromNativeXcomponentsMapById(const std::string& xcomponentId)
79     {
80         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
81         auto it = nativeXcomponentsMap_.find(idWithContainerId);
82         if (it == nativeXcomponentsMap_.end()) {
83             return;
84         }
85         if (it->second.second) {
86             delete it->second.second;
87             it->second.second = nullptr;
88         }
89         nativeXcomponentsMap_.erase(it);
90     }
91 
AddJsValToJsValMap(const std::string & xcomponentId,const JSRef<JSVal> & jsVal)92     void AddJsValToJsValMap(const std::string& xcomponentId, const JSRef<JSVal>& jsVal)
93     {
94         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
95         auto result = jsValMap_.try_emplace(idWithContainerId, jsVal);
96         if (!result.second) {
97             result.first->second = jsVal;
98         }
99     }
100 
DeleteFromJsValMapById(const std::string & xcomponentId)101     void DeleteFromJsValMapById(const std::string& xcomponentId)
102     {
103         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
104         auto it = jsValMap_.find(idWithContainerId);
105         if (it == jsValMap_.end()) {
106             return;
107         }
108         jsValMap_.erase(it);
109     }
110 
GetJSVal(const std::string & xcomponentId,JSRef<JSVal> & jsVal)111     bool GetJSVal(const std::string& xcomponentId, JSRef<JSVal>& jsVal)
112     {
113         auto idWithContainerId = xcomponentId + std::to_string(Container::CurrentId());
114         auto iter = jsValMap_.find(idWithContainerId);
115         if (iter != jsValMap_.end()) {
116             jsVal = iter->second;
117             jsValMap_.erase(iter);
118             return true;
119         }
120         return false;
121     }
122 
123 private:
124     XComponentClient() = default;
125     std::unordered_map<std::string, RefPtr<JSXComponentController>> jsXComponentControllersMap_;
126     std::unordered_map<std::string, std::pair<RefPtr<OHOS::Ace::NativeXComponentImpl>, OH_NativeXComponent*>>
127         nativeXcomponentsMap_;
128     std::unordered_map<std::string, JSRef<JSVal>> jsValMap_;
129 };
130 
131 class ACE_EXPORT JSXComponent : public JSContainerBase {
132 public:
133     static void JSBind(BindingTarget globalObj);
134     static void Create(const JSCallbackInfo& info);
135     static void JsOnLoad(const JSCallbackInfo& args);
136     static void JsOnDestroy(const JSCallbackInfo& args);
137     static void OmitEvent(const JSCallbackInfo& args);
138 };
139 } // namespace OHOS::Ace::Framework
140 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_XCOMPONENT_H