• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     std::vector<uint64_t> colors;
52     int32_t wallpaperId = 0;
53     std::string eventType = "";
54     std::string parameter = "";
55     bool result = false;
56     std::shared_ptr<OHOS::Media::PixelMap> pixelMap;
57     napi_status status = napi_generic_failure;
GetContextInfoGetContextInfo58     GetContextInfo() : Context(nullptr, nullptr) {};
GetContextInfoGetContextInfo59     GetContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
60 
operatorGetContextInfo61     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
62     {
63         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
64         return Context::operator()(env, argc, argv, self);
65     }
operatorGetContextInfo66     napi_status operator()(napi_env env, napi_value *result) override
67     {
68         if (status != napi_ok) {
69             output_ = nullptr;
70             return status;
71         }
72         return Context::operator()(env, result);
73     }
74 };
75 
76 struct GetMinContextInfo : public Call::Context {
77     int32_t minHeight = 0;
78     int32_t minWidth = 0;
79     napi_status status = napi_generic_failure;
GetMinContextInfoGetMinContextInfo80     GetMinContextInfo() : Context(nullptr, nullptr) {};
GetMinContextInfoGetMinContextInfo81     GetMinContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
82 
operatorGetMinContextInfo83     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
84     {
85         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
86         return Context::operator()(env, argc, argv, self);
87     }
operatorGetMinContextInfo88     napi_status operator()(napi_env env, napi_value *result) override
89     {
90         if (status != napi_ok) {
91             output_ = nullptr;
92             return status;
93         }
94         return Context::operator()(env, result);
95     }
96 };
97 
98 struct PermissionContextInfo : public Call::Context {
99     bool isChangePermitted = false;
100     bool isOperationAllowed = false;
101     napi_status status = napi_generic_failure;
PermissionContextInfoPermissionContextInfo102     PermissionContextInfo() : Context(nullptr, nullptr) {};
PermissionContextInfoPermissionContextInfo103     PermissionContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
104 
operatorPermissionContextInfo105     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
106     {
107         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
108         return Context::operator()(env, argc, argv, self);
109     }
operatorPermissionContextInfo110     napi_status operator()(napi_env env, napi_value *result) override
111     {
112         if (status != napi_ok) {
113             output_ = nullptr;
114             return status;
115         }
116         return Context::operator()(env, result);
117     }
118 };
119 
120 struct SetContextInfo : public Call::Context {
121     int32_t wallpaperType = 0;
122     std::string uri = "";
123     std::shared_ptr<OHOS::Media::PixelMap> pixelMap;
124     napi_status status = napi_generic_failure;
125     bool isPixelEmp = false;
126     int32_t xOffset = 0;
127     int32_t yOffset = 0;
128     bool isSetOffset = false;
SetContextInfoSetContextInfo129     SetContextInfo() : Context(nullptr, nullptr) {};
SetContextInfoSetContextInfo130     SetContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
131 
operatorSetContextInfo132     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
133     {
134         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
135         return Context::operator()(env, argc, argv, self);
136     }
operatorSetContextInfo137     napi_status operator()(napi_env env, napi_value *result) override
138     {
139         if (status != napi_ok) {
140             output_ = nullptr;
141             return status;
142         }
143         return Context::operator()(env, result);
144     }
145 };
146 
147 struct GetFileContextInfo : public Call::Context {
148     static constexpr int32_t INVALID_FD = -1;
149     int32_t wallpaperType = 0;
150     int32_t wallpaperFd = INVALID_FD;
151     napi_status status = napi_generic_failure;
GetFileContextInfoGetFileContextInfo152     GetFileContextInfo() : Context(nullptr, nullptr) {};
GetFileContextInfoGetFileContextInfo153     GetFileContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
154 
operatorGetFileContextInfo155     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
156     {
157         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
158         return Context::operator()(env, argc, argv, self);
159     }
operatorGetFileContextInfo160     napi_status operator()(napi_env env, napi_value *result) override
161     {
162         if (status != napi_ok) {
163             output_ = nullptr;
164             return status;
165         }
166         return Context::operator()(env, result);
167     }
168 };
169 
170 class NapiWallpaperAbility : public WallpaperMgrService::WallpaperEventListener,
171                              public std::enable_shared_from_this<NapiWallpaperAbility> {
172 public:
173     NapiWallpaperAbility(napi_env env, napi_value callback);
174     virtual ~NapiWallpaperAbility();
175     void OnColorsChange(const std::vector<uint64_t> &color, int32_t wallpaperType) override;
176     void OnWallpaperChange(
177         WallpaperType wallpaperType, WallpaperResourceType resourceType, const std::string &uri) override;
178     static bool IsValidArgCount(size_t argc, size_t expectationSize);
179     static bool IsValidArgType(napi_env env, napi_value argValue, napi_valuetype expectationType);
180     static bool IsValidArgRange(napi_env env, napi_value argValue);
181     static bool CheckValidArgWallpaperType(
182         napi_env env, size_t argc, napi_value argValue, std::shared_ptr<Call::Context> ctx);
183     static void GetColorsInner(std::shared_ptr<GetContextInfo> context, const ApiInfo &apiInfo);
184     static void GetIdInner(std::shared_ptr<GetContextInfo> context);
185     static void GetFileInner(std::shared_ptr<GetFileContextInfo> context, const ApiInfo &apiInfo);
186     static void GetMinHeightInner(std::shared_ptr<GetMinContextInfo> context, const ApiInfo &apiInfo);
187     static void GetMinWidthInner(std::shared_ptr<GetMinContextInfo> context, const ApiInfo &apiInfo);
188     static void IsChangeAllowedInner(std::shared_ptr<PermissionContextInfo> context);
189     static void IsUserChangeAllowedInner(std::shared_ptr<PermissionContextInfo> context);
190     static void RestoreInner(std::shared_ptr<SetContextInfo> context, const ApiInfo &apiInfo);
191     static void SetImageInput(std::shared_ptr<SetContextInfo> context);
192     static void SetImageExec(std::shared_ptr<SetContextInfo> context, const ApiInfo &apiInfo);
193     static void GetImageInner(std::shared_ptr<GetContextInfo> context, const ApiInfo &apiInfo);
194     static void SetVideoInner(std::shared_ptr<SetContextInfo> context);
195     static void SendEventInner(std::shared_ptr<GetContextInfo> context);
196     static void SetCustomWallpaper(std::shared_ptr<SetContextInfo> context);
197 
198 private:
199     struct WallpaperChangedData {
WallpaperChangedDataWallpaperChangedData200         WallpaperChangedData(const std::shared_ptr<NapiWallpaperAbility> &listenerIn, const WallpaperType &type,
201             const WallpaperResourceType &resType, const std::string &uri)
202             : listener(listenerIn), wallpaperType(type), resourceType(resType), uri(uri)
203         {
204         }
205         const std::shared_ptr<NapiWallpaperAbility> listener = nullptr;
206         WallpaperType wallpaperType;
207         WallpaperResourceType resourceType;
208         std::string uri;
209     };
210 
211     struct EventDataWorker {
212         const std::shared_ptr<NapiWallpaperAbility> listener = nullptr;
213         const std::vector<uint64_t> color;
214         const int32_t wallpaperType;
EventDataWorkerEventDataWorker215         EventDataWorker(const std::shared_ptr<NapiWallpaperAbility> &listenerIn, const std::vector<uint64_t> &colorIn,
216             const int32_t wallpaperTypeIn)
217             : listener(listenerIn), color(colorIn), wallpaperType(wallpaperTypeIn)
218         {
219         }
220     };
221     static void OnWallpaperChangeLambdaFunction(uv_work_t *work, int status);
222     static void OnColorsChangeLambdaFunction(uv_work_t *work, int status);
223     napi_ref callback_ = nullptr;
224     napi_env env_;
225     uv_loop_s *loop_ = nullptr;
226 };
227 
228 napi_value NAPI_GetColors(napi_env env, napi_callback_info info);
229 napi_value NAPI_GetColorsSync(napi_env env, napi_callback_info info);
230 napi_value NAPI_GetId(napi_env env, napi_callback_info info);
231 napi_value NAPI_GetIdSync(napi_env env, napi_callback_info info);
232 napi_value NAPI_GetFile(napi_env env, napi_callback_info info);
233 napi_value NAPI_GetFileSync(napi_env env, napi_callback_info info);
234 napi_value NAPI_GetMinHeight(napi_env env, napi_callback_info info);
235 napi_value NAPI_GetMinHeightSync(napi_env env, napi_callback_info info);
236 napi_value NAPI_GetMinWidth(napi_env env, napi_callback_info info);
237 napi_value NAPI_GetMinWidthSync(napi_env env, napi_callback_info info);
238 napi_value NAPI_IsChangePermitted(napi_env env, napi_callback_info info);
239 napi_value NAPI_IsChangeAllowed(napi_env env, napi_callback_info info);
240 napi_value NAPI_IsOperationAllowed(napi_env env, napi_callback_info info);
241 napi_value NAPI_IsUserChangeAllowed(napi_env env, napi_callback_info info);
242 napi_value NAPI_Reset(napi_env env, napi_callback_info info);
243 napi_value NAPI_Restore(napi_env env, napi_callback_info info);
244 napi_value NAPI_SetWallpaper(napi_env env, napi_callback_info info);
245 napi_value NAPI_SetImage(napi_env env, napi_callback_info info);
246 napi_value NAPI_GetPixelMap(napi_env env, napi_callback_info info);
247 napi_value NAPI_GetImage(napi_env env, napi_callback_info info);
248 napi_value NAPI_On(napi_env env, napi_callback_info info);
249 napi_value NAPI_Off(napi_env env, napi_callback_info info);
250 napi_value NAPI_SetVideo(napi_env env, napi_callback_info info);
251 napi_value NAPI_SendEvent(napi_env env, napi_callback_info info);
252 napi_value NAPI_SetCustomWallpaper(napi_env env, napi_callback_info info);
253 } // namespace WallpaperNAPI
254 } // namespace OHOS
255 #endif //  NAPI_WALLPAPER_ABILITY_H