• 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_ENGINE_FUNCTION_JS_FUNCTION_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_FUNCTION_JS_FUNCTION_H
18 
19 #include <functional>
20 
21 #include "base/memory/ace_type.h"
22 #include "frameworks/bridge/declarative_frontend/engine/bindings.h"
23 #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h"
24 #include "frameworks/bridge/declarative_frontend/engine/js_execution_scope_defines.h"
25 #if defined(XCOMPONENT_SUPPORTED)
26 #include "frameworks/bridge/declarative_frontend/jsview/js_xcomponent_controller.h"
27 #endif
28 
29 namespace OHOS::Ace::Framework {
30 
31 class ACE_EXPORT JsFunction : public virtual AceType {
32     DECLARE_ACE_TYPE(JsFunction, AceType);
33 
34 public:
35     explicit JsFunction(const JSRef<JSFunc>& jsFunction);
36     JsFunction(const JSRef<JSObject>& jsObject, const JSRef<JSFunc>& jsFunction);
37     ~JsFunction() override;
38     void Execute();
39     void Execute(const JSRef<JSObject>& jsParamsObject);
40     void Execute(const std::vector<std::string>& keys, const std::string& param);
41     void ExecuteNew(const std::vector<std::string>& keys, const std::string& param);
42     JSRef<JSVal> ExecuteJS(int argc = 0, JSRef<JSVal>* argv = nullptr);
43 
44 protected:
45     JSRef<JSFunc> jsFunction_;
46     JSWeak<JSVal> jsThis_;
47 };
48 
49 template<class T, int32_t ARGC = 0>
50 class ACE_EXPORT JsEventFunction : public JsFunction {
51     DECLARE_ACE_TYPE(JsEventFunction, JsFunction);
52 
53 public:
54     using ParseFunc = std::function<JSRef<JSVal>(const T&)>;
55     JsEventFunction() = delete;
JsEventFunction(const JSRef<JSFunc> & jsFunction,ParseFunc parser)56     JsEventFunction(const JSRef<JSFunc>& jsFunction, ParseFunc parser)
57         : JsFunction(JSRef<JSObject>(), jsFunction), parser_(parser)
58     {}
59     ~JsEventFunction() override = default;
60 
Execute()61     void Execute()
62     {
63         JsFunction::ExecuteJS();
64     }
65 
Execute(const T & eventInfo)66     void Execute(const T& eventInfo)
67     {
68         JSRef<JSVal> param;
69         if (parser_) {
70             param = parser_(eventInfo);
71         }
72         JsFunction::ExecuteJS(ARGC, &param);
73     }
74 
ExecuteWithValue(const T & eventInfo)75     JSRef<JSVal> ExecuteWithValue(const T& eventInfo)
76     {
77         JSRef<JSVal> param;
78         if (parser_) {
79             param = parser_(eventInfo);
80         }
81         return JsFunction::ExecuteJS(ARGC, &param);
82     }
83 
84 private:
85     ParseFunc parser_;
86 };
87 
88 JSRef<JSObject> CreateEventTargetObject(const BaseEventInfo& info);
89 
90 } // namespace OHOS::Ace::Framework
91 
92 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_FUNCTION_JS_FUNCTION_H
93