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 
27 namespace OHOS {
28 namespace AbilityRuntime {
29 napi_value JsFormHostInit(napi_env env, napi_value exportObj);
30 
31 class FormRouterProxyCallbackClient : public std::enable_shared_from_this<FormRouterProxyCallbackClient> {
32 public:
33     FormRouterProxyCallbackClient(napi_env env, napi_ref callbackRef);
34 
35     ~FormRouterProxyCallbackClient();
36 
37     void ProcessFormRouterProxy(const OHOS::AAFwk::Want &want);
38 
39 private:
40     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
41     napi_ref callbackRef_ = nullptr;
42     napi_env env_;
43 };
44 
45 class JsFormRouterProxyMgr : public AppExecFwk::FormHostDelegateStub {
46 public:
47     JsFormRouterProxyMgr() = default;
48 
49     virtual ~JsFormRouterProxyMgr() = default;
50 
51     static sptr<JsFormRouterProxyMgr> GetInstance();
52 
53     void AddFormRouterProxyCallback(napi_env env, napi_value callback, const std::vector<int64_t> &formIds);
54 
55     void RemoveFormRouterProxyCallback(const std::vector<int64_t> &formIds);
56 
57     ErrCode RouterEvent(const int64_t formId, const OHOS::AAFwk::Want &want);
58 
59 private:
60     static std::mutex mutex_;
61     static sptr<JsFormRouterProxyMgr> instance_;
62     mutable std::mutex FormRouterProxyCallbackMutex_;
63 
64     std::map<int64_t, std::shared_ptr<FormRouterProxyCallbackClient>> formRouterProxyCallbackMap_;
65     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
66     DISALLOW_COPY_AND_MOVE(JsFormRouterProxyMgr);
67 };
68 } // namespace AbilityRuntime
69 } // namespace OHOS
70 #endif /* OHOS_FORM_FWK_JS_FORM_HOST_H */
71 
72