• 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 <map>
20 #include <string>
21 #include <vector>
22 
23 #include "call.h"
24 #include "napi/native_api.h"
25 #include "napi/native_common.h"
26 #include "napi/native_node_api.h"
27 #include "pixel_map.h"
28 #include "pixel_map_napi.h"
29 #include "wallpaper_common.h"
30 #include "wallpaper_event_listener.h"
31 #include "wallpaper_js_util.h"
32 #include "wallpaper_manager_common_info.h"
33 
34 #define BUFFER_LENGTH_MAX (128)
35 #define DEFAULT_STACK_ID (1)
36 #define DEFAULT_LAST_MEMORY_LEVEL (-1)
37 #define DEFAULT_WEIGHT (-1)
38 
39 #define MAX_MISSION_NUM (65535)
40 #define QUERY_RECENT_RUNNING_MISSION_INFO_TYPE (2)
41 #define BUSINESS_ERROR_CODE_OK 0
42 namespace OHOS {
43 namespace WallpaperNAPI {
44 
45 using namespace WallpaperMgrService;
46 
47 struct GetContextInfo : public Call::Context {
48     int32_t wallpaperType = 0;
49     std::vector<uint64_t> colors;
50     int32_t wallpaperId = 0;
51     std::string eventType = "";
52     std::string parameter = "";
53     bool result = false;
54     std::shared_ptr<OHOS::Media::PixelMap> pixelMap;
55     napi_status status = napi_generic_failure;
GetContextInfoGetContextInfo56     GetContextInfo() : Context(nullptr, nullptr){};
GetContextInfoGetContextInfo57     GetContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){};
58 
operatorGetContextInfo59     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
60     {
61         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
62         return Context::operator()(env, argc, argv, self);
63     }
operatorGetContextInfo64     napi_status operator()(napi_env env, napi_value *result) override
65     {
66         if (status != napi_ok) {
67             output_ = nullptr;
68             return status;
69         }
70         return Context::operator()(env, result);
71     }
72 };
73 
74 struct GetMinContextInfo : public Call::Context {
75     int32_t minHeight = 0;
76     int32_t minWidth = 0;
77     napi_status status = napi_generic_failure;
GetMinContextInfoGetMinContextInfo78     GetMinContextInfo() : Context(nullptr, nullptr){};
GetMinContextInfoGetMinContextInfo79     GetMinContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){};
80 
operatorGetMinContextInfo81     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
82     {
83         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
84         return Context::operator()(env, argc, argv, self);
85     }
operatorGetMinContextInfo86     napi_status operator()(napi_env env, napi_value *result) override
87     {
88         if (status != napi_ok) {
89             output_ = nullptr;
90             return status;
91         }
92         return Context::operator()(env, result);
93     }
94 };
95 
96 struct PermissionContextInfo : public Call::Context {
97     bool isChangePermitted = false;
98     bool isOperationAllowed = false;
99     napi_status status = napi_generic_failure;
PermissionContextInfoPermissionContextInfo100     PermissionContextInfo() : Context(nullptr, nullptr){};
PermissionContextInfoPermissionContextInfo101     PermissionContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){};
102 
operatorPermissionContextInfo103     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
104     {
105         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
106         return Context::operator()(env, argc, argv, self);
107     }
operatorPermissionContextInfo108     napi_status operator()(napi_env env, napi_value *result) override
109     {
110         if (status != napi_ok) {
111             output_ = nullptr;
112             return status;
113         }
114         return Context::operator()(env, result);
115     }
116 };
117 
118 struct SetContextInfo : public Call::Context {
119     int32_t wallpaperType = 0;
120     std::string uri = "";
121     std::shared_ptr<OHOS::Media::PixelMap> pixelMap;
122     napi_status status = napi_generic_failure;
123     bool isPixelEmp = false;
124     int32_t xOffset = 0;
125     int32_t yOffset = 0;
126     bool isSetOffset = false;
SetContextInfoSetContextInfo127     SetContextInfo() : Context(nullptr, nullptr){};
SetContextInfoSetContextInfo128     SetContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){};
129 
operatorSetContextInfo130     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
131     {
132         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
133         return Context::operator()(env, argc, argv, self);
134     }
operatorSetContextInfo135     napi_status operator()(napi_env env, napi_value *result) override
136     {
137         if (status != napi_ok) {
138             output_ = nullptr;
139             return status;
140         }
141         return Context::operator()(env, result);
142     }
143 };
144 
145 struct GetFileContextInfo : public Call::Context {
146     static constexpr int32_t INVALID_FD = -1;
147     int32_t wallpaperType = 0;
148     int32_t wallpaperFd = INVALID_FD;
149     napi_status status = napi_generic_failure;
GetFileContextInfoGetFileContextInfo150     GetFileContextInfo() : Context(nullptr, nullptr){};
GetFileContextInfoGetFileContextInfo151     GetFileContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){};
152 
operatorGetFileContextInfo153     napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
154     {
155         NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
156         return Context::operator()(env, argc, argv, self);
157     }
operatorGetFileContextInfo158     napi_status operator()(napi_env env, napi_value *result) override
159     {
160         if (status != napi_ok) {
161             output_ = nullptr;
162             return status;
163         }
164         return Context::operator()(env, result);
165     }
166 };
167 
168 class NapiWallpaperAbility : public WallpaperMgrService::WallpaperEventListener,
169                              public std::enable_shared_from_this<NapiWallpaperAbility> {
170 public:
171     NapiWallpaperAbility(napi_env env, napi_value callback);
172     virtual ~NapiWallpaperAbility();
173     void OnColorsChange(const std::vector<uint64_t> &color, int32_t wallpaperType) override;
174     void OnWallpaperChange(WallpaperType wallpaperType, WallpaperResourceType resourceType,
175         const std::string &uri) override;
176     static bool IsValidArgCount(size_t argc, size_t expectationSize);
177     static bool IsValidArgType(napi_env env, napi_value argValue, napi_valuetype expectationType);
178     static bool IsValidArgRange(napi_env env, napi_value argValue);
179     static bool CheckValidArgWallpaperType(napi_env env, size_t argc, napi_value argValue,
180         std::shared_ptr<Call::Context> ctx);
181     static void GetColorsInner(std::shared_ptr<GetContextInfo> context, const ApiInfo &apiInfo);
182     static void GetIdInner(std::shared_ptr<GetContextInfo> context);
183     static void GetFileInner(std::shared_ptr<GetFileContextInfo> context, const ApiInfo &apiInfo);
184     static void GetMinHeightInner(std::shared_ptr<GetMinContextInfo> context, const ApiInfo &apiInfo);
185     static void GetMinWidthInner(std::shared_ptr<GetMinContextInfo> context, const ApiInfo &apiInfo);
186     static void IsChangeAllowedInner(std::shared_ptr<PermissionContextInfo> context);
187     static void IsUserChangeAllowedInner(std::shared_ptr<PermissionContextInfo> context);
188     static void RestoreInner(std::shared_ptr<SetContextInfo> context, const ApiInfo &apiInfo);
189     static void SetImageInput(std::shared_ptr<SetContextInfo> context);
190     static void SetImageExec(std::shared_ptr<SetContextInfo> context, const ApiInfo &apiInfo);
191     static void GetImageInner(std::shared_ptr<GetContextInfo> context, const ApiInfo &apiInfo);
192     static void SetVideoInner(std::shared_ptr<SetContextInfo> context);
193     static void SendEventInner(std::shared_ptr<GetContextInfo> context);
194     static void SetCustomWallpaper(std::shared_ptr<SetContextInfo> context);
195 
196 private:
197     struct WallpaperChangedData {
WallpaperChangedDataWallpaperChangedData198         WallpaperChangedData(const std::shared_ptr<NapiWallpaperAbility> &listenerIn, const WallpaperType &type,
199             const WallpaperResourceType &resType, const std::string &uri)
200             : listener(listenerIn), wallpaperType(type), resourceType(resType), uri(uri)
201         {
202         }
203         const std::shared_ptr<NapiWallpaperAbility> listener = nullptr;
204         WallpaperType wallpaperType;
205         WallpaperResourceType resourceType;
206         std::string uri;
207     };
208 
209     struct EventDataWorker {
210         const std::shared_ptr<NapiWallpaperAbility> listener = nullptr;
211         const std::vector<uint64_t> color;
212         const int32_t wallpaperType;
EventDataWorkerEventDataWorker213         EventDataWorker(const std::shared_ptr<NapiWallpaperAbility> &listenerIn, const std::vector<uint64_t> &colorIn,
214             const int32_t wallpaperTypeIn)
215             : listener(listenerIn), color(colorIn), wallpaperType(wallpaperTypeIn)
216         {
217         }
218     };
219     napi_ref callback_ = nullptr;
220     napi_env env_;
221     uv_loop_s *loop_ = nullptr;
222 };
223 
224 napi_value NAPI_GetColors(napi_env env, napi_callback_info info);
225 napi_value NAPI_GetColorsSync(napi_env env, napi_callback_info info);
226 napi_value NAPI_GetId(napi_env env, napi_callback_info info);
227 napi_value NAPI_GetIdSync(napi_env env, napi_callback_info info);
228 napi_value NAPI_GetFile(napi_env env, napi_callback_info info);
229 napi_value NAPI_GetFileSync(napi_env env, napi_callback_info info);
230 napi_value NAPI_GetMinHeight(napi_env env, napi_callback_info info);
231 napi_value NAPI_GetMinHeightSync(napi_env env, napi_callback_info info);
232 napi_value NAPI_GetMinWidth(napi_env env, napi_callback_info info);
233 napi_value NAPI_GetMinWidthSync(napi_env env, napi_callback_info info);
234 napi_value NAPI_IsChangePermitted(napi_env env, napi_callback_info info);
235 napi_value NAPI_IsChangeAllowed(napi_env env, napi_callback_info info);
236 napi_value NAPI_IsOperationAllowed(napi_env env, napi_callback_info info);
237 napi_value NAPI_IsUserChangeAllowed(napi_env env, napi_callback_info info);
238 napi_value NAPI_Reset(napi_env env, napi_callback_info info);
239 napi_value NAPI_Restore(napi_env env, napi_callback_info info);
240 napi_value NAPI_SetWallpaper(napi_env env, napi_callback_info info);
241 napi_value NAPI_SetImage(napi_env env, napi_callback_info info);
242 napi_value NAPI_GetPixelMap(napi_env env, napi_callback_info info);
243 napi_value NAPI_GetImage(napi_env env, napi_callback_info info);
244 napi_value NAPI_On(napi_env env, napi_callback_info info);
245 napi_value NAPI_Off(napi_env env, napi_callback_info info);
246 napi_value NAPI_SetVideo(napi_env env, napi_callback_info info);
247 napi_value NAPI_SendEvent(napi_env env, napi_callback_info info);
248 napi_value NAPI_SetCustomWallpaper(napi_env env, napi_callback_info info);
249 } // namespace WallpaperNAPI
250 } // namespace OHOS
251 #endif //  NAPI_WALLPAPER_ABILITY_H