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 #include "ui_observer_listener.h"
17
18 namespace OHOS::Ace::Napi {
OnNavigationStateChange(const std::string & navigationId,const std::string & navDestinationName,NG::NavDestinationState navState)19 void UIObserverListener::OnNavigationStateChange(
20 const std::string& navigationId, const std::string& navDestinationName, NG::NavDestinationState navState)
21 {
22 if (!env_ || !callback_) {
23 TAG_LOGW(AceLogTag::ACE_OBSERVER,
24 "Handle navDestination state change failed, runtime or callback function invalid!");
25 return;
26 }
27 napi_value callback = nullptr;
28 napi_get_reference_value(env_, callback_, &callback);
29 napi_value objValue = nullptr;
30 napi_create_object(env_, &objValue);
31 napi_value id = nullptr;
32 napi_value name = nullptr;
33 napi_value state = nullptr;
34 napi_create_string_utf8(env_, navigationId.c_str(), navigationId.length(), &id);
35 napi_create_string_utf8(env_, navDestinationName.c_str(), navDestinationName.length(), &name);
36 napi_create_int32(env_, static_cast<int32_t>(navState), &state);
37 napi_set_named_property(env_, objValue, "navigationId", id);
38 napi_set_named_property(env_, objValue, "name", name);
39 napi_set_named_property(env_, objValue, "state", state);
40 napi_value argv[] = { objValue };
41 napi_call_function(env_, nullptr, callback, 1, argv, nullptr);
42 }
43
OnRouterPageStateChange(napi_value context,int32_t index,const std::string & name,const std::string & path,NG::RouterPageState state)44 void UIObserverListener::OnRouterPageStateChange(napi_value context, int32_t index,
45 const std::string& name, const std::string& path, NG::RouterPageState state)
46 {
47 if (!env_ || !callback_) {
48 TAG_LOGW(AceLogTag::ACE_OBSERVER,
49 "Handle router page state change failed, runtime or callback function invalid!");
50 return;
51 }
52 napi_value callback = nullptr;
53 napi_get_reference_value(env_, callback_, &callback);
54 napi_value objValue = nullptr;
55 napi_create_object(env_, &objValue);
56 napi_value napiCtx = context;
57 napi_value napiIndex = nullptr;
58 napi_value napiName = nullptr;
59 napi_value napiPath = nullptr;
60 napi_value napiState = nullptr;
61 napi_create_int32(env_, index, &napiIndex);
62 napi_create_string_utf8(env_, name.c_str(), name.length(), &napiName);
63 napi_create_string_utf8(env_, path.c_str(), path.length(), &napiPath);
64 napi_create_int32(env_, static_cast<int32_t>(state), &napiState);
65 napi_set_named_property(env_, objValue, "context", napiCtx);
66 napi_set_named_property(env_, objValue, "index", napiIndex);
67 napi_set_named_property(env_, objValue, "name", napiName);
68 napi_set_named_property(env_, objValue, "path", napiPath);
69 napi_set_named_property(env_, objValue, "state", napiState);
70 napi_value argv[] = { objValue };
71 napi_call_function(env_, nullptr, callback, 1, argv, nullptr);
72 }
73
GetNapiCallback()74 napi_value UIObserverListener::GetNapiCallback()
75 {
76 napi_value callback = nullptr;
77 napi_get_reference_value(env_, callback_, &callback);
78 return callback;
79 }
80
NapiEqual(napi_value cb)81 bool UIObserverListener::NapiEqual(napi_value cb)
82 {
83 bool isEquals = false;
84 napi_strict_equals(env_, cb, GetNapiCallback(), &isEquals);
85 return isEquals;
86 }
87 } // namespace OHOS::Ace::Napi
88