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 std::vector<std::string>& keys, const std::string& param); 40 #if defined(XCOMPONENT_SUPPORTED) 41 void ExecuteNew(const std::vector<std::string>& keys, const std::string& param, 42 RefPtr<JSXComponentController>& jsXComponentController); 43 #endif 44 JSRef<JSVal> ExecuteJS(int argc = 0, JSRef<JSVal>* argv = nullptr); 45 46 protected: 47 JSRef<JSFunc> jsFunction_; 48 JSWeak<JSVal> jsThis_; 49 }; 50 51 template<class T, int32_t ARGC = 0> 52 class ACE_EXPORT JsEventFunction : public JsFunction { 53 DECLARE_ACE_TYPE(JsEventFunction, JsFunction); 54 55 public: 56 using ParseFunc = std::function<JSRef<JSVal>(const T&)>; 57 JsEventFunction() = delete; JsEventFunction(const JSRef<JSFunc> & jsFunction,ParseFunc parser)58 JsEventFunction(const JSRef<JSFunc>& jsFunction, ParseFunc parser) 59 : JsFunction(JSRef<JSObject>(), jsFunction), parser_(parser) 60 {} 61 ~JsEventFunction() override = default; 62 Execute()63 void Execute() 64 { 65 JsFunction::ExecuteJS(); 66 } 67 Execute(const T & eventInfo)68 void Execute(const T& eventInfo) 69 { 70 JSRef<JSVal> param; 71 if (parser_) { 72 param = parser_(eventInfo); 73 } 74 JsFunction::ExecuteJS(ARGC, ¶m); 75 } 76 ExecuteWithValue(const T & eventInfo)77 JSRef<JSVal> ExecuteWithValue(const T& eventInfo) 78 { 79 JSRef<JSVal> param; 80 if (parser_) { 81 param = parser_(eventInfo); 82 } 83 return JsFunction::ExecuteJS(ARGC, ¶m); 84 } 85 86 private: 87 ParseFunc parser_; 88 }; 89 90 JSRef<JSObject> CreateEventTargetObject(const BaseEventInfo& info); 91 92 } // namespace OHOS::Ace::Framework 93 94 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_FUNCTION_JS_FUNCTION_H 95