1 /*
2  * Copyright (c) 2022-2024 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 #ifndef OHOS_JS_WINDOW_REGISTER_MANAGER_H
16 #define OHOS_JS_WINDOW_REGISTER_MANAGER_H
17 #include <map>
18 #include <mutex>
19 #include "js_window_listener.h"
20 #include "native_engine/native_engine.h"
21 #include "native_engine/native_reference.h"
22 #include "native_engine/native_value.h"
23 #include "refbase.h"
24 #include "window.h"
25 namespace OHOS {
26 namespace Rosen {
27 enum class CaseType {
28     CASE_WINDOW_MANAGER = 0,
29     CASE_WINDOW,
30     CASE_STAGE
31 };
32 
33 enum class ListenerFunctionType : uint32_t {
34     SYSTEM_AVOID_AREA_CHANGE_CB,
35     AVOID_AREA_CHANGE_CB,
36 };
37 
38 class JsWindowRegisterManager {
39 public:
40     JsWindowRegisterManager();
41     ~JsWindowRegisterManager();
42     WmErrorCode RegisterListener(sptr<Window> window, std::string type,
43         CaseType caseType, napi_env env, napi_value callback, napi_value parameter = nullptr);
44     WmErrorCode UnregisterListener(sptr<Window> window, std::string type,
45         CaseType caseType, napi_env env, napi_value value);
46 
47 private:
48     bool IsCallbackRegistered(napi_env env, std::string& type, napi_value jsListenerObject);
49     WmErrorCode ProcessSystemAvoidAreaChangeRegister(sptr<JsWindowListener> listener, sptr<Window> window,
50         bool isRegister, napi_env env, napi_value parameter = nullptr);
51     WmErrorCode ProcessAvoidAreaChangeRegister(sptr<JsWindowListener> listener, sptr<Window> window, bool isRegister,
52         napi_env env, napi_value parameter = nullptr);
53     WmErrorCode ProcessRegisterCallback(ListenerFunctionType listenerFunctionType, CaseType caseType,
54         const sptr<JsWindowListener>& listener, const sptr<Window>& window, bool isRegister, napi_env env,
55         napi_value parameter);
56     std::map<std::string, std::map<std::shared_ptr<NativeReference>, sptr<JsWindowListener>>> jsCbMap_;
57     std::mutex mtx_;
58 };
59 } // namespace Rosen
60 } // namespace OHOS
61 #endif // OHOS_JS_WINDOW_REGISTER_MANAGER_H