• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, "JsWindowUtils"};
29 }
30 
WindowTypeInit(NativeEngine * engine)31 NativeValue* WindowTypeInit(NativeEngine* engine)
32 {
33     WLOGFD("[NAPI]WindowTypeInit");
34 
35     if (engine == nullptr) {
36         WLOGFE("[NAPI]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("[NAPI]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("[NAPI]AvoidAreaTypeInit");
90 
91     if (engine == nullptr) {
92         WLOGFE("[NAPI]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("[NAPI]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("[NAPI]WindowModeInit");
116 
117     if (engine == nullptr) {
118         WLOGFE("[NAPI]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("[NAPI]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("[NAPI]ColorSpaceInit");
145 
146     if (engine == nullptr) {
147         WLOGFE("[NAPI]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("[NAPI]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("[NAPI]OrientationInit");
168 
169     if (engine == nullptr) {
170         WLOGFE("[NAPI]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("[NAPI]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("[NAPI]WindowStageEventTypeInit");
211 
212     if (engine == nullptr) {
213         WLOGFE("[NAPI]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("[NAPI]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 
WindowLayoutModeInit(NativeEngine * engine)235 NativeValue* WindowLayoutModeInit(NativeEngine* engine)
236 {
237     WLOGFD("[NAPI]WindowLayoutModeInit");
238     if (engine == nullptr) {
239         WLOGFE("[NAPI]Engine is nullptr");
240         return nullptr;
241     }
242 
243     NativeValue *objValue = engine->CreateObject();
244     NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
245     if (object == nullptr) {
246         WLOGFE("[NAPI]Failed to get object");
247         return nullptr;
248     }
249 
250     object->SetProperty("WINDOW_LAYOUT_MODE_CASCADE", CreateJsValue(*engine,
251         static_cast<int32_t>(WindowLayoutMode::CASCADE)));
252     object->SetProperty("WINDOW_LAYOUT_MODE_TILE", CreateJsValue(*engine,
253         static_cast<int32_t>(WindowLayoutMode::TILE)));
254     return objValue;
255 }
256 
BlurStyleInit(NativeEngine * engine)257 NativeValue* BlurStyleInit(NativeEngine* engine)
258 {
259     WLOGFI("[NAPI]BlurStyleInit");
260     if (engine == nullptr) {
261         WLOGFE("[NAPI]Engine is nullptr");
262         return nullptr;
263     }
264 
265     NativeValue *objValue = engine->CreateObject();
266     NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
267     if (object == nullptr) {
268         WLOGFE("[NAPI]Failed to get object");
269         return nullptr;
270     }
271 
272     object->SetProperty("OFF", CreateJsValue(*engine,
273         static_cast<int32_t>(WindowBlurStyle::WINDOW_BLUR_OFF)));
274     object->SetProperty("THIN", CreateJsValue(*engine,
275         static_cast<int32_t>(WindowBlurStyle::WINDOW_BLUR_THIN)));
276     object->SetProperty("REGULAR", CreateJsValue(*engine,
277         static_cast<int32_t>(WindowBlurStyle::WINDOW_BLUR_REGULAR)));
278     object->SetProperty("THICK", CreateJsValue(*engine,
279         static_cast<int32_t>(WindowBlurStyle::WINDOW_BLUR_THICK)));
280     return objValue;
281 }
282 
WindowErrorInit(NativeEngine * engine)283 NativeValue* WindowErrorInit(NativeEngine* engine)
284 {
285     WLOGFD("[NAPI]WindowErrorInit");
286     if (engine == nullptr) {
287         WLOGFE("[NAPI]Engine is nullptr");
288         return nullptr;
289     }
290 
291     NativeValue *objValue = engine->CreateObject();
292     NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
293     if (object == nullptr) {
294         WLOGFE("[NAPI]Failed to get object");
295         return nullptr;
296     }
297 
298     object->SetProperty("WM_DO_NOTHING", CreateJsValue(*engine,
299         static_cast<int32_t>(WMError::WM_DO_NOTHING)));
300     object->SetProperty("WM_ERROR_NO_MEM", CreateJsValue(*engine,
301         static_cast<int32_t>(WMError::WM_ERROR_NO_MEM)));
302     object->SetProperty("WM_ERROR_DESTROYED_OBJECT", CreateJsValue(*engine,
303         static_cast<int32_t>(WMError::WM_ERROR_DESTROYED_OBJECT)));
304     object->SetProperty("WM_ERROR_INVALID_WINDOW", CreateJsValue(*engine,
305         static_cast<int32_t>(WMError::WM_ERROR_INVALID_WINDOW)));
306     object->SetProperty("WM_ERROR_INVALID_WINDOW_MODE_OR_SIZE", CreateJsValue(*engine,
307         static_cast<int32_t>(WMError::WM_ERROR_INVALID_WINDOW_MODE_OR_SIZE)));
308     object->SetProperty("WM_ERROR_INVALID_OPERATION", CreateJsValue(*engine,
309         static_cast<int32_t>(WMError::WM_ERROR_INVALID_OPERATION)));
310     object->SetProperty("WM_ERROR_INVALID_PERMISSION", CreateJsValue(*engine,
311         static_cast<int32_t>(WMError::WM_ERROR_INVALID_PERMISSION)));
312     object->SetProperty("WM_ERROR_NO_REMOTE_ANIMATION", CreateJsValue(*engine,
313         static_cast<int32_t>(WMError::WM_ERROR_NO_REMOTE_ANIMATION)));
314     object->SetProperty("WM_ERROR_DEVICE_NOT_SUPPORT", CreateJsValue(*engine,
315         static_cast<int32_t>(WMError::WM_ERROR_DEVICE_NOT_SUPPORT)));
316     object->SetProperty("WM_ERROR_NULLPTR", CreateJsValue(*engine,
317         static_cast<int32_t>(WMError::WM_ERROR_NULLPTR)));
318     object->SetProperty("WM_ERROR_INVALID_TYPE", CreateJsValue(*engine,
319         static_cast<int32_t>(WMError::WM_ERROR_INVALID_TYPE)));
320     object->SetProperty("WM_ERROR_INVALID_PARAM", CreateJsValue(*engine,
321         static_cast<int32_t>(WMError::WM_ERROR_INVALID_PARAM)));
322     object->SetProperty("WM_ERROR_SAMGR", CreateJsValue(*engine,
323         static_cast<int32_t>(WMError::WM_ERROR_SAMGR)));
324     object->SetProperty("WM_ERROR_IPC_FAILED", CreateJsValue(*engine,
325         static_cast<int32_t>(WMError::WM_ERROR_IPC_FAILED)));
326     object->SetProperty("WM_ERROR_START_ABILITY_FAILED", CreateJsValue(*engine,
327         static_cast<int32_t>(WMError::WM_ERROR_START_ABILITY_FAILED)));
328     return objValue;
329 }
330 
WindowErrorCodeInit(NativeEngine * engine)331 NativeValue* WindowErrorCodeInit(NativeEngine* engine)
332 {
333     WLOGFD("[NAPI]WindowErrorCodeInit");
334     if (engine == nullptr) {
335         WLOGFE("[NAPI]Engine is nullptr");
336         return nullptr;
337     }
338 
339     NativeValue *objValue = engine->CreateObject();
340     NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
341     if (object == nullptr) {
342         WLOGFE("[NAPI]Failed to get object");
343         return nullptr;
344     }
345     object->SetProperty("WM_ERROR_NO_PERMISSION", CreateJsValue(*engine,
346         static_cast<int32_t>(WmErrorCode::WM_ERROR_NO_PERMISSION)));
347     object->SetProperty("WM_ERROR_INVALID_PARAM", CreateJsValue(*engine,
348         static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM)));
349     object->SetProperty("WM_ERROR_DEVICE_NOT_SUPPORT", CreateJsValue(*engine,
350         static_cast<int32_t>(WmErrorCode::WM_ERROR_DEVICE_NOT_SUPPORT)));
351     object->SetProperty("WM_ERROR_REPEAT_OPERATION", CreateJsValue(*engine,
352         static_cast<int32_t>(WmErrorCode::WM_ERROR_REPEAT_OPERATION)));
353     object->SetProperty("WM_ERROR_STATE_ABNORMALLY", CreateJsValue(*engine,
354         static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY)));
355     object->SetProperty("WM_ERROR_SYSTEM_ABNORMALLY", CreateJsValue(*engine,
356         static_cast<int32_t>(WmErrorCode::WM_ERROR_SYSTEM_ABNORMALLY)));
357     object->SetProperty("WM_ERROR_INVALID_CALLING", CreateJsValue(*engine,
358         static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_CALLING)));
359     object->SetProperty("WM_ERROR_STAGE_ABNORMALLY", CreateJsValue(*engine,
360         static_cast<int32_t>(WmErrorCode::WM_ERROR_STAGE_ABNORMALLY)));
361     object->SetProperty("WM_ERROR_CONTEXT_ABNORMALLY", CreateJsValue(*engine,
362         static_cast<int32_t>(WmErrorCode::WM_ERROR_CONTEXT_ABNORMALLY)));
363     object->SetProperty("WM_ERROR_START_ABILITY_FAILED", CreateJsValue(*engine,
364         static_cast<int32_t>(WmErrorCode::WM_ERROR_START_ABILITY_FAILED)));
365     return objValue;
366 }
367 
GetRectAndConvertToJsValue(NativeEngine & engine,const Rect & rect)368 NativeValue* GetRectAndConvertToJsValue(NativeEngine& engine, const Rect& rect)
369 {
370     NativeValue* objValue = engine.CreateObject();
371     NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
372     if (object == nullptr) {
373         WLOGFE("[NAPI]Failed to convert rect to jsObject");
374         return nullptr;
375     }
376     object->SetProperty("left", CreateJsValue(engine, rect.posX_));
377     object->SetProperty("top", CreateJsValue(engine, rect.posY_));
378     object->SetProperty("width", CreateJsValue(engine, rect.width_));
379     object->SetProperty("height", CreateJsValue(engine, rect.height_));
380     return objValue;
381 }
382 
CreateJsWindowPropertiesObject(NativeEngine & engine,sptr<Window> & window)383 NativeValue* CreateJsWindowPropertiesObject(NativeEngine& engine, sptr<Window>& window)
384 {
385     WLOGFI("[NAPI]CreateJsWindowPropertiesObject");
386     NativeValue* objValue = engine.CreateObject();
387     NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
388     if (object == nullptr) {
389         WLOGFE("[NAPI]Failed to convert windowProperties to jsObject");
390         return nullptr;
391     }
392 
393     Rect rect = window->GetRect();
394     NativeValue* rectObj = GetRectAndConvertToJsValue(engine, rect);
395     if (rectObj == nullptr) {
396         WLOGFE("[NAPI]GetRect failed!");
397     }
398     object->SetProperty("windowRect", rectObj);
399     WindowType type = window->GetType();
400     if (NATIVE_JS_TO_WINDOW_TYPE_MAP.count(type) != 0) {
401         object->SetProperty("type", CreateJsValue(engine, NATIVE_JS_TO_WINDOW_TYPE_MAP.at(type)));
402     } else {
403         object->SetProperty("type", CreateJsValue(engine, type));
404     }
405     object->SetProperty("isLayoutFullScreen", CreateJsValue(engine, window->IsLayoutFullScreen()));
406     object->SetProperty("isFullScreen", CreateJsValue(engine, window->IsFullScreen()));
407     object->SetProperty("touchable", CreateJsValue(engine, window->GetTouchable()));
408     object->SetProperty("focusable", CreateJsValue(engine, window->GetFocusable()));
409     object->SetProperty("name", CreateJsValue(engine, window->GetWindowName()));
410     object->SetProperty("isPrivacyMode", CreateJsValue(engine, window->IsPrivacyMode()));
411     object->SetProperty("isKeepScreenOn", CreateJsValue(engine, window->IsKeepScreenOn()));
412     object->SetProperty("brightness", CreateJsValue(engine, window->GetBrightness()));
413     object->SetProperty("isTransparent", CreateJsValue(engine, window->IsTransparent()));
414     object->SetProperty("isRoundCorner", CreateJsValue(engine, false)); // empty method
415     object->SetProperty("dimBehindValue", CreateJsValue(engine, 0));
416     object->SetProperty("id", CreateJsValue(engine, window->GetWindowId()));
417     return objValue;
418 }
GetHexColor(uint32_t color)419 static std::string GetHexColor(uint32_t color)
420 {
421     std::stringstream ioss;
422     std::string temp;
423     ioss << std::setiosflags(std::ios::uppercase) << std::hex << color;
424     ioss >> temp;
425     int count = RGBA_LENGTH - static_cast<int>(temp.length());
426     std::string tmpColor(count, '0');
427     tmpColor += temp;
428     std::string finalColor("#");
429     finalColor += tmpColor;
430     return finalColor;
431 }
432 
CreateJsSystemBarRegionTintObject(NativeEngine & engine,const SystemBarRegionTint & tint)433 static NativeValue* CreateJsSystemBarRegionTintObject(NativeEngine& engine, const SystemBarRegionTint& tint)
434 {
435     NativeValue* objValue = engine.CreateObject();
436     NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
437     if (object == nullptr) {
438         WLOGFE("[NAPI]Failed to convert SystemBarProperty to jsObject");
439         return nullptr;
440     }
441     if (NATIVE_JS_TO_WINDOW_TYPE_MAP.count(tint.type_) != 0) {
442         object->SetProperty("type", CreateJsValue(engine, NATIVE_JS_TO_WINDOW_TYPE_MAP.at(tint.type_)));
443     } else {
444         object->SetProperty("type", CreateJsValue(engine, tint.type_));
445     }
446     object->SetProperty("isEnable", CreateJsValue(engine, tint.prop_.enable_));
447     std::string bkgColor = GetHexColor(tint.prop_.backgroundColor_);
448     object->SetProperty("backgroundColor", CreateJsValue(engine, bkgColor));
449     std::string contentColor = GetHexColor(tint.prop_.contentColor_);
450     object->SetProperty("contentColor", CreateJsValue(engine, contentColor));
451     Rect rect = tint.region_;
452     object->SetProperty("region", GetRectAndConvertToJsValue(engine, rect));
453 
454     WLOGFI("[NAPI]Type %{public}u [%{public}u %{public}s %{public}s]",
455         tint.type_, tint.prop_.enable_, bkgColor.c_str(), contentColor.c_str());
456     WLOGFI("[NAPI]Region [%{public}d %{public}d %{public}u %{public}u]",
457         rect.posX_, rect.posY_, rect.width_, rect.height_);
458     return objValue;
459 }
460 
CreateJsSystemBarRegionTintArrayObject(NativeEngine & engine,const SystemBarRegionTints & tints)461 NativeValue* CreateJsSystemBarRegionTintArrayObject(NativeEngine& engine, const SystemBarRegionTints& tints)
462 {
463     if (tints.empty()) {
464         WLOGFE("[NAPI]Empty tints");
465         return nullptr;
466     }
467     NativeValue* objValue = engine.CreateArray(tints.size());
468     NativeArray* array = ConvertNativeValueTo<NativeArray>(objValue);
469     if (array == nullptr) {
470         WLOGFE("[NAPI]Failed to convert SystemBarPropertys to jsArrayObject");
471         return nullptr;
472     }
473     uint32_t index = 0;
474     for (size_t i = 0; i < tints.size(); i++) {
475         array->SetElement(index++, CreateJsSystemBarRegionTintObject(engine, tints[i]));
476     }
477     return objValue;
478 }
479 
GetSystemBarStatus(std::map<WindowType,SystemBarProperty> & systemBarProperties,NativeEngine & engine,NativeCallbackInfo & info,sptr<Window> & window)480 bool GetSystemBarStatus(std::map<WindowType, SystemBarProperty>& systemBarProperties,
481                         NativeEngine& engine, NativeCallbackInfo& info, sptr<Window>& window)
482 {
483     NativeArray* nativeArray = nullptr;
484     uint32_t size = 0;
485     if (info.argc > 0 && info.argv[0]->TypeOf() != NATIVE_FUNCTION) {
486         nativeArray = ConvertNativeValueTo<NativeArray>(info.argv[0]);
487         if (nativeArray == nullptr) {
488             WLOGFE("[NAPI]Failed to convert parameter to SystemBarArray");
489             return false;
490         }
491         size = nativeArray->GetLength();
492     }
493     auto statusProperty = window->GetSystemBarPropertyByType(WindowType::WINDOW_TYPE_STATUS_BAR);
494     auto navProperty = window->GetSystemBarPropertyByType(WindowType::WINDOW_TYPE_NAVIGATION_BAR);
495     statusProperty.enable_ = false;
496     navProperty.enable_ = false;
497     systemBarProperties[WindowType::WINDOW_TYPE_STATUS_BAR] = statusProperty;
498     systemBarProperties[WindowType::WINDOW_TYPE_NAVIGATION_BAR] = navProperty;
499     for (uint32_t i = 0; i < size; i++) {
500         std::string name;
501         if (!ConvertFromJsValue(engine, nativeArray->GetElement(i), name)) {
502             WLOGFE("[NAPI]Failed to convert parameter to SystemBarName");
503             return false;
504         }
505         if (name.compare("status") == 0) {
506             systemBarProperties[WindowType::WINDOW_TYPE_STATUS_BAR].enable_ = true;
507         } else if (name.compare("navigation") == 0) {
508             systemBarProperties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].enable_ = true;
509         }
510     }
511     return true;
512 }
513 
GetColorFromJs(NativeEngine & engine,NativeObject * jsObject,const char * name,uint32_t defaultColor)514 static uint32_t GetColorFromJs(NativeEngine& engine, NativeObject* jsObject,
515     const char* name, uint32_t defaultColor)
516 {
517     NativeValue* jsColor = jsObject->GetProperty(name);
518     if (jsColor->TypeOf() != NATIVE_UNDEFINED) {
519         std::string colorStr;
520         if (!ConvertFromJsValue(engine, jsColor, colorStr)) {
521             WLOGFE("[NAPI]Failed to convert parameter to color");
522             return defaultColor;
523         }
524         std::regex pattern("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$");
525         if (!std::regex_match(colorStr, pattern)) {
526             WLOGFE("[NAPI]Invalid color input");
527             return defaultColor;
528         }
529         std::string color = colorStr.substr(1);
530         if (color.length() == RGB_LENGTH) {
531             color = "FF" + color; // ARGB
532         }
533         std::stringstream ss;
534         uint32_t hexColor;
535         ss << std::hex << color;
536         ss >> hexColor;
537         WLOGFI("[NAPI]Origin %{public}s, process %{public}s, final %{public}x",
538             colorStr.c_str(), color.c_str(), hexColor);
539         return hexColor;
540     }
541     return defaultColor;
542 }
543 
SetSystemBarPropertiesFromJs(NativeEngine & engine,NativeObject * jsObject,std::map<WindowType,SystemBarProperty> & properties,sptr<Window> & window)544 bool SetSystemBarPropertiesFromJs(NativeEngine& engine, NativeObject* jsObject,
545     std::map<WindowType, SystemBarProperty>& properties, sptr<Window>& window)
546 {
547     auto statusProperty = window->GetSystemBarPropertyByType(WindowType::WINDOW_TYPE_STATUS_BAR);
548     auto navProperty = window->GetSystemBarPropertyByType(WindowType::WINDOW_TYPE_NAVIGATION_BAR);
549     properties[WindowType::WINDOW_TYPE_STATUS_BAR] = statusProperty;
550     properties[WindowType::WINDOW_TYPE_NAVIGATION_BAR] = navProperty;
551     properties[WindowType::WINDOW_TYPE_STATUS_BAR].backgroundColor_ = GetColorFromJs(engine,
552         jsObject, "statusBarColor", statusProperty.backgroundColor_);
553     properties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].backgroundColor_ = GetColorFromJs(engine,
554         jsObject, "navigationBarColor", navProperty.backgroundColor_);
555     NativeValue* jsStatusContentColor = jsObject->GetProperty("statusBarContentColor");
556     NativeValue* jsStatusIcon = jsObject->GetProperty("isStatusBarLightIcon");
557     if (jsStatusContentColor->TypeOf() != NATIVE_UNDEFINED) {
558         properties[WindowType::WINDOW_TYPE_STATUS_BAR].contentColor_ =  GetColorFromJs(engine,
559             jsObject, "statusBarContentColor", statusProperty.contentColor_);
560     } else if (jsStatusIcon->TypeOf() != NATIVE_UNDEFINED) {
561         bool isStatusBarLightIcon;
562         if (!ConvertFromJsValue(engine, jsStatusIcon, isStatusBarLightIcon)) {
563             WLOGFE("[NAPI]Failed to convert parameter to isStatusBarLightIcon");
564             return false;
565         }
566         if (isStatusBarLightIcon) {
567             properties[WindowType::WINDOW_TYPE_STATUS_BAR].contentColor_ = SYSTEM_COLOR_WHITE;
568         } else {
569             properties[WindowType::WINDOW_TYPE_STATUS_BAR].contentColor_ = SYSTEM_COLOR_BLACK;
570         }
571     }
572     NativeValue* jsNavigationContentColor = jsObject->GetProperty("navigationBarContentColor");
573     NativeValue* jsNavigationIcon = jsObject->GetProperty("isNavigationBarLightIcon");
574     if (jsNavigationContentColor->TypeOf() != NATIVE_UNDEFINED) {
575         properties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].contentColor_ = GetColorFromJs(engine,
576             jsObject, "navigationBarContentColor", navProperty.contentColor_);
577     } else if (jsNavigationIcon->TypeOf() != NATIVE_UNDEFINED) {
578         bool isNavigationBarLightIcon;
579         if (!ConvertFromJsValue(engine, jsNavigationIcon, isNavigationBarLightIcon)) {
580             WLOGFE("[NAPI]Failed to convert parameter to isNavigationBarLightIcon");
581             return false;
582         }
583         if (isNavigationBarLightIcon) {
584             properties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].contentColor_ = SYSTEM_COLOR_WHITE;
585         } else {
586             properties[WindowType::WINDOW_TYPE_NAVIGATION_BAR].contentColor_ = SYSTEM_COLOR_BLACK;
587         }
588     }
589     return true;
590 }
591 
ConvertAvoidAreaToJsValue(NativeEngine & engine,const AvoidArea & avoidArea,AvoidAreaType type)592 NativeValue* ConvertAvoidAreaToJsValue(NativeEngine& engine, const AvoidArea& avoidArea, AvoidAreaType type)
593 {
594     NativeValue* objValue = engine.CreateObject();
595     NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
596     if (object == nullptr) {
597         WLOGFE("[NAPI]Failed to convert avoidArea to jsObject");
598         return nullptr;
599     }
600     object->SetProperty("visible", CreateJsValue(engine, type == AvoidAreaType::TYPE_CUTOUT ? false : true));
601     object->SetProperty("leftRect", GetRectAndConvertToJsValue(engine, avoidArea.leftRect_));
602     object->SetProperty("topRect", GetRectAndConvertToJsValue(engine, avoidArea.topRect_));
603     object->SetProperty("rightRect", GetRectAndConvertToJsValue(engine, avoidArea.rightRect_));
604     object->SetProperty("bottomRect", GetRectAndConvertToJsValue(engine, avoidArea.bottomRect_));
605     return objValue;
606 }
607 
CheckCallingPermission(std::string permission)608 bool CheckCallingPermission(std::string permission)
609 {
610     WLOGFI("[NAPI]Permission: %{public}s", permission.c_str());
611     if (!permission.empty() &&
612         Security::AccessToken::AccessTokenKit::VerifyAccessToken(IPCSkeleton::GetCallingTokenID(), permission)
613         != AppExecFwk::Constants::PERMISSION_GRANTED) {
614         WLOGFE("[NAPI]Permission %{public}s is not granted", permission.c_str());
615         return false;
616     }
617     return true;
618 }
619 
GetAPI7Ability(NativeEngine & engine,AppExecFwk::Ability * & ability)620 bool GetAPI7Ability(NativeEngine& engine, AppExecFwk::Ability* &ability)
621 {
622     napi_value global;
623     auto env = reinterpret_cast<napi_env>(&engine);
624     if (napi_get_global(env, &global) != napi_ok) {
625         WLOGFI("[NAPI]Get global failed");
626         return false;
627     }
628     napi_value jsAbility;
629     napi_status status = napi_get_named_property(env, global, "ability", &jsAbility);
630     if (status != napi_ok || jsAbility == nullptr) {
631         WLOGFI("[NAPI]Get ability property failed");
632         return false;
633     }
634 
635     if (napi_get_value_external(env, jsAbility, reinterpret_cast<void **>(&ability)) != napi_ok) {
636         WLOGFI("[NAPI]Get ability external failed");
637         return false;
638     }
639     if (ability == nullptr) {
640         return false;
641     } else {
642         WLOGFI("[NAPI]Get ability");
643     }
644     return true;
645 }
646 } // namespace Rosen
647 } // namespace OHOS
648