• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.h"
17 
18 #include "js_extension_window_utils.h"
19 #include "js_runtime_utils.h"
20 #include "js_window_utils.h"
21 #include "js_window.h"
22 #include "window_manager_hilog.h"
23 #include "wm_common.h"
24 #include "extension_window.h"
25 #include "ui_content.h"
26 #include "permission.h"
27 
28 namespace OHOS {
29 namespace Rosen {
30 using namespace AbilityRuntime;
31 namespace {
32 constexpr Rect g_emptyRect = {0, 0, 0, 0};
33 constexpr size_t INDEX_ZERO = 0;
34 constexpr size_t INDEX_ONE = 1;
35 constexpr size_t INDEX_TWO = 2;
36 constexpr size_t ARG_COUNT_ONE = 1;
37 constexpr size_t ARG_COUNT_TWO = 2;
38 constexpr size_t ARG_COUNT_THREE = 3;
39 constexpr size_t FOUR_PARAMS_SIZE = 4;
40 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "JsExtensionWindow"};
41 const std::unordered_set<std::string> g_emptyListener = {
42     "windowVisibilityChange",
43     "noInteractionDetected",
44     "dialogTargetTouch",
45     "windowEvent",
46     "windowStatusChange",
47     "windowTitleButtonRectChange",
48     "windowRectChange",
49     "rotationChange",
50     "touchOutside",
51 };
52 const std::unordered_set<std::string> g_unsupportListener = {
53     "windowWillClose",
54     "windowHighlightChange",
55 };
56 const std::unordered_set<std::string> g_invalidListener = {
57     "subWindowClose",
58 };
59 
IsEmptyListener(const std::string & type)60 bool IsEmptyListener(const std::string& type)
61 {
62     return g_emptyListener.find(type) != g_emptyListener.end();
63 }
64 
IsUnsupportListener(const std::string & type)65 bool IsUnsupportListener(const std::string& type)
66 {
67     return g_unsupportListener.find(type) != g_unsupportListener.end();
68 }
69 
IsInvalidListener(const std::string & type)70 bool IsInvalidListener(const std::string& type)
71 {
72     return g_invalidListener.find(type) != g_invalidListener.end();
73 }
74 } // namespace
75 
JsExtensionWindow(const std::shared_ptr<Rosen::ExtensionWindow> extensionWindow,int32_t hostWindowId)76 JsExtensionWindow::JsExtensionWindow(
77     const std::shared_ptr<Rosen::ExtensionWindow> extensionWindow,
78     int32_t hostWindowId)
79     : extensionWindow_(extensionWindow), hostWindowId_(hostWindowId),
80     extensionRegisterManager_(std::make_unique<JsExtensionWindowRegisterManager>()) {
81 }
82 
JsExtensionWindow(const std::shared_ptr<Rosen::ExtensionWindow> extensionWindow,sptr<AAFwk::SessionInfo> sessionInfo)83 JsExtensionWindow::JsExtensionWindow(const std::shared_ptr<Rosen::ExtensionWindow> extensionWindow,
84     sptr<AAFwk::SessionInfo> sessionInfo)
85     : extensionWindow_(extensionWindow), hostWindowId_(-1), sessionInfo_(sessionInfo),
86     extensionRegisterManager_(std::make_unique<JsExtensionWindowRegisterManager>()) {
87 }
88 
~JsExtensionWindow()89 JsExtensionWindow::~JsExtensionWindow() {}
90 
CreateJsExtensionWindow(napi_env env,sptr<Rosen::Window> window,int32_t hostWindowId)91 napi_value JsExtensionWindow::CreateJsExtensionWindow(napi_env env, sptr<Rosen::Window> window, int32_t hostWindowId)
92 {
93     TLOGD(WmsLogTag::WMS_UIEXT, "Called.");
94     napi_value objValue = nullptr;
95     napi_create_object(env, &objValue);
96 
97     if (env == nullptr || window == nullptr || objValue == nullptr) {
98         TLOGE(WmsLogTag::WMS_UIEXT, "JsExtensionWindow env or window is nullptr");
99         return nullptr;
100     }
101 
102     std::shared_ptr<ExtensionWindow> extensionWindow = std::make_shared<ExtensionWindowImpl>(window);
103     std::unique_ptr<JsExtensionWindow> jsExtensionWindow =
104         std::make_unique<JsExtensionWindow>(extensionWindow, hostWindowId);
105     napi_wrap(env, objValue, jsExtensionWindow.release(), JsExtensionWindow::Finalizer, nullptr, nullptr);
106 
107     napi_property_descriptor desc[] = {
108         DECLARE_NAPI_GETTER("properties", JsExtensionWindow::GetProperties)
109     };
110     NAPI_CALL(env, napi_define_properties(env, objValue, sizeof(desc) / sizeof(desc[0]), desc));
111 
112     const char *moduleName = "JsExtensionWindow";
113     BindNativeFunction(env, objValue, "getWindowAvoidArea", moduleName, JsExtensionWindow::GetWindowAvoidArea);
114     BindNativeFunction(env, objValue, "on", moduleName, JsExtensionWindow::RegisterExtensionWindowCallback);
115     BindNativeFunction(env, objValue, "off", moduleName, JsExtensionWindow::UnRegisterExtensionWindowCallback);
116     BindNativeFunction(env, objValue, "hideNonSecureWindows", moduleName, JsExtensionWindow::HideNonSecureWindows);
117     BindNativeFunction(env, objValue, "createSubWindowWithOptions", moduleName,
118         JsExtensionWindow::CreateSubWindowWithOptions);
119     BindNativeFunction(env, objValue, "setWaterMarkFlag", moduleName, JsExtensionWindow::SetWaterMarkFlag);
120     BindNativeFunction(env, objValue, "hidePrivacyContentForHost", moduleName,
121                        JsExtensionWindow::HidePrivacyContentForHost);
122     BindNativeFunction(env, objValue, "occupyEvents", moduleName, JsExtensionWindow::OccupyEvents);
123 
124     return objValue;
125 }
126 
CreateJsExtensionWindowObject(napi_env env,sptr<Rosen::Window> window,sptr<AAFwk::SessionInfo> sessionInfo)127 napi_value JsExtensionWindow::CreateJsExtensionWindowObject(napi_env env, sptr<Rosen::Window> window,
128     sptr<AAFwk::SessionInfo> sessionInfo)
129 {
130     TLOGI(WmsLogTag::WMS_UIEXT, "[NAPI]");
131     napi_value objValue = nullptr;
132     napi_create_object(env, &objValue);
133 
134     if (env == nullptr || window == nullptr || objValue == nullptr) {
135         TLOGE(WmsLogTag::WMS_UIEXT, "JsExtensionWindow env or window is nullptr");
136         return nullptr;
137     }
138 
139     std::shared_ptr<ExtensionWindow> extensionWindow = std::make_shared<ExtensionWindowImpl>(window);
140     std::unique_ptr<JsExtensionWindow> jsExtensionWindow = std::make_unique<JsExtensionWindow>(extensionWindow,
141         sessionInfo);
142     napi_wrap(env, objValue, jsExtensionWindow.release(), JsExtensionWindow::Finalizer, nullptr, nullptr);
143 
144     const char *moduleName = "JsExtensionWindow";
145     BindNativeFunction(env, objValue, "on", moduleName, JsExtensionWindow::AtomicRegisterExtensionWindowCallback);
146     BindNativeFunction(env, objValue, "off", moduleName, JsExtensionWindow::AtomicUnRegisterExtensionWindowCallback);
147     BindNativeFunction(env, objValue, "getUIContext", moduleName, JsExtensionWindow::GetUIContext);
148     BindNativeFunction(env, objValue, "setWindowBrightness", moduleName, JsExtensionWindow::SetWindowBrightness);
149     BindNativeFunction(env, objValue, "setWindowKeepScreenOn", moduleName, JsExtensionWindow::SetWindowKeepScreenOn);
150     BindNativeFunction(env, objValue, "loadContent", moduleName, JsExtensionWindow::LoadContent);
151     BindNativeFunction(env, objValue, "loadContentByName", moduleName, JsExtensionWindow::LoadContentByName);
152     BindNativeFunction(env, objValue, "setUIContent", moduleName, JsExtensionWindow::SetUIContent);
153     BindNativeFunction(env, objValue, "isWindowShowing", moduleName, JsExtensionWindow::IsWindowShowingSync);
154     BindNativeFunction(env, objValue, "getWindowProperties", moduleName, JsExtensionWindow::GetWindowPropertiesSync);
155     BindNativeFunction(env, objValue, "getWindowAvoidArea", moduleName, JsExtensionWindow::GetWindowAvoidArea);
156     BindNativeFunction(env, objValue, "setSpecificSystemBarEnabled", moduleName,
157         JsExtensionWindow::SetSpecificSystemBarEnabled);
158     BindNativeFunction(env, objValue, "getPreferredOrientation", moduleName,
159         JsExtensionWindow::GetPreferredOrientation);
160     BindNativeFunction(env, objValue, "createSubWindowWithOptions", moduleName,
161         JsExtensionWindow::AtomicServiceCreateSubWindowWithOptions);
162     BindNativeFunction(env, objValue, "setWindowLayoutFullScreen", moduleName,
163         JsExtensionWindow::SetWindowLayoutFullScreen);
164     BindNativeFunction(env, objValue, "getWindowColorSpace", moduleName, JsExtensionWindow::GetWindowColorSpace);
165     BindNativeFunction(env, objValue, "setWindowColorSpace", moduleName, JsExtensionWindow::SetWindowColorSpace);
166     BindNativeFunction(env, objValue, "setWindowPrivacyMode", moduleName, JsExtensionWindow::SetWindowPrivacyMode);
167     BindNativeFunction(env, objValue, "setWindowSystemBarEnable", moduleName,
168         JsExtensionWindow::SetWindowSystemBarEnable);
169     BindNativeFunction(env, objValue, "isGestureBackEnabled", moduleName, JsExtensionWindow::GetGestureBackEnabled);
170     BindNativeFunction(env, objValue, "setGestureBackEnabled", moduleName, JsExtensionWindow::SetGestureBackEnabled);
171     BindNativeFunction(env, objValue, "getImmersiveModeEnabledState", moduleName,
172         JsExtensionWindow::GetImmersiveModeEnabledState);
173     BindNativeFunction(env, objValue, "setImmersiveModeEnabledState", moduleName,
174         JsExtensionWindow::SetImmersiveModeEnabledState);
175     BindNativeFunction(env, objValue, "isFocused", moduleName, JsExtensionWindow::IsFocused);
176     BindNativeFunction(env, objValue, "isWindowSupportWideGamut", moduleName,
177         JsExtensionWindow::IsWindowSupportWideGamut);
178     BindNativeFunction(env, objValue, "getGlobalRect", moduleName, JsExtensionWindow::GetGlobalScaledRect);
179     BindNativeFunction(env, objValue, "getStatusBarProperty", moduleName, JsExtensionWindow::GetStatusBarProperty);
180 
181     //return default value
182     BindNativeFunction(env, objValue, "getTitleButtonRect", moduleName, JsExtensionWindow::GetTitleButtonRect);
183     BindNativeFunction(env, objValue, "getWindowStatus", moduleName, JsExtensionWindow::GetWindowStatus);
184     BindNativeFunction(env, objValue, "getWindowDensityInfo", moduleName, JsExtensionWindow::GetWindowDensityInfo);
185     BindNativeFunction(env, objValue, "getWindowSystemBarProperties", moduleName,
186         JsExtensionWindow::GetWindowSystemBarProperties);
187 
188     RegisterUnsupportFuncs(env, objValue, moduleName);
189 
190     return objValue;
191 }
192 
RegisterUnsupportFuncs(napi_env env,napi_value objValue,const char * moduleName)193 void JsExtensionWindow::RegisterUnsupportFuncs(napi_env env, napi_value objValue, const char *moduleName)
194 {
195     BindNativeFunction(env, objValue, "moveWindowTo", moduleName, JsExtensionWindow::EmptyAsyncCall);
196     BindNativeFunction(env, objValue, "moveWindowToAsync", moduleName, JsExtensionWindow::EmptyAsyncCall);
197     BindNativeFunction(env, objValue, "moveWindowToGlobal", moduleName, JsExtensionWindow::EmptyAsyncCall);
198     BindNativeFunction(env, objValue, "resize", moduleName, JsExtensionWindow::EmptyAsyncCall);
199     BindNativeFunction(env, objValue, "resizeAsync", moduleName, JsExtensionWindow::EmptyAsyncCall);
200     BindNativeFunction(env, objValue, "showWindow", moduleName, JsExtensionWindow::EmptyAsyncCall);
201     BindNativeFunction(env, objValue, "destroyWindow", moduleName, JsExtensionWindow::EmptyAsyncCall);
202     BindNativeFunction(env, objValue, "setPreferredOrientation", moduleName, JsExtensionWindow::EmptyAsyncCall);
203     BindNativeFunction(env, objValue, "setWindowFocusable", moduleName, JsExtensionWindow::EmptyAsyncCall);
204     BindNativeFunction(env, objValue, "setExclusivelyHighlighted", moduleName, JsExtensionWindow::EmptyAsyncCall);
205     BindNativeFunction(env, objValue, "setWindowTouchable", moduleName, JsExtensionWindow::EmptyAsyncCall);
206     BindNativeFunction(env, objValue, "snapshot", moduleName, JsExtensionWindow::EmptyAsyncCall);
207     BindNativeFunction(env, objValue, "snapshotIgnorePrivacy", moduleName, JsExtensionWindow::EmptyAsyncCall);
208     BindNativeFunction(env, objValue, "setAspectRatio", moduleName, JsExtensionWindow::EmptyAsyncCall);
209     BindNativeFunction(env, objValue, "resetAspectRatio", moduleName, JsExtensionWindow::EmptyAsyncCall);
210     BindNativeFunction(env, objValue, "minimize", moduleName, JsExtensionWindow::EmptyAsyncCall);
211     BindNativeFunction(env, objValue, "maximize", moduleName, JsExtensionWindow::EmptyAsyncCall);
212     BindNativeFunction(env, objValue, "setResizeByDragEnabled", moduleName, JsExtensionWindow::EmptyAsyncCall);
213     BindNativeFunction(env, objValue, "enableLandscapeMultiWindow", moduleName, JsExtensionWindow::EmptyAsyncCall);
214     BindNativeFunction(env, objValue, "disableLandscapeMultiWindow", moduleName, JsExtensionWindow::EmptyAsyncCall);
215     BindNativeFunction(env, objValue, "setWindowMask", moduleName, JsExtensionWindow::EmptyAsyncCall);
216     BindNativeFunction(env, objValue, "setWindowGrayScale", moduleName, JsExtensionWindow::EmptyAsyncCall);
217     BindNativeFunction(env, objValue, "setParentWindow", moduleName, JsExtensionWindow::EmptyAsyncCall);
218     BindNativeFunction(env, objValue, "setFollowParentMultiScreenPolicy", moduleName,
219         JsExtensionWindow::EmptyAsyncCall);
220     BindNativeFunction(env, objValue, "setWindowSystemBarProperties", moduleName, JsExtensionWindow::EmptyAsyncCall);
221     BindNativeFunction(env, objValue, "setStatusBarColor", moduleName, JsExtensionWindow::EmptyAsyncCall);
222 
223     BindNativeFunction(env, objValue, "setWindowBackgroundColor", moduleName, JsExtensionWindow::EmptySyncCall);
224     BindNativeFunction(env, objValue, "setWindowDecorVisible", moduleName, JsExtensionWindow::EmptySyncCall);
225     BindNativeFunction(env, objValue, "getParentWindow", moduleName, JsExtensionWindow::EmptySyncCall);
226 
227     BindNativeFunction(env, objValue, "setFollowParentWindowLayoutEnabled", moduleName,
228         JsExtensionWindow::UnsupportAsyncCall);
229     BindNativeFunction(env, objValue, "setSystemAvoidAreaEnabled", moduleName, JsExtensionWindow::UnsupportAsyncCall);
230     BindNativeFunction(env, objValue, "setWindowTopmost", moduleName, JsExtensionWindow::UnsupportAsyncCall);
231     BindNativeFunction(env, objValue, "setWindowLimits", moduleName, JsExtensionWindow::UnsupportAsyncCall);
232     BindNativeFunction(env, objValue, "recover", moduleName, JsExtensionWindow::UnsupportAsyncCall);
233     BindNativeFunction(env, objValue, "restore", moduleName, JsExtensionWindow::UnsupportAsyncCall);
234     BindNativeFunction(env, objValue, "setWindowTitle", moduleName, JsExtensionWindow::UnsupportAsyncCall);
235     BindNativeFunction(env, objValue, "setSubWindowModal", moduleName, JsExtensionWindow::UnsupportAsyncCall);
236     BindNativeFunction(env, objValue, "startMoving", moduleName, JsExtensionWindow::UnsupportAsyncCall);
237     BindNativeFunction(env, objValue, "stopMoving", moduleName, JsExtensionWindow::UnsupportAsyncCall);
238     BindNativeFunction(env, objValue, "setTitleAndDockHoverShown", moduleName, JsExtensionWindow::UnsupportAsyncCall);
239 
240     BindNativeFunction(env, objValue, "isSystemAvoidAreaEnabled", moduleName, JsExtensionWindow::UnsupportSyncCall);
241     BindNativeFunction(env, objValue, "isWindowHighlighted", moduleName, JsExtensionWindow::UnsupportSyncCall);
242     BindNativeFunction(env, objValue, "getWindowLimits", moduleName, JsExtensionWindow::UnsupportSyncCall);
243     BindNativeFunction(env, objValue, "getWindowDecorVisible", moduleName, JsExtensionWindow::UnsupportSyncCall);
244     BindNativeFunction(env, objValue, "setWindowTitleMoveEnabled", moduleName, JsExtensionWindow::UnsupportSyncCall);
245     BindNativeFunction(env, objValue, "setWindowDecorHeight", moduleName, JsExtensionWindow::UnsupportSyncCall);
246     BindNativeFunction(env, objValue, "getWindowDecorHeight", moduleName, JsExtensionWindow::UnsupportSyncCall);
247     BindNativeFunction(env, objValue, "setDecorButtonStyle", moduleName, JsExtensionWindow::UnsupportSyncCall);
248     BindNativeFunction(env, objValue, "getDecorButtonStyle", moduleName, JsExtensionWindow::UnsupportSyncCall);
249     BindNativeFunction(env, objValue, "setWindowTitleButtonVisible", moduleName, JsExtensionWindow::UnsupportSyncCall);
250     BindNativeFunction(env, objValue, "setWindowDelayRaiseOnDrag", moduleName, JsExtensionWindow::UnsupportSyncCall);
251 
252     BindNativeFunction(env, objValue, "setDialogBackGestureEnabled", moduleName, JsExtensionWindow::InvalidAsyncCall);
253     BindNativeFunction(env, objValue, "setSubWindowZLevel", moduleName, JsExtensionWindow::InvalidAsyncCall);
254     BindNativeFunction(env, objValue, "setWindowCornerRadius", moduleName, JsExtensionWindow::InvalidAsyncCall);
255 
256     BindNativeFunction(env, objValue, "keepKeyboardOnFocus", moduleName, JsExtensionWindow::InvalidSyncCall);
257     BindNativeFunction(env, objValue, "getSubWindowZLevel", moduleName, JsExtensionWindow::InvalidSyncCall);
258     BindNativeFunction(env, objValue, "getWindowCornerRadius", moduleName, JsExtensionWindow::InvalidSyncCall);
259     BindNativeFunction(env, objValue, "setWindowShadowRadius", moduleName, JsExtensionWindow::InvalidSyncCall);
260 }
261 
Finalizer(napi_env env,void * data,void * hint)262 void JsExtensionWindow::Finalizer(napi_env env, void* data, void* hint)
263 {
264     TLOGI(WmsLogTag::WMS_UIEXT, "[NAPI]");
265     std::unique_ptr<JsExtensionWindow>(static_cast<JsExtensionWindow*>(data));
266 }
267 
GetWindowAvoidArea(napi_env env,napi_callback_info info)268 napi_value JsExtensionWindow::GetWindowAvoidArea(napi_env env, napi_callback_info info)
269 {
270     TLOGI(WmsLogTag::WMS_UIEXT, "[NAPI]");
271     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
272     return (me != nullptr) ? me->OnGetWindowAvoidArea(env, info) : nullptr;
273 }
274 
RegisterExtensionWindowCallback(napi_env env,napi_callback_info info)275 napi_value JsExtensionWindow::RegisterExtensionWindowCallback(napi_env env, napi_callback_info info)
276 {
277     TLOGI(WmsLogTag::WMS_UIEXT, "[NAPI]");
278     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
279     return (me != nullptr) ? me->OnRegisterExtensionWindowCallback(env, info) : nullptr;
280 }
281 
UnRegisterExtensionWindowCallback(napi_env env,napi_callback_info info)282 napi_value JsExtensionWindow::UnRegisterExtensionWindowCallback(napi_env env, napi_callback_info info)
283 {
284     TLOGI(WmsLogTag::WMS_UIEXT, "[NAPI]");
285     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
286     return (me != nullptr) ? me->OnUnRegisterExtensionWindowCallback(env, info) : nullptr;
287 }
288 
AtomicRegisterExtensionWindowCallback(napi_env env,napi_callback_info info)289 napi_value JsExtensionWindow::AtomicRegisterExtensionWindowCallback(napi_env env, napi_callback_info info)
290 {
291     TLOGI(WmsLogTag::WMS_UIEXT, "[NAPI]");
292     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
293     return (me != nullptr) ? me->OnRegisterExtensionWindowCallback(env, info, true) : nullptr;
294 }
295 
AtomicUnRegisterExtensionWindowCallback(napi_env env,napi_callback_info info)296 napi_value JsExtensionWindow::AtomicUnRegisterExtensionWindowCallback(napi_env env, napi_callback_info info)
297 {
298     TLOGI(WmsLogTag::WMS_UIEXT, "[NAPI]");
299     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
300     return (me != nullptr) ? me->OnUnRegisterExtensionWindowCallback(env, info, true) : nullptr;
301 }
302 
303 
HideNonSecureWindows(napi_env env,napi_callback_info info)304 napi_value JsExtensionWindow::HideNonSecureWindows(napi_env env, napi_callback_info info)
305 {
306     TLOGI(WmsLogTag::WMS_UIEXT, "[NAPI]");
307     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
308     return (me != nullptr) ? me->OnHideNonSecureWindows(env, info) : nullptr;
309 }
310 
CreateSubWindowWithOptions(napi_env env,napi_callback_info info)311 napi_value JsExtensionWindow::CreateSubWindowWithOptions(napi_env env, napi_callback_info info)
312 {
313     TLOGI(WmsLogTag::WMS_UIEXT, "[NAPI]");
314     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
315     return (me != nullptr) ? me->OnCreateSubWindowWithOptions(env, info) : nullptr;
316 }
317 
SetWaterMarkFlag(napi_env env,napi_callback_info info)318 napi_value JsExtensionWindow::SetWaterMarkFlag(napi_env env, napi_callback_info info)
319 {
320     TLOGI(WmsLogTag::WMS_UIEXT, "[NAPI]");
321     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
322     return (me != nullptr) ? me->OnSetWaterMarkFlag(env, info) : nullptr;
323 }
324 
HidePrivacyContentForHost(napi_env env,napi_callback_info info)325 napi_value JsExtensionWindow::HidePrivacyContentForHost(napi_env env, napi_callback_info info)
326 {
327     TLOGD(WmsLogTag::WMS_UIEXT, "[NAPI]");
328     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
329     return (me != nullptr) ? me->OnHidePrivacyContentForHost(env, info) : nullptr;
330 }
331 
LoadContent(napi_env env,napi_callback_info info)332 napi_value JsExtensionWindow::LoadContent(napi_env env, napi_callback_info info)
333 {
334     TLOGI(WmsLogTag::WMS_UIEXT, "[NAPI]");
335     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
336     return (me != nullptr) ? me->OnLoadContent(env, info, false) : nullptr;
337 }
338 
LoadContentByName(napi_env env,napi_callback_info info)339 napi_value JsExtensionWindow::LoadContentByName(napi_env env, napi_callback_info info)
340 {
341     TLOGI(WmsLogTag::WMS_UIEXT, "[NAPI]");
342     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
343     return (me != nullptr) ? me->OnLoadContent(env, info, true) : nullptr;
344 }
345 
IsWindowShowingSync(napi_env env,napi_callback_info info)346 napi_value JsExtensionWindow::IsWindowShowingSync(napi_env env, napi_callback_info info)
347 {
348     TLOGI(WmsLogTag::WMS_UIEXT, "[NAPI]");
349     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
350     return (me != nullptr) ? me->OnIsWindowShowingSync(env, info) : nullptr;
351 }
352 
SetUIContent(napi_env env,napi_callback_info info)353 napi_value JsExtensionWindow::SetUIContent(napi_env env, napi_callback_info info)
354 {
355     TLOGI(WmsLogTag::WMS_UIEXT, "[NAPI]");
356     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
357     return (me != nullptr) ? me->OnSetUIContent(env, info) : nullptr;
358 }
359 
GetWindowPropertiesSync(napi_env env,napi_callback_info info)360 napi_value JsExtensionWindow::GetWindowPropertiesSync(napi_env env, napi_callback_info info)
361 {
362     TLOGD(WmsLogTag::WMS_UIEXT, "[NAPI]");
363     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
364     return (me != nullptr) ? me->OnGetWindowPropertiesSync(env, info) : nullptr;
365 }
366 
SetSpecificSystemBarEnabled(napi_env env,napi_callback_info info)367 napi_value JsExtensionWindow::SetSpecificSystemBarEnabled(napi_env env, napi_callback_info info)
368 {
369     TLOGI(WmsLogTag::WMS_UIEXT, "[NAPI]");
370     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
371     return (me != nullptr) ? me->OnSetSpecificSystemBarEnabled(env, info) : nullptr;
372 }
373 
GetPreferredOrientation(napi_env env,napi_callback_info info)374 napi_value JsExtensionWindow::GetPreferredOrientation(napi_env env, napi_callback_info info)
375 {
376     TLOGD(WmsLogTag::WMS_UIEXT, "[NAPI]");
377     ApiOrientation apiOrientation = ApiOrientation::UNSPECIFIED;
378     return CreateJsValue(env, static_cast<uint32_t>(apiOrientation));
379 }
380 
GetUIContext(napi_env env,napi_callback_info info)381 napi_value JsExtensionWindow::GetUIContext(napi_env env, napi_callback_info info)
382 {
383     TLOGD(WmsLogTag::WMS_UIEXT, "[NAPI]");
384     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
385     return (me != nullptr) ? me->OnGetUIContext(env, info) : nullptr;
386 }
387 
SetWindowBrightness(napi_env env,napi_callback_info info)388 napi_value JsExtensionWindow::SetWindowBrightness(napi_env env, napi_callback_info info)
389 {
390     TLOGI(WmsLogTag::WMS_UIEXT, "[NAPI]");
391     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
392     return (me != nullptr) ? me->OnSetWindowBrightness(env, info) : nullptr;
393 }
394 
SetWindowKeepScreenOn(napi_env env,napi_callback_info info)395 napi_value JsExtensionWindow::SetWindowKeepScreenOn(napi_env env, napi_callback_info info)
396 {
397     TLOGI(WmsLogTag::WMS_UIEXT, "[NAPI]");
398     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
399     return (me != nullptr) ? me->OnSetWindowKeepScreenOn(env, info) : nullptr;
400 }
401 
OccupyEvents(napi_env env,napi_callback_info info)402 napi_value JsExtensionWindow::OccupyEvents(napi_env env, napi_callback_info info)
403 {
404     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
405     return (me != nullptr) ? me->OnOccupyEvents(env, info) : nullptr;
406 }
407 
AtomicServiceCreateSubWindowWithOptions(napi_env env,napi_callback_info info)408 napi_value JsExtensionWindow::AtomicServiceCreateSubWindowWithOptions(napi_env env, napi_callback_info info)
409 {
410     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
411     return (me != nullptr) ? me->OnUnsupportAsyncCall(env, info) : nullptr;
412 }
413 
SetWindowLayoutFullScreen(napi_env env,napi_callback_info info)414 napi_value JsExtensionWindow::SetWindowLayoutFullScreen(napi_env env, napi_callback_info info)
415 {
416     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
417     return (me != nullptr) ? me->OnSetWindowLayoutFullScreen(env, info) : nullptr;
418 }
419 
GetWindowColorSpace(napi_env env,napi_callback_info info)420 napi_value JsExtensionWindow::GetWindowColorSpace(napi_env env, napi_callback_info info)
421 {
422     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
423     return (me != nullptr) ? me->OnGetWindowColorSpace(env, info) : nullptr;
424 }
425 
SetWindowColorSpace(napi_env env,napi_callback_info info)426 napi_value JsExtensionWindow::SetWindowColorSpace(napi_env env, napi_callback_info info)
427 {
428     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
429     return (me != nullptr) ? me->OnSetWindowColorSpace(env, info) : nullptr;
430 }
431 
SetWindowPrivacyMode(napi_env env,napi_callback_info info)432 napi_value JsExtensionWindow::SetWindowPrivacyMode(napi_env env, napi_callback_info info)
433 {
434     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
435     return (me != nullptr) ? me->OnSetWindowPrivacyMode(env, info) : nullptr;
436 }
437 
SetWindowSystemBarEnable(napi_env env,napi_callback_info info)438 napi_value JsExtensionWindow::SetWindowSystemBarEnable(napi_env env, napi_callback_info info)
439 {
440     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
441     return (me != nullptr) ? me->OnSetWindowSystemBarEnable(env, info) : nullptr;
442 }
443 
GetGestureBackEnabled(napi_env env,napi_callback_info info)444 napi_value JsExtensionWindow::GetGestureBackEnabled(napi_env env, napi_callback_info info)
445 {
446     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
447     return (me != nullptr) ? me->OnGetGestureBackEnabled(env, info) : nullptr;
448 }
449 
SetGestureBackEnabled(napi_env env,napi_callback_info info)450 napi_value JsExtensionWindow::SetGestureBackEnabled(napi_env env, napi_callback_info info)
451 {
452     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
453     return (me != nullptr) ? me->OnSetGestureBackEnabled(env, info) : nullptr;
454 }
455 
GetImmersiveModeEnabledState(napi_env env,napi_callback_info info)456 napi_value JsExtensionWindow::GetImmersiveModeEnabledState(napi_env env, napi_callback_info info)
457 {
458     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
459     return (me != nullptr) ? me->OnGetImmersiveModeEnabledState(env, info) : nullptr;
460 }
461 
SetImmersiveModeEnabledState(napi_env env,napi_callback_info info)462 napi_value JsExtensionWindow::SetImmersiveModeEnabledState(napi_env env, napi_callback_info info)
463 {
464     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
465     return (me != nullptr) ? me->OnSetImmersiveModeEnabledState(env, info) : nullptr;
466 }
467 
IsFocused(napi_env env,napi_callback_info info)468 napi_value JsExtensionWindow::IsFocused(napi_env env, napi_callback_info info)
469 {
470     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
471     return (me != nullptr) ? me->OnIsFocused(env, info) : nullptr;
472 }
473 
IsWindowSupportWideGamut(napi_env env,napi_callback_info info)474 napi_value JsExtensionWindow::IsWindowSupportWideGamut(napi_env env, napi_callback_info info)
475 {
476     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
477     return (me != nullptr) ? me->OnIsWindowSupportWideGamut(env, info) : nullptr;
478 }
479 
GetGlobalScaledRect(napi_env env,napi_callback_info info)480 napi_value JsExtensionWindow::GetGlobalScaledRect(napi_env env, napi_callback_info info)
481 {
482     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
483     return (me != nullptr) ? me->OnGetGlobalScaledRect(env, info) : nullptr;
484 }
485 
GetStatusBarProperty(napi_env env,napi_callback_info info)486 napi_value JsExtensionWindow::GetStatusBarProperty(napi_env env, napi_callback_info info)
487 {
488     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
489     return (me != nullptr) ? me->OnGetStatusBarPropertySync(env, info) : nullptr;
490 }
491 
GetTitleButtonRect(napi_env env,napi_callback_info info)492 napi_value JsExtensionWindow::GetTitleButtonRect(napi_env env, napi_callback_info info)
493 {
494     TitleButtonRect titleButtonRect { 0, 0, 0, 0 };
495     napi_value TitleButtonAreaObj = ConvertTitleButtonAreaToJsValue(env, titleButtonRect);
496     if (TitleButtonAreaObj == nullptr) {
497         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
498     }
499     return TitleButtonAreaObj;
500 }
501 
GetWindowStatus(napi_env env,napi_callback_info info)502 napi_value JsExtensionWindow::GetWindowStatus(napi_env env, napi_callback_info info)
503 {
504     WindowStatus windowStatus = WindowStatus::WINDOW_STATUS_UNDEFINED;
505     return CreateJsValue(env, static_cast<uint32_t>(windowStatus));
506 }
507 
EmptyAsyncCall(napi_env env,napi_callback_info info)508 napi_value JsExtensionWindow::EmptyAsyncCall(napi_env env, napi_callback_info info)
509 {
510     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
511     return (me != nullptr) ? me->OnEmptyAsyncCall(env, info) : nullptr;
512 }
513 
EmptySyncCall(napi_env env,napi_callback_info info)514 napi_value JsExtensionWindow::EmptySyncCall(napi_env env, napi_callback_info info)
515 {
516     return NapiGetUndefined(env);
517 }
518 
UnsupportAsyncCall(napi_env env,napi_callback_info info)519 napi_value JsExtensionWindow::UnsupportAsyncCall(napi_env env, napi_callback_info info)
520 {
521     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
522     return (me != nullptr) ? me->OnUnsupportAsyncCall(env, info) : nullptr;
523 }
524 
UnsupportSyncCall(napi_env env,napi_callback_info info)525 napi_value JsExtensionWindow::UnsupportSyncCall(napi_env env, napi_callback_info info)
526 {
527     return NapiThrowError(env, WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT);
528 }
529 
InvalidAsyncCall(napi_env env,napi_callback_info info)530 napi_value JsExtensionWindow::InvalidAsyncCall(napi_env env, napi_callback_info info)
531 {
532     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
533     return (me != nullptr) ? me->OnInvalidAsyncCall(env, info) : nullptr;
534 }
535 
InvalidSyncCall(napi_env env,napi_callback_info info)536 napi_value JsExtensionWindow::InvalidSyncCall(napi_env env, napi_callback_info info)
537 {
538     return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_CALLING);
539 }
540 
GetWindowDensityInfo(napi_env env,napi_callback_info info)541 napi_value JsExtensionWindow::GetWindowDensityInfo(napi_env env, napi_callback_info info)
542 {
543     WindowDensityInfo densityInfo {};
544     auto objValue = ConvertWindowDensityInfoToJsValue(env, densityInfo);
545     if (objValue == nullptr) {
546         TLOGE(WmsLogTag::WMS_ATTRIBUTE, "create js windowDensityInfo failed");
547         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
548     }
549     return objValue;
550 }
551 
GetWindowSystemBarProperties(napi_env env,napi_callback_info info)552 napi_value JsExtensionWindow::GetWindowSystemBarProperties(napi_env env, napi_callback_info info)
553 {
554     auto window = sptr<Window>::MakeSptr();
555     auto objValue = CreateJsSystemBarPropertiesObject(env, window);
556     if (objValue == nullptr) {
557         TLOGE(WmsLogTag::WMS_IMMS, "get properties failed");
558         return NapiThrowError(env, WmErrorCode::WM_ERROR_SYSTEM_ABNORMALLY);
559     }
560     return objValue;
561 }
562 
GetType(napi_env env,napi_value value)563 napi_valuetype GetType(napi_env env, napi_value value)
564 {
565     napi_valuetype res = napi_undefined;
566     napi_typeof(env, value, &res);
567     return res;
568 }
569 
LoadContentTask(std::shared_ptr<NativeReference> contentStorage,std::string contextUrl,const std::shared_ptr<Rosen::ExtensionWindow> win,napi_env env,NapiAsyncTask & task,sptr<IRemoteObject> parentToken,bool isLoadedByName)570 static void LoadContentTask(std::shared_ptr<NativeReference> contentStorage, std::string contextUrl,
571     const std::shared_ptr<Rosen::ExtensionWindow> win, napi_env env, NapiAsyncTask& task,
572     sptr<IRemoteObject> parentToken, bool isLoadedByName)
573 {
574     napi_value nativeStorage = (contentStorage == nullptr) ? nullptr : contentStorage->GetNapiValue();
575     sptr<Window> windowImpl = win->GetWindow();
576     WMError ret;
577     if (isLoadedByName) {
578         ret = windowImpl->SetUIContentByName(contextUrl, env, nativeStorage);
579     } else {
580         ret = windowImpl->NapiSetUIContent(contextUrl, env, nativeStorage, BackupAndRestoreType::NONE, parentToken);
581     }
582     if (ret == WMError::WM_OK) {
583         task.Resolve(env, NapiGetUndefined(env));
584     } else {
585         task.Reject(env, CreateJsError(env, static_cast<int32_t>(ret), "Window load content failed"));
586     }
587     TLOGI(WmsLogTag::WMS_UIEXT, "Window [%{public}u, %{public}s] end, ret=%{public}d",
588         windowImpl->GetWindowId(), windowImpl->GetWindowName().c_str(), ret);
589 }
590 
OnSetWindowKeepScreenOn(napi_env env,napi_callback_info info)591 napi_value JsExtensionWindow::OnSetWindowKeepScreenOn(napi_env env, napi_callback_info info)
592 {
593     WmErrorCode errCode = WmErrorCode::WM_OK;
594     size_t argc = ARG_COUNT_TWO;
595     napi_value argv[INDEX_TWO] = { nullptr };
596     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
597     if (argc < ARG_COUNT_ONE) {
598         TLOGE(WmsLogTag::WMS_UIEXT, "Argc is invalid: %{public}zu", argc);
599         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
600     }
601     bool keepScreenOn = true;
602     if (errCode == WmErrorCode::WM_OK) {
603         napi_value nativeVal = argv[INDEX_ZERO];
604         if (nativeVal == nullptr) {
605             TLOGE(WmsLogTag::WMS_UIEXT, "Failed to convert parameter to keepScreenOn");
606             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
607         } else {
608             CHECK_NAPI_RETCODE(errCode, WmErrorCode::WM_ERROR_INVALID_PARAM,
609                 napi_get_value_bool(env, nativeVal, &keepScreenOn));
610         }
611     }
612     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
613         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
614     }
615     wptr<Window> weakToken(extensionWindow_->GetWindow());
616     std::shared_ptr<WmErrorCode> errCodePtr = std::make_shared<WmErrorCode>(WmErrorCode::WM_OK);
617     NapiAsyncTask::ExecuteCallback execute = [weakToken, keepScreenOn, errCodePtr] {
618         if (errCodePtr == nullptr) {
619             return;
620         }
621         auto weakWindow = weakToken.promote();
622         if (weakWindow == nullptr) {
623             *errCodePtr = WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
624             return;
625         }
626         *errCodePtr = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->ExtensionSetKeepScreenOn(keepScreenOn));
627         TLOGNI(WmsLogTag::WMS_UIEXT, "Window [%{public}u, %{public}s] set keep screen on end",
628             weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str());
629     };
630     NapiAsyncTask::CompleteCallback complete =
631         [weakToken, keepScreenOn, errCodePtr](napi_env env, NapiAsyncTask& task, int32_t status) {
632             if (errCodePtr == nullptr) {
633                 task.Reject(env, CreateJsError(env,  static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY),
634                     "System abnormal."));
635                 return;
636             }
637             if (*errCodePtr == WmErrorCode::WM_OK) {
638                 task.Resolve(env, NapiGetUndefined(env));
639             } else {
640                 task.Reject(env, CreateJsError(env, static_cast<int32_t>(*errCodePtr),
641                     "Window set keep screen on failed"));
642             }
643         };
644 
645     napi_value lastParam = nullptr;
646     if (argc > ARG_COUNT_ONE && argv[INDEX_ONE] != nullptr && GetType(env, argv[INDEX_ONE]) == napi_function) {
647         lastParam = argv[INDEX_ONE];
648     }
649     napi_value result = nullptr;
650     NapiAsyncTask::Schedule("JsExtensionWindow::OnSetWindowKeepScreenOn",
651         env, CreateAsyncTaskWithLastParam(env, lastParam, std::move(execute), std::move(complete), &result));
652     return result;
653 }
654 
OnSetWindowBrightness(napi_env env,napi_callback_info info)655 napi_value JsExtensionWindow::OnSetWindowBrightness(napi_env env, napi_callback_info info)
656 {
657     WmErrorCode errCode = WmErrorCode::WM_OK;
658     size_t argc = ARG_COUNT_TWO;
659     napi_value argv[INDEX_TWO] = {nullptr};
660     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
661     if (argc < ARG_COUNT_ONE) {
662         TLOGE(WmsLogTag::WMS_UIEXT, "Argc is invalid: %{public}zu", argc);
663         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
664     }
665     double brightness = UNDEFINED_BRIGHTNESS;
666     if (errCode == WmErrorCode::WM_OK) {
667         napi_value nativeVal = argv[0];
668         if (nativeVal == nullptr) {
669             TLOGE(WmsLogTag::WMS_UIEXT, "Failed to convert parameter to brightness");
670             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
671         } else {
672             CHECK_NAPI_RETCODE(errCode, WmErrorCode::WM_ERROR_INVALID_PARAM,
673                 napi_get_value_double(env, nativeVal, &brightness));
674         }
675     }
676     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
677         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
678     }
679 
680     napi_value lastParam = nullptr;
681     if (argc > ARG_COUNT_ONE && argv[INDEX_ONE] != nullptr && GetType(env, argv[INDEX_ONE]) == napi_function) {
682         lastParam = argv[INDEX_ONE];
683     }
684     napi_value result = nullptr;
685     std::shared_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, lastParam, &result);
686     auto asyncTask = [weakToken = wptr<Window>(extensionWindow_->GetWindow()), brightness, env, task = napiAsyncTask] {
687         auto weakWindow = weakToken.promote();
688         if (weakWindow == nullptr) {
689             task->Reject(env,
690                 CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "Invalidate params."));
691             return;
692         }
693         WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->ExtensionSetBrightness(brightness));
694         if (ret == WmErrorCode::WM_OK) {
695             task->Resolve(env, NapiGetUndefined(env));
696         } else {
697             task->Reject(env, CreateJsError(env, static_cast<int32_t>(ret), "Window set brightness failed"));
698         }
699         TLOGNI(WmsLogTag::WMS_ATTRIBUTE, "Window [%{public}u, %{public}s] set brightness end, result: %{public}d",
700             weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
701     };
702     if (napi_status::napi_ok != napi_send_event(env, asyncTask, napi_eprio_high)) {
703         TLOGE(WmsLogTag::WMS_IMMS, "napi_send_event failed");
704         napiAsyncTask->Reject(env,
705             CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "failed to send event"));
706     }
707     return result;
708 }
709 
OnGetUIContext(napi_env env,napi_callback_info info)710 napi_value JsExtensionWindow::OnGetUIContext(napi_env env, napi_callback_info info)
711 {
712     size_t argc = 4;
713     napi_value argv[4] = {nullptr};
714     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
715     if (argc >= 1) {
716         TLOGE(WmsLogTag::WMS_UIEXT, "Argc is invalid: %{public}zu, expect zero params", argc);
717         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
718     }
719     sptr<Window> windowImpl = extensionWindow_->GetWindow();
720     if (windowImpl == nullptr) {
721         TLOGE(WmsLogTag::WMS_UIEXT, "window is nullptr");
722         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
723     }
724 
725     auto uicontent = windowImpl->GetUIContent();
726     if (uicontent == nullptr) {
727         TLOGW(WmsLogTag::WMS_UIEXT, "uicontent is nullptr");
728         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
729     }
730 
731     napi_value uiContext = uicontent->GetUINapiContext();
732     if (uiContext == nullptr) {
733         TLOGE(WmsLogTag::WMS_UIEXT, "uiContext obtained from jsEngine is nullptr");
734         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
735     } else {
736         return uiContext;
737     }
738 }
739 
OnSetSpecificSystemBarEnabled(napi_env env,napi_callback_info info)740 napi_value JsExtensionWindow::OnSetSpecificSystemBarEnabled(napi_env env, napi_callback_info info)
741 {
742     size_t argc = FOUR_PARAMS_SIZE;
743     napi_value argv[FOUR_PARAMS_SIZE] = { nullptr };
744     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
745     std::string name;
746     if (!ConvertFromJsValue(env, argv[INDEX_ZERO], name) ||
747         (name.compare("status") != 0 && name.compare("navigation") != 0 && name.compare("navigationIndicator") != 0)) {
748         TLOGE(WmsLogTag::WMS_IMMS, "invalid systemBar name");
749         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
750     }
751 
752     bool systemBarEnable = false;
753     bool systemBarEnableAnimation = false;
754     if (!GetSpecificBarStatus(env, info, systemBarEnable, systemBarEnableAnimation)) {
755         TLOGE(WmsLogTag::WMS_IMMS, "invalid param or argc:%{public}zu", argc);
756         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
757     }
758     napi_value result = nullptr;
759     const char* const where = __func__;
760     std::shared_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, nullptr, &result);
761     auto asyncTask = [weakToken = wptr<Window>(extensionWindow_->GetWindow()), env, task = napiAsyncTask,
762         name, systemBarEnable, systemBarEnableAnimation, where] {
763         auto window = weakToken.promote();
764         if (window == nullptr) {
765             TLOGNE(WmsLogTag::WMS_IMMS, "%{public}s window is nullptr", where);
766             task->Reject(env, CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
767             return;
768         }
769         auto ret = window->UpdateHostSpecificSystemBarEnabled(name, systemBarEnable, systemBarEnableAnimation);
770         auto errCode = WM_JS_TO_ERROR_CODE_MAP.at(ret);
771         if (errCode == WmErrorCode::WM_OK) {
772             task->Resolve(env, NapiGetUndefined(env));
773         } else {
774             TLOGNE(WmsLogTag::WMS_IMMS, "%{public}s failed, ret %{public}d", where, errCode);
775             task->Reject(env, CreateJsError(env, static_cast<int32_t>(errCode),
776                 "JsExtensionWindow::OnSetSpecificSystemBarEnabled failed"));
777         }
778     };
779     if (napi_send_event(env, asyncTask, napi_eprio_high, "OnSetSpecificSystemBarEnabled") != napi_status::napi_ok) {
780         TLOGE(WmsLogTag::WMS_IMMS, "napi_send_event failed");
781         napiAsyncTask->Reject(env, CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY),
782             "JsExtensionWindow::OnSetSpecificSystemBarEnabled failed"));
783     }
784     return result;
785 }
786 
OnGetWindowPropertiesSync(napi_env env,napi_callback_info info)787 napi_value JsExtensionWindow::OnGetWindowPropertiesSync(napi_env env, napi_callback_info info)
788 {
789     sptr<Window> windowImpl = extensionWindow_->GetWindow();
790     if (windowImpl == nullptr) {
791         TLOGW(WmsLogTag::WMS_UIEXT, "window is nullptr");
792         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
793     }
794     auto objValue = CreateJsExtensionWindowProperties(env, windowImpl);
795     TLOGI(WmsLogTag::WMS_UIEXT, "Window [%{public}u, %{public}s] get properties end",
796         windowImpl->GetWindowId(), windowImpl->GetWindowName().c_str());
797     if (objValue != nullptr) {
798         return objValue;
799     } else {
800         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
801     }
802 }
803 
OnIsWindowShowingSync(napi_env env,napi_callback_info info)804 napi_value JsExtensionWindow::OnIsWindowShowingSync(napi_env env, napi_callback_info info)
805 {
806     sptr<Window> windowImpl = extensionWindow_->GetWindow();
807     if (windowImpl == nullptr) {
808         TLOGE(WmsLogTag::WMS_UIEXT, "window is nullptr");
809         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
810     }
811     bool state = (windowImpl->GetWindowState() == WindowState::STATE_SHOWN);
812     TLOGI(WmsLogTag::WMS_UIEXT, "Window [%{public}u, %{public}s] get show state end, state=%{public}u",
813         windowImpl->GetWindowId(), windowImpl->GetWindowName().c_str(), state);
814     return CreateJsValue(env, state);
815 }
816 
OnSetUIContent(napi_env env,napi_callback_info info)817 napi_value JsExtensionWindow::OnSetUIContent(napi_env env, napi_callback_info info)
818 {
819     WmErrorCode errCode = WmErrorCode::WM_OK;
820     size_t argc = 4;
821     napi_value argv[4] = {nullptr};
822     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
823     if (argc < 1) { // 2 maximum param num
824         TLOGE(WmsLogTag::WMS_UIEXT, "Argc is invalid: %{public}zu", argc);
825         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
826     }
827     std::string contextUrl;
828     if (errCode == WmErrorCode::WM_OK && !ConvertFromJsValue(env, argv[0], contextUrl)) {
829         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to convert parameter to context url");
830         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
831     }
832     napi_value lastParam = nullptr;
833     if (argc >= 2) { // 2 param num
834         lastParam = argv[1];
835     }
836     std::shared_ptr<NativeReference> contentStorage = nullptr;
837     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
838         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
839     }
840 
841     sptr<IRemoteObject> parentToken = sessionInfo_->parentToken;
842     napi_value result = nullptr;
843     std::shared_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, lastParam, &result);
844     auto asyncTask = [extWindow = extensionWindow_, contentStorage, contextUrl, parentToken,
845         env, task = napiAsyncTask]() {
846         if (extWindow == nullptr) {
847             TLOGNE(WmsLogTag::WMS_UIEXT, "Window is nullptr");
848             task->Reject(env,
849                 CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
850             return;
851         }
852         LoadContentTask(contentStorage, contextUrl, extWindow, env, *task, parentToken, false);
853     };
854     if (napi_send_event(env, asyncTask, napi_eprio_high, "OnSetUIContent") != napi_status::napi_ok) {
855         napiAsyncTask->Reject(env, CreateJsError(env,
856             static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "send event failed"));
857     }
858     return result;
859 }
860 
OnLoadContent(napi_env env,napi_callback_info info,bool isLoadedByName)861 napi_value JsExtensionWindow::OnLoadContent(napi_env env, napi_callback_info info, bool isLoadedByName)
862 {
863     TLOGI(WmsLogTag::WMS_UIEXT, "OnLoadContent is called");
864     WmErrorCode errCode = WmErrorCode::WM_OK;
865     size_t argc = 4;
866     napi_value argv[4] = {nullptr};
867     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
868     std::string contextUrl;
869     if (!ConvertFromJsValue(env, argv[0], contextUrl)) {
870         TLOGI(WmsLogTag::WMS_UIEXT, "Failed to convert parameter to context url");
871         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
872     }
873     napi_value storage = nullptr;
874     napi_value lastParam = nullptr;
875     napi_value value1 = argv[1];
876     napi_value value2 = argv[2];
877     if (GetType(env, value1) == napi_function) {
878         lastParam = value1;
879     } else if (GetType(env, value1) == napi_object) {
880         storage = value1;
881     }
882     if (GetType(env, value2) == napi_function) {
883         lastParam = value2;
884     }
885     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
886         TLOGI(WmsLogTag::WMS_UIEXT, "Invalid param");
887         napi_throw(env, CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
888         return NapiGetUndefined(env);
889     }
890 
891     std::shared_ptr<NativeReference> contentStorage = nullptr;
892     if (storage != nullptr) {
893         napi_ref result = nullptr;
894         napi_create_reference(env, storage, 1, &result);
895         contentStorage = std::shared_ptr<NativeReference>(reinterpret_cast<NativeReference*>(result));
896     }
897 
898     sptr<IRemoteObject> parentToken = sessionInfo_->parentToken;
899     napi_value result = nullptr;
900     std::shared_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, lastParam, &result);
901     auto asyncTask = [extWindow = extensionWindow_, contentStorage, contextUrl, parentToken, isLoadedByName,
902         env, task = napiAsyncTask]() {
903         if (extWindow == nullptr) {
904             TLOGNE(WmsLogTag::WMS_UIEXT, "Window is nullptr");
905             task->Reject(env, CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
906             return;
907         }
908         LoadContentTask(contentStorage, contextUrl, extWindow, env, *task, parentToken, isLoadedByName);
909     };
910     if (napi_send_event(env, asyncTask, napi_eprio_high, "OnLoadContent") != napi_status::napi_ok) {
911         napiAsyncTask->Reject(env, CreateJsError(env,
912             static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "send event failed"));
913     }
914     return result;
915 }
916 
OnGetWindowAvoidArea(napi_env env,napi_callback_info info)917 napi_value JsExtensionWindow::OnGetWindowAvoidArea(napi_env env, napi_callback_info info)
918 {
919     TLOGD(WmsLogTag::WMS_UIEXT, "OnGetWindowAvoidArea is called");
920 
921     WmErrorCode errCode = WmErrorCode::WM_OK;
922     size_t argc = 4;
923     napi_value argv[4] = {nullptr};
924     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
925     if (argc < 1) { // 1: params num
926         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
927     }
928     AvoidAreaType avoidAreaType = AvoidAreaType::TYPE_SYSTEM;
929     napi_value nativeMode = argv[0];
930     if (nativeMode == nullptr) {
931         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
932     } else {
933         uint32_t resultValue = 0;
934         napi_get_value_uint32(env, nativeMode, &resultValue);
935         avoidAreaType = static_cast<AvoidAreaType>(resultValue);
936         errCode = avoidAreaType >= AvoidAreaType::TYPE_END ?
937                   WmErrorCode::WM_ERROR_INVALID_PARAM : WmErrorCode::WM_OK;
938     }
939     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
940         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
941     }
942 
943     if (extensionWindow_ == nullptr) {
944         TLOGE(WmsLogTag::WMS_UIEXT, "extensionWindow_ is nullptr");
945         napi_throw(env, CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STAGE_ABNORMALLY)));
946         return CreateJsValue(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STAGE_ABNORMALLY));
947     }
948     // getAvoidRect by avoidAreaType
949     AvoidArea avoidArea;
950     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(extensionWindow_->GetAvoidAreaByType(avoidAreaType, avoidArea));
951     if (ret != WmErrorCode::WM_OK) {
952         TLOGE(WmsLogTag::WMS_UIEXT, "OnGetAvoidAreaByType failed");
953         avoidArea.topRect_ = g_emptyRect;
954         avoidArea.leftRect_ = g_emptyRect;
955         avoidArea.rightRect_ = g_emptyRect;
956         avoidArea.bottomRect_ = g_emptyRect;
957     }
958     napi_value avoidAreaObj = ConvertAvoidAreaToJsValue(env, avoidArea, avoidAreaType);
959     if (avoidAreaObj != nullptr) {
960         TLOGD(WmsLogTag::WMS_UIEXT, "avoidAreaObj is finish");
961         return avoidAreaObj;
962     } else {
963         TLOGE(WmsLogTag::WMS_UIEXT, "avoidAreaObj is nullptr");
964         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
965     }
966 }
967 
OnRegisterRectChangeCallback(napi_env env,size_t argc,napi_value * argv,const sptr<Window> & windowImpl)968 napi_value JsExtensionWindow::OnRegisterRectChangeCallback(napi_env env, size_t argc, napi_value* argv,
969     const sptr<Window>& windowImpl)
970 {
971     if (argc < ARG_COUNT_THREE) {
972         TLOGE(WmsLogTag::WMS_UIEXT, "OnRectChange: argc is invalid: %{public}zu", argc);
973         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
974     }
975     if (!windowImpl->IsPcWindow()) {
976         TLOGE(WmsLogTag::WMS_UIEXT, "Device is not PC");
977         return NapiThrowError(env, WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT);
978     }
979     uint32_t reasons = 0;
980     if (!ConvertFromJsValue(env, argv[INDEX_ONE], reasons)) {
981         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to convert parameter to rectChangeReasons");
982         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
983     }
984     if (reasons != static_cast<uint32_t>(ComponentRectChangeReason::HOST_WINDOW_RECT_CHANGE)) {
985         TLOGE(WmsLogTag::WMS_UIEXT, "Unsupported rect change reasons");
986         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
987     }
988     napi_value cbValue = argv[INDEX_TWO];
989     if (!NapiIsCallable(env, cbValue)) {
990         TLOGE(WmsLogTag::WMS_UIEXT, "Callback(info->argv[2]) is not callable");
991         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
992     }
993     WmErrorCode ret = extensionRegisterManager_->RegisterListener(windowImpl, RECT_CHANGE, CaseType::CASE_WINDOW,
994         env, cbValue);
995     if (ret != WmErrorCode::WM_OK) {
996         TLOGW(WmsLogTag::WMS_UIEXT, "Failed, window [%{public}u, %{public}s], type=%{public}s, reasons=%{public}u",
997             windowImpl->GetWindowId(), windowImpl->GetWindowName().c_str(), RECT_CHANGE.c_str(), reasons);
998         return NapiThrowError(env, ret);
999     }
1000     TLOGI(WmsLogTag::WMS_UIEXT, "Success, window [%{public}u, %{public}s], type=%{public}s, reasons=%{public}u",
1001         windowImpl->GetWindowId(), windowImpl->GetWindowName().c_str(), RECT_CHANGE.c_str(), reasons);
1002     return NapiGetUndefined(env);
1003 }
1004 
OnRegisterExtensionWindowCallback(napi_env env,napi_callback_info info,bool atomicService)1005 napi_value JsExtensionWindow::OnRegisterExtensionWindowCallback(napi_env env, napi_callback_info info,
1006     bool atomicService)
1007 {
1008     sptr<Window> windowImpl = extensionWindow_->GetWindow();
1009     if (windowImpl == nullptr) {
1010         TLOGE(WmsLogTag::WMS_UIEXT, "WindowImpl is nullptr");
1011         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1012     }
1013     size_t argc = FOUR_PARAMS_SIZE;
1014     napi_value argv[FOUR_PARAMS_SIZE] = {nullptr};
1015     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1016     if (argc < ARG_COUNT_TWO) {
1017         TLOGE(WmsLogTag::WMS_UIEXT, "Argc is invalid: %{public}zu", argc);
1018         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1019     }
1020     std::string cbType;
1021     if (!ConvertFromJsValue(env, argv[INDEX_ZERO], cbType)) {
1022         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to convert parameter to callbackType");
1023         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1024     }
1025     if (cbType == RECT_CHANGE) {
1026         return OnRegisterRectChangeCallback(env, argc, argv, windowImpl);
1027     }
1028     if (atomicService) {
1029         if (IsInvalidListener(cbType)) {
1030             return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_CALLING);
1031         }
1032         if (IsEmptyListener(cbType)) {
1033             return NapiGetUndefined(env);
1034         }
1035         if (IsUnsupportListener(cbType)) {
1036             return NapiThrowError(env, WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT);
1037         }
1038     }
1039     napi_value value = argv[INDEX_ONE];
1040     if (!NapiIsCallable(env, value)) {
1041         TLOGE(WmsLogTag::WMS_UIEXT, "Callback(info->argv[1]) is not callable");
1042         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1043     }
1044     WmErrorCode ret = WmErrorCode::WM_OK;
1045     if (atomicService) {
1046         ret = extensionRegisterManager_->AtomicServiceRegisterListener(windowImpl, cbType, CaseType::CASE_WINDOW,
1047             env, value);
1048     } else {
1049         ret = extensionRegisterManager_->RegisterListener(windowImpl, cbType, CaseType::CASE_WINDOW,
1050             env, value);
1051     }
1052     if (ret != WmErrorCode::WM_OK) {
1053         TLOGE(WmsLogTag::WMS_UIEXT, "Callback(info->argv[1]) is not callable");
1054         return NapiThrowError(env, ret);
1055     }
1056     TLOGI(WmsLogTag::WMS_UIEXT, "Register end, window [%{public}u, %{public}s], type=%{public}s",
1057           windowImpl->GetWindowId(), windowImpl->GetWindowName().c_str(), cbType.c_str());
1058     return NapiGetUndefined(env);
1059 }
1060 
OnUnRegisterExtensionWindowCallback(napi_env env,napi_callback_info info,bool atomicService)1061 napi_value JsExtensionWindow::OnUnRegisterExtensionWindowCallback(napi_env env, napi_callback_info info,
1062     bool atomicService)
1063 {
1064     sptr<Window> windowImpl = extensionWindow_->GetWindow();
1065     if (windowImpl == nullptr) {
1066         TLOGE(WmsLogTag::WMS_UIEXT, "windowImpl is nullptr");
1067         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1068     }
1069     size_t argc = FOUR_PARAMS_SIZE;
1070     napi_value argv[FOUR_PARAMS_SIZE] = {nullptr};
1071     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1072     if (argc < ARG_COUNT_ONE) {
1073         TLOGE(WmsLogTag::WMS_UIEXT, "Argc is invalid: %{public}zu", argc);
1074         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1075     }
1076     std::string cbType;
1077     if (!ConvertFromJsValue(env, argv[INDEX_ZERO], cbType)) {
1078         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to convert parameter to callbackType");
1079         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1080     }
1081     if (cbType == RECT_CHANGE) {
1082         if (!windowImpl->IsPcWindow()) {
1083             TLOGE(WmsLogTag::WMS_UIEXT, "Device is not PC");
1084             return NapiThrowError(env, WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT);
1085         }
1086     }
1087     if (atomicService) {
1088         if (IsInvalidListener(cbType)) {
1089             return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_CALLING);
1090         }
1091         if (IsEmptyListener(cbType)) {
1092             return NapiGetUndefined(env);
1093         }
1094         if (IsUnsupportListener(cbType)) {
1095             return NapiThrowError(env, WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT);
1096         }
1097     }
1098 
1099     napi_value value = nullptr;
1100     WmErrorCode ret = WmErrorCode::WM_OK;
1101     if (argc > ARG_COUNT_ONE && argv[INDEX_ONE] != nullptr && NapiIsCallable(env, argv[INDEX_ONE])) {
1102         value = argv[INDEX_ONE];
1103     }
1104     if (atomicService) {
1105         ret = extensionRegisterManager_->AtomicServiceUnregisterListener(windowImpl, cbType,
1106             CaseType::CASE_WINDOW, env, value);
1107     } else {
1108         ret = extensionRegisterManager_->UnregisterListener(windowImpl, cbType, CaseType::CASE_WINDOW, env, value);
1109     }
1110 
1111     if (ret != WmErrorCode::WM_OK) {
1112         return NapiThrowError(env, ret);
1113     }
1114     TLOGI(WmsLogTag::WMS_UIEXT, "UnRegister end, window [%{public}u, %{public}s], type=%{public}s",
1115           windowImpl->GetWindowId(), windowImpl->GetWindowName().c_str(), cbType.c_str());
1116     return NapiGetUndefined(env);
1117 }
1118 
OnHideNonSecureWindows(napi_env env,napi_callback_info info)1119 napi_value JsExtensionWindow::OnHideNonSecureWindows(napi_env env, napi_callback_info info)
1120 {
1121     if (extensionWindow_ == nullptr) {
1122         TLOGE(WmsLogTag::WMS_UIEXT, "extensionWindow_ is nullptr");
1123         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1124     }
1125     sptr<Window> windowImpl = extensionWindow_->GetWindow();
1126     if (windowImpl == nullptr) {
1127         TLOGE(WmsLogTag::WMS_UIEXT, "windowImpl is nullptr");
1128         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1129     }
1130     size_t argc = 4;
1131     napi_value argv[4] = {nullptr};
1132     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1133     if (argc < 1) {
1134         TLOGE(WmsLogTag::WMS_UIEXT, "Argc is invalid: %{public}zu", argc);
1135         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1136     }
1137     bool shouldHide = false;
1138     if (!ConvertFromJsValue(env, argv[0], shouldHide)) {
1139         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to convert parameter to bool");
1140         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1141     }
1142 
1143     WmErrorCode ret = WmErrorCode::WM_OK;
1144     ret = WM_JS_TO_ERROR_CODE_MAP.at(extensionWindow_->HideNonSecureWindows(shouldHide));
1145     if (ret != WmErrorCode::WM_OK) {
1146         return NapiThrowError(env, ret);
1147     }
1148     TLOGI(WmsLogTag::WMS_UIEXT, "end, window [%{public}u, %{public}s], shouldHide:%{public}u",
1149           windowImpl->GetWindowId(), windowImpl->GetWindowName().c_str(), shouldHide);
1150     return NapiGetUndefined(env);
1151 }
1152 
OnSetWaterMarkFlag(napi_env env,napi_callback_info info)1153 napi_value JsExtensionWindow::OnSetWaterMarkFlag(napi_env env, napi_callback_info info)
1154 {
1155     if (extensionWindow_ == nullptr) {
1156         TLOGE(WmsLogTag::WMS_UIEXT, "extensionWindow_ is nullptr");
1157         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1158     }
1159     sptr<Window> windowImpl = extensionWindow_->GetWindow();
1160     if (windowImpl == nullptr) {
1161         TLOGE(WmsLogTag::WMS_UIEXT, "windowImpl is nullptr");
1162         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1163     }
1164     size_t argc = 4;
1165     napi_value argv[4] = {nullptr};
1166     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1167     if (argc < 1) {
1168         TLOGE(WmsLogTag::WMS_UIEXT, "Argc is invalid: %{public}zu", argc);
1169         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1170     }
1171     bool isEnable = false;
1172     if (!ConvertFromJsValue(env, argv[0], isEnable)) {
1173         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to convert parameter to bool");
1174         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1175     }
1176 
1177     WmErrorCode ret = WmErrorCode::WM_OK;
1178     ret = WM_JS_TO_ERROR_CODE_MAP.at(extensionWindow_->SetWaterMarkFlag(isEnable));
1179     if (ret != WmErrorCode::WM_OK) {
1180         return NapiThrowError(env, ret);
1181     }
1182     TLOGI(WmsLogTag::WMS_UIEXT, "end, window [%{public}u, %{public}s], isEnable:%{public}u.",
1183           windowImpl->GetWindowId(), windowImpl->GetWindowName().c_str(), isEnable);
1184     return NapiGetUndefined(env);
1185 }
1186 
OnHidePrivacyContentForHost(napi_env env,napi_callback_info info)1187 napi_value JsExtensionWindow::OnHidePrivacyContentForHost(napi_env env, napi_callback_info info)
1188 {
1189     if (extensionWindow_ == nullptr) {
1190         TLOGE(WmsLogTag::WMS_UIEXT, "extension window is nullptr");
1191         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1192     }
1193 
1194     sptr<Window> windowImpl = extensionWindow_->GetWindow();
1195     if (windowImpl == nullptr) {
1196         TLOGE(WmsLogTag::WMS_UIEXT, "windowImpl is nullptr");
1197         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1198     }
1199 
1200     size_t argc = 4;
1201     napi_value argv[4] = {nullptr};
1202     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1203     if (argc < 1) {
1204         TLOGE(WmsLogTag::WMS_UIEXT, "Argc is invalid: %{public}zu", argc);
1205         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1206     }
1207 
1208     bool needHide = false;
1209     if (!ConvertFromJsValue(env, argv[0], needHide)) {
1210         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to convert parameter to bool");
1211         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1212     }
1213 
1214     auto ret = WM_JS_TO_ERROR_CODE_MAP.at(extensionWindow_->HidePrivacyContentForHost(needHide));
1215     if (ret != WmErrorCode::WM_OK) {
1216         return NapiThrowError(env, ret);
1217     }
1218 
1219     TLOGI(WmsLogTag::WMS_UIEXT, "finished, window [%{public}u, %{public}s], needHide:%{public}u.",
1220           windowImpl->GetWindowId(), windowImpl->GetWindowName().c_str(), needHide);
1221 
1222     return NapiGetUndefined(env);
1223 }
1224 
GetProperties(napi_env env,napi_callback_info info)1225 napi_value JsExtensionWindow::GetProperties(napi_env env, napi_callback_info info)
1226 {
1227     TLOGI(WmsLogTag::WMS_UIEXT, "in");
1228     napi_value jsThis;
1229     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr));
1230 
1231     JsExtensionWindow* jsExtensionWindow = nullptr;
1232     NAPI_CALL(env, napi_unwrap(env, jsThis, reinterpret_cast<void**>(&jsExtensionWindow)));
1233     if (!jsExtensionWindow || !jsExtensionWindow->extensionWindow_) {
1234         TLOGE(WmsLogTag::WMS_UIEXT, "window is nullptr");
1235         return nullptr;
1236     }
1237     sptr<Rosen::Window> window = jsExtensionWindow->extensionWindow_->GetWindow();
1238     return CreateJsExtensionWindowPropertiesObject(env, window);
1239 }
1240 
OnCreateSubWindowWithOptions(napi_env env,napi_callback_info info)1241 napi_value JsExtensionWindow::OnCreateSubWindowWithOptions(napi_env env, napi_callback_info info)
1242 {
1243     if (extensionWindow_ == nullptr) {
1244         TLOGE(WmsLogTag::WMS_UIEXT, "extensionWindow is null");
1245         napi_throw(env, CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
1246         return NapiGetUndefined(env);
1247     }
1248     size_t argc = 4;
1249     napi_value argv[4] = {nullptr};
1250     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1251     std::string windowName;
1252     if (!ConvertFromJsValue(env, argv[0], windowName)) {
1253         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to convert parameter to windowName");
1254         napi_throw(env, CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
1255         return NapiGetUndefined(env);
1256     }
1257     sptr<WindowOption> option = new WindowOption();
1258     if (!ParseSubWindowOptions(env, argv[1], option)) {
1259         TLOGE(WmsLogTag::WMS_UIEXT, "Get invalid options param");
1260         napi_throw(env, CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
1261         return NapiGetUndefined(env);
1262     }
1263     if ((option->GetWindowFlags() & static_cast<uint32_t>(WindowFlag::WINDOW_FLAG_IS_APPLICATION_MODAL)) &&
1264         !extensionWindow_->IsPcOrPadFreeMultiWindowMode()) {
1265         TLOGE(WmsLogTag::WMS_SUB, "device not support");
1266         napi_throw(env, CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT)));
1267         return NapiGetUndefined(env);
1268     }
1269     if (option->GetWindowTopmost() && !Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
1270         TLOGE(WmsLogTag::WMS_SUB, "Modal subwindow has topmost, but no system permission");
1271         napi_throw(env, CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_NOT_SYSTEM_APP)));
1272         return NapiGetUndefined(env);
1273     }
1274     option->SetParentId(hostWindowId_);
1275     const char* const where = __func__;
1276     napi_value lastParam = (argv[2] != nullptr && GetType(env, argv[2]) == napi_function) ? argv[2] : nullptr;
1277     napi_value result = nullptr;
1278     std::shared_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, lastParam, &result);
1279     auto asyncTask = [where, extensionWindow = extensionWindow_, windowName = std::move(windowName),
1280         windowOption = option, env, task = napiAsyncTask]() mutable {
1281         auto extWindow = extensionWindow->GetWindow();
1282         if (extWindow == nullptr) {
1283             task->Reject(env, CreateJsError(env,
1284                 static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "extension's window is null"));
1285             return;
1286         }
1287         windowOption->SetWindowType(Rosen::WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
1288         windowOption->SetWindowMode(Rosen::WindowMode::WINDOW_MODE_FLOATING);
1289         windowOption->SetOnlySupportSceneBoard(true);
1290         windowOption->SetIsUIExtFirstSubWindow(true);
1291         auto window = Window::Create(windowName, windowOption, extWindow->GetContext());
1292         if (window == nullptr) {
1293             task->Reject(env, CreateJsError(env,
1294                 static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "create sub window failed"));
1295             return;
1296         }
1297         if (!window->IsTopmost()) {
1298             extWindow->NotifyModalUIExtensionMayBeCovered(false);
1299         }
1300         task->Resolve(env, CreateJsWindowObject(env, window));
1301         TLOGNI(WmsLogTag::WMS_UIEXT, "%{public}s %{public}s end", where, windowName.c_str());
1302     };
1303     if (napi_send_event(env, asyncTask, napi_eprio_high, "OnCreateSubWindowWithOptions") != napi_status::napi_ok) {
1304         napiAsyncTask->Reject(env, CreateJsError(env,
1305             static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "send event failed"));
1306     }
1307     return result;
1308 }
1309 
OnOccupyEvents(napi_env env,napi_callback_info info)1310 napi_value JsExtensionWindow::OnOccupyEvents(napi_env env, napi_callback_info info)
1311 {
1312     size_t argc = FOUR_PARAMS_SIZE;
1313     napi_value argv[FOUR_PARAMS_SIZE] = {nullptr};
1314     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1315     if (argc < 1) {
1316         TLOGE(WmsLogTag::WMS_UIEXT, "Argc is invalid: %{public}zu", argc);
1317         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1318     }
1319     int32_t eventFlags = 0;
1320     if (!ConvertFromJsValue(env, argv[0], eventFlags)) {
1321         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to convert parameter to int32_t");
1322         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1323     }
1324     napi_value lastParam = (argc <= 1) ? nullptr : (GetType(env, argv[1]) == napi_function ? argv[1] : nullptr);
1325     napi_value result = nullptr;
1326     std::shared_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, lastParam, &result);
1327     auto asyncTask = [weakToken = std::weak_ptr<ExtensionWindow>(extensionWindow_), eventFlags, env,
1328         task = napiAsyncTask] {
1329         auto weakWindow = weakToken.lock();
1330         if (weakWindow == nullptr) {
1331             TLOGNE(WmsLogTag::WMS_UIEXT, "window is nullptr");
1332             task->Reject(env, CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY),
1333                 "OnOccupyEvents failed"));
1334             return;
1335         }
1336         auto ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->OccupyEvents(eventFlags));
1337         if (ret == WmErrorCode::WM_OK) {
1338             task->Resolve(env, NapiGetUndefined(env));
1339         } else {
1340             TLOGNE(WmsLogTag::WMS_UIEXT, "OnOccupyEvents failed, code: %{public}d", ret);
1341             task->Reject(env, CreateJsError(env, static_cast<int32_t>(ret), "OnOccupyEvents failed"));
1342         }
1343     };
1344     if (napi_send_event(env, asyncTask, napi_eprio_high, "OnOccupyEvents") != napi_status::napi_ok) {
1345         TLOGE(WmsLogTag::WMS_UIEXT, "napi_send_event failed");
1346         napiAsyncTask->Reject(env,
1347             CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "failed to send event"));
1348     }
1349     return result;
1350 }
1351 
OnSetWindowLayoutFullScreen(napi_env env,napi_callback_info info)1352 napi_value JsExtensionWindow::OnSetWindowLayoutFullScreen(napi_env env, napi_callback_info info)
1353 {
1354     WmErrorCode errCode = WmErrorCode::WM_OK;
1355     size_t argc = 4;
1356     napi_value argv[4] = {nullptr};
1357     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1358     if (argc < 1) { // 1: params num
1359         TLOGE(WmsLogTag::WMS_IMMS, "Argc is invalid %{public}zu", argc);
1360         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1361     }
1362     bool isLayoutFullScreen = false;
1363     if (errCode == WmErrorCode::WM_OK) {
1364         napi_value nativeVal = argv[0];
1365         if (nativeVal == nullptr) {
1366             TLOGE(WmsLogTag::WMS_IMMS, "Failed to convert parameter to isLayoutFullScreen");
1367             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1368         } else {
1369             CHECK_NAPI_RETCODE(errCode, WmErrorCode::WM_ERROR_INVALID_PARAM,
1370                 napi_get_value_bool(env, nativeVal, &isLayoutFullScreen));
1371         }
1372     }
1373     if (errCode != WmErrorCode::WM_OK) {
1374         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1375     }
1376     const char* const where = __func__;
1377     NapiAsyncTask::CompleteCallback complete =
1378         [weakToken = wptr<Window>(extensionWindow_->GetWindow()), isLayoutFullScreen, where](napi_env env,
1379             NapiAsyncTask& task, int32_t status) {
1380             auto window = weakToken.promote();
1381             if (window == nullptr) {
1382                 TLOGNE(WmsLogTag::WMS_IMMS, "%{public}s window is nullptr", where);
1383                 task.Reject(env, CreateJsError(env,
1384                     static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "Invalidate params."));
1385                 return;
1386             }
1387             // compatibleModeInPc need apply avoidArea method
1388             if (window->IsPcOrPadFreeMultiWindowMode() && !window->GetCompatibleModeInPc()) {
1389                 TLOGNE(WmsLogTag::WMS_IMMS, "%{public}s device not support", where);
1390                 task.Resolve(env, NapiGetUndefined(env));
1391                 return;
1392             }
1393             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(window->SetLayoutFullScreen(isLayoutFullScreen));
1394             if (ret == WmErrorCode::WM_OK) {
1395                 task.Resolve(env, NapiGetUndefined(env));
1396             } else {
1397                 TLOGNE(WmsLogTag::WMS_IMMS, "%{public}s failed, ret %{public}d", where, ret);
1398                 task.Reject(env, CreateJsError(env, static_cast<int32_t>(ret), "Window OnSetLayoutFullScreen failed."));
1399             }
1400         };
1401 
1402     napi_value lastParam = (argc <= 1) ? nullptr :
1403         ((argv[1] != nullptr && GetType(env, argv[1]) == napi_function) ? argv[1] : nullptr);
1404     napi_value result = nullptr;
1405     auto asyncTask = CreateAsyncTask(env, lastParam, nullptr,
1406         std::make_unique<NapiAsyncTask::CompleteCallback>(std::move(complete)), &result);
1407     NapiAsyncTask::Schedule("JsExtensionWindow::OnSetWindowLayoutFullScreen", env, std::move(asyncTask));
1408     return result;
1409 }
1410 
OnGetWindowColorSpace(napi_env env,napi_callback_info info)1411 napi_value JsExtensionWindow::OnGetWindowColorSpace(napi_env env, napi_callback_info info)
1412 {
1413     sptr<Window> window = extensionWindow_->GetWindow();
1414     if (window == nullptr) {
1415         TLOGE(WmsLogTag::WMS_UIEXT, "window is nullptr");
1416         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1417     }
1418     ColorSpace colorSpace = window->GetColorSpace();
1419     TLOGI(WmsLogTag::WMS_UIEXT, "end, window [%{public}u, %{public}s] colorSpace=%{public}u",
1420         window->GetWindowId(), window->GetWindowName().c_str(), static_cast<uint32_t>(colorSpace));
1421 
1422     return CreateJsValue(env, static_cast<uint32_t>(colorSpace));
1423 }
1424 
OnSetWindowColorSpace(napi_env env,napi_callback_info info)1425 napi_value JsExtensionWindow::OnSetWindowColorSpace(napi_env env, napi_callback_info info)
1426 {
1427     WmErrorCode errCode = WmErrorCode::WM_OK;
1428     ColorSpace colorSpace = ColorSpace::COLOR_SPACE_DEFAULT;
1429     size_t argc = 4;
1430     napi_value argv[4] = {nullptr};
1431     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1432     if (argc < 1) { // 1: params num
1433         TLOGE(WmsLogTag::WMS_UIEXT, "Argc is invalid: %{public}zu", argc);
1434         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1435     } else {
1436         napi_value nativeType = argv[0];
1437         if (nativeType == nullptr) {
1438             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1439             TLOGE(WmsLogTag::WMS_UIEXT, "Failed to convert parameter to ColorSpace");
1440         } else {
1441             uint32_t resultValue = 0;
1442             CHECK_NAPI_RETCODE(errCode, WmErrorCode::WM_ERROR_INVALID_PARAM,
1443                 napi_get_value_uint32(env, nativeType, &resultValue));
1444             colorSpace = static_cast<ColorSpace>(resultValue);
1445             if (colorSpace > ColorSpace::COLOR_SPACE_WIDE_GAMUT || colorSpace < ColorSpace::COLOR_SPACE_DEFAULT) {
1446                 TLOGE(WmsLogTag::WMS_UIEXT, "ColorSpace %{public}u invalid!", static_cast<uint32_t>(colorSpace));
1447                 errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1448             }
1449         }
1450     }
1451     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
1452         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1453     }
1454 
1455     napi_value lastParam = (argc <= 1) ? nullptr :
1456         ((argv[1] != nullptr && GetType(env, argv[1]) == napi_function) ? argv[1] : nullptr);
1457     napi_value result = nullptr;
1458     std::shared_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, lastParam, &result);
1459     const char* const where = __func__;
1460     auto asyncTask = [weakToken = wptr<Window>(extensionWindow_->GetWindow()), colorSpace, where, env,
1461         task = napiAsyncTask] {
1462         auto weakWindow = weakToken.promote();
1463         if (weakWindow == nullptr) {
1464             WLOGFE("window is nullptr");
1465             task->Reject(env, CreateJsError(env,
1466                 static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "OnSetWindowColorSpace failed"));
1467             return;
1468         }
1469         weakWindow->SetColorSpace(colorSpace);
1470         task->Resolve(env, NapiGetUndefined(env));
1471         WLOGI("%{public}s end, window [%{public}u, %{public}s] colorSpace=%{public}u",
1472             where, weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(),
1473             static_cast<uint32_t>(colorSpace));
1474     };
1475     if (napi_send_event(env, asyncTask, napi_eprio_high, "OnSetWindowColorSpace") != napi_status::napi_ok) {
1476         TLOGE(WmsLogTag::WMS_IMMS, "napi_send_event failed");
1477         napiAsyncTask->Reject(env,
1478             CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "failed to send event"));
1479     }
1480     return result;
1481 }
1482 
OnSetWindowPrivacyMode(napi_env env,napi_callback_info info)1483 napi_value JsExtensionWindow::OnSetWindowPrivacyMode(napi_env env, napi_callback_info info)
1484 {
1485     WmErrorCode errCode = WmErrorCode::WM_OK;
1486     size_t argc = 4;
1487     napi_value argv[4] = {nullptr};
1488     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1489     if (argc < 1) { // 1: params num
1490         WLOGFE("Argc is invalid: %{public}zu", argc);
1491         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1492     }
1493     bool isPrivacyMode = false;
1494     if (errCode == WmErrorCode::WM_OK) {
1495         napi_value nativeVal = argv[0];
1496         if (nativeVal == nullptr) {
1497             WLOGFE("Failed to convert parameter to isPrivacyMode");
1498             errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
1499         } else {
1500             CHECK_NAPI_RETCODE(errCode, WmErrorCode::WM_ERROR_INVALID_PARAM,
1501                 napi_get_value_bool(env, nativeVal, &isPrivacyMode));
1502         }
1503     }
1504     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
1505         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1506     }
1507     const char* const where = __func__;
1508     napi_value lastParam = (argc <= 1) ? nullptr :
1509         ((argv[1] != nullptr && GetType(env, argv[1]) == napi_function) ? argv[1] : nullptr);
1510     napi_value result = nullptr;
1511     std::shared_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, lastParam, &result);
1512     auto asyncTask = [weakToken = wptr<Window>(extensionWindow_->GetWindow()), isPrivacyMode, where, env,
1513         task = napiAsyncTask] {
1514         auto weakWindow = weakToken.promote();
1515         if (weakWindow == nullptr) {
1516             task->Reject(env,
1517                 CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "Invalidate params"));
1518             return;
1519         }
1520         WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(weakWindow->SetPrivacyMode(isPrivacyMode));
1521         if (ret == WmErrorCode::WM_ERROR_NO_PERMISSION) {
1522             task->Reject(env, CreateJsError(env, static_cast<int32_t>(ret)));
1523             WLOGI("%{public}s failed, window [%{public}u, %{public}s] mode=%{public}u",
1524                 where, weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), isPrivacyMode);
1525             return;
1526         }
1527         task->Resolve(env, NapiGetUndefined(env));
1528         WLOGI("%{public}s succeed, window [%{public}u, %{public}s] mode=%{public}u",
1529             where, weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), isPrivacyMode);
1530     };
1531     if (napi_send_event(env, asyncTask, napi_eprio_high, "OnSetWindowPrivacyMode") != napi_status::napi_ok) {
1532         TLOGE(WmsLogTag::WMS_IMMS, "napi_send_event failed");
1533         napiAsyncTask->Reject(env,
1534             CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "failed to send event"));
1535     }
1536     return result;
1537 }
1538 
OnSetWindowSystemBarEnable(napi_env env,napi_callback_info info)1539 napi_value JsExtensionWindow::OnSetWindowSystemBarEnable(napi_env env, napi_callback_info info)
1540 {
1541     size_t argc = FOUR_PARAMS_SIZE;
1542     napi_value argv[FOUR_PARAMS_SIZE] = { nullptr };
1543     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1544     std::unordered_map<WindowType, SystemBarProperty> systemBarProperties;
1545     std::unordered_map<WindowType, SystemBarPropertyFlag> systemBarPropertyFlags;
1546     if (argc < ARG_COUNT_ONE || !GetSystemBarStatus(env, info, systemBarProperties, systemBarPropertyFlags)) {
1547         TLOGE(WmsLogTag::WMS_IMMS, "failed to convert parameter to systemBarProperties");
1548         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1549     }
1550     napi_value lastParam = nullptr;
1551     if (argc >= ARG_COUNT_ONE && argv[INDEX_ZERO] != nullptr && GetType(env, argv[INDEX_ZERO]) == napi_function) {
1552         lastParam = argv[INDEX_ZERO];
1553     } else if (argc >= ARG_COUNT_TWO && argv[INDEX_ONE] != nullptr && GetType(env, argv[INDEX_ONE]) == napi_function) {
1554         lastParam = argv[INDEX_ONE];
1555     }
1556     napi_value result = nullptr;
1557     std::shared_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, lastParam, &result);
1558     auto asyncTask = [weakToken = wptr<Window>(extensionWindow_->GetWindow()), env, task = napiAsyncTask,
1559         systemBarProperties = std::move(systemBarProperties),
1560         systemBarPropertyFlags = std::move(systemBarPropertyFlags)] {
1561         auto window = weakToken.promote();
1562         if (window == nullptr) {
1563             TLOGNE(WmsLogTag::WMS_IMMS, "window is nullptr");
1564             task->Reject(env, CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
1565             return;
1566         }
1567         auto errCode = WM_JS_TO_ERROR_CODE_MAP.at(
1568             window->UpdateSystemBarProperties(systemBarProperties, systemBarPropertyFlags));
1569         if (errCode == WmErrorCode::WM_OK) {
1570             task->Resolve(env, NapiGetUndefined(env));
1571         } else {
1572             TLOGNE(WmsLogTag::WMS_IMMS, "set system bar enable failed, errcode: %{public}d", errCode);
1573             task->Reject(env, CreateJsError(env, static_cast<int32_t>(errCode),
1574                 "OnSetWindowSystemBarEnable failed"));
1575         }
1576     };
1577     if (napi_send_event(env, asyncTask, napi_eprio_high, "OnSetWindowSystemBarEnable") != napi_status::napi_ok) {
1578         TLOGE(WmsLogTag::WMS_IMMS, "napi_send_event failed");
1579         napiAsyncTask->Reject(env, CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY),
1580             "OnSetWindowSystemBarEnable failed"));
1581     }
1582     return result;
1583 }
1584 
OnGetGestureBackEnabled(napi_env env,napi_callback_info info)1585 napi_value JsExtensionWindow::OnGetGestureBackEnabled(napi_env env, napi_callback_info info)
1586 {
1587     sptr<Window> windowImpl = extensionWindow_->GetWindow();
1588     if (windowImpl == nullptr) {
1589         TLOGE(WmsLogTag::WMS_IMMS, "extensionWindow is nullptr");
1590         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1591     }
1592     bool enable = true;
1593     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowImpl->GetGestureBackEnabled(enable));
1594     if (ret != WmErrorCode::WM_OK) {
1595         TLOGE(WmsLogTag::WMS_IMMS, "get failed, ret %{public}d", ret);
1596         return NapiThrowError(env, WmErrorCode::WM_ERROR_SYSTEM_ABNORMALLY);
1597     }
1598     TLOGI(WmsLogTag::WMS_IMMS, "win [%{public}u, %{public}s] enable %{public}u",
1599         windowImpl->GetWindowId(), windowImpl->GetWindowName().c_str(), enable);
1600     return CreateJsValue(env, enable);
1601 }
1602 
OnSetGestureBackEnabled(napi_env env,napi_callback_info info)1603 napi_value JsExtensionWindow::OnSetGestureBackEnabled(napi_env env, napi_callback_info info)
1604 {
1605     size_t argc = FOUR_PARAMS_SIZE;
1606     napi_value argv[FOUR_PARAMS_SIZE] = {nullptr};
1607     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1608     if (argc < INDEX_ONE) {
1609         TLOGE(WmsLogTag::WMS_IMMS, "argc is invalid: %{public}zu", argc);
1610         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1611     }
1612     bool enabled = true;
1613     if (argv[INDEX_ZERO] == nullptr || napi_get_value_bool(env, argv[INDEX_ZERO], &enabled) != napi_ok) {
1614         TLOGE(WmsLogTag::WMS_IMMS, "failed to convert parameter to enabled");
1615         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1616     }
1617     std::shared_ptr<WmErrorCode> errCodePtr = std::make_shared<WmErrorCode>(WmErrorCode::WM_OK);
1618     const char* const where = __func__;
1619     auto execute = [weakToken = wptr<Window>(extensionWindow_->GetWindow()), errCodePtr, enabled, where] {
1620         auto window = weakToken.promote();
1621         if (window == nullptr) {
1622             TLOGNE(WmsLogTag::WMS_IMMS, "%{public}s window is nullptr", where);
1623             *errCodePtr = WmErrorCode::WM_ERROR_STATE_ABNORMALLY;
1624             return;
1625         }
1626         *errCodePtr = WM_JS_TO_ERROR_CODE_MAP.at(window->SetGestureBackEnabled(enabled));
1627     };
1628     auto complete = [errCodePtr, where](napi_env env, NapiAsyncTask& task, int32_t status) {
1629         if (*errCodePtr == WmErrorCode::WM_OK) {
1630             task.Resolve(env, NapiGetUndefined(env));
1631         } else {
1632             TLOGNE(WmsLogTag::WMS_IMMS, "%{public}s set failed, ret %{public}d", where, *errCodePtr);
1633             task.Reject(env, CreateJsError(env, static_cast<int32_t>(*errCodePtr), "set failed."));
1634         }
1635     };
1636     napi_value result = nullptr;
1637     NapiAsyncTask::Schedule("JsExtensionWindow::OnSetGestureBackEnabled",
1638         env, CreateAsyncTaskWithLastParam(env, nullptr, std::move(execute), std::move(complete), &result));
1639     return result;
1640 }
1641 
OnSetImmersiveModeEnabledState(napi_env env,napi_callback_info info)1642 napi_value JsExtensionWindow::OnSetImmersiveModeEnabledState(napi_env env, napi_callback_info info)
1643 {
1644     size_t argc = 4;
1645     napi_value argv[4] = {nullptr};
1646     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1647     if (argc != 1) {
1648         TLOGW(WmsLogTag::WMS_IMMS, "Argc is invalid %{public}zu", argc);
1649         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1650     }
1651     sptr<Window> windowImpl = extensionWindow_->GetWindow();
1652     if (windowImpl == nullptr) {
1653         TLOGE(WmsLogTag::WMS_IMMS, "extensionWindow is nullptr");
1654         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1655     }
1656     if (windowImpl->IsPcOrPadFreeMultiWindowMode()) {
1657         TLOGE(WmsLogTag::WMS_IMMS, "device not support");
1658         return NapiGetUndefined(env);
1659     }
1660     napi_value nativeVal = argv[0];
1661     if (nativeVal == nullptr) {
1662         TLOGE(WmsLogTag::WMS_IMMS, "Failed to convert parameter to enable");
1663         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
1664     }
1665     bool enable = true;
1666     napi_get_value_bool(env, nativeVal, &enable);
1667     TLOGI(WmsLogTag::WMS_IMMS, "enable %{public}d", static_cast<int32_t>(enable));
1668     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowImpl->SetImmersiveModeEnabledState(enable));
1669     if (ret != WmErrorCode::WM_OK) {
1670         TLOGE(WmsLogTag::WMS_IMMS, "set failed, ret %{public}d", ret);
1671         return NapiThrowError(env, WmErrorCode::WM_ERROR_SYSTEM_ABNORMALLY);
1672     }
1673     TLOGI(WmsLogTag::WMS_IMMS, "win %{public}u set end", windowImpl->GetWindowId());
1674     return NapiGetUndefined(env);
1675 }
1676 
OnGetImmersiveModeEnabledState(napi_env env,napi_callback_info info)1677 napi_value JsExtensionWindow::OnGetImmersiveModeEnabledState(napi_env env, napi_callback_info info)
1678 {
1679     sptr<Window> windowImpl = extensionWindow_->GetWindow();
1680     if (windowImpl == nullptr) {
1681         TLOGE(WmsLogTag::WMS_IMMS, "extensionWindow is nullptr");
1682         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1683     }
1684     bool enable = windowImpl->GetImmersiveModeEnabledState();
1685     TLOGI(WmsLogTag::WMS_IMMS, "win %{public}u isEnabled %{public}u set end", windowImpl->GetWindowId(), enable);
1686     return CreateJsValue(env, enable);
1687 }
1688 
OnIsFocused(napi_env env,napi_callback_info info)1689 napi_value JsExtensionWindow::OnIsFocused(napi_env env, napi_callback_info info)
1690 {
1691     sptr<Window> windowImpl = extensionWindow_->GetWindow();
1692     if (windowImpl == nullptr) {
1693         TLOGE(WmsLogTag::WMS_FOCUS, "window is nullptr");
1694         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1695     }
1696 
1697     bool isFocused = windowImpl->IsComponentFocused();
1698     TLOGI(WmsLogTag::WMS_FOCUS, "end, window [%{public}u, %{public}s] isFocused=%{public}u",
1699         windowImpl->GetWindowId(), windowImpl->GetWindowName().c_str(), isFocused);
1700     return CreateJsValue(env, isFocused);
1701 }
1702 
OnIsWindowSupportWideGamut(napi_env env,napi_callback_info info)1703 napi_value JsExtensionWindow::OnIsWindowSupportWideGamut(napi_env env, napi_callback_info info)
1704 {
1705     const char* const where = __func__;
1706     size_t argc = 4;
1707     napi_value argv[4] = {nullptr};
1708     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1709     napi_value lastParam = (argc == 0) ? nullptr :
1710         ((argv[0] != nullptr && GetType(env, argv[0]) == napi_function) ? argv[0] : nullptr);
1711     napi_value result = nullptr;
1712     std::shared_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, lastParam, &result);
1713     auto asyncTask = [weakToken = wptr<Window>(extensionWindow_->GetWindow()), where, env, task = napiAsyncTask] {
1714         auto weakWindow = weakToken.promote();
1715         if (weakWindow == nullptr) {
1716             TLOGE(WmsLogTag::WMS_IMMS, "window is nullptr or get invalid param");
1717             task->Reject(env,
1718                 CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
1719             return;
1720         }
1721         bool flag = weakWindow->IsSupportWideGamut();
1722         task->Resolve(env, CreateJsValue(env, flag));
1723         TLOGE(WmsLogTag::WMS_IMMS, "%{public}s end, window [%{public}u, %{public}s] ret=%{public}u",
1724             where, weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), flag);
1725     };
1726     if (napi_send_event(env, asyncTask, napi_eprio_high, "OnIsWindowSupportWideGamut") != napi_status::napi_ok) {
1727         TLOGE(WmsLogTag::WMS_IMMS, "napi_send_event failed");
1728         napiAsyncTask->Reject(env,
1729             CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "failed to send event"));
1730     }
1731     return result;
1732 }
1733 
OnGetGlobalScaledRect(napi_env env,napi_callback_info info)1734 napi_value JsExtensionWindow::OnGetGlobalScaledRect(napi_env env, napi_callback_info info)
1735 {
1736     sptr<Window> windowImpl = extensionWindow_->GetWindow();
1737     if (windowImpl == nullptr) {
1738         TLOGE(WmsLogTag::WMS_LAYOUT, "extensionWindow is nullptr");
1739         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1740     }
1741     Rect globalScaledRect;
1742     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowImpl->GetGlobalScaledRect(globalScaledRect));
1743     if (ret != WmErrorCode::WM_OK) {
1744         return NapiThrowError(env, ret);
1745     }
1746     TLOGI(WmsLogTag::WMS_LAYOUT, "Window [%{public}u, %{public}s] end",
1747         windowImpl->GetWindowId(), windowImpl->GetWindowName().c_str());
1748     napi_value globalScaledRectObj = GetRectAndConvertToJsValue(env, globalScaledRect);
1749     if (globalScaledRectObj == nullptr) {
1750         TLOGE(WmsLogTag::WMS_LAYOUT, "globalScaledRectObj is nullptr");
1751         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1752     }
1753     return globalScaledRectObj;
1754 }
1755 
OnGetStatusBarPropertySync(napi_env env,napi_callback_info info)1756 napi_value JsExtensionWindow::OnGetStatusBarPropertySync(napi_env env, napi_callback_info info)
1757 {
1758     sptr<Window> windowImpl = extensionWindow_->GetWindow();
1759     if (windowImpl == nullptr) {
1760         TLOGE(WmsLogTag::WMS_IMMS, "window is null");
1761         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
1762     }
1763     auto objValue = GetStatusBarPropertyObject(env, windowImpl);
1764     if (objValue == nullptr) {
1765         TLOGE(WmsLogTag::WMS_IMMS, "get property failed");
1766         return NapiThrowError(env, WmErrorCode::WM_ERROR_SYSTEM_ABNORMALLY);
1767     }
1768     return objValue;
1769 }
1770 
OnUnsupportAsyncCall(napi_env env,napi_callback_info info)1771 napi_value JsExtensionWindow::OnUnsupportAsyncCall(napi_env env, napi_callback_info info)
1772 {
1773     size_t argc = 4;
1774     napi_value argv[4] = {nullptr};
1775     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1776     napi_value lastParam = (argc <= 0) ? nullptr :
1777         ((argv[argc - 1] != nullptr && GetType(env, argv[argc - 1]) == napi_function) ? argv[argc - 1] : nullptr);
1778     napi_value result = nullptr;
1779     std::shared_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, lastParam, &result);
1780     auto asyncTask = [env, task = napiAsyncTask] {
1781         task->Reject(env, CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT)));
1782     };
1783     if (napi_send_event(env, asyncTask, napi_eprio_high, "OnUnsupportAsyncCall") != napi_status::napi_ok) {
1784         napiAsyncTask->Reject(env,
1785             CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "failed to send event"));
1786     }
1787     return result;
1788 }
1789 
OnEmptyAsyncCall(napi_env env,napi_callback_info info)1790 napi_value JsExtensionWindow::OnEmptyAsyncCall(napi_env env, napi_callback_info info)
1791 {
1792     size_t argc = 4;
1793     napi_value argv[4] = {nullptr};
1794     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1795     napi_value lastParam = (argc <= 0) ? nullptr :
1796         ((argv[argc - 1] != nullptr && GetType(env, argv[argc - 1]) == napi_function) ? argv[argc - 1] : nullptr);
1797     napi_value result = nullptr;
1798     std::shared_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, lastParam, &result);
1799     auto asyncTask = [env, task = napiAsyncTask] {
1800         task->Resolve(env, NapiGetUndefined(env));
1801     };
1802     if (napi_send_event(env, asyncTask, napi_eprio_high, "OnEmptyAsyncCall") != napi_status::napi_ok) {
1803         napiAsyncTask->Reject(env,
1804             CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "failed to send event"));
1805     }
1806     return result;
1807 }
1808 
OnInvalidAsyncCall(napi_env env,napi_callback_info info)1809 napi_value JsExtensionWindow::OnInvalidAsyncCall(napi_env env, napi_callback_info info)
1810 {
1811     size_t argc = 4;
1812     napi_value argv[4] = {nullptr};
1813     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
1814     napi_value lastParam = (argc <= 0) ? nullptr :
1815         ((argv[argc - 1] != nullptr && GetType(env, argv[argc - 1]) == napi_function) ? argv[argc - 1] : nullptr);
1816     napi_value result = nullptr;
1817     std::shared_ptr<NapiAsyncTask> napiAsyncTask = CreateEmptyAsyncTask(env, lastParam, &result);
1818     auto asyncTask = [env, task = napiAsyncTask] {
1819         task->Reject(env, CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING)));
1820     };
1821     if (napi_send_event(env, asyncTask, napi_eprio_high, "OnInvalidAsyncCall") != napi_status::napi_ok) {
1822         napiAsyncTask->Reject(env,
1823             CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "failed to send event"));
1824     }
1825     return result;
1826 }
1827 }  // namespace Rosen
1828 }  // namespace OHOS