1 /* 2 * Copyright (c) 2023 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_UI_EXTENSION_H 17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_UI_EXTENSION_H 18 19 #include "bridge/declarative_frontend/jsview/js_interactable_view.h" 20 #include "bridge/declarative_frontend/jsview/js_view_abstract.h" 21 #include "core/components_ng/pattern/ui_extension/ui_extension_component/ui_extension_proxy.h" 22 #include "core/components_ng/pattern/ui_extension/ui_extension_config.h" 23 24 namespace OHOS::Ace::Framework { 25 class JSUIExtension : public JSViewAbstract, public JSInteractableView { 26 public: 27 static void JSBind(BindingTarget globalObj); 28 static void Create(const JSCallbackInfo& info); 29 static void OnRemoteReady(const JSCallbackInfo& info); 30 static void OnReceive(const JSCallbackInfo& info); 31 static void OnRelease(const JSCallbackInfo& info); 32 static void OnResult(const JSCallbackInfo& info); 33 static void OnError(const JSCallbackInfo& info); 34 static void OnTerminated(const JSCallbackInfo& info); 35 static void OnDrawReady(const JSCallbackInfo& info); 36 private: 37 static void ResolveAreaPlaceholderParams(const JSRef<JSObject>& obj, 38 std::map<NG::PlaceholderType, RefPtr<NG::FrameNode>>& placeholderMap); 39 }; 40 41 enum class RegisterType { 42 SYNC = 0, 43 ASYNC, 44 UNKNOWN 45 }; 46 47 using CallbackFuncPairList = std::list<std::pair<napi_ref, std::function<void(const RefPtr<NG::UIExtensionProxy>&)>>>; 48 49 class JSUIExtensionProxy : public Referenced { 50 public: 51 JSUIExtensionProxy() = default; 52 ~JSUIExtensionProxy() override = default; 53 static void JSBind(BindingTarget globalObj); 54 void Send(const JSCallbackInfo& info); 55 void SendSync(const JSCallbackInfo& info); 56 void On(const JSCallbackInfo& info); 57 void Off(const JSCallbackInfo& info); 58 void SetProxy(const RefPtr<NG::UIExtensionProxy>& proxy); 59 void SetInstanceId(int32_t instanceId); 60 private: 61 static void Constructor(const JSCallbackInfo& info); 62 static void Destructor(JSUIExtensionProxy* uiExtensionProxy); 63 64 CallbackFuncPairList::const_iterator FindCbList(napi_env env, napi_value cb, 65 CallbackFuncPairList& callbackFuncPairList); 66 void AddCallbackToList(napi_env env, napi_value cb, napi_handle_scope scope, RegisterType type, 67 const std::function<void(const RefPtr<NG::UIExtensionProxy>&)>&& onFunc); 68 void DeleteCallbackFromList(uint32_t argc, napi_env env, napi_value cb, RegisterType type); 69 std::list<std::function<void(const RefPtr<NG::UIExtensionProxy>&)>> GetOnFuncList(RegisterType type); 70 RegisterType GetRegisterType(const std::string& strType); 71 72 RefPtr<NG::UIExtensionProxy> proxy_; 73 int32_t instanceId_ = INSTANCE_ID_UNDEFINED; 74 CallbackFuncPairList onSyncOnCallbackList_; 75 CallbackFuncPairList onAsyncOnCallbackList_; 76 std::mutex callbackLisLock_; 77 }; 78 } // namespace OHOS::Ace::Framework 79 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_UI_EXTENSION_H 80