1 /*
2  * Copyright (c) 2023-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 "js_extension_window_utils.h"
17 
18 #include "js_window_utils.h"
19 #include "js_extension_window.h"
20 #include "window_manager_hilog.h"
21 
22 namespace OHOS {
23 namespace Rosen {
24 using namespace  AbilityRuntime;
25 
GetRectAndConvertToJsValue(napi_env env,const Rect & rect)26 napi_value GetRectAndConvertToJsValue(napi_env env, const Rect& rect)
27 {
28     napi_value objValue = nullptr;
29     napi_create_object(env, &objValue);
30     if (objValue == nullptr) {
31         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to convert rect to jsObject");
32         return nullptr;
33     }
34     napi_set_named_property(env, objValue, "left", CreateJsValue(env, rect.posX_));
35     napi_set_named_property(env, objValue, "top", CreateJsValue(env, rect.posY_));
36     napi_set_named_property(env, objValue, "width", CreateJsValue(env, rect.width_));
37     napi_set_named_property(env, objValue, "height", CreateJsValue(env, rect.height_));
38     return objValue;
39 }
40 
ConvertAvoidAreaToJsValue(napi_env env,const AvoidArea & avoidArea,AvoidAreaType type)41 napi_value ConvertAvoidAreaToJsValue(napi_env env, const AvoidArea& avoidArea, AvoidAreaType type)
42 {
43     napi_value objValue = nullptr;
44     napi_create_object(env, &objValue);
45     if (objValue == nullptr) {
46         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to convert avoidArea to jsObject");
47         return nullptr;
48     }
49     napi_set_named_property(env, objValue, "visible",
50                             CreateJsValue(env, type == AvoidAreaType::TYPE_CUTOUT ? false : true));
51     napi_set_named_property(env, objValue, "leftRect", GetRectAndConvertToJsValue(env, avoidArea.leftRect_));
52     napi_set_named_property(env, objValue, "topRect", GetRectAndConvertToJsValue(env, avoidArea.topRect_));
53     napi_set_named_property(env, objValue, "rightRect", GetRectAndConvertToJsValue(env, avoidArea.rightRect_));
54     napi_set_named_property(env, objValue, "bottomRect", GetRectAndConvertToJsValue(env, avoidArea.bottomRect_));
55     return objValue;
56 }
57 
CreateJsExtensionWindowPropertiesObject(napi_env env,sptr<Window> & window)58 napi_value CreateJsExtensionWindowPropertiesObject(napi_env env, sptr<Window>& window)
59 {
60     TLOGI(WmsLogTag::WMS_UIEXT, "CreateJsExtensionWindowPropertiesObject is called");
61     napi_value objValue = nullptr;
62     napi_create_object(env, &objValue);
63     if (objValue == nullptr) {
64         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to convert windowProperties to jsObject");
65         return nullptr;
66     }
67 
68     Rect windowRect = window->GetRect();
69     napi_value windowRectObj = GetRectAndConvertToJsValue(env, windowRect);
70     if (windowRectObj == nullptr) {
71         TLOGE(WmsLogTag::WMS_UIEXT, "GetWindowRect Failed");
72     }
73     napi_set_named_property(env, objValue, "uiExtensionHostWindowProxyRect", windowRectObj);
74     return objValue;
75 }
76 
CreateJsExtensionWindowProperties(napi_env env,sptr<Window> & window)77 napi_value CreateJsExtensionWindowProperties(napi_env env, sptr<Window>& window)
78 {
79     TLOGI(WmsLogTag::WMS_UIEXT, "CreateJsWindowPropertiesObject is called");
80     napi_value objValue = nullptr;
81     napi_create_object(env, &objValue);
82     if (objValue == nullptr) {
83         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to convert windowProperties to jsObject");
84         return nullptr;
85     }
86 
87     Rect windowRect = window->GetRect();
88     napi_value windowRectObj = GetRectAndConvertToJsValue(env, windowRect);
89     if (windowRectObj == nullptr) {
90         TLOGE(WmsLogTag::WMS_UIEXT, "GetWindowRect failed!");
91     }
92     napi_set_named_property(env, objValue, "windowRect", windowRectObj);
93 
94     WindowType type = window->GetType();
95     if (NATIVE_JS_TO_WINDOW_TYPE_MAP.count(type) != 0) {
96         napi_set_named_property(env, objValue, "type", CreateJsValue(env, NATIVE_JS_TO_WINDOW_TYPE_MAP.at(type)));
97     } else {
98         napi_set_named_property(env, objValue, "type", CreateJsValue(env, type));
99     }
100     napi_set_named_property(env, objValue, "isLayoutFullScreen", CreateJsValue(env, window->IsLayoutFullScreen()));
101     napi_set_named_property(env, objValue, "isFullScreen", CreateJsValue(env, window->IsFullScreen()));
102     napi_set_named_property(env, objValue, "touchable", CreateJsValue(env, window->GetTouchable()));
103     napi_set_named_property(env, objValue, "focusable", CreateJsValue(env, window->GetFocusable()));
104     napi_set_named_property(env, objValue, "name", CreateJsValue(env, window->GetWindowName()));
105     napi_set_named_property(env, objValue, "isPrivacyMode", CreateJsValue(env, window->IsPrivacyMode()));
106     napi_set_named_property(env, objValue, "isKeepScreenOn", CreateJsValue(env, window->IsKeepScreenOn()));
107     napi_set_named_property(env, objValue, "brightness", CreateJsValue(env, window->GetBrightness()));
108     napi_set_named_property(env, objValue, "isTransparent", CreateJsValue(env, window->IsTransparent()));
109     napi_set_named_property(env, objValue, "isRoundCorner", CreateJsValue(env, false)); // empty method
110     napi_set_named_property(env, objValue, "dimBehindValue", CreateJsValue(env, 0));
111     napi_set_named_property(env, objValue, "id", CreateJsValue(env, window->GetWindowId()));
112     return objValue;
113 }
114 
NapiIsCallable(napi_env env,napi_value value)115 bool NapiIsCallable(napi_env env, napi_value value)
116 {
117     bool result = false;
118     napi_is_callable(env, value, &result);
119     return result;
120 }
121 
NapiGetUndefined(napi_env env)122 napi_value NapiGetUndefined(napi_env env)
123 {
124     napi_value result = nullptr;
125     napi_get_undefined(env, &result);
126     return result;
127 }
128 
NapiThrowError(napi_env env,WmErrorCode errCode)129 napi_value NapiThrowError(napi_env env, WmErrorCode errCode)
130 {
131     napi_throw(env, CreateJsError(env, static_cast<int32_t>(errCode)));
132     return NapiGetUndefined(env);
133 }
134 }  // namespace Rosen
135 }  // namespace OHOS