• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 OHOS_FORM_FWK_JS_FORM_STATE_OBSERVER_H
17 #define OHOS_FORM_FWK_JS_FORM_STATE_OBSERVER_H
18 
19 #include <map>
20 #include <mutex>
21 #include <vector>
22 #include <singleton.h>
23 #include "event_handler.h"
24 #include "form_mgr_interface.h"
25 #include "form_mgr.h"
26 #include "js_form_state_observer_stub.h"
27 #include "js_runtime_utils.h"
28 #include "napi/native_api.h"
29 #include "napi/native_common.h"
30 #include "napi/native_node_api.h"
31 #include "running_form_info.h"
32 
33 namespace OHOS {
34 namespace AbilityRuntime {
35 class FormEventCallbackList : public std::enable_shared_from_this<FormEventCallbackList> {
36 public:
FormEventCallbackList(const std::string & bundle,const std::string & type,napi_env env)37     FormEventCallbackList(const std::string &bundle, const std::string &type, napi_env env)
38         : bindHostBundleName(bundle), eventType_(type), env_(env)
39     {}
40     ~FormEventCallbackList();
41     bool ContainEqualCallback(napi_value callback) const;
42 
IsEmpty()43     bool IsEmpty() const
44     {
45         return callbacks_.empty();
46     }
47     void PushCallback(napi_value call);
48 
49     void RemoveCallback(napi_value call);
50 
51     void HandleFormEvent(const AppExecFwk::RunningFormInfo &runningFormInfo) const;
52 
BindHostBundleName()53     std::string BindHostBundleName() const
54     {
55         return bindHostBundleName;
56     }
57 
BindFormEventType()58     std::string BindFormEventType() const
59     {
60         return eventType_;
61     }
62 
63 private:
64     std::string bindHostBundleName;
65     std::string eventType_;
66     std::vector<napi_ref> callbacks_;
67     napi_env env_ = nullptr;
68 };
69 
70 class FormAddCallbackClient : public std::enable_shared_from_this<FormAddCallbackClient> {
71 public:
72     FormAddCallbackClient(napi_env env, napi_ref callbackRef);
73 
74     ~FormAddCallbackClient();
75 
76     void ProcessFormAdd(const std::string &bundleName, const AppExecFwk::RunningFormInfo &runningFormInfo);
77 
78     bool IsStrictEqual(napi_value callback);
79 
80 private:
81     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
82     napi_ref callbackRef_ {};
83     napi_env env_;
84 };
85 
86 class FormRemoveCallbackClient : public std::enable_shared_from_this<FormRemoveCallbackClient> {
87 public:
88     FormRemoveCallbackClient(napi_env env, napi_ref callbackRef);
89 
90     ~FormRemoveCallbackClient();
91 
92     void ProcessFormRemove(const std::string &bundleName, const AppExecFwk::RunningFormInfo &runningFormInfo);
93 
94     bool IsStrictEqual(napi_value callback);
95 
96 private:
97     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
98     napi_ref callbackRef_ = nullptr;
99     napi_env env_;
100 };
101 
102 class JsFormStateObserver : public JsFormStateObserverStub,
103                             public DelayedRefSingleton<JsFormStateObserver> {
104 public:
105     JsFormStateObserver() = default;
106 
107     virtual ~JsFormStateObserver() = default;
108 
109     static sptr<JsFormStateObserver> GetInstance();
110 
111     bool RegisterFormAddCallback(const napi_env env, const std::string &bundleName, const napi_value callback);
112 
113     bool RegisterFormRemoveCallback(const napi_env env, const std::string &bundleName, const napi_value callback);
114 
115     void ClearFormAddCallbackByBundle(const std::string &bundleName);
116 
117     void DelFormAddCallbackByBundle(const napi_value callback, const std::string &bundleName);
118 
119     void ClearFormRemoveCallbackByBundle(const std::string &bundleName);
120 
121     void DelFormRemoveCallbackByBundle(const napi_value callback, const std::string &bundleName);
122 
123     int32_t OnAddForm(const std::string &bundleName, const AppExecFwk::RunningFormInfo &runningFormInfo);
124 
125     int32_t OnRemoveForm(const std::string &bundleName, const AppExecFwk::RunningFormInfo &runningFormInfo);
126 
127     bool CheckMapSize(const std::string &type, const std::string &bundleName);
128 
129     int RegisterFormInstanceCallback(napi_env env, napi_value jsObserverObject,
130         bool isVisibility, std::string &bundleName, sptr<JsFormStateObserver> &formObserver);
131 
132     int32_t NotifyWhetherFormsVisible(const AppExecFwk::FormVisibilityType visibleType, const std::string &bundleName,
133         std::vector<AppExecFwk::FormInstance> &runningFormInfos);
134 
135     ErrCode ClearFormNotifyVisibleCallbackByBundle(const std::string bundleName, bool isVisibility,
136         sptr<JsFormStateObserver> &formObserver);
137 
138     ErrCode DelFormNotifyVisibleCallbackByBundle(const std::string bundleName, bool isVisibility,
139         napi_value jsObserverObject, sptr<JsFormStateObserver> &formObserver);
140 
141     ErrCode OnFormClickEvent(const std::string &bundleName, const std::string &callType,
142         const AppExecFwk::RunningFormInfo &runningFormInfo);
143 
144     ErrCode RegisterClickEventCallback(const napi_env env, const std::string &bundleName,
145         const napi_value callback, const std::string &type);
146 
147     ErrCode ClearFormClickCallbackByBundleName(const std::string &type, const std::string &bundleName);
148 
149     ErrCode ClearFormClickCallback(const std::string &type, const std::string &bundleName, const napi_value &callback);
150 
151 private:
152     static std::mutex mutex_;
153     static sptr<JsFormStateObserver> instance_;
154     mutable std::mutex addFormCallbackMutex_;
155     mutable std::mutex removeFormCallbackMutex_;
156     mutable std::mutex formIsvisibleCallbackMutex_;
157     mutable std::mutex formEventMapMutex_;
158 
159     std::map<std::string, std::vector<std::shared_ptr<FormAddCallbackClient>>> formAddCallbackMap_;
160     std::map<std::string, std::vector<std::shared_ptr<FormRemoveCallbackClient>>> formRemoveCallbackMap_;
161     std::map<std::string, std::shared_ptr<NativeReference>> formVisibleCallbackMap_;
162     std::map<std::string, std::shared_ptr<NativeReference>> formInvisibleCallbackMap_;
163     std::unordered_map<std::string, std::vector<std::shared_ptr<FormEventCallbackList>>> formEventMap_;;
164     napi_env env_ = nullptr;
165     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
166 };
167 }  // namespace AbilityRuntime
168 }  // namespace OHOS
169 #endif  // OHOS_FORM_FWK_JS_FORM_STATE_OBSERVER_H
170