• 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/components/xcomponent/xcomponent_component.h"
20 #include "frameworks/bridge/declarative_frontend/engine/functions/js_function.h"
21 #include "frameworks/bridge/declarative_frontend/jsview/js_container_base.h"
22 #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.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 GetCallback = std::function<bool(RefPtr<XComponentComponent> &component)>;
30     using GetJSValCallback = std::function<bool(JSRef<JSVal> &param)>;
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 
RegisterCallback(GetCallback && callback)41     void RegisterCallback(GetCallback &&callback)
42     {
43         getCallback_ = callback;
44     }
45 
RegisterJSValCallback(GetJSValCallback && callback)46     void RegisterJSValCallback(GetJSValCallback &&callback)
47     {
48         getJSValCallback_ = callback;
49     }
50 
SetRegisterCallbackToNull()51     void SetRegisterCallbackToNull()
52     {
53         getCallback_ = nullptr;
54     }
55 
GetXComponent(RefPtr<XComponentComponent> & component)56     bool GetXComponent(RefPtr<XComponentComponent> &component)
57     {
58         if (getCallback_) {
59             return getCallback_(component);
60         } else {
61             return false;
62         }
63     }
64 
SetJSValCallToNull()65     void SetJSValCallToNull()
66     {
67         getJSValCallback_ = nullptr;
68     }
69 
GetJSVal(JSRef<JSVal> & param)70     bool GetJSVal(JSRef<JSVal> &param)
71     {
72         if (getJSValCallback_) {
73             return getJSValCallback_(param);
74         } else {
75             return false;
76         }
77     }
78 
79 private:
80     XComponentClient() = default;
81     GetCallback getCallback_ = nullptr;
82     GetJSValCallback getJSValCallback_ = nullptr;
83 };
84 
85 class ACE_EXPORT JSXComponent : public JSContainerBase {
86 public:
87     static void JSBind(BindingTarget globalObj);
88     static void Create(const JSCallbackInfo& info);
89     static void JsOnLoad(const JSCallbackInfo& args);
90     static void JsOnDestroy(const JSCallbackInfo& args);
91 
92 private:
93     static EventMarker GetEventMarker(const JSCallbackInfo& info, const std::vector<std::string>& keys);
94     static RefPtr<JSXComponentController> jsXComponentController_;
95 };
96 }
97 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_XCOMPONENT_H