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 "async_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_color_change_listener.h" 30 #include "wallpaper_js_util.h" 31 #include "wallpaper_manager_common_info.h" 32 #include "wallpaper_common.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 using namespace OHOS::WallpaperMgrService; 45 struct GetContextInfo : public AsyncCall::Context { 46 int wallpaperType = 0; 47 std::vector<uint64_t> colors; 48 int wallpaperId = 0; 49 std::shared_ptr<OHOS::Media::PixelMap> pixelMap; 50 napi_status status = napi_generic_failure; GetContextInfoGetContextInfo51 GetContextInfo() : Context(nullptr, nullptr){}; GetContextInfoGetContextInfo52 GetContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; 53 operatorGetContextInfo54 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 55 { 56 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 57 return Context::operator()(env, argc, argv, self); 58 } operatorGetContextInfo59 napi_status operator()(napi_env env, napi_value *result) override 60 { 61 if (status != napi_ok) { 62 output_ = nullptr; 63 return status; 64 } 65 return Context::operator()(env, result); 66 } 67 }; 68 69 struct GetMinContextInfo : public AsyncCall::Context { 70 int minHeight = 0; 71 int minWidth = 0; 72 napi_status status = napi_generic_failure; GetMinContextInfoGetMinContextInfo73 GetMinContextInfo() : Context(nullptr, nullptr){}; GetMinContextInfoGetMinContextInfo74 GetMinContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; 75 operatorGetMinContextInfo76 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 77 { 78 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 79 return Context::operator()(env, argc, argv, self); 80 } operatorGetMinContextInfo81 napi_status operator()(napi_env env, napi_value *result) override 82 { 83 if (status != napi_ok) { 84 output_ = nullptr; 85 return status; 86 } 87 return Context::operator()(env, result); 88 } 89 }; 90 91 struct PermissionContextInfo : public AsyncCall::Context { 92 bool isChangePermitted = false; 93 bool isOperationAllowed = false; 94 napi_status status = napi_generic_failure; PermissionContextInfoPermissionContextInfo95 PermissionContextInfo() : Context(nullptr, nullptr){}; PermissionContextInfoPermissionContextInfo96 PermissionContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; 97 operatorPermissionContextInfo98 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 99 { 100 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 101 return Context::operator()(env, argc, argv, self); 102 } operatorPermissionContextInfo103 napi_status operator()(napi_env env, napi_value *result) override 104 { 105 if (status != napi_ok) { 106 output_ = nullptr; 107 return status; 108 } 109 return Context::operator()(env, result); 110 } 111 }; 112 113 struct SetContextInfo : public AsyncCall::Context { 114 int wallpaperType = 0; 115 std::string url = ""; 116 std::shared_ptr<OHOS::Media::PixelMap> pixelMap; 117 napi_status status = napi_generic_failure; 118 bool isPixelEmp = false; SetContextInfoSetContextInfo119 SetContextInfo() : Context(nullptr, nullptr){}; SetContextInfoSetContextInfo120 SetContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; 121 operatorSetContextInfo122 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 123 { 124 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 125 return Context::operator()(env, argc, argv, self); 126 } operatorSetContextInfo127 napi_status operator()(napi_env env, napi_value *result) override 128 { 129 if (status != napi_ok) { 130 output_ = nullptr; 131 return status; 132 } 133 return Context::operator()(env, result); 134 } 135 }; 136 137 struct GetFileContextInfo : public AsyncCall::Context { 138 static constexpr int32_t INVALID_FD = -1; 139 int32_t wallpaperType = 0; 140 int32_t wallpaperFd = INVALID_FD; 141 napi_status status = napi_generic_failure; GetFileContextInfoGetFileContextInfo142 GetFileContextInfo() : Context(nullptr, nullptr){}; GetFileContextInfoGetFileContextInfo143 GetFileContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; 144 operatorGetFileContextInfo145 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 146 { 147 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 148 return Context::operator()(env, argc, argv, self); 149 } operatorGetFileContextInfo150 napi_status operator()(napi_env env, napi_value *result) override 151 { 152 if (status != napi_ok) { 153 output_ = nullptr; 154 return status; 155 } 156 return Context::operator()(env, result); 157 } 158 }; 159 160 class NapiWallpaperAbility : public WallpaperMgrService::WallpaperColorChangeListener, 161 public std::enable_shared_from_this<NapiWallpaperAbility> { 162 public: 163 NapiWallpaperAbility(napi_env env, napi_value callback); 164 virtual ~NapiWallpaperAbility(); 165 void OnColorsChange(const std::vector<uint64_t> &color, int wallpaperType) override; 166 static bool IsValidArgCount(size_t argc, size_t expectationSize); 167 static bool IsValidArgType(napi_env env, napi_value argValue, napi_valuetype expectationType); 168 static bool IsValidArgRange(napi_env env, napi_value argValue); 169 static bool CheckValidArgWallpaperType( 170 napi_env env, size_t argc, napi_value argValue, std::shared_ptr<AsyncCall::Context> ctx); 171 static void GetColorsInner(std::shared_ptr<GetContextInfo> context); 172 static void GetIdInner(std::shared_ptr<GetContextInfo> context); 173 static void GetFileInner(std::shared_ptr<GetFileContextInfo> context, const ApiInfo &apiInfo); 174 static void GetMinHeightInner(std::shared_ptr<GetMinContextInfo> context); 175 static void GetMinWidthInner(std::shared_ptr<GetMinContextInfo> context); 176 static void IsChangeAllowedInner(std::shared_ptr<PermissionContextInfo> context); 177 static void IsUserChangeAllowedInner(std::shared_ptr<PermissionContextInfo> context); 178 static void RestoreInner(std::shared_ptr<SetContextInfo> context, const ApiInfo &apiInfo); 179 static void SetImageInput(std::shared_ptr<SetContextInfo> context); 180 static void SetImageExec(std::shared_ptr<SetContextInfo> context, const ApiInfo &apiInfo); 181 static void GetImageInner(std::shared_ptr<GetContextInfo> context, const ApiInfo &apiInfo); 182 183 private: 184 struct EventDataWorker { 185 const std::shared_ptr<NapiWallpaperAbility> listener = nullptr; 186 const std::vector<uint64_t> color; 187 const int32_t wallpaperType; EventDataWorkerEventDataWorker188 EventDataWorker(const std::shared_ptr<NapiWallpaperAbility> &listenerIn, const std::vector<uint64_t> &colorIn, 189 const int32_t wallpaperTypeIn) 190 : listener(listenerIn), color(colorIn), wallpaperType(wallpaperTypeIn) 191 { 192 } 193 }; 194 napi_ref callback_ = nullptr; 195 napi_env env_; 196 uv_loop_s *loop_ = nullptr; 197 }; 198 199 napi_value NAPI_GetColors(napi_env env, napi_callback_info info); 200 napi_value NAPI_GetColorsSync(napi_env env, napi_callback_info info); 201 napi_value NAPI_GetId(napi_env env, napi_callback_info info); 202 napi_value NAPI_GetIdSync(napi_env env, napi_callback_info info); 203 napi_value NAPI_GetFile(napi_env env, napi_callback_info info); 204 napi_value NAPI_GetFileSync(napi_env env, napi_callback_info info); 205 napi_value NAPI_GetMinHeight(napi_env env, napi_callback_info info); 206 napi_value NAPI_GetMinHeightSync(napi_env env, napi_callback_info info); 207 napi_value NAPI_GetMinWidth(napi_env env, napi_callback_info info); 208 napi_value NAPI_GetMinWidthSync(napi_env env, napi_callback_info info); 209 napi_value NAPI_IsChangePermitted(napi_env env, napi_callback_info info); 210 napi_value NAPI_IsChangeAllowed(napi_env env, napi_callback_info info); 211 napi_value NAPI_IsOperationAllowed(napi_env env, napi_callback_info info); 212 napi_value NAPI_IsUserChangeAllowed(napi_env env, napi_callback_info info); 213 napi_value NAPI_Reset(napi_env env, napi_callback_info info); 214 napi_value NAPI_Restore(napi_env env, napi_callback_info info); 215 napi_value NAPI_SetWallpaper(napi_env env, napi_callback_info info); 216 napi_value NAPI_SetImage(napi_env env, napi_callback_info info); 217 napi_value NAPI_GetPixelMap(napi_env env, napi_callback_info info); 218 napi_value NAPI_GetImage(napi_env env, napi_callback_info info); 219 napi_value NAPI_On(napi_env env, napi_callback_info info); 220 napi_value NAPI_Off(napi_env env, napi_callback_info info); 221 } // namespace WallpaperNAPI 222 } // namespace OHOS 223 #endif // NAPI_WALLPAPER_ABILITY_H