• 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 } // namespace
34 
JsExtensionWindow(const std::shared_ptr<Rosen::ExtensionWindow> extensionWindow,int32_t hostWindowId)35 JsExtensionWindow::JsExtensionWindow(
36     const std::shared_ptr<Rosen::ExtensionWindow> extensionWindow,
37     int32_t hostWindowId)
38     : extensionWindow_(extensionWindow), hostWindowId_(hostWindowId),
39     extensionRegisterManager_(std::make_unique<JsExtensionWindowRegisterManager>()) {
40 }
41 
JsExtensionWindow(const std::shared_ptr<Rosen::ExtensionWindow> extensionWindow,sptr<AAFwk::SessionInfo> sessionInfo)42 JsExtensionWindow::JsExtensionWindow(const std::shared_ptr<Rosen::ExtensionWindow> extensionWindow,
43     sptr<AAFwk::SessionInfo> sessionInfo)
44     : extensionWindow_(extensionWindow), hostWindowId_(-1), sessionInfo_(sessionInfo),
45     extensionRegisterManager_(std::make_unique<JsExtensionWindowRegisterManager>()) {
46 }
47 
~JsExtensionWindow()48 JsExtensionWindow::~JsExtensionWindow() {}
49 
CreateJsExtensionWindow(napi_env env,sptr<Rosen::Window> window,int32_t hostWindowId)50 napi_value JsExtensionWindow::CreateJsExtensionWindow(napi_env env, sptr<Rosen::Window> window, int32_t hostWindowId)
51 {
52     TLOGD(WmsLogTag::WMS_UIEXT, "Called.");
53     napi_value objValue = nullptr;
54     napi_create_object(env, &objValue);
55 
56     if (env == nullptr || window == nullptr) {
57         TLOGE(WmsLogTag::WMS_UIEXT, "JsExtensionWindow env or window is nullptr");
58         return nullptr;
59     }
60 
61     std::shared_ptr<ExtensionWindow> extensionWindow = std::make_shared<ExtensionWindowImpl>(window);
62     std::unique_ptr<JsExtensionWindow> jsExtensionWindow =
63         std::make_unique<JsExtensionWindow>(extensionWindow, hostWindowId);
64     napi_wrap(env, objValue, jsExtensionWindow.release(), JsExtensionWindow::Finalizer, nullptr, nullptr);
65 
66     napi_property_descriptor desc[] = {
67         DECLARE_NAPI_GETTER("properties", JsExtensionWindow::GetProperties)
68     };
69     NAPI_CALL(env, napi_define_properties(env, objValue, sizeof(desc) / sizeof(desc[0]), desc));
70 
71     const char *moduleName = "JsExtensionWindow";
72     BindNativeFunction(env, objValue, "getWindowAvoidArea", moduleName, JsExtensionWindow::GetWindowAvoidArea);
73     BindNativeFunction(env, objValue, "on", moduleName, JsExtensionWindow::RegisterExtensionWindowCallback);
74     BindNativeFunction(env, objValue, "off", moduleName, JsExtensionWindow::UnRegisterExtensionWindowCallback);
75     BindNativeFunction(env, objValue, "hideNonSecureWindows", moduleName, JsExtensionWindow::HideNonSecureWindows);
76     BindNativeFunction(env, objValue, "createSubWindowWithOptions", moduleName,
77         JsExtensionWindow::CreateSubWindowWithOptions);
78     BindNativeFunction(env, objValue, "setWaterMarkFlag", moduleName, JsExtensionWindow::SetWaterMarkFlag);
79     BindNativeFunction(env, objValue, "hidePrivacyContentForHost", moduleName,
80                        JsExtensionWindow::HidePrivacyContentForHost);
81 
82     return objValue;
83 }
84 
CreateJsExtensionWindowObject(napi_env env,sptr<Rosen::Window> window,sptr<AAFwk::SessionInfo> sessionInfo)85 napi_value JsExtensionWindow::CreateJsExtensionWindowObject(napi_env env, sptr<Rosen::Window> window,
86     sptr<AAFwk::SessionInfo> sessionInfo)
87 {
88     TLOGI(WmsLogTag::WMS_UIEXT, "JsExtensionWindow CreateJsExtensionWindow");
89     napi_value objValue = nullptr;
90     napi_create_object(env, &objValue);
91 
92     if (env == nullptr || window == nullptr) {
93         TLOGE(WmsLogTag::WMS_UIEXT, "JsExtensionWindow env or window is nullptr");
94         return nullptr;
95     }
96 
97     std::shared_ptr<ExtensionWindow> extensionWindow = std::make_shared<ExtensionWindowImpl>(window);
98     std::unique_ptr<JsExtensionWindow> jsExtensionWindow = std::make_unique<JsExtensionWindow>(extensionWindow,
99         sessionInfo);
100     napi_wrap(env, objValue, jsExtensionWindow.release(), JsExtensionWindow::Finalizer, nullptr, nullptr);
101 
102     const char *moduleName = "JsExtensionWindow";
103     BindNativeFunction(env, objValue, "on", moduleName, JsExtensionWindow::RegisterExtensionWindowCallback);
104     BindNativeFunction(env, objValue, "off", moduleName, JsExtensionWindow::UnRegisterExtensionWindowCallback);
105     BindNativeFunction(env, objValue, "moveWindowTo", moduleName, JsExtensionWindow::MoveWindowTo);
106     BindNativeFunction(env, objValue, "resize", moduleName, JsExtensionWindow::ResizeWindow);
107     BindNativeFunction(env, objValue, "getUIContext", moduleName, JsExtensionWindow::GetUIContext);
108     BindNativeFunction(env, objValue, "setWindowBrightness", moduleName, JsExtensionWindow::SetWindowBrightness);
109     BindNativeFunction(env, objValue, "setWindowKeepScreenOn", moduleName, JsExtensionWindow::SetWindowKeepScreenOn);
110     BindNativeFunction(env, objValue, "showWindow", moduleName, JsExtensionWindow::ShowWindow);
111     BindNativeFunction(env, objValue, "destroyWindow", moduleName, JsExtensionWindow::DestroyWindow);
112     BindNativeFunction(env, objValue, "loadContent", moduleName, JsExtensionWindow::LoadContent);
113     BindNativeFunction(env, objValue, "loadContentByName", moduleName, JsExtensionWindow::LoadContentByName);
114     BindNativeFunction(env, objValue, "setUIContent", moduleName, JsExtensionWindow::SetUIContent);
115     BindNativeFunction(env, objValue, "isWindowShowing", moduleName, JsExtensionWindow::IsWindowShowingSync);
116     BindNativeFunction(env, objValue, "getWindowProperties", moduleName, JsExtensionWindow::GetWindowPropertiesSync);
117     BindNativeFunction(env, objValue, "getWindowAvoidArea", moduleName, JsExtensionWindow::GetWindowAvoidArea);
118     BindNativeFunction(env, objValue, "setWindowBackgroundColor", moduleName,
119         JsExtensionWindow::SetWindowBackgroundColorSync);
120     BindNativeFunction(env, objValue, "setSpecificSystemBarEnabled", moduleName,
121         JsExtensionWindow::SetSpecificSystemBarEnabled);
122     BindNativeFunction(env, objValue, "setPreferredOrientation", moduleName,
123         JsExtensionWindow::SetPreferredOrientation);
124     BindNativeFunction(env, objValue, "getPreferredOrientation", moduleName,
125         JsExtensionWindow::GetPreferredOrientation);
126     return objValue;
127 }
128 
Finalizer(napi_env env,void * data,void * hint)129 void JsExtensionWindow::Finalizer(napi_env env, void* data, void* hint)
130 {
131     TLOGI(WmsLogTag::WMS_UIEXT, "Finalizer is called");
132     std::unique_ptr<JsExtensionWindow>(static_cast<JsExtensionWindow*>(data));
133 }
134 
GetWindowAvoidArea(napi_env env,napi_callback_info info)135 napi_value JsExtensionWindow::GetWindowAvoidArea(napi_env env, napi_callback_info info)
136 {
137     TLOGI(WmsLogTag::WMS_UIEXT, "GetWindowAvoidArea is called");
138     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
139     return (me != nullptr) ? me->OnGetWindowAvoidArea(env, info) : nullptr;
140 }
141 
RegisterExtensionWindowCallback(napi_env env,napi_callback_info info)142 napi_value JsExtensionWindow::RegisterExtensionWindowCallback(napi_env env, napi_callback_info info)
143 {
144     TLOGI(WmsLogTag::WMS_UIEXT, "RegisterExtensionWindowCallback is called");
145     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
146     return (me != nullptr) ? me->OnRegisterExtensionWindowCallback(env, info) : nullptr;
147 }
148 
UnRegisterExtensionWindowCallback(napi_env env,napi_callback_info info)149 napi_value JsExtensionWindow::UnRegisterExtensionWindowCallback(napi_env env, napi_callback_info info)
150 {
151     TLOGI(WmsLogTag::WMS_UIEXT, "UnRegisterExtensionWindowCallback is called");
152     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
153     return (me != nullptr) ? me->OnUnRegisterExtensionWindowCallback(env, info) : nullptr;
154 }
155 
HideNonSecureWindows(napi_env env,napi_callback_info info)156 napi_value JsExtensionWindow::HideNonSecureWindows(napi_env env, napi_callback_info info)
157 {
158     TLOGI(WmsLogTag::WMS_UIEXT, "HideNonSecureWindows is called");
159     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
160     return (me != nullptr) ? me->OnHideNonSecureWindows(env, info) : nullptr;
161 }
162 
CreateSubWindowWithOptions(napi_env env,napi_callback_info info)163 napi_value JsExtensionWindow::CreateSubWindowWithOptions(napi_env env, napi_callback_info info)
164 {
165     TLOGI(WmsLogTag::WMS_UIEXT, "CreateSubWindowWithOptions is called");
166     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
167     return (me != nullptr) ? me->OnCreateSubWindowWithOptions(env, info) : nullptr;
168 }
169 
SetWaterMarkFlag(napi_env env,napi_callback_info info)170 napi_value JsExtensionWindow::SetWaterMarkFlag(napi_env env, napi_callback_info info)
171 {
172     TLOGI(WmsLogTag::WMS_UIEXT, "SetWaterMark is called");
173     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
174     return (me != nullptr) ? me->OnSetWaterMarkFlag(env, info) : nullptr;
175 }
176 
HidePrivacyContentForHost(napi_env env,napi_callback_info info)177 napi_value JsExtensionWindow::HidePrivacyContentForHost(napi_env env, napi_callback_info info)
178 {
179     TLOGD(WmsLogTag::WMS_UIEXT, "enter");
180     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
181     return (me != nullptr) ? me->OnHidePrivacyContentForHost(env, info) : nullptr;
182 }
183 
LoadContent(napi_env env,napi_callback_info info)184 napi_value JsExtensionWindow::LoadContent(napi_env env, napi_callback_info info)
185 {
186     TLOGI(WmsLogTag::WMS_UIEXT, "LoadContent is called");
187     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
188     return (me != nullptr) ? me->OnLoadContent(env, info, false) : nullptr;
189 }
190 
LoadContentByName(napi_env env,napi_callback_info info)191 napi_value JsExtensionWindow::LoadContentByName(napi_env env, napi_callback_info info)
192 {
193     TLOGI(WmsLogTag::WMS_UIEXT, "LoadContentByName is called");
194     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
195     return (me != nullptr) ? me->OnLoadContent(env, info, true) : nullptr;
196 }
197 
ShowWindow(napi_env env,napi_callback_info info)198 napi_value JsExtensionWindow::ShowWindow(napi_env env, napi_callback_info info)
199 {
200     TLOGI(WmsLogTag::WMS_UIEXT, "ShowWindow is called");
201     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
202     return (me != nullptr) ? me->OnShowWindow(env, info) : nullptr;
203 }
204 
IsWindowShowingSync(napi_env env,napi_callback_info info)205 napi_value JsExtensionWindow::IsWindowShowingSync(napi_env env, napi_callback_info info)
206 {
207     TLOGI(WmsLogTag::WMS_UIEXT, "IsShowing is called");
208     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
209     return (me != nullptr) ? me->OnIsWindowShowingSync(env, info) : nullptr;
210 }
211 
SetUIContent(napi_env env,napi_callback_info info)212 napi_value JsExtensionWindow::SetUIContent(napi_env env, napi_callback_info info)
213 {
214     TLOGI(WmsLogTag::WMS_UIEXT, "LoadContent is called");
215     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
216     return (me != nullptr) ? me->OnSetUIContent(env, info) : nullptr;
217 }
218 
DestroyWindow(napi_env env,napi_callback_info info)219 napi_value JsExtensionWindow::DestroyWindow(napi_env env, napi_callback_info info)
220 {
221     TLOGI(WmsLogTag::WMS_UIEXT, "Destroy is called");
222     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
223     return (me != nullptr) ? me->OnDestroyWindow(env, info) : nullptr;
224 }
225 
SetWindowBackgroundColorSync(napi_env env,napi_callback_info info)226 napi_value JsExtensionWindow::SetWindowBackgroundColorSync(napi_env env, napi_callback_info info)
227 {
228     TLOGI(WmsLogTag::WMS_UIEXT, "SetBackgroundColor is called");
229     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
230     return (me != nullptr) ? me->OnSetWindowBackgroundColorSync(env, info) : nullptr;
231 }
232 
GetWindowPropertiesSync(napi_env env,napi_callback_info info)233 napi_value JsExtensionWindow::GetWindowPropertiesSync(napi_env env, napi_callback_info info)
234 {
235     TLOGD(WmsLogTag::WMS_UIEXT, "GetProperties is called");
236     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
237     return (me != nullptr) ? me->OnGetWindowPropertiesSync(env, info) : nullptr;
238 }
239 
MoveWindowTo(napi_env env,napi_callback_info info)240 napi_value JsExtensionWindow::MoveWindowTo(napi_env env, napi_callback_info info)
241 {
242     TLOGD(WmsLogTag::WMS_UIEXT, "MoveTo");
243     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
244     return (me != nullptr) ? me->OnMoveWindowTo(env, info) : nullptr;
245 }
246 
ResizeWindow(napi_env env,napi_callback_info info)247 napi_value JsExtensionWindow::ResizeWindow(napi_env env, napi_callback_info info)
248 {
249     TLOGI(WmsLogTag::WMS_UIEXT, "Resize");
250     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
251     return (me != nullptr) ? me->OnResizeWindow(env, info) : nullptr;
252 }
253 
SetSpecificSystemBarEnabled(napi_env env,napi_callback_info info)254 napi_value JsExtensionWindow::SetSpecificSystemBarEnabled(napi_env env, napi_callback_info info)
255 {
256     TLOGI(WmsLogTag::WMS_UIEXT, "SetSystemBarEnable");
257     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
258     return (me != nullptr) ? me->OnSetSpecificSystemBarEnabled(env, info) : nullptr;
259 }
260 
SetPreferredOrientation(napi_env env,napi_callback_info info)261 napi_value JsExtensionWindow::SetPreferredOrientation(napi_env env, napi_callback_info info)
262 {
263     TLOGD(WmsLogTag::WMS_UIEXT, "SetPreferredOrientation");
264     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
265     return (me != nullptr) ? me->OnSetPreferredOrientation(env, info) : nullptr;
266 }
267 
GetPreferredOrientation(napi_env env,napi_callback_info info)268 napi_value JsExtensionWindow::GetPreferredOrientation(napi_env env, napi_callback_info info)
269 {
270     TLOGD(WmsLogTag::WMS_UIEXT, "GetPreferredOrientation");
271     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
272     return (me != nullptr) ? me->OnGetPreferredOrientation(env, info) : nullptr;
273 }
274 
GetUIContext(napi_env env,napi_callback_info info)275 napi_value JsExtensionWindow::GetUIContext(napi_env env, napi_callback_info info)
276 {
277     TLOGD(WmsLogTag::WMS_UIEXT, "GetUIContext");
278     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
279     return (me != nullptr) ? me->OnGetUIContext(env, info) : nullptr;
280 }
281 
SetWindowBrightness(napi_env env,napi_callback_info info)282 napi_value JsExtensionWindow::SetWindowBrightness(napi_env env, napi_callback_info info)
283 {
284     TLOGI(WmsLogTag::WMS_UIEXT, "SetBrightness");
285     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
286     return (me != nullptr) ? me->OnSetWindowBrightness(env, info) : nullptr;
287 }
288 
SetWindowKeepScreenOn(napi_env env,napi_callback_info info)289 napi_value JsExtensionWindow::SetWindowKeepScreenOn(napi_env env, napi_callback_info info)
290 {
291     TLOGI(WmsLogTag::WMS_UIEXT, "SetKeepScreenOn");
292     JsExtensionWindow* me = CheckParamsAndGetThis<JsExtensionWindow>(env, info);
293     return (me != nullptr) ? me->OnSetWindowKeepScreenOn(env, info) : nullptr;
294 }
295 
GetType(napi_env env,napi_value value)296 napi_valuetype GetType(napi_env env, napi_value value)
297 {
298     napi_valuetype res = napi_undefined;
299     napi_typeof(env, value, &res);
300     return res;
301 }
302 
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)303 static void LoadContentTask(std::shared_ptr<NativeReference> contentStorage, std::string contextUrl,
304     const std::shared_ptr<Rosen::ExtensionWindow> win, napi_env env, NapiAsyncTask& task,
305     sptr<IRemoteObject> parentToken, bool isLoadedByName)
306 {
307     napi_value nativeStorage = (contentStorage == nullptr) ? nullptr : contentStorage->GetNapiValue();
308     sptr<Window> windowImpl = win->GetWindow();
309     WMError ret;
310     if (isLoadedByName) {
311         ret = windowImpl->SetUIContentByName(contextUrl, env, nativeStorage);
312     } else {
313         ret = windowImpl->NapiSetUIContent(contextUrl, env, nativeStorage, false, parentToken);
314     }
315     if (ret == WMError::WM_OK) {
316         task.Resolve(env, NapiGetUndefined(env));
317     } else {
318         task.Reject(env, CreateJsError(env, static_cast<int32_t>(ret), "Window load content failed"));
319     }
320     TLOGI(WmsLogTag::WMS_UIEXT, "Window [%{public}u, %{public}s] load content end, ret = %{public}d",
321         windowImpl->GetWindowId(), windowImpl->GetWindowName().c_str(), ret);
322 }
323 
OnSetWindowKeepScreenOn(napi_env env,napi_callback_info info)324 napi_value JsExtensionWindow::OnSetWindowKeepScreenOn(napi_env env, napi_callback_info info)
325 {
326     size_t argc = 4;
327     napi_value argv[4] = {nullptr};
328     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
329     NapiAsyncTask::CompleteCallback complete =
330         [](napi_env env, NapiAsyncTask& task, int32_t status) {
331             task.Reject(env,
332                 CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT)));
333         };
334     napi_value LastParam = (argc <= 1) ? nullptr :
335         ((argv[1] != nullptr && GetType(env, argv[1]) == napi_function) ? argv[1] : nullptr);
336     napi_value result = nullptr;
337     NapiAsyncTask::Schedule("JsExtensionWindow::OnSetWindowKeepScreenOn",
338         env, CreateAsyncTaskWithLastParam(env, LastParam, nullptr, std::move(complete), &result));
339     return result;
340 }
341 
OnSetWindowBrightness(napi_env env,napi_callback_info info)342 napi_value JsExtensionWindow::OnSetWindowBrightness(napi_env env, napi_callback_info info)
343 {
344     size_t argc = 4;
345     napi_value argv[4] = {nullptr};
346     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
347     NapiAsyncTask::CompleteCallback complete =
348         [](napi_env env, NapiAsyncTask& task, int32_t status) {
349                 task.Reject(env,
350                     CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT)));
351         };
352 
353     napi_value lastParam = (argc <= 1) ? nullptr :
354         ((argv[1] != nullptr && GetType(env, argv[1]) == napi_function) ? argv[1] : nullptr);
355     napi_value result = nullptr;
356     NapiAsyncTask::Schedule("JsExtensionWindow::OnSetWindowBrightness",
357         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
358     return result;
359 }
360 
OnGetUIContext(napi_env env,napi_callback_info info)361 napi_value JsExtensionWindow::OnGetUIContext(napi_env env, napi_callback_info info)
362 {
363     return NapiThrowError(env, WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT);
364 }
365 
OnSetPreferredOrientation(napi_env env,napi_callback_info info)366 napi_value JsExtensionWindow::OnSetPreferredOrientation(napi_env env, napi_callback_info info)
367 {
368     size_t argc = 4;
369     napi_value argv[4] = {nullptr};
370     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
371     NapiAsyncTask::CompleteCallback complete =
372         [](napi_env env, NapiAsyncTask& task, int32_t status) {
373                 task.Reject(env,
374                     CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT)));
375         };
376 
377     napi_value lastParam = (argc <= 1) ? nullptr :
378         ((argv[1] != nullptr && GetType(env, argv[1]) == napi_function) ? argv[1] : nullptr);
379     napi_value result = nullptr;
380     NapiAsyncTask::Schedule("JsExtensionWindow::OnSetPreferredOrientation",
381         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
382     return result;
383 }
384 
OnGetPreferredOrientation(napi_env env,napi_callback_info info)385 napi_value JsExtensionWindow::OnGetPreferredOrientation(napi_env env, napi_callback_info info)
386 {
387     return NapiThrowError(env, WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT);
388 }
389 
OnSetSpecificSystemBarEnabled(napi_env env,napi_callback_info info)390 napi_value JsExtensionWindow::OnSetSpecificSystemBarEnabled(napi_env env, napi_callback_info info)
391 {
392     NapiAsyncTask::CompleteCallback complete =
393         [](napi_env env, NapiAsyncTask& task, int32_t status) {
394                 task.Reject(env,
395                     CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT)));
396         };
397     napi_value result = nullptr;
398     NapiAsyncTask::Schedule("JsExtensionWindow::OnSetSpecificSystemBarEnabled",
399         env, CreateAsyncTaskWithLastParam(env, nullptr, nullptr, std::move(complete), &result));
400     return result;
401 }
402 
OnResizeWindow(napi_env env,napi_callback_info info)403 napi_value JsExtensionWindow::OnResizeWindow(napi_env env, napi_callback_info info)
404 {
405     size_t argc = 4;
406     napi_value argv[4] = {nullptr};
407     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
408     NapiAsyncTask::CompleteCallback complete =
409         [](napi_env env, NapiAsyncTask& task, int32_t status) {
410                 task.Reject(env,
411                     CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT)));
412         };
413     // 2: params num; 2: index of callback
414     napi_value lastParam = (argc <= 2) ? nullptr :
415         ((argv[2] != nullptr && GetType(env, argv[2]) == napi_function) ? argv[2] : nullptr);
416     napi_value result = nullptr;
417     NapiAsyncTask::Schedule("JsExtensionWindow::OnResizeWindow",
418         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
419     return result;
420 }
421 
OnMoveWindowTo(napi_env env,napi_callback_info info)422 napi_value JsExtensionWindow::OnMoveWindowTo(napi_env env, napi_callback_info info)
423 {
424     size_t argc = 4;
425     napi_value argv[4] = {nullptr};
426     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
427     NapiAsyncTask::CompleteCallback complete =
428         [](napi_env env, NapiAsyncTask& task, int32_t status) {
429                 task.Reject(env,
430                     CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT)));
431         };
432     // 2: params num; 2: index of callback
433     napi_value lastParam = (argc <= 2) ? nullptr :
434         ((argv[2] != nullptr && GetType(env, argv[2]) == napi_function) ? argv[2] : nullptr);
435     napi_value result = nullptr;
436     NapiAsyncTask::Schedule("JsExtensionWindow::OnMoveWindowTo",
437         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
438     return result;
439 }
440 
OnGetWindowPropertiesSync(napi_env env,napi_callback_info info)441 napi_value JsExtensionWindow::OnGetWindowPropertiesSync(napi_env env, napi_callback_info info)
442 {
443     sptr<Window> windowImpl = extensionWindow_->GetWindow();
444     if (windowImpl == nullptr) {
445         TLOGW(WmsLogTag::WMS_UIEXT, "window is nullptr or get invalid param");
446         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
447     }
448     auto objValue = CreateJsExtensionWindowProperties(env, windowImpl);
449     TLOGI(WmsLogTag::WMS_UIEXT, "Window [%{public}u, %{public}s] get properties end",
450         windowImpl->GetWindowId(), windowImpl->GetWindowName().c_str());
451     if (objValue != nullptr) {
452         return objValue;
453     } else {
454         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
455     }
456 }
457 
OnSetWindowBackgroundColorSync(napi_env env,napi_callback_info info)458 napi_value JsExtensionWindow::OnSetWindowBackgroundColorSync(napi_env env, napi_callback_info info)
459 {
460     return NapiThrowError(env, WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT);
461 }
462 
OnDestroyWindow(napi_env env,napi_callback_info info)463 napi_value JsExtensionWindow::OnDestroyWindow(napi_env env, napi_callback_info info)
464 {
465     NapiAsyncTask::CompleteCallback complete =
466         [extwin = extensionWindow_](napi_env env, NapiAsyncTask& task, int32_t status) {
467             if (extwin == nullptr) {
468                 TLOGNE(WmsLogTag::WMS_UIEXT, "extensionWindow is null");
469                 task.Reject(env,
470                     CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
471                 return;
472             }
473             sptr<Window> windowImpl = extwin->GetWindow();
474             if (windowImpl == nullptr) {
475                 TLOGE(WmsLogTag::WMS_UIEXT, "window is nullptr or get invalid param");
476                 task.Reject(env,
477                     CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
478                 return;
479             }
480             WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(windowImpl->Destroy());
481             TLOGI(WmsLogTag::WMS_UIEXT, "Window [%{public}u, %{public}s] destroy end, ret = %{public}d",
482                 windowImpl->GetWindowId(), windowImpl->GetWindowName().c_str(), ret);
483             if (ret != WmErrorCode::WM_OK) {
484                 task.Reject(env,
485                     CreateJsError(env, static_cast<int32_t>(ret),
486                     "Window destroy failed"));
487                 return;
488             }
489             windowImpl = nullptr; // ensure window dtor when finalizer invalid
490             task.Resolve(env, NapiGetUndefined(env));
491         };
492     size_t argc = 4;
493     napi_value argv[4] = {nullptr};
494     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
495     napi_value lastParam = (argc == 0) ? nullptr :
496         ((argv[0] != nullptr && GetType(env, argv[0]) == napi_function) ? argv[0] : nullptr);
497     napi_value result = nullptr;
498     NapiAsyncTask::Schedule("JsExtensionWindow::OnDestroyWindow",
499         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
500     return result;
501 }
502 
OnIsWindowShowingSync(napi_env env,napi_callback_info info)503 napi_value JsExtensionWindow::OnIsWindowShowingSync(napi_env env, napi_callback_info info)
504 {
505     sptr<Window> windowImpl = extensionWindow_->GetWindow();
506     if (windowImpl == nullptr) {
507         TLOGE(WmsLogTag::WMS_UIEXT, "window is nullptr");
508         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
509     }
510     bool state = (windowImpl->GetWindowState() == WindowState::STATE_SHOWN);
511     TLOGI(WmsLogTag::WMS_UIEXT, "Window [%{public}u, %{public}s] get show state end, state = %{public}u",
512         windowImpl->GetWindowId(), windowImpl->GetWindowName().c_str(), state);
513     return CreateJsValue(env, state);
514 }
515 
OnShowWindow(napi_env env,napi_callback_info info)516 napi_value JsExtensionWindow::OnShowWindow(napi_env env, napi_callback_info info)
517 {
518     NapiAsyncTask::CompleteCallback complete =
519         [extwin = extensionWindow_](napi_env env, NapiAsyncTask& task, int32_t status) {
520             if (extwin == nullptr) {
521                 TLOGNE(WmsLogTag::WMS_UIEXT, "extensionWindow is null");
522                 task.Reject(env, CreateJsError(env,
523                     static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
524                 return;
525             }
526             sptr<Window> windowImpl = extwin->GetWindow();
527             if (windowImpl == nullptr) {
528                 task.Reject(env, CreateJsError(env,
529                     static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
530                 TLOGE(WmsLogTag::WMS_UIEXT, "window is nullptr or get invalid param");
531                 return;
532             }
533             WMError ret = windowImpl->Show(0, false);
534             TLOGI(WmsLogTag::WMS_UIEXT, "Window [%{public}u, %{public}s] show with ret = %{public}d",
535                 windowImpl->GetWindowId(), windowImpl->GetWindowName().c_str(), ret);
536             if (ret == WMError::WM_OK) {
537                 task.Resolve(env, NapiGetUndefined(env));
538             } else {
539                 task.Reject(env, CreateJsError(env,
540                     static_cast<int32_t>(WM_JS_TO_ERROR_CODE_MAP.at(ret)), "Window show failed"));
541             }
542             TLOGI(WmsLogTag::WMS_UIEXT, "Window [%{public}u, %{public}s] show end, ret = %{public}d",
543                 windowImpl->GetWindowId(), windowImpl->GetWindowName().c_str(), ret);
544         };
545     napi_value result = nullptr;
546     size_t argc = 4;
547     napi_value argv[4] = {nullptr};
548     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
549     napi_value lastParam = (argc == 0) ? nullptr :
550         ((argv[0] != nullptr && GetType(env, argv[0]) == napi_function) ? argv[0] : nullptr);
551     NapiAsyncTask::Schedule("JsExtensionWindow::OnShowWindow",
552         env, CreateAsyncTaskWithLastParam(env, lastParam, nullptr, std::move(complete), &result));
553     return result;
554 }
555 
OnSetUIContent(napi_env env,napi_callback_info info)556 napi_value JsExtensionWindow::OnSetUIContent(napi_env env, napi_callback_info info)
557 {
558     WmErrorCode errCode = WmErrorCode::WM_OK;
559     size_t argc = 4;
560     napi_value argv[4] = {nullptr};
561     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
562     if (argc < 1) { // 2 maximum param num
563         TLOGE(WmsLogTag::WMS_UIEXT, "Argc is invalid: %{public}zu", argc);
564         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
565     }
566     std::string contextUrl;
567     if (errCode == WmErrorCode::WM_OK && !ConvertFromJsValue(env, argv[0], contextUrl)) {
568         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to convert parameter to context url");
569         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
570     }
571     napi_value callBack = nullptr;
572     if (argc >= 2) { // 2 param num
573         callBack = argv[1];
574     }
575     std::shared_ptr<NativeReference> contentStorage = nullptr;
576     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
577         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
578     }
579 
580     sptr<IRemoteObject> parentToken = sessionInfo_->parentToken;
581     NapiAsyncTask::CompleteCallback complete =
582         [extwin = extensionWindow_, contentStorage, contextUrl, parentToken]
583         (napi_env env, NapiAsyncTask& task, int32_t status) {
584             if (extwin == nullptr) {
585                 TLOGE(WmsLogTag::WMS_UIEXT, "Window is nullptr");
586                 task.Reject(env,
587                     CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
588                 return;
589             }
590             LoadContentTask(contentStorage, contextUrl, extwin, env, task, parentToken, false);
591         };
592     napi_value result = nullptr;
593     NapiAsyncTask::Schedule("JsExtensionWindow::OnSetUIContent",
594         env, CreateAsyncTaskWithLastParam(env, callBack, nullptr, std::move(complete), &result));
595     return result;
596 }
597 
OnLoadContent(napi_env env,napi_callback_info info,bool isLoadedByName)598 napi_value JsExtensionWindow::OnLoadContent(napi_env env, napi_callback_info info, bool isLoadedByName)
599 {
600     TLOGI(WmsLogTag::WMS_UIEXT, "OnLoadContent is called");
601     WmErrorCode errCode = WmErrorCode::WM_OK;
602     size_t argc = 4;
603     napi_value argv[4] = {nullptr};
604     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
605     std::string contextUrl;
606     if (!ConvertFromJsValue(env, argv[0], contextUrl)) {
607         TLOGI(WmsLogTag::WMS_UIEXT, "Failed to convert parameter to context url");
608         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
609     }
610     napi_value storage = nullptr;
611     napi_value callBack = nullptr;
612     napi_value value1 = argv[1];
613     napi_value value2 = argv[2];
614     if (GetType(env, value1) == napi_function) {
615         callBack = value1;
616     } else if (GetType(env, value1) == napi_object) {
617         storage = value1;
618     }
619     if (GetType(env, value2) == napi_function) {
620         callBack = value2;
621     }
622     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
623         TLOGI(WmsLogTag::WMS_UIEXT, "Window is null or get invalid param");
624         napi_throw(env, CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
625         return NapiGetUndefined(env);
626     }
627 
628     std::shared_ptr<NativeReference> contentStorage = nullptr;
629     if (storage != nullptr) {
630         napi_ref result = nullptr;
631         napi_create_reference(env, storage, 1, &result);
632         contentStorage = std::shared_ptr<NativeReference>(reinterpret_cast<NativeReference*>(result));
633     }
634 
635     sptr<IRemoteObject> parentToken = sessionInfo_->parentToken;
636     NapiAsyncTask::CompleteCallback complete =
637         [extwin = extensionWindow_, contentStorage, contextUrl, parentToken, isLoadedByName](
638             napi_env env, NapiAsyncTask& task, int32_t status) {
639             if (extwin == nullptr) {
640                 TLOGE(WmsLogTag::WMS_UIEXT, "Window is nullptr or get invalid param");
641                 task.Reject(env, CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
642                 return;
643             }
644             LoadContentTask(contentStorage, contextUrl, extwin, env, task, parentToken, isLoadedByName);
645         };
646     napi_value result = nullptr;
647     NapiAsyncTask::Schedule("JsExtensionWindow::OnLoadContent",
648         env, CreateAsyncTaskWithLastParam(env, callBack, nullptr, std::move(complete), &result));
649     return result;
650 }
651 
OnGetWindowAvoidArea(napi_env env,napi_callback_info info)652 napi_value JsExtensionWindow::OnGetWindowAvoidArea(napi_env env, napi_callback_info info)
653 {
654     TLOGI(WmsLogTag::WMS_UIEXT, "OnGetWindowAvoidArea is called");
655 
656     WmErrorCode errCode = WmErrorCode::WM_OK;
657     size_t argc = 4;
658     napi_value argv[4] = {nullptr};
659     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
660     if (argc < 1) { // 1: params num
661         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
662     }
663     AvoidAreaType avoidAreaType = AvoidAreaType::TYPE_SYSTEM;
664     napi_value nativeMode = argv[0];
665     if (nativeMode == nullptr) {
666         errCode = WmErrorCode::WM_ERROR_INVALID_PARAM;
667     } else {
668         uint32_t resultValue = 0;
669         napi_get_value_uint32(env, nativeMode, &resultValue);
670         avoidAreaType = static_cast<AvoidAreaType>(resultValue);
671         errCode = ((avoidAreaType > AvoidAreaType::TYPE_NAVIGATION_INDICATOR) ||
672                    (avoidAreaType < AvoidAreaType::TYPE_SYSTEM)) ?
673                   WmErrorCode::WM_ERROR_INVALID_PARAM : WmErrorCode::WM_OK;
674     }
675     if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {
676         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
677     }
678 
679     if (extensionWindow_ == nullptr) {
680         TLOGE(WmsLogTag::WMS_UIEXT, "extensionWindow_ is nullptr");
681         napi_throw(env, CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STAGE_ABNORMALLY)));
682         return CreateJsValue(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_STAGE_ABNORMALLY));
683     }
684     // getAvoidRect by avoidAreaType
685     AvoidArea avoidArea;
686     WmErrorCode ret = WM_JS_TO_ERROR_CODE_MAP.at(extensionWindow_->GetAvoidAreaByType(avoidAreaType, avoidArea));
687     if (ret != WmErrorCode::WM_OK) {
688         TLOGE(WmsLogTag::WMS_UIEXT, "OnGetAvoidAreaByType failed");
689         avoidArea.topRect_ = g_emptyRect;
690         avoidArea.leftRect_ = g_emptyRect;
691         avoidArea.rightRect_ = g_emptyRect;
692         avoidArea.bottomRect_ = g_emptyRect;
693     }
694     napi_value avoidAreaObj = ConvertAvoidAreaToJsValue(env, avoidArea, avoidAreaType);
695     if (avoidAreaObj != nullptr) {
696         TLOGI(WmsLogTag::WMS_UIEXT, "avoidAreaObj is finish");
697         return avoidAreaObj;
698     } else {
699         TLOGE(WmsLogTag::WMS_UIEXT, "avoidAreaObj is nullptr");
700         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
701     }
702 }
703 
OnRegisterExtensionWindowCallback(napi_env env,napi_callback_info info)704 napi_value JsExtensionWindow::OnRegisterExtensionWindowCallback(napi_env env, napi_callback_info info)
705 {
706     sptr<Window> windowImpl = extensionWindow_->GetWindow();
707     if (windowImpl == nullptr) {
708         TLOGE(WmsLogTag::WMS_UIEXT, "WindowImpl is nullptr");
709         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
710     }
711     size_t argc = 4;
712     napi_value argv[4] = {nullptr};
713     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
714     if (argc < 2) { // 2: params num
715         TLOGE(WmsLogTag::WMS_UIEXT, "Argc is invalid: %{public}zu", argc);
716         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
717     }
718     std::string cbType;
719     if (!ConvertFromJsValue(env, argv[0], cbType)) {
720         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to convert parameter to callbackType");
721         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
722     }
723     napi_value value = argv[1];
724     if (!NapiIsCallable(env, value)) {
725         TLOGE(WmsLogTag::WMS_UIEXT, "Callback(info->argv[1]) is not callable");
726         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
727     }
728     WmErrorCode ret = extensionRegisterManager_->RegisterListener(windowImpl, cbType, CaseType::CASE_WINDOW,
729         env, value);
730     if (ret != WmErrorCode::WM_OK) {
731         TLOGE(WmsLogTag::WMS_UIEXT, "Callback(info->argv[1]) is not callable");
732         return NapiThrowError(env, ret);
733     }
734     TLOGI(WmsLogTag::WMS_UIEXT, "Register end, window [%{public}u, %{public}s], type = %{public}s",
735           windowImpl->GetWindowId(), windowImpl->GetWindowName().c_str(), cbType.c_str());
736     return NapiGetUndefined(env);
737 }
738 
OnUnRegisterExtensionWindowCallback(napi_env env,napi_callback_info info)739 napi_value JsExtensionWindow::OnUnRegisterExtensionWindowCallback(napi_env env, napi_callback_info info)
740 {
741     sptr<Window> windowImpl = extensionWindow_->GetWindow();
742     if (windowImpl == nullptr) {
743         TLOGE(WmsLogTag::WMS_UIEXT, "windowImpl is nullptr");
744         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
745     }
746     size_t argc = 4;
747     napi_value argv[4] = {nullptr};
748     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
749     if (argc < 1) {
750         TLOGE(WmsLogTag::WMS_UIEXT, "Argc is invalid: %{public}zu", argc);
751         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
752     }
753     std::string cbType;
754     if (!ConvertFromJsValue(env, argv[0], cbType)) {
755         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to convert parameter to callbackType");
756         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
757     }
758 
759     napi_value value = nullptr;
760     WmErrorCode ret = WmErrorCode::WM_OK;
761     if (argc == 1) {
762         ret = extensionRegisterManager_->UnregisterListener(windowImpl, cbType, CaseType::CASE_WINDOW, env, value);
763     } else {
764         value = argv[1];
765         if (value == nullptr || !NapiIsCallable(env, value)) {
766             ret = extensionRegisterManager_->UnregisterListener(windowImpl, cbType, CaseType::CASE_WINDOW,
767                 env, nullptr);
768         } else {
769             ret = extensionRegisterManager_->UnregisterListener(windowImpl, cbType, CaseType::CASE_WINDOW, env, value);
770         }
771     }
772 
773     if (ret != WmErrorCode::WM_OK) {
774         return NapiThrowError(env, ret);
775     }
776     TLOGI(WmsLogTag::WMS_UIEXT, "UnRegister end, window [%{public}u, %{public}s], type = %{public}s",
777           windowImpl->GetWindowId(), windowImpl->GetWindowName().c_str(), cbType.c_str());
778     return NapiGetUndefined(env);
779 }
780 
OnHideNonSecureWindows(napi_env env,napi_callback_info info)781 napi_value JsExtensionWindow::OnHideNonSecureWindows(napi_env env, napi_callback_info info)
782 {
783     if (extensionWindow_ == nullptr) {
784         TLOGE(WmsLogTag::WMS_UIEXT, "extensionWindow_ is nullptr");
785         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
786     }
787     sptr<Window> windowImpl = extensionWindow_->GetWindow();
788     if (windowImpl == nullptr) {
789         TLOGE(WmsLogTag::WMS_UIEXT, "windowImpl is nullptr");
790         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
791     }
792     size_t argc = 4;
793     napi_value argv[4] = {nullptr};
794     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
795     if (argc < 1) {
796         TLOGE(WmsLogTag::WMS_UIEXT, "Argc is invalid: %{public}zu", argc);
797         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
798     }
799     bool shouldHide = false;
800     if (!ConvertFromJsValue(env, argv[0], shouldHide)) {
801         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to convert parameter to bool");
802         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
803     }
804 
805     WmErrorCode ret = WmErrorCode::WM_OK;
806     ret = WM_JS_TO_ERROR_CODE_MAP.at(extensionWindow_->HideNonSecureWindows(shouldHide));
807     if (ret != WmErrorCode::WM_OK) {
808         return NapiThrowError(env, ret);
809     }
810     TLOGI(WmsLogTag::WMS_UIEXT, "OnHideNonSecureWindows end, window [%{public}u, %{public}s], shouldHide:%{public}u",
811           windowImpl->GetWindowId(), windowImpl->GetWindowName().c_str(), shouldHide);
812     return NapiGetUndefined(env);
813 }
814 
OnSetWaterMarkFlag(napi_env env,napi_callback_info info)815 napi_value JsExtensionWindow::OnSetWaterMarkFlag(napi_env env, napi_callback_info info)
816 {
817     if (extensionWindow_ == nullptr) {
818         TLOGE(WmsLogTag::WMS_UIEXT, "extensionWindow_ is nullptr");
819         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
820     }
821     sptr<Window> windowImpl = extensionWindow_->GetWindow();
822     if (windowImpl == nullptr) {
823         TLOGE(WmsLogTag::WMS_UIEXT, "windowImpl is nullptr");
824         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
825     }
826     size_t argc = 4;
827     napi_value argv[4] = {nullptr};
828     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
829     if (argc < 1) {
830         TLOGE(WmsLogTag::WMS_UIEXT, "Argc is invalid: %{public}zu", argc);
831         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
832     }
833     bool isEnable = false;
834     if (!ConvertFromJsValue(env, argv[0], isEnable)) {
835         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to convert parameter to bool");
836         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
837     }
838 
839     WmErrorCode ret = WmErrorCode::WM_OK;
840     ret = WM_JS_TO_ERROR_CODE_MAP.at(extensionWindow_->SetWaterMarkFlag(isEnable));
841     if (ret != WmErrorCode::WM_OK) {
842         return NapiThrowError(env, ret);
843     }
844     TLOGI(WmsLogTag::WMS_UIEXT, "OnSetWaterMark end, window [%{public}u, %{public}s], isEnable:%{public}u.",
845           windowImpl->GetWindowId(), windowImpl->GetWindowName().c_str(), isEnable);
846     return NapiGetUndefined(env);
847 }
848 
OnHidePrivacyContentForHost(napi_env env,napi_callback_info info)849 napi_value JsExtensionWindow::OnHidePrivacyContentForHost(napi_env env, napi_callback_info info)
850 {
851     if (extensionWindow_ == nullptr) {
852         TLOGE(WmsLogTag::WMS_UIEXT, "extension window is nullptr");
853         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
854     }
855 
856     sptr<Window> windowImpl = extensionWindow_->GetWindow();
857     if (windowImpl == nullptr) {
858         TLOGE(WmsLogTag::WMS_UIEXT, "windowImpl is nullptr");
859         return NapiThrowError(env, WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
860     }
861 
862     size_t argc = 4;
863     napi_value argv[4] = {nullptr};
864     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
865     if (argc < 1) {
866         TLOGE(WmsLogTag::WMS_UIEXT, "Argc is invalid: %{public}zu", argc);
867         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
868     }
869 
870     bool needHide = false;
871     if (!ConvertFromJsValue(env, argv[0], needHide)) {
872         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to convert parameter to bool");
873         return NapiThrowError(env, WmErrorCode::WM_ERROR_INVALID_PARAM);
874     }
875 
876     auto ret = WM_JS_TO_ERROR_CODE_MAP.at(extensionWindow_->HidePrivacyContentForHost(needHide));
877     if (ret != WmErrorCode::WM_OK) {
878         return NapiThrowError(env, ret);
879     }
880 
881     TLOGI(WmsLogTag::WMS_UIEXT, "finished, window [%{public}u, %{public}s], needHide:%{public}u.",
882           windowImpl->GetWindowId(), windowImpl->GetWindowName().c_str(), needHide);
883 
884     return NapiGetUndefined(env);
885 }
886 
GetProperties(napi_env env,napi_callback_info info)887 napi_value JsExtensionWindow::GetProperties(napi_env env, napi_callback_info info)
888 {
889     TLOGI(WmsLogTag::WMS_UIEXT, "GetProperties is called");
890     napi_value jsThis;
891     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr));
892 
893     JsExtensionWindow* jsExtensionWindow = nullptr;
894     NAPI_CALL(env, napi_unwrap(env, jsThis, reinterpret_cast<void**>(&jsExtensionWindow)));
895     if (!jsExtensionWindow || !jsExtensionWindow->extensionWindow_) {
896         TLOGE(WmsLogTag::WMS_UIEXT, "window is nullptr");
897         return nullptr;
898     }
899     sptr<Rosen::Window> window = jsExtensionWindow->extensionWindow_->GetWindow();
900     return CreateJsExtensionWindowPropertiesObject(env, window);
901 }
902 
OnCreateSubWindowWithOptions(napi_env env,napi_callback_info info)903 napi_value JsExtensionWindow::OnCreateSubWindowWithOptions(napi_env env, napi_callback_info info)
904 {
905     size_t argc = 4;
906     napi_value argv[4] = {nullptr};
907     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
908     std::string windowName;
909     if (!ConvertFromJsValue(env, argv[0], windowName)) {
910         TLOGE(WmsLogTag::WMS_UIEXT, "[NAPI]Failed to convert parameter to windowName");
911         napi_throw(env, CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
912         return NapiGetUndefined(env);
913     }
914     WindowOption option;
915     if (!ParseSubWindowOptions(env, argv[1], option)) {
916         TLOGE(WmsLogTag::WMS_UIEXT, "[NAPI]Get invalid options param");
917         napi_throw(env, CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
918         return NapiGetUndefined(env);
919     }
920     if (option.GetWindowTopmost() && !Permission::IsSystemCalling() && !Permission::IsStartByHdcd()) {
921         TLOGE(WmsLogTag::WMS_SUB, "Modal subwindow has topmost, but no system permission");
922         napi_throw(env, CreateJsError(env, static_cast<int32_t>(WmErrorCode::WM_ERROR_NOT_SYSTEM_APP)));
923         return NapiGetUndefined(env);
924     }
925     NapiAsyncTask::CompleteCallback complete =
926         [weak = extensionWindow_, windowName, option](napi_env env, NapiAsyncTask& task, int32_t status) {
927             if (weak == nullptr) {
928                 task.Reject(env, CreateJsError(env,
929                     static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "extensionWindow_ is null"));
930             }
931             sptr<Rosen::WindowOption> windowOption = new WindowOption(option);
932             JsExtensionWindow::SetWindowOption(windowOption);
933             auto extWindow = weak->GetWindow();
934             if (extWindow == nullptr) {
935                 task.Reject(env, CreateJsError(env,
936                     static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "extension's window is null"));
937             }
938             auto window = Window::Create(windowName, windowOption, extWindow->GetContext());
939             if (window == nullptr) {
940                 task.Reject(env, CreateJsError(env,
941                     static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY), "create sub window failed"));
942                 return;
943             }
944             if (!window->IsTopmost()) {
945                 extWindow->NotifyModalUIExtensionMayBeCovered(false);
946             }
947             task.Resolve(env, CreateJsWindowObject(env, window));
948             TLOGI(WmsLogTag::WMS_UIEXT, "[NAPI]Create sub window %{public}s end", windowName.c_str());
949         };
950     napi_value callback = (argv[2] != nullptr && GetType(env, argv[2]) == napi_function) ? argv[2] : nullptr;
951     napi_value result = nullptr;
952     NapiAsyncTask::Schedule("JsExtensionWindow::OnCreateSubWindowWithOptions",
953         env, CreateAsyncTaskWithLastParam(env, callback, nullptr, std::move(complete), &result));
954     return result;
955 }
956 
SetWindowOption(sptr<Rosen::WindowOption> windowOption)957 void JsExtensionWindow::SetWindowOption(sptr<Rosen::WindowOption> windowOption)
958 {
959     windowOption->SetWindowType(Rosen::WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
960     windowOption->SetWindowMode(Rosen::WindowMode::WINDOW_MODE_FLOATING);
961     windowOption->SetOnlySupportSceneBoard(true);
962     windowOption->SetExtensionTag(true);
963 }
964 
ParseSubWindowOptions(napi_env env,napi_value jsObject,WindowOption & option)965 bool JsExtensionWindow::ParseSubWindowOptions(napi_env env, napi_value jsObject, WindowOption& option)
966 {
967     if (jsObject == nullptr) {
968         TLOGE(WmsLogTag::WMS_UIEXT, "jsObject is null");
969         return false;
970     }
971     std::string title;
972     if (!ParseJsValue(jsObject, env, "title", title)) {
973         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to convert parameter to title");
974         return false;
975     }
976     bool decorEnabled = false;
977     if (!ParseJsValue(jsObject, env, "decorEnabled", decorEnabled)) {
978         TLOGE(WmsLogTag::WMS_UIEXT, "Failed to convert parameter to decorEnabled");
979         return false;
980     }
981     bool isModal = false;
982     if (ParseJsValue(jsObject, env, "isModal", isModal)) {
983         TLOGI(WmsLogTag::WMS_UIEXT, "isModal:%{public}d", isModal);
984         if (isModal) {
985             option.AddWindowFlag(WindowFlag::WINDOW_FLAG_IS_MODAL);
986         }
987     }
988     bool isTopmost = false;
989     if (ParseJsValue(jsObject, env, "isTopmost", isTopmost)) {
990         if (!isModal && isTopmost) {
991             TLOGE(WmsLogTag::WMS_SUB, "Normal subwindow is topmost");
992             return false;
993         }
994         option.SetWindowTopmost(isTopmost);
995     }
996 
997     option.SetSubWindowTitle(title);
998     option.SetSubWindowDecorEnable(decorEnabled);
999     option.SetParentId(hostWindowId_);
1000     return true;
1001 }
1002 }  // namespace Rosen
1003 }  // namespace OHOS
1004