1 /*
2 * Copyright (c) 2025-2025 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 "js_fb_utils.h"
17
18 #include <string>
19 #include "window.h"
20 #include "wm_common.h"
21 #include "window_manager_hilog.h"
22
23 namespace OHOS {
24 namespace Rosen {
25 using namespace AbilityRuntime;
NapiGetUndefined(napi_env env)26 napi_value NapiGetUndefined(napi_env env)
27 {
28 napi_value result = nullptr;
29 napi_get_undefined(env, &result);
30 return result;
31 }
32
NapiThrowInvalidParam(napi_env env,const std::string & msg)33 napi_value NapiThrowInvalidParam(napi_env env, const std::string& msg)
34 {
35 napi_throw(env, AbilityRuntime::CreateJsError(env,
36 static_cast<int32_t>(WmErrorCode::WM_ERROR_FB_PARAM_INVALID), msg));
37 return NapiGetUndefined(env);
38 }
39
GetType(napi_env env,napi_value value)40 napi_valuetype GetType(napi_env env, napi_value value)
41 {
42 napi_valuetype res = napi_undefined;
43 napi_typeof(env, value, &res);
44 return res;
45 }
46
NapiIsCallable(napi_env env,napi_value value)47 bool NapiIsCallable(napi_env env, napi_value value)
48 {
49 bool result = false;
50 napi_is_callable(env, value, &result);
51 return result;
52 }
53
SetEnumProperty(napi_env env,napi_value & obj,const std::string & enumName,int32_t enumValue)54 static napi_status SetEnumProperty(napi_env env, napi_value& obj, const std::string& enumName, int32_t enumValue)
55 {
56 napi_value value = nullptr;
57 napi_status ret = napi_create_int32(env, enumValue, &value);
58 if (ret != napi_ok) {
59 return ret;
60 }
61 ret = napi_set_named_property(env, obj, enumName.c_str(), value);
62 if (ret != napi_ok) {
63 return ret;
64 }
65 return ret;
66 }
67
ExportFloatingBallState(napi_env env,napi_callback_info info)68 static napi_value ExportFloatingBallState(napi_env env, napi_callback_info info)
69 {
70 napi_value result = nullptr;
71 napi_create_object(env, &result);
72 if (result == nullptr) {
73 TLOGE(WmsLogTag::DEFAULT, "Failed to get object");
74 return nullptr;
75 }
76 (void)SetEnumProperty(env, result, "STARTED", static_cast<int32_t>(FloatingBallState::STARTED));
77 (void)SetEnumProperty(env, result, "STOPPED", static_cast<int32_t>(FloatingBallState::STOPPED));
78 napi_object_freeze(env, result);
79 return result;
80 }
81
ExportFloatingBallTemplate(napi_env env,napi_callback_info info)82 static napi_value ExportFloatingBallTemplate(napi_env env, napi_callback_info info)
83 {
84 napi_value result = nullptr;
85 napi_create_object(env, &result);
86 if (result == nullptr) {
87 TLOGE(WmsLogTag::DEFAULT, "Failed to get object");
88 return nullptr;
89 }
90 (void)SetEnumProperty(env, result, "STATIC", static_cast<int32_t>(FloatingBallTemplate::STATIC));
91 (void)SetEnumProperty(env, result, "NORMAL", static_cast<int32_t>(FloatingBallTemplate::NORMAL));
92 (void)SetEnumProperty(env, result, "EMPHATIC", static_cast<int32_t>(FloatingBallTemplate::EMPHATIC));
93 (void)SetEnumProperty(env, result, "SIMPLE", static_cast<int32_t>(FloatingBallTemplate::SIMPLE));
94 napi_object_freeze(env, result);
95 return result;
96 }
97
InitFbEnums(napi_env env,napi_value exports)98 napi_status InitFbEnums(napi_env env, napi_value exports)
99 {
100 const napi_property_descriptor properties[] = {
101 DECLARE_NAPI_GETTER("FloatingBallState", ExportFloatingBallState),
102 DECLARE_NAPI_GETTER("FloatingBallTemplate", ExportFloatingBallTemplate),
103 };
104 const size_t count = sizeof(properties) / sizeof(napi_property_descriptor);
105 return napi_define_properties(env, exports, count, properties);
106 }
107
108 } // Rosen
109 } // OHOS