1 /* 2 * Copyright (c) 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 OHOS_FORM_FWK_JS_FORM_HOST_H 17 #define OHOS_FORM_FWK_JS_FORM_HOST_H 18 19 #include "event_handler.h" 20 #include "form_host_delegate_stub.h" 21 #include "js_runtime_utils.h" 22 #include "napi/native_api.h" 23 #include "napi/native_common.h" 24 #include "napi/native_node_api.h" 25 #include "napi_common_want.h" 26 #include "form_instance.h" 27 28 namespace OHOS { 29 namespace AbilityRuntime { 30 napi_value JsFormHostInit(napi_env env, napi_value exportObj); 31 32 class FormRouterProxyCallbackClient : public std::enable_shared_from_this<FormRouterProxyCallbackClient> { 33 public: 34 FormRouterProxyCallbackClient(napi_env env, napi_ref callbackRef); 35 36 ~FormRouterProxyCallbackClient(); 37 38 void ProcessFormRouterProxy(const OHOS::AAFwk::Want &want); 39 40 private: 41 std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr; 42 napi_ref callbackRef_ = nullptr; 43 napi_env env_; 44 }; 45 46 /* formId:the Id of form. 47 * overflowInfo: overflow information, including overflow area and overflow duration. 48 * isOverflow: whether overflow, true means request overflow, false means cancel overflow. 49 * state:activation state, 1 means activate, 0 means deactivate. 50 * condition:condition variable for thread synchronization 51 * mutex:mutex locks for shared resources. 52 * isReady:used to indicate whether the asynchronous operation is completed or not. 53 * result:the result of the operation. 54 */ 55 typedef struct LiveFormInterfaceParam { 56 std::string formId; 57 AppExecFwk::OverflowInfo overflowInfo; 58 bool isOverflow = true; 59 int32_t state; 60 std::condition_variable condition; 61 std::mutex mutex; 62 bool isReady = false; 63 bool result = false; 64 AppExecFwk::Rect formRect; 65 std::unordered_map<std::string, std::string> liveFormStatusMap; 66 } LiveFormInterfaceParam; 67 68 class JsFormRouterProxyMgr : public AppExecFwk::FormHostDelegateStub { 69 public: 70 JsFormRouterProxyMgr() = default; 71 72 virtual ~JsFormRouterProxyMgr() = default; 73 74 static sptr<JsFormRouterProxyMgr> GetInstance(); 75 76 void AddFormRouterProxyCallback(napi_env env, napi_value callback, const std::vector<int64_t> &formIds); 77 78 void RemoveFormRouterProxyCallback(const std::vector<int64_t> &formIds); 79 80 ErrCode RouterEvent(const int64_t formId, const OHOS::AAFwk::Want &want); 81 82 bool RegisterOverflowListener(napi_env env, napi_ref callback); 83 84 bool UnregisterOverflowListener(); 85 86 bool RegisterChangeSceneAnimationStateListener(napi_env env, napi_ref callback); 87 88 bool UnregisterChangeSceneAnimationStateListener(); 89 90 bool RegisterGetFormRectListener(napi_env env, napi_ref callback); 91 92 bool UnregisterGetFormRectListener(); 93 94 bool RegisterGetLiveFormStatusListener(napi_env env, napi_ref callback); 95 96 bool UnregisterGetLiveFormStatusListener(); 97 private: 98 static std::mutex mutex_; 99 static sptr<JsFormRouterProxyMgr> instance_; 100 mutable std::mutex FormRouterProxyCallbackMutex_; 101 102 std::map<int64_t, std::shared_ptr<FormRouterProxyCallbackClient>> formRouterProxyCallbackMap_; 103 std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr; 104 DISALLOW_COPY_AND_MOVE(JsFormRouterProxyMgr); 105 napi_ref overflowRegisterCallback_ = nullptr; 106 napi_env overflowEnv_; 107 napi_ref changeSceneAnimationStateRigisterCallback_ = nullptr; 108 napi_env changeSceneAnimationStateEnv_; 109 ErrCode RequestOverflow(const int64_t formId, const AppExecFwk::OverflowInfo &overflowInfo, bool isOverflow = true); 110 void CreateFormOverflowInfo(napi_env env, AppExecFwk::OverflowInfo &overflowInfo, napi_value* result); 111 void RequestOverflowInner(LiveFormInterfaceParam* dataParam); 112 ErrCode ChangeSceneAnimationState(const int64_t formId, int32_t state); 113 void ChangeSceneAnimationStateInner(LiveFormInterfaceParam* dataParam); 114 napi_ref getFormRectCallbackRef_ = nullptr; 115 napi_env getFormRectEnv_; 116 ErrCode GetFormRect(const int64_t formId, AppExecFwk::Rect &rect); 117 void GetFormRectInner(LiveFormInterfaceParam* dataParam); 118 void CallPromise(napi_value funcResult, LiveFormInterfaceParam *liveFormInterfaceParam); 119 static napi_value PromiseCallback(napi_env env, napi_callback_info info); 120 static bool ConvertFunctionResult(napi_env env, napi_value funcResult, AppExecFwk::Rect &rect); 121 122 napi_ref getLiveFormStatusCallbackRef_ = nullptr; 123 napi_env getLiveFormStatusEnv_; 124 ErrCode GetLiveFormStatus(std::unordered_map<std::string, std::string> &liveFormStatusMap); 125 void GetLiveFormStatusInner(LiveFormInterfaceParam *dataParam); 126 bool ConvertNapiValueToMap(napi_env env, napi_value value, std::unordered_map<std::string, std::string> &uMap); 127 }; 128 129 class PromiseCallbackInfo { 130 public: 131 static PromiseCallbackInfo* Create(LiveFormInterfaceParam* liveFormInterfaceParam); 132 133 static void Destroy(PromiseCallbackInfo* callbackInfo); 134 135 LiveFormInterfaceParam* GetJsCallBackParam(); 136 137 private: 138 explicit PromiseCallbackInfo(LiveFormInterfaceParam* liveFormInterfaceParam); 139 140 ~PromiseCallbackInfo(); 141 142 LiveFormInterfaceParam* liveFormInterfaceParam_ = nullptr; 143 }; 144 } // namespace AbilityRuntime 145 } // namespace OHOS 146 #endif /* OHOS_FORM_FWK_JS_FORM_HOST_H */ 147 148