1 /*
2 * Copyright (c) 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
16 #include "event_handler.h"
17 #include "event_runner.h"
18 #include "js_window_listener.h"
19 #include "js_runtime_utils.h"
20 #include "window_manager_hilog.h"
21
22 namespace OHOS {
23 namespace Rosen {
24 using namespace AbilityRuntime;
25 namespace {
26 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "JsListener"};
27 }
28
~JsWindowListener()29 JsWindowListener::~JsWindowListener()
30 {
31 WLOGI("[NAPI]~JsWindowListener");
32 }
33
SetMainEventHandler()34 void JsWindowListener::SetMainEventHandler()
35 {
36 auto mainRunner = AppExecFwk::EventRunner::GetMainEventRunner();
37 if (mainRunner == nullptr) {
38 return;
39 }
40 eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(mainRunner);
41 }
42
CallJsMethod(const char * methodName,napi_value const * argv,size_t argc)43 void JsWindowListener::CallJsMethod(const char* methodName, napi_value const* argv, size_t argc)
44 {
45 WLOGFD("[NAPI]methodName = %{public}s", methodName);
46 if (env_ == nullptr || jsCallBack_ == nullptr) {
47 WLOGFE("[NAPI]env_ nullptr or jsCallBack_ is nullptr");
48 return;
49 }
50 napi_value method = jsCallBack_->GetNapiValue();
51 if (method == nullptr) {
52 WLOGFE("[NAPI]Failed to get method callback from object");
53 return;
54 }
55 napi_value result = nullptr;
56 napi_get_undefined(env_, &result);
57 napi_call_function(env_, result, method, argc, argv, nullptr);
58 }
59
OnAvoidAreaChanged(const AvoidArea avoidArea,AvoidAreaType type)60 void JsWindowListener::OnAvoidAreaChanged(const AvoidArea avoidArea, AvoidAreaType type)
61 {
62 WLOGFD("[NAPI]type = %{public}d", type);
63 // js callback should run in js thread
64 std::unique_ptr<NapiAsyncTask::CompleteCallback> complete = std::make_unique<NapiAsyncTask::CompleteCallback> (
65 [self = weakRef_, avoidArea, type, eng = env_] (napi_env env,
66 NapiAsyncTask& task, int32_t status) {
67 auto thisListener = self.promote();
68 if (thisListener == nullptr || eng == nullptr) {
69 WLOGFE("[NAPI]this listener or eng is nullptr");
70 return;
71 }
72 napi_value avoidAreaValue = ConvertAvoidAreaToJsValue(env, avoidArea, type);
73 if (avoidAreaValue == nullptr) {
74 WLOGFE("Convert AvoidArea To JsValue failed");
75 return;
76 }
77 if (thisListener->isDeprecatedInterface_) {
78 napi_value argv[] = { avoidAreaValue };
79 thisListener->CallJsMethod(SYSTEM_AVOID_AREA_CHANGE_CB.c_str(), argv, ArraySize(argv));
80 } else {
81 napi_value objValue = nullptr;
82 napi_create_object(env, &objValue);
83 if (objValue == nullptr) {
84 WLOGFE("Failed to get object");
85 return;
86 }
87 napi_set_named_property(env, objValue, "type", CreateJsValue(env, static_cast<uint32_t>(type)));
88 napi_set_named_property(env, objValue, "area", avoidAreaValue);
89 napi_value argv[] = { objValue };
90 thisListener->CallJsMethod(AVOID_AREA_CHANGE_CB.c_str(), argv, ArraySize(argv));
91 }
92 }
93 );
94
95 napi_ref callback = nullptr;
96 std::unique_ptr<NapiAsyncTask::ExecuteCallback> execute = nullptr;
97 NapiAsyncTask::Schedule("JsWindowListener::OnAvoidAreaChanged",
98 env_, std::make_unique<NapiAsyncTask>(callback, std::move(execute), std::move(complete)));
99 }
100
101 } // namespace Rosen
102 } // namespace OHOS