1 /*
2 * Copyright (c) 2021-2022 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 #include "js_window_utils.h"
16 #include <iomanip>
17 #include <regex>
18 #include <sstream>
19 #include "accesstoken_kit.h"
20 #include "bundle_constants.h"
21 #include "ipc_skeleton.h"
22 #include "window_manager_hilog.h"
23
24 namespace OHOS {
25 namespace Rosen {
26 using namespace AbilityRuntime;
27 namespace {
28 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "JsUtils"};
29 }
30
WindowTypeInit(NativeEngine * engine)31 NativeValue* WindowTypeInit(NativeEngine* engine)
32 {
33 WLOGFD("WindowTypeInit");
34
35 if (engine == nullptr) {
36 WLOGFE("Engine is nullptr");
37 return nullptr;
38 }
39
40 NativeValue *objValue = engine->CreateObject();
41 NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
42 if (object == nullptr) {
43 WLOGFE("Failed to get object");
44 return nullptr;
45 }
46
47 object->SetProperty("TYPE_APP", CreateJsValue(*engine,
48 static_cast<int32_t>(ApiWindowType::TYPE_APP)));
49 object->SetProperty("TYPE_SYSTEM_ALERT", CreateJsValue(*engine,
50 static_cast<int32_t>(ApiWindowType::TYPE_SYSTEM_ALERT)));
51 object->SetProperty("TYPE_INPUT_METHOD", CreateJsValue(*engine,
52 static_cast<int32_t>(ApiWindowType::TYPE_INPUT_METHOD)));
53 object->SetProperty("TYPE_STATUS_BAR", CreateJsValue(*engine,
54 static_cast<int32_t>(ApiWindowType::TYPE_STATUS_BAR)));
55 object->SetProperty("TYPE_PANEL", CreateJsValue(*engine,
56 static_cast<int32_t>(ApiWindowType::TYPE_PANEL)));
57 object->SetProperty("TYPE_KEYGUARD", CreateJsValue(*engine,
58 static_cast<int32_t>(ApiWindowType::TYPE_KEYGUARD)));
59 object->SetProperty("TYPE_VOLUME_OVERLAY", CreateJsValue(*engine,
60 static_cast<int32_t>(ApiWindowType::TYPE_VOLUME_OVERLAY)));
61 object->SetProperty("TYPE_NAVIGATION_BAR", CreateJsValue(*engine,
62 static_cast<int32_t>(ApiWindowType::TYPE_NAVIGATION_BAR)));
63 object->SetProperty("TYPE_FLOAT", CreateJsValue(*engine,
64 static_cast<int32_t>(ApiWindowType::TYPE_FLOAT)));
65 object->SetProperty("TYPE_FLOAT_CAMERA", CreateJsValue(*engine,
66 static_cast<int32_t>(ApiWindowType::TYPE_FLOAT_CAMERA)));
67 object->SetProperty("TYPE_WALLPAPER", CreateJsValue(*engine,
68 static_cast<int32_t>(ApiWindowType::TYPE_WALLPAPER)));
69 object->SetProperty("TYPE_DESKTOP", CreateJsValue(*engine,
70 static_cast<int32_t>(ApiWindowType::TYPE_DESKTOP)));
71 object->SetProperty("TYPE_LAUNCHER_RECENT", CreateJsValue(*engine,
72 static_cast<int32_t>(ApiWindowType::TYPE_LAUNCHER_RECENT)));
73 object->SetProperty("TYPE_LAUNCHER_DOCK", CreateJsValue(*engine,
74 static_cast<int32_t>(ApiWindowType::TYPE_LAUNCHER_DOCK)));
75 object->SetProperty("TYPE_VOICE_INTERACTION", CreateJsValue(*engine,
76 static_cast<int32_t>(ApiWindowType::TYPE_VOICE_INTERACTION)));
77 object->SetProperty("TYPE_DIALOG", CreateJsValue(*engine,
78 static_cast<int32_t>(ApiWindowType::TYPE_DIALOG)));
79 object->SetProperty("TYPE_POINTER", CreateJsValue(*engine,
80 static_cast<int32_t>(ApiWindowType::TYPE_POINTER)));
81 object->SetProperty("TYPE_SCREENSHOT", CreateJsValue(*engine,
82 static_cast<int32_t>(ApiWindowType::TYPE_SCREENSHOT)));
83
84 return objValue;
85 }
86
AvoidAreaTypeInit(NativeEngine * engine)87 NativeValue* AvoidAreaTypeInit(NativeEngine* engine)
88 {
89 WLOGFD("AvoidAreaTypeInit");
90
91 if (engine == nullptr) {
92 WLOGFE("Engine is nullptr");
93 return nullptr;
94 }
95
96 NativeValue *objValue = engine->CreateObject();
97 NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
98 if (object == nullptr) {
99 WLOGFE("Failed to get object");
100 return nullptr;
101 }
102
103 object->SetProperty("TYPE_SYSTEM", CreateJsValue(*engine,
104 static_cast<int32_t>(AvoidAreaType::TYPE_SYSTEM)));
105 object->SetProperty("TYPE_CUTOUT", CreateJsValue(*engine,
106 static_cast<int32_t>(AvoidAreaType::TYPE_CUTOUT)));
107 object->SetProperty("TYPE_SYSTEM_GESTURE", CreateJsValue(*engine,
108 static_cast<int32_t>(AvoidAreaType::TYPE_SYSTEM_GESTURE)));
109 object->SetProperty("TYPE_KEYBOARD", CreateJsValue(*engine, static_cast<int32_t>(AvoidAreaType::TYPE_KEYBOARD)));
110 return objValue;
111 }
112
WindowModeInit(NativeEngine * engine)113 NativeValue* WindowModeInit(NativeEngine* engine)
114 {
115 WLOGFD("WindowModeInit");
116
117 if (engine == nullptr) {
118 WLOGFE("Engine is nullptr");
119 return nullptr;
120 }
121
122 NativeValue *objValue = engine->CreateObject();
123 NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
124 if (object == nullptr) {
125 WLOGFE("Failed to get object");
126 return nullptr;
127 }
128
129 object->SetProperty("UNDEFINED", CreateJsValue(*engine,
130 static_cast<int32_t>(ApiWindowMode::UNDEFINED)));
131 object->SetProperty("FULLSCREEN", CreateJsValue(*engine,
132 static_cast<int32_t>(ApiWindowMode::FULLSCREEN)));
133 object->SetProperty("PRIMARY", CreateJsValue(*engine,
134 static_cast<int32_t>(ApiWindowMode::PRIMARY)));
135 object->SetProperty("SECONDARY", CreateJsValue(*engine,
136 static_cast<int32_t>(ApiWindowMode::SECONDARY)));
137 object->SetProperty("FLOATING", CreateJsValue(*engine,
138 static_cast<int32_t>(ApiWindowMode::FLOATING)));
139 return objValue;
140 }
141
ColorSpaceInit(NativeEngine * engine)142 NativeValue* ColorSpaceInit(NativeEngine* engine)
143 {
144 WLOGFD("ColorSpaceInit");
145
146 if (engine == nullptr) {
147 WLOGFE("Engine is nullptr");
148 return nullptr;
149 }
150
151 NativeValue *objValue = engine->CreateObject();
152 NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
153 if (object == nullptr) {
154 WLOGFE("Failed to get object");
155 return nullptr;
156 }
157
158 object->SetProperty("DEFAULT", CreateJsValue(*engine,
159 static_cast<int32_t>(ColorSpace::COLOR_SPACE_DEFAULT)));
160 object->SetProperty("WIDE_GAMUT", CreateJsValue(*engine,
161 static_cast<int32_t>(ColorSpace::COLOR_SPACE_WIDE_GAMUT)));
162 return objValue;
163 }
164
OrientationInit(NativeEngine * engine)165 NativeValue* OrientationInit(NativeEngine* engine)
166 {
167 WLOGFD("OrientationInit");
168
169 if (engine == nullptr) {
170 WLOGFE("Engine is nullptr");
171 return nullptr;
172 }
173
174 NativeValue *objValue = engine->CreateObject();
175 NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
176 if (object == nullptr) {
177 WLOGFE("Failed to get object");
178 return nullptr;
179 }
180
181 object->SetProperty("UNSPECIFIED", CreateJsValue(*engine,
182 static_cast<int32_t>(Orientation::UNSPECIFIED)));
183 object->SetProperty("PORTRAIT", CreateJsValue(*engine,
184 static_cast<int32_t>(Orientation::VERTICAL)));
185 object->SetProperty("LANDSCAPE", CreateJsValue(*engine,
186 static_cast<int32_t>(Orientation::HORIZONTAL)));
187 object->SetProperty("PORTRAIT_INVERTED", CreateJsValue(*engine,
188 static_cast<int32_t>(Orientation::REVERSE_VERTICAL)));
189 object->SetProperty("LANDSCAPE_INVERTED", CreateJsValue(*engine,
190 static_cast<int32_t>(Orientation::REVERSE_HORIZONTAL)));
191 object->SetProperty("AUTO_ROTATION", CreateJsValue(*engine,
192 static_cast<int32_t>(Orientation::SENSOR)));
193 object->SetProperty("AUTO_ROTATION_PORTRAIT", CreateJsValue(*engine,
194 static_cast<int32_t>(Orientation::SENSOR_VERTICAL)));
195 object->SetProperty("AUTO_ROTATION_LANDSCAPE", CreateJsValue(*engine,
196 static_cast<int32_t>(Orientation::SENSOR_HORIZONTAL)));
197 object->SetProperty("AUTO_ROTATION_RESTRICTED", CreateJsValue(*engine,
198 static_cast<int32_t>(Orientation::AUTO_ROTATION_RESTRICTED)));
199 object->SetProperty("AUTO_ROTATION_PORTRAIT_RESTRICTED", CreateJsValue(*engine,
200 static_cast<int32_t>(Orientation::AUTO_ROTATION_PORTRAIT_RESTRICTED)));
201 object->SetProperty("AUTO_ROTATION_LANDSCAPE_RESTRICTED", CreateJsValue(*engine,
202 static_cast<int32_t>(Orientation::AUTO_ROTATION_LANDSCAPE_RESTRICTED)));
203 object->SetProperty("LOCKED", CreateJsValue(*engine,
204 static_cast<int32_t>(Orientation::LOCKED)));
205 return objValue;
206 }
207
WindowStageEventTypeInit(NativeEngine * engine)208 NativeValue* WindowStageEventTypeInit(NativeEngine* engine)
209 {
210 WLOGFD("WindowStageEventTypeInit");
211
212 if (engine == nullptr) {
213 WLOGFE("Engine is nullptr");
214 return nullptr;
215 }
216
217 NativeValue *objValue = engine->CreateObject();
218 NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
219 if (object == nullptr) {
220 WLOGFE("Failed to get object");
221 return nullptr;
222 }
223
224 object->SetProperty("SHOWN", CreateJsValue(*engine,
225 static_cast<int32_t>(LifeCycleEventType::FOREGROUND)));
226 object->SetProperty("ACTIVE", CreateJsValue(*engine,
227 static_cast<int32_t>(LifeCycleEventType::ACTIVE)));
228 object->SetProperty("INACTIVE", CreateJsValue(*engine,
229 static_cast<int32_t>(LifeCycleEventType::INACTIVE)));
230 object->SetProperty("HIDDEN", CreateJsValue(*engine,
231 static_cast<int32_t>(LifeCycleEventType::BACKGROUND)));
232 return objValue;
233 }
234
WindowEventTypeInit(NativeEngine * engine)235 NativeValue* WindowEventTypeInit(NativeEngine* engine)
236 {
237 WLOGFD("WindowEventTypeInit");
238
239 if (engine == nullptr) {
240 WLOGFE("Engine is nullptr");
241 return nullptr;
242 }
243
244 NativeValue *objValue = engine->CreateObject();
245 NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
246 if (object == nullptr) {
247 WLOGFE("Failed to get object");
248 return nullptr;
249 }
250
251 object->SetProperty("WINDOW_SHOWN", CreateJsValue(*engine,
252 static_cast<int32_t>(LifeCycleEventType::FOREGROUND)));
253 object->SetProperty("WINDOW_ACTIVE", CreateJsValue(*engine,
254 static_cast<int32_t>(LifeCycleEventType::ACTIVE)));
255 object->SetProperty("WINDOW_INACTIVE", CreateJsValue(*engine,
256 static_cast<int32_t>(LifeCycleEventType::INACTIVE)));
257 object->SetProperty("WINDOW_HIDDEN", CreateJsValue(*engine,
258 static_cast<int32_t>(LifeCycleEventType::BACKGROUND)));
259 return objValue;
260 }
261
WindowLayoutModeInit(NativeEngine * engine)262 NativeValue* WindowLayoutModeInit(NativeEngine* engine)
263 {
264 WLOGFD("WindowLayoutModeInit");
265 if (engine == nullptr) {
266 WLOGFE("Engine is nullptr");
267 return nullptr;
268 }
269
270 NativeValue *objValue = engine->CreateObject();
271 NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
272 if (object == nullptr) {
273 WLOGFE("Failed to get object");
274 return nullptr;
275 }
276
277 object->SetProperty("WINDOW_LAYOUT_MODE_CASCADE", CreateJsValue(*engine,
278 static_cast<int32_t>(WindowLayoutMode::CASCADE)));
279 object->SetProperty("WINDOW_LAYOUT_MODE_TILE", CreateJsValue(*engine,
280 static_cast<int32_t>(WindowLayoutMode::TILE)));
281 return objValue;
282 }
283
BlurStyleInit(NativeEngine * engine)284 NativeValue* BlurStyleInit(NativeEngine* engine)
285 {
286 WLOGI("BlurStyleInit");
287 if (engine == nullptr) {
288 WLOGFE("Engine is nullptr");
289 return nullptr;
290 }
291
292 NativeValue *objValue = engine->CreateObject();
293 NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
294 if (object == nullptr) {
295 WLOGFE("Failed to get object");
296 return nullptr;
297 }
298
299 object->SetProperty("OFF", CreateJsValue(*engine,
300 static_cast<int32_t>(WindowBlurStyle::WINDOW_BLUR_OFF)));
301 object->SetProperty("THIN", CreateJsValue(*engine,
302 static_cast<int32_t>(WindowBlurStyle::WINDOW_BLUR_THIN)));
303 object->SetProperty("REGULAR", CreateJsValue(*engine,
304 static_cast<int32_t>(WindowBlurStyle::WINDOW_BLUR_REGULAR)));
305 object->SetProperty("THICK", CreateJsValue(*engine,
306 static_cast<int32_t>(WindowBlurStyle::WINDOW_BLUR_THICK)));
307 return objValue;
308 }
309
WindowErrorInit(NativeEngine * engine)310 NativeValue* WindowErrorInit(NativeEngine* engine)
311 {
312 WLOGFD("WindowErrorInit");
313 if (engine == nullptr) {
314 WLOGFE("Engine is nullptr");
315 return nullptr;
316 }
317
318 NativeValue *objValue = engine->CreateObject();
319 NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
320 if (object == nullptr) {
321 WLOGFE("Failed to get object");
322 return nullptr;
323 }
324
325 object->SetProperty("WM_DO_NOTHING", CreateJsValue(*engine,
326 static_cast<int32_t>(WMError::WM_DO_NOTHING)));
327 object->SetProperty("WM_ERROR_NO_MEM", CreateJsValue(*engine,
328 static_cast<int32_t>(WMError::WM_ERROR_NO_MEM)));
329 object->SetProperty("WM_ERROR_DESTROYED_OBJECT", CreateJsValue(*engine,
330 static_cast<int32_t>(WMError::WM_ERROR_DESTROYED_OBJECT)));
331 object->SetProperty("WM_ERROR_INVALID_WINDOW", CreateJsValue(*engine,
332 static_cast<int32_t>(WMError::WM_ERROR_INVALID_WINDOW)));
333 object->SetProperty("WM_ERROR_INVALID_WINDOW_MODE_OR_SIZE", CreateJsValue(*engine,
334 static_cast<int32_t>(WMError::WM_ERROR_INVALID_WINDOW_MODE_OR_SIZE)));
335 object->SetProperty("WM_ERROR_INVALID_OPERATION", CreateJsValue(*engine,
336 static_cast<int32_t>(WMError::WM_ERROR_INVALID_OPERATION)));
337 object->SetProperty("WM_ERROR_INVALID_PERMISSION", CreateJsValue(*engine,
338 static_cast<int32_t>(WMError::WM_ERROR_INVALID_PERMISSION)));
339 object->SetProperty("WM_ERROR_NO_REMOTE_ANIMATION", CreateJsValue(*engine,
340 static_cast<int32_t>(WMError::WM_ERROR_NO_REMOTE_ANIMATION)));
341 object->SetProperty("WM_ERROR_DEVICE_NOT_SUPPORT", CreateJsValue(*engine,
342 static_cast<int32_t>(WMError::WM_ERROR_DEVICE_NOT_SUPPORT)));
343 object->SetProperty("WM_ERROR_NULLPTR", CreateJsValue(*engine,
344 static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
345 object->SetProperty("WM_ERROR_INVALID_TYPE", CreateJsValue(*engine,
346 static_cast<int32_t>(WMError::WM_ERROR_INVALID_TYPE)));
347 object->SetProperty("WM_ERROR_INVALID_PARAM", CreateJsValue(*engine,
348 static_cast<int32_t>(WMError::WM_ERROR_INVALID_PARAM)));
349 object->SetProperty("WM_ERROR_SAMGR", CreateJsValue(*engine,
350 static_cast<int32_t>(WMError::WM_ERROR_SAMGR)));
351 object->SetProperty("WM_ERROR_IPC_FAILED", CreateJsValue(*engine,
352 static_cast<int32_t>(WMError::WM_ERROR_IPC_FAILED)));
353 object->SetProperty("WM_ERROR_START_ABILITY_FAILED", CreateJsValue(*engine,
354 static_cast<int32_t>(WMError::WM_ERROR_START_ABILITY_FAILED)));
355 return objValue;
356 }
357
WindowErrorCodeInit(NativeEngine * engine)358 NativeValue* WindowErrorCodeInit(NativeEngine* engine)
359 {
360 WLOGFD("WindowErrorCodeInit");
361 if (engine == nullptr) {
362 WLOGFE("Engine is nullptr");
363 return nullptr;
364 }
365
366 NativeValue *objValue = engine->CreateObject();
367 NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
368 if (object == nullptr) {
369 WLOGFE("Failed to get object");
370 return nullptr;
371 }
372 object->SetProperty("WM_ERROR_NO_PERMISSION", CreateJsValue(*engine,
373 static_cast<int32_t>(WmErrorCode::WM_ERROR_NO_PERMISSION)));
374 object->SetProperty("WM_ERROR_NOT_SYSTEM_APP", CreateJsValue(*engine,
375 static_cast<int32_t>(WmErrorCode::WM_ERROR_NOT_SYSTEM_APP)));
376 object->SetProperty("WM_ERROR_INVALID_PARAM", CreateJsValue(*engine,
377 static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
378 object->SetProperty("WM_ERROR_DEVICE_NOT_SUPPORT", CreateJsValue(*engine,
379 static_cast<int32_t>(WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT)));
380 object->SetProperty("WM_ERROR_REPEAT_OPERATION", CreateJsValue(*engine,
381 static_cast<int32_t>(WmErrorCode::WM_ERROR_REPEAT_OPERATION)));
382 object->SetProperty("WM_ERROR_STATE_ABNORMALLY", CreateJsValue(*engine,
383 static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
384 object->SetProperty("WM_ERROR_SYSTEM_ABNORMALLY", CreateJsValue(*engine,
385 static_cast<int32_t>(WmErrorCode::WM_ERROR_SYSTEM_ABNORMALLY)));
386 object->SetProperty("WM_ERROR_INVALID_CALLING", CreateJsValue(*engine,
387 static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING)));
388 object->SetProperty("WM_ERROR_STAGE_ABNORMALLY", CreateJsValue(*engine,
389 static_cast<int32_t>(WmErrorCode::WM_ERROR_STAGE_ABNORMALLY)));
390 object->SetProperty("WM_ERROR_CONTEXT_ABNORMALLY", CreateJsValue(*engine,
391 static_cast<int32_t>(WmErrorCode::WM_ERROR_CONTEXT_ABNORMALLY)));
392 object->SetProperty("WM_ERROR_START_ABILITY_FAILED", CreateJsValue(*engine,
393 static_cast<int32_t>(WmErrorCode::WM_ERROR_START_ABILITY_FAILED)));
394 return objValue;
395 }
396
GetRectAndConvertToJsValue(NativeEngine & engine,const Rect & rect)397 NativeValue* GetRectAndConvertToJsValue(NativeEngine& engine, const Rect& rect)
398 {
399 NativeValue* objValue = engine.CreateObject();
400 NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
401 if (object == nullptr) {
402 WLOGFE("Failed to convert rect to jsObject");
403 return nullptr;
404 }
405 object->SetProperty("left", CreateJsValue(engine, rect.posX_));
406 object->SetProperty("top", CreateJsValue(engine, rect.posY_));
407 object->SetProperty("width", CreateJsValue(engine, rect.width_));
408 object->SetProperty("height", CreateJsValue(engine, rect.height_));
409 return objValue;
410 }
411
CreateJsWindowPropertiesObject(NativeEngine & engine,sptr<Window> & window)412 NativeValue* CreateJsWindowPropertiesObject(NativeEngine& engine, sptr<Window>& window)
413 {
414 WLOGI("CreateJsWindowPropertiesObject");
415 NativeValue* objValue = engine.CreateObject();
416 NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
417 if (object == nullptr) {
418 WLOGFE("Failed to convert windowProperties to jsObject");
419 return nullptr;
420 }
421
422 Rect rect = window->GetRect();
423 NativeValue* rectObj = GetRectAndConvertToJsValue(engine, rect);
424 if (rectObj == nullptr) {
425 WLOGFE("GetRect failed!");
426 }
427 object->SetProperty("windowRect", rectObj);
428 WindowType type = window->GetType();
429 if (NATIVE_JS_TO_WINDOW_TYPE_MAP.count(type) != 0) {
430 object->SetProperty("type", CreateJsValue(engine, NATIVE_JS_TO_WINDOW_TYPE_MAP.at(type)));
431 } else {
432 object->SetProperty("type", CreateJsValue(engine, type));
433 }
434 object->SetProperty("isLayoutFullScreen", CreateJsValue(engine, window->IsLayoutFullScreen()));
435 object->SetProperty("isFullScreen", CreateJsValue(engine, window->IsFullScreen()));
436 object->SetProperty("touchable", CreateJsValue(engine, window->GetTouchable()));
437 object->SetProperty("focusable", CreateJsValue(engine, window->GetFocusable()));
438 object->SetProperty("name", CreateJsValue(engine, window->GetWindowName()));
439 object->SetProperty("isPrivacyMode", CreateJsValue(engine, window->IsPrivacyMode()));
440 object->SetProperty("isKeepScreenOn", CreateJsValue(engine, window->IsKeepScreenOn()));
441 object->SetProperty("brightness", CreateJsValue(engine, window->GetBrightness()));
442 object->SetProperty("isTransparent", CreateJsValue(engine, window->IsTransparent()));
443 object->SetProperty("isRoundCorner", CreateJsValue(engine, false)); // empty method
444 object->SetProperty("dimBehindValue", CreateJsValue(engine, 0));
445 object->SetProperty("id", CreateJsValue(engine, window->GetWindowId()));
446 return objValue;
447 }
GetHexColor(uint32_t color)448 static std::string GetHexColor(uint32_t color)
449 {
450 std::stringstream ioss;
451 std::string temp;
452 ioss << std::setiosflags(std::ios::uppercase) << std::hex << color;
453 ioss >> temp;
454 int count = RGBA_LENGTH - static_cast<int>(temp.length());
455 std::string tmpColor(count, '0');
456 tmpColor += temp;
457 std::string finalColor("#");
458 finalColor += tmpColor;
459 return finalColor;
460 }
461
CreateJsSystemBarRegionTintObject(NativeEngine & engine,const SystemBarRegionTint & tint)462 static NativeValue* CreateJsSystemBarRegionTintObject(NativeEngine& engine, const SystemBarRegionTint& tint)
463 {
464 NativeValue* objValue = engine.CreateObject();
465 NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
466 if (object == nullptr) {
467 WLOGFE("Failed to convert SystemBarProperty to jsObject");
468 return nullptr;
469 }
470 if (NATIVE_JS_TO_WINDOW_TYPE_MAP.count(tint.type_) != 0) {
471 object->SetProperty("type", CreateJsValue(engine, NATIVE_JS_TO_WINDOW_TYPE_MAP.at(tint.type_)));
472 } else {
473 object->SetProperty("type", CreateJsValue(engine, tint.type_));
474 }
475 object->SetProperty("isEnable", CreateJsValue(engine, tint.prop_.enable_));
476 std::string bkgColor = GetHexColor(tint.prop_.backgroundColor_);
477 object->SetProperty("backgroundColor", CreateJsValue(engine, bkgColor));
478 std::string contentColor = GetHexColor(tint.prop_.contentColor_);
479 object->SetProperty("contentColor", CreateJsValue(engine, contentColor));
480 Rect rect = tint.region_;
481 object->SetProperty("region", GetRectAndConvertToJsValue(engine, rect));
482
483 WLOGFD("Type %{public}u [%{public}u %{public}s %{public}s]",
484 tint.type_, tint.prop_.enable_, bkgColor.c_str(), contentColor.c_str());
485 WLOGFD("Region [%{public}d %{public}d %{public}u %{public}u]",
486 rect.posX_, rect.posY_, rect.width_, rect.height_);
487 return objValue;
488 }
489
CreateJsSystemBarRegionTintArrayObject(NativeEngine & engine,const SystemBarRegionTints & tints)490 NativeValue* CreateJsSystemBarRegionTintArrayObject(NativeEngine& engine, const SystemBarRegionTints& tints)
491 {
492 if (tints.empty()) {
493 WLOGFE("Empty tints");
494 return nullptr;
495 }
496 NativeValue* objValue = engine.CreateArray(tints.size());
497 NativeArray* array = ConvertNativeValueTo<NativeArray>(objValue);
498 if (array == nullptr) {
499 WLOGFE("Failed to convert SystemBarProperties to jsArrayObject");
500 return nullptr;
501 }
502 uint32_t index = 0;
503 for (size_t i = 0; i < tints.size(); i++) {
504 array->SetElement(index++, CreateJsSystemBarRegionTintObject(engine, tints[i]));
505 }
506 return objValue;
507 }
508
GetSystemBarStatus(std::map<WindowType,SystemBarProperty> & systemBarProperties,std::map<WindowType,SystemBarPropertyFlag> & systemBarpropertyFlags,NativeEngine & engine,NativeCallbackInfo & info,sptr<Window> & window)509 bool GetSystemBarStatus(std::map<WindowType, SystemBarProperty>& systemBarProperties,
510 std::map<WindowType, SystemBarPropertyFlag>& systemBarpropertyFlags,
511 NativeEngine& engine, NativeCallbackInfo& info, sptr<Window>& window)
512 {
513 NativeArray* nativeArray = nullptr;
514 uint32_t size = 0;
515 if (info.argc > 0 && info.argv[0]->TypeOf() != NATIVE_FUNCTION) {
516 nativeArray = ConvertNativeValueTo<NativeArray>(info.argv[0]);
517 if (nativeArray == nullptr) {
518 WLOGFE("Failed to convert parameter to SystemBarArray");
519 return false;
520 }
521 size = nativeArray->GetLength();
522 }
523 auto statusProperty = window->GetSystemBarPropertyByType(WindowType::WINDOW_TYPE_STATUS_BAR);
524 auto navProperty = window->GetSystemBarPropertyByType(WindowType::WINDOW_TYPE_NAVIGATION_BAR);
525 statusProperty.enable_ = false;
526 navProperty.enable_ = false;
527 systemBarProperties[WindowType::WINDOW_TYPE_STATUS_BAR] = statusProperty;
528 systemBarProperties[WindowType::WINDOW_TYPE_NAVIGATION_BAR] = navProperty;
529 systemBarpropertyFlags[WindowType::WINDOW_TYPE_STATUS_BAR] = SystemBarPropertyFlag();
530 systemBarpropertyFlags[WindowType::WINDOW_TYPE_NAVIGATION_BAR] = SystemBarPropertyFlag();
531 for (uint32_t i = 0; i < size; i++) {
532 std::string name;
533 if (!ConvertFromJsValue(engine, nativeArray->GetElement(i), name)) {
534 WLOGFE("Failed to convert parameter to SystemBarName");
535 return false;
536 }
537 if (name.compare("status") == 0) {
538 systemBarProperties[WindowType::WINDOW_TYPE_STATUS_BAR].enable_ = true;
539 } else if (name.compare("navigation") == 0) {
540 systemBarProperties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].enable_ = true;
541 }
542 }
543 systemBarpropertyFlags[WindowType::WINDOW_TYPE_STATUS_BAR].enableFlag = true;
544 systemBarpropertyFlags[WindowType::WINDOW_TYPE_NAVIGATION_BAR].enableFlag = true;
545 return true;
546 }
547
GetColorFromJs(NativeEngine & engine,NativeObject * jsObject,const char * name,uint32_t defaultColor,bool & flag)548 static uint32_t GetColorFromJs(NativeEngine& engine, NativeObject* jsObject,
549 const char* name, uint32_t defaultColor, bool& flag)
550 {
551 NativeValue* jsColor = jsObject->GetProperty(name);
552 if (jsColor->TypeOf() != NATIVE_UNDEFINED) {
553 std::string colorStr;
554 if (!ConvertFromJsValue(engine, jsColor, colorStr)) {
555 WLOGFE("Failed to convert parameter to color");
556 return defaultColor;
557 }
558 std::regex pattern("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$");
559 if (!std::regex_match(colorStr, pattern)) {
560 WLOGFE("Invalid color input");
561 return defaultColor;
562 }
563 std::string color = colorStr.substr(1);
564 if (color.length() == RGB_LENGTH) {
565 color = "FF" + color; // ARGB
566 }
567 flag = true;
568 std::stringstream ss;
569 uint32_t hexColor;
570 ss << std::hex << color;
571 ss >> hexColor;
572 WLOGI("Origin %{public}s, process %{public}s, final %{public}x",
573 colorStr.c_str(), color.c_str(), hexColor);
574 return hexColor;
575 }
576 return defaultColor;
577 }
578
SetSystemBarPropertiesFromJs(NativeEngine & engine,NativeObject * jsObject,std::map<WindowType,SystemBarProperty> & properties,std::map<WindowType,SystemBarPropertyFlag> & propertyFlags,sptr<Window> & window)579 bool SetSystemBarPropertiesFromJs(NativeEngine& engine, NativeObject* jsObject,
580 std::map<WindowType, SystemBarProperty>& properties, std::map<WindowType, SystemBarPropertyFlag>& propertyFlags,
581 sptr<Window>& window)
582 {
583 auto statusProperty = window->GetSystemBarPropertyByType(WindowType::WINDOW_TYPE_STATUS_BAR);
584 auto navProperty = window->GetSystemBarPropertyByType(WindowType::WINDOW_TYPE_NAVIGATION_BAR);
585 properties[WindowType::WINDOW_TYPE_STATUS_BAR] = statusProperty;
586 properties[WindowType::WINDOW_TYPE_NAVIGATION_BAR] = navProperty;
587 propertyFlags[WindowType::WINDOW_TYPE_STATUS_BAR] = SystemBarPropertyFlag();
588 propertyFlags[WindowType::WINDOW_TYPE_NAVIGATION_BAR] = SystemBarPropertyFlag();
589 properties[WindowType::WINDOW_TYPE_STATUS_BAR].backgroundColor_ = GetColorFromJs(engine, jsObject, "statusBarColor",
590 statusProperty.backgroundColor_, propertyFlags[WindowType::WINDOW_TYPE_STATUS_BAR].backgroundColorFlag);
591 properties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].backgroundColor_ = GetColorFromJs(engine,
592 jsObject, "navigationBarColor", navProperty.backgroundColor_,
593 propertyFlags[WindowType::WINDOW_TYPE_NAVIGATION_BAR].backgroundColorFlag);
594 NativeValue* jsStatusContentColor = jsObject->GetProperty("statusBarContentColor");
595 NativeValue* jsStatusIcon = jsObject->GetProperty("isStatusBarLightIcon");
596 if (jsStatusContentColor->TypeOf() != NATIVE_UNDEFINED) {
597 properties[WindowType::WINDOW_TYPE_STATUS_BAR].contentColor_ = GetColorFromJs(engine,
598 jsObject, "statusBarContentColor", statusProperty.contentColor_,
599 propertyFlags[WindowType::WINDOW_TYPE_STATUS_BAR].contentColorFlag);
600 } else if (jsStatusIcon->TypeOf() != NATIVE_UNDEFINED) {
601 bool isStatusBarLightIcon;
602 if (!ConvertFromJsValue(engine, jsStatusIcon, isStatusBarLightIcon)) {
603 return false;
604 }
605 if (isStatusBarLightIcon) {
606 properties[WindowType::WINDOW_TYPE_STATUS_BAR].contentColor_ = SYSTEM_COLOR_WHITE;
607 } else {
608 properties[WindowType::WINDOW_TYPE_STATUS_BAR].contentColor_ = SYSTEM_COLOR_BLACK;
609 }
610 propertyFlags[WindowType::WINDOW_TYPE_STATUS_BAR].contentColorFlag = true;
611 }
612 NativeValue* jsNavigationContentColor = jsObject->GetProperty("navigationBarContentColor");
613 NativeValue* jsNavigationIcon = jsObject->GetProperty("isNavigationBarLightIcon");
614 if (jsNavigationContentColor->TypeOf() != NATIVE_UNDEFINED) {
615 properties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].contentColor_ = GetColorFromJs(engine,
616 jsObject, "navigationBarContentColor", navProperty.contentColor_,
617 propertyFlags[WindowType::WINDOW_TYPE_NAVIGATION_BAR].contentColorFlag);
618 } else if (jsNavigationIcon->TypeOf() != NATIVE_UNDEFINED) {
619 bool isNavigationBarLightIcon;
620 if (!ConvertFromJsValue(engine, jsNavigationIcon, isNavigationBarLightIcon)) {
621 return false;
622 }
623 if (isNavigationBarLightIcon) {
624 properties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].contentColor_ = SYSTEM_COLOR_WHITE;
625 } else {
626 properties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].contentColor_ = SYSTEM_COLOR_BLACK;
627 }
628 propertyFlags[WindowType::WINDOW_TYPE_NAVIGATION_BAR].contentColorFlag = true;
629 }
630 return true;
631 }
632
ConvertAvoidAreaToJsValue(NativeEngine & engine,const AvoidArea & avoidArea,AvoidAreaType type)633 NativeValue* ConvertAvoidAreaToJsValue(NativeEngine& engine, const AvoidArea& avoidArea, AvoidAreaType type)
634 {
635 NativeValue* objValue = engine.CreateObject();
636 NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
637 if (object == nullptr) {
638 WLOGFE("Failed to convert avoidArea to jsObject");
639 return nullptr;
640 }
641 object->SetProperty("visible", CreateJsValue(engine, type == AvoidAreaType::TYPE_CUTOUT ? false : true));
642 object->SetProperty("leftRect", GetRectAndConvertToJsValue(engine, avoidArea.leftRect_));
643 object->SetProperty("topRect", GetRectAndConvertToJsValue(engine, avoidArea.topRect_));
644 object->SetProperty("rightRect", GetRectAndConvertToJsValue(engine, avoidArea.rightRect_));
645 object->SetProperty("bottomRect", GetRectAndConvertToJsValue(engine, avoidArea.bottomRect_));
646 return objValue;
647 }
648
CheckCallingPermission(std::string permission)649 bool CheckCallingPermission(std::string permission)
650 {
651 WLOGD("Permission: %{public}s", permission.c_str());
652 if (!permission.empty() &&
653 Security::AccessToken::AccessTokenKit::VerifyAccessToken(IPCSkeleton::GetCallingTokenID(), permission)
654 != AppExecFwk::Constants::PERMISSION_GRANTED) {
655 WLOGFE("Permission %{public}s is not granted", permission.c_str());
656 return false;
657 }
658 return true;
659 }
660
GetAPI7Ability(NativeEngine & engine,AppExecFwk::Ability * & ability)661 bool GetAPI7Ability(NativeEngine& engine, AppExecFwk::Ability* &ability)
662 {
663 napi_value global;
664 auto env = reinterpret_cast<napi_env>(&engine);
665 if (napi_get_global(env, &global) != napi_ok) {
666 WLOGI("Get global failed");
667 return false;
668 }
669 napi_value jsAbility;
670 napi_status status = napi_get_named_property(env, global, "ability", &jsAbility);
671 if (status != napi_ok || jsAbility == nullptr) {
672 WLOGI("Get ability property failed");
673 return false;
674 }
675
676 if (napi_get_value_external(env, jsAbility, reinterpret_cast<void **>(&ability)) != napi_ok) {
677 WLOGI("Get ability external failed");
678 return false;
679 }
680 if (ability == nullptr) {
681 return false;
682 } else {
683 WLOGI("Get ability");
684 }
685 return true;
686 }
687 } // namespace Rosen
688 } // namespace OHOS
689