1 /*
2 * Copyright (c) 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 #define LOG_TAG "WallpaperJSUtil"
16 #include "color.h"
17 #include "hilog_wrapper.h"
18 #include "wallpaper_js_util.h"
19
20 namespace OHOS::WallpaperNAPI {
21 using namespace WallpaperMgrService;
22 constexpr const uint32_t COLOR_MASK{ 0xFF };
23
Convert2String(napi_env env,napi_value jsString)24 std::string WallpaperJSUtil::Convert2String(napi_env env, napi_value jsString)
25 {
26 size_t maxLen = WallpaperJSUtil::MAX_LEN;
27 napi_status status = napi_get_value_string_utf8(env, jsString, NULL, 0, &maxLen);
28 if (status != napi_ok) {
29 GET_AND_THROW_LAST_ERROR((env));
30 maxLen = WallpaperJSUtil::MAX_LEN;
31 }
32 if (maxLen == 0) {
33 return std::string();
34 }
35 char *buf = new (std::nothrow) char[maxLen + 1]();
36 if (buf == nullptr) {
37 return std::string();
38 }
39 size_t len = 0;
40 status = napi_get_value_string_utf8(env, jsString, buf, maxLen + 1, &len);
41 if (status != napi_ok) {
42 GET_AND_THROW_LAST_ERROR((env));
43 }
44 buf[len] = 0;
45 std::string value(buf);
46 delete[] buf;
47 return value;
48 }
49
Convert2JSRgbaArray(napi_env env,const std::vector<uint64_t> & color)50 napi_value WallpaperJSUtil::Convert2JSRgbaArray(napi_env env, const std::vector<uint64_t> &color)
51 {
52 HILOG_DEBUG("Convert2JSRgbaArray in.");
53 napi_value result = nullptr;
54 napi_status status = napi_create_array_with_length(env, color.size(), &result);
55 if (status != napi_ok) {
56 HILOG_DEBUG("Convert2JSRgbaArray failed!");
57 return nullptr;
58 }
59 int32_t index = 0;
60 for (const auto it : color) {
61 HILOG_DEBUG("Convert2JSRgbaArray for.");
62 ColorManager::Color colors(it);
63 napi_value red = nullptr;
64 napi_value green = nullptr;
65 napi_value blue = nullptr;
66 napi_value alpha = nullptr;
67 napi_create_int32(env, static_cast<int32_t>(colors.r * COLOR_MASK), &red);
68 napi_create_int32(env, static_cast<int32_t>(colors.g * COLOR_MASK), &green);
69 napi_create_int32(env, static_cast<int32_t>(colors.b * COLOR_MASK), &blue);
70 napi_create_int32(env, static_cast<int32_t>(colors.a * COLOR_MASK), &alpha);
71 napi_value element = nullptr;
72 napi_create_object(env, &element);
73 napi_set_named_property(env, element, "red", red);
74 napi_set_named_property(env, element, "green", green);
75 napi_set_named_property(env, element, "blue", blue);
76 napi_set_named_property(env, element, "alpha", alpha);
77 napi_set_element(env, result, index++, element);
78 }
79 return result;
80 }
81
Convert2WallpaperInfo(napi_env env,napi_value jsWallpaper,WallpaperInfo & wallpaperInfo)82 napi_status WallpaperJSUtil::Convert2WallpaperInfo(napi_env env, napi_value jsWallpaper, WallpaperInfo &wallpaperInfo)
83 {
84 HILOG_DEBUG("Convert2WallpaperInfo in.");
85 napi_value value = nullptr;
86
87 NAPI_CALL_BASE(env, napi_get_named_property(env, jsWallpaper, "foldState", &value), napi_invalid_arg);
88 int32_t foldState;
89 NAPI_CALL_BASE(env, napi_get_value_int32(env, value, &foldState), napi_invalid_arg);
90 wallpaperInfo.foldState = static_cast<FoldState>(foldState);
91
92 NAPI_CALL_BASE(env, napi_get_named_property(env, jsWallpaper, "rotateState", &value), napi_invalid_arg);
93 int32_t rotateState;
94 NAPI_CALL_BASE(env, napi_get_value_int32(env, value, &rotateState), napi_invalid_arg);
95 wallpaperInfo.rotateState = static_cast<RotateState>(rotateState);
96
97 NAPI_CALL_BASE(env, napi_get_named_property(env, jsWallpaper, "source", &value), napi_invalid_arg);
98 wallpaperInfo.source = Convert2String(env, value);
99 return napi_ok;
100 }
101
Convert2WallpaperInfos(napi_env env,napi_value jsWallpapers,std::vector<WallpaperInfo> & wallpaperInfos)102 napi_status WallpaperJSUtil::Convert2WallpaperInfos(napi_env env, napi_value jsWallpapers,
103 std::vector<WallpaperInfo> &wallpaperInfos)
104 {
105 HILOG_DEBUG("Convert2WallpaperInfos in.");
106 uint32_t length = 0;
107 NAPI_CALL_BASE(env, napi_get_array_length(env, jsWallpapers, &length), napi_invalid_arg);
108 for (uint32_t i = 0; i < length; ++i) {
109 napi_value item = nullptr;
110 napi_status status = napi_get_element(env, jsWallpapers, i, &item);
111 NAPI_ASSERT_BASE(env, status == napi_ok && item != nullptr, "not a WallpaperInfo", napi_invalid_arg);
112 WallpaperInfo wallpaperInfo;
113 NAPI_CALL_BASE(env, Convert2WallpaperInfo(env, item, wallpaperInfo), napi_invalid_arg);
114 wallpaperInfos.push_back(std::move(wallpaperInfo));
115 }
116 return napi_ok;
117 }
118 } // namespace OHOS::WallpaperNAPI