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 
16 #ifndef NAPI_WALLPAPER_ABILITY_H
17 #define NAPI_WALLPAPER_ABILITY_H
18 
19 #include <uv.h>
20 
21 #include <map>
22 #include <string>
23 #include <vector>
24 
25 #include "call.h"
26 #include "napi/native_api.h"
27 #include "napi/native_common.h"
28 #include "napi/native_node_api.h"
29 #include "pixel_map.h"
30 #include "pixel_map_napi.h"
31 #include "wallpaper_common.h"
32 #include "wallpaper_event_listener.h"
33 #include "wallpaper_js_util.h"
34 #include "wallpaper_manager_common_info.h"
35 
36 #define BUFFER_LENGTH_MAX (128)
37 #define DEFAULT_STACK_ID (1)
38 #define DEFAULT_LAST_MEMORY_LEVEL (-1)
39 #define DEFAULT_WEIGHT (-1)
40 
41 #define MAX_MISSION_NUM (65535)
42 #define QUERY_RECENT_RUNNING_MISSION_INFO_TYPE (2)
43 #define BUSINESS_ERROR_CODE_OK 0
44 namespace OHOS {
45 namespace WallpaperNAPI {
46 
47 using namespace WallpaperMgrService;
48 
49 struct GetContextInfo : public Call::Context {
50     int32_t wallpaperType = 0;
51     int32_t foldState = 0;
52     int32_t rotateState = 0;
53     std::vector<uint64_t> colors;
54     int32_t wallpaperId = 0;
55     std::string eventType = "";
56     std::string parameter = "";
57     bool result = false;
58     std::shared_ptr<OHOS::Media::PixelMap> pixelMap;
59     napi_status status = napi_generic_failure;
GetContextInfoOHOS::WallpaperNAPI::GetContextInfo60     GetContextInfo() : Context(nullptr, nullptr) {};
GetContextInfoOHOS::WallpaperNAPI::GetContextInfo61     GetContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
62 
63     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
64     {
65         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
66         return Context::operator()(env, argc, argv, self);
67     }
68     napi_status operator()(napi_env env, napi_value *result) override
69     {
70         if (status != napi_ok) {
71             output_ = nullptr;
72             return status;
73         }
74         return Context::operator()(env, result);
75     }
76 };
77 
78 struct GetMinContextInfo : public Call::Context {
79     int32_t minHeight = 0;
80     int32_t minWidth = 0;
81     napi_status status = napi_generic_failure;
GetMinContextInfoOHOS::WallpaperNAPI::GetMinContextInfo82     GetMinContextInfo() : Context(nullptr, nullptr) {};
GetMinContextInfoOHOS::WallpaperNAPI::GetMinContextInfo83     GetMinContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
84 
85     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
86     {
87         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
88         return Context::operator()(env, argc, argv, self);
89     }
90     napi_status operator()(napi_env env, napi_value *result) override
91     {
92         if (status != napi_ok) {
93             output_ = nullptr;
94             return status;
95         }
96         return Context::operator()(env, result);
97     }
98 };
99 
100 struct PermissionContextInfo : public Call::Context {
101     bool isChangePermitted = false;
102     bool isOperationAllowed = false;
103     napi_status status = napi_generic_failure;
PermissionContextInfoOHOS::WallpaperNAPI::PermissionContextInfo104     PermissionContextInfo() : Context(nullptr, nullptr) {};
PermissionContextInfoOHOS::WallpaperNAPI::PermissionContextInfo105     PermissionContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
106 
107     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
108     {
109         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
110         return Context::operator()(env, argc, argv, self);
111     }
112     napi_status operator()(napi_env env, napi_value *result) override
113     {
114         if (status != napi_ok) {
115             output_ = nullptr;
116             return status;
117         }
118         return Context::operator()(env, result);
119     }
120 };
121 
122 struct SetContextInfo : public Call::Context {
123     int32_t wallpaperType = 0;
124     std::string uri = "";
125     std::shared_ptr<OHOS::Media::PixelMap> pixelMap;
126     napi_status status = napi_generic_failure;
127     bool isPixelEmp = false;
128     int32_t xOffset = 0;
129     int32_t yOffset = 0;
130     bool isSetOffset = false;
131     std::vector<WallpaperInfo> wallpaperInfos;
SetContextInfoOHOS::WallpaperNAPI::SetContextInfo132     SetContextInfo() : Context(nullptr, nullptr) {};
SetContextInfoOHOS::WallpaperNAPI::SetContextInfo133     SetContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
134 
135     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
136     {
137         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
138         return Context::operator()(env, argc, argv, self);
139     }
140     napi_status operator()(napi_env env, napi_value *result) override
141     {
142         if (status != napi_ok) {
143             output_ = nullptr;
144             return status;
145         }
146         return Context::operator()(env, result);
147     }
148 };
149 
150 struct GetFileContextInfo : public Call::Context {
151     static constexpr int32_t INVALID_FD = -1;
152     int32_t wallpaperType = 0;
153     int32_t wallpaperFd = INVALID_FD;
154     napi_status status = napi_generic_failure;
GetFileContextInfoOHOS::WallpaperNAPI::GetFileContextInfo155     GetFileContextInfo() : Context(nullptr, nullptr) {};
GetFileContextInfoOHOS::WallpaperNAPI::GetFileContextInfo156     GetFileContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
157 
158     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
159     {
160         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
161         return Context::operator()(env, argc, argv, self);
162     }
163     napi_status operator()(napi_env env, napi_value *result) override
164     {
165         if (status != napi_ok) {
166             output_ = nullptr;
167             return status;
168         }
169         return Context::operator()(env, result);
170     }
171 };
172 
173 class NapiWallpaperAbility : public WallpaperMgrService::WallpaperEventListener,
174                              public std::enable_shared_from_this<NapiWallpaperAbility> {
175 public:
176     NapiWallpaperAbility(napi_env env, napi_value callback);
177     virtual ~NapiWallpaperAbility();
178     void OnColorsChange(const std::vector<uint64_t> &color, int32_t wallpaperType) override;
179     void OnWallpaperChange(
180         WallpaperType wallpaperType, WallpaperResourceType resourceType, const std::string &uri) override;
181     static bool IsValidArgCount(size_t argc, size_t expectationSize);
182     static bool IsValidArgType(napi_env env, napi_value argValue, napi_valuetype expectationType);
183     static bool IsValidArgRange(napi_env env, napi_value argValue);
184     static bool IsValidFoldStateRange(napi_env env, napi_value argValue);
185     static bool IsValidRotateStateRange(napi_env env, napi_value argValue);
186     static bool CheckValidArgWallpaperType(
187         napi_env env, size_t argc, napi_value argValue, std::shared_ptr<Call::Context> ctx);
188     static void GetColorsInner(std::shared_ptr<GetContextInfo> context, const ApiInfo &apiInfo);
189     static void GetIdInner(std::shared_ptr<GetContextInfo> context);
190     static void GetFileInner(std::shared_ptr<GetFileContextInfo> context, const ApiInfo &apiInfo);
191     static void GetMinHeightInner(std::shared_ptr<GetMinContextInfo> context, const ApiInfo &apiInfo);
192     static void GetMinWidthInner(std::shared_ptr<GetMinContextInfo> context, const ApiInfo &apiInfo);
193     static void IsChangeAllowedInner(std::shared_ptr<PermissionContextInfo> context);
194     static void IsUserChangeAllowedInner(std::shared_ptr<PermissionContextInfo> context);
195     static void RestoreInner(std::shared_ptr<SetContextInfo> context, const ApiInfo &apiInfo);
196     static void SetImageInput(std::shared_ptr<SetContextInfo> context);
197     static void SetImageExec(std::shared_ptr<SetContextInfo> context, const ApiInfo &apiInfo);
198     static void GetImageInner(std::shared_ptr<GetContextInfo> context, const ApiInfo &apiInfo);
199     static void GetCorrespondWallpaperInner(std::shared_ptr<GetContextInfo> context, const ApiInfo &apiInfo);
200     static void SetVideoInner(std::shared_ptr<SetContextInfo> context);
201     static void SendEventInner(std::shared_ptr<GetContextInfo> context);
202     static void SetCustomWallpaper(std::shared_ptr<SetContextInfo> context);
203     static void SetAllWallpapers(std::shared_ptr<SetContextInfo> context);
204 
205 private:
206     struct WallpaperChangedData {
WallpaperChangedDataOHOS::WallpaperNAPI::NapiWallpaperAbility::WallpaperChangedData207         WallpaperChangedData(const std::shared_ptr<NapiWallpaperAbility> &listenerIn, const WallpaperType &type,
208             const WallpaperResourceType &resType, const std::string &uri)
209             : listener(listenerIn), wallpaperType(type), resourceType(resType), uri(uri)
210         {
211         }
212         const std::shared_ptr<NapiWallpaperAbility> listener = nullptr;
213         WallpaperType wallpaperType;
214         WallpaperResourceType resourceType;
215         std::string uri;
216     };
217 
218     struct EventDataWorker {
219         const std::shared_ptr<NapiWallpaperAbility> listener = nullptr;
220         const std::vector<uint64_t> color;
221         const int32_t wallpaperType;
EventDataWorkerOHOS::WallpaperNAPI::NapiWallpaperAbility::EventDataWorker222         EventDataWorker(const std::shared_ptr<NapiWallpaperAbility> &listenerIn, const std::vector<uint64_t> &colorIn,
223             const int32_t wallpaperTypeIn)
224             : listener(listenerIn), color(colorIn), wallpaperType(wallpaperTypeIn)
225         {
226         }
227     };
228     static void OnWallpaperChangeLambdaFunction(uv_work_t *work, int status);
229     static void OnColorsChangeLambdaFunction(uv_work_t *work, int status);
230     napi_ref callback_ = nullptr;
231     napi_env env_;
232     uv_loop_s *loop_ = nullptr;
233 };
234 
235 napi_value NAPI_GetColors(napi_env env, napi_callback_info info);
236 napi_value NAPI_GetColorsSync(napi_env env, napi_callback_info info);
237 napi_value NAPI_GetId(napi_env env, napi_callback_info info);
238 napi_value NAPI_GetIdSync(napi_env env, napi_callback_info info);
239 napi_value NAPI_GetFile(napi_env env, napi_callback_info info);
240 napi_value NAPI_GetFileSync(napi_env env, napi_callback_info info);
241 napi_value NAPI_GetMinHeight(napi_env env, napi_callback_info info);
242 napi_value NAPI_GetMinHeightSync(napi_env env, napi_callback_info info);
243 napi_value NAPI_GetMinWidth(napi_env env, napi_callback_info info);
244 napi_value NAPI_GetMinWidthSync(napi_env env, napi_callback_info info);
245 napi_value NAPI_IsChangePermitted(napi_env env, napi_callback_info info);
246 napi_value NAPI_IsChangeAllowed(napi_env env, napi_callback_info info);
247 napi_value NAPI_IsOperationAllowed(napi_env env, napi_callback_info info);
248 napi_value NAPI_IsUserChangeAllowed(napi_env env, napi_callback_info info);
249 napi_value NAPI_Reset(napi_env env, napi_callback_info info);
250 napi_value NAPI_Restore(napi_env env, napi_callback_info info);
251 napi_value NAPI_SetWallpaper(napi_env env, napi_callback_info info);
252 napi_value NAPI_SetImage(napi_env env, napi_callback_info info);
253 napi_value NAPI_GetPixelMap(napi_env env, napi_callback_info info);
254 napi_value NAPI_GetImage(napi_env env, napi_callback_info info);
255 napi_value NAPI_GetCorrespondWallpaper(napi_env env, napi_callback_info info);
256 napi_value NAPI_On(napi_env env, napi_callback_info info);
257 napi_value NAPI_Off(napi_env env, napi_callback_info info);
258 napi_value NAPI_SetVideo(napi_env env, napi_callback_info info);
259 napi_value NAPI_SendEvent(napi_env env, napi_callback_info info);
260 napi_value NAPI_SetCustomWallpaper(napi_env env, napi_callback_info info);
261 napi_value NAPI_SetAllWallpapers(napi_env env, napi_callback_info info);
262 } // namespace WallpaperNAPI
263 } // namespace OHOS
264 #endif //  NAPI_WALLPAPER_ABILITY_H