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; GetContextInfoGetContextInfo60 GetContextInfo() : Context(nullptr, nullptr) {}; GetContextInfoGetContextInfo61 GetContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {}; 62 operatorGetContextInfo63 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 } operatorGetContextInfo68 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; GetMinContextInfoGetMinContextInfo82 GetMinContextInfo() : Context(nullptr, nullptr) {}; GetMinContextInfoGetMinContextInfo83 GetMinContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {}; 84 operatorGetMinContextInfo85 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 } operatorGetMinContextInfo90 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; PermissionContextInfoPermissionContextInfo104 PermissionContextInfo() : Context(nullptr, nullptr) {}; PermissionContextInfoPermissionContextInfo105 PermissionContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {}; 106 operatorPermissionContextInfo107 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 } operatorPermissionContextInfo112 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; SetContextInfoSetContextInfo132 SetContextInfo() : Context(nullptr, nullptr) {}; SetContextInfoSetContextInfo133 SetContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {}; 134 operatorSetContextInfo135 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 } operatorSetContextInfo140 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; GetFileContextInfoGetFileContextInfo155 GetFileContextInfo() : Context(nullptr, nullptr) {}; GetFileContextInfoGetFileContextInfo156 GetFileContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {}; 157 operatorGetFileContextInfo158 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 } operatorGetFileContextInfo163 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 IsValidWallpaperInfos(napi_env env, napi_value argValue); 187 static bool CheckValidArgWallpaperType( 188 napi_env env, size_t argc, napi_value argValue, std::shared_ptr<Call::Context> ctx); 189 static void GetColorsInner(std::shared_ptr<GetContextInfo> context, const ApiInfo &apiInfo); 190 static void GetIdInner(std::shared_ptr<GetContextInfo> context); 191 static void GetFileInner(std::shared_ptr<GetFileContextInfo> context, const ApiInfo &apiInfo); 192 static void GetMinHeightInner(std::shared_ptr<GetMinContextInfo> context, const ApiInfo &apiInfo); 193 static void GetMinWidthInner(std::shared_ptr<GetMinContextInfo> context, const ApiInfo &apiInfo); 194 static void IsChangeAllowedInner(std::shared_ptr<PermissionContextInfo> context); 195 static void IsUserChangeAllowedInner(std::shared_ptr<PermissionContextInfo> context); 196 static void RestoreInner(std::shared_ptr<SetContextInfo> context, const ApiInfo &apiInfo); 197 static void SetImageInput(std::shared_ptr<SetContextInfo> context); 198 static void SetImageExec(std::shared_ptr<SetContextInfo> context, const ApiInfo &apiInfo); 199 static void GetImageInner(std::shared_ptr<GetContextInfo> context, const ApiInfo &apiInfo); 200 static void GetCorrespondWallpaperInner(std::shared_ptr<GetContextInfo> context, const ApiInfo &apiInfo); 201 static void SetVideoInner(std::shared_ptr<SetContextInfo> context); 202 static void SendEventInner(std::shared_ptr<GetContextInfo> context); 203 static void SetCustomWallpaper(std::shared_ptr<SetContextInfo> context); 204 static void SetAllWallpapers(std::shared_ptr<SetContextInfo> context); 205 206 private: 207 struct WallpaperChangedData { WallpaperChangedDataWallpaperChangedData208 WallpaperChangedData(const std::shared_ptr<NapiWallpaperAbility> &listenerIn, const WallpaperType &type, 209 const WallpaperResourceType &resType, const std::string &uri) 210 : listener(listenerIn), wallpaperType(type), resourceType(resType), uri(uri) 211 { 212 } 213 const std::shared_ptr<NapiWallpaperAbility> listener = nullptr; 214 WallpaperType wallpaperType; 215 WallpaperResourceType resourceType; 216 std::string uri; 217 }; 218 219 struct EventDataWorker { 220 const std::shared_ptr<NapiWallpaperAbility> listener = nullptr; 221 const std::vector<uint64_t> color; 222 const int32_t wallpaperType; EventDataWorkerEventDataWorker223 EventDataWorker(const std::shared_ptr<NapiWallpaperAbility> &listenerIn, const std::vector<uint64_t> &colorIn, 224 const int32_t wallpaperTypeIn) 225 : listener(listenerIn), color(colorIn), wallpaperType(wallpaperTypeIn) 226 { 227 } 228 }; 229 static void OnWallpaperChangeLambdaFunction(uv_work_t *work, int status); 230 static void OnColorsChangeLambdaFunction(uv_work_t *work, int status); 231 napi_ref callback_ = nullptr; 232 napi_env env_; 233 uv_loop_s *loop_ = nullptr; 234 }; 235 236 napi_value NAPI_GetColors(napi_env env, napi_callback_info info); 237 napi_value NAPI_GetColorsSync(napi_env env, napi_callback_info info); 238 napi_value NAPI_GetId(napi_env env, napi_callback_info info); 239 napi_value NAPI_GetIdSync(napi_env env, napi_callback_info info); 240 napi_value NAPI_GetFile(napi_env env, napi_callback_info info); 241 napi_value NAPI_GetFileSync(napi_env env, napi_callback_info info); 242 napi_value NAPI_GetMinHeight(napi_env env, napi_callback_info info); 243 napi_value NAPI_GetMinHeightSync(napi_env env, napi_callback_info info); 244 napi_value NAPI_GetMinWidth(napi_env env, napi_callback_info info); 245 napi_value NAPI_GetMinWidthSync(napi_env env, napi_callback_info info); 246 napi_value NAPI_IsChangePermitted(napi_env env, napi_callback_info info); 247 napi_value NAPI_IsChangeAllowed(napi_env env, napi_callback_info info); 248 napi_value NAPI_IsOperationAllowed(napi_env env, napi_callback_info info); 249 napi_value NAPI_IsUserChangeAllowed(napi_env env, napi_callback_info info); 250 napi_value NAPI_Reset(napi_env env, napi_callback_info info); 251 napi_value NAPI_Restore(napi_env env, napi_callback_info info); 252 napi_value NAPI_SetWallpaper(napi_env env, napi_callback_info info); 253 napi_value NAPI_SetImage(napi_env env, napi_callback_info info); 254 napi_value NAPI_GetPixelMap(napi_env env, napi_callback_info info); 255 napi_value NAPI_GetImage(napi_env env, napi_callback_info info); 256 napi_value NAPI_GetCorrespondWallpaper(napi_env env, napi_callback_info info); 257 napi_value NAPI_On(napi_env env, napi_callback_info info); 258 napi_value NAPI_Off(napi_env env, napi_callback_info info); 259 napi_value NAPI_SetVideo(napi_env env, napi_callback_info info); 260 napi_value NAPI_SendEvent(napi_env env, napi_callback_info info); 261 napi_value NAPI_SetCustomWallpaper(napi_env env, napi_callback_info info); 262 napi_value NAPI_SetAllWallpapers(napi_env env, napi_callback_info info); 263 } // namespace WallpaperNAPI 264 } // namespace OHOS 265 #endif // NAPI_WALLPAPER_ABILITY_H