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_common.h" 31 #include "wallpaper_js_util.h" 32 #include "wallpaper_manager_common_info.h" 33 #include "wallpaper_common.h" 34 35 #define BUFFER_LENGTH_MAX (128) 36 #define DEFAULT_STACK_ID (1) 37 #define DEFAULT_LAST_MEMORY_LEVEL (-1) 38 #define DEFAULT_WEIGHT (-1) 39 40 #define MAX_MISSION_NUM (65535) 41 #define QUERY_RECENT_RUNNING_MISSION_INFO_TYPE (2) 42 #define BUSINESS_ERROR_CODE_OK 0 43 namespace OHOS { 44 namespace WallpaperNAPI { 45 using namespace OHOS::WallpaperMgrService; 46 47 struct GetContextInfo : public AsyncCall::Context { 48 int wallpaperType = 0; 49 std::vector<uint64_t> colors; 50 int wallpaperId = 0; 51 std::shared_ptr<OHOS::Media::PixelMap> pixelMap; 52 napi_status status = napi_generic_failure; GetContextInfoGetContextInfo53 GetContextInfo() : Context(nullptr, nullptr){}; GetContextInfoGetContextInfo54 GetContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; 55 operatorGetContextInfo56 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 57 { 58 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 59 return Context::operator()(env, argc, argv, self); 60 } operatorGetContextInfo61 napi_status operator()(napi_env env, napi_value *result) override 62 { 63 if (status != napi_ok) { 64 output_ = nullptr; 65 return status; 66 } 67 return Context::operator()(env, result); 68 } 69 }; 70 71 struct GetMinContextInfo : public AsyncCall::Context { 72 int minHeight = 0; 73 int minWidth = 0; 74 napi_status status = napi_generic_failure; GetMinContextInfoGetMinContextInfo75 GetMinContextInfo() : Context(nullptr, nullptr){}; GetMinContextInfoGetMinContextInfo76 GetMinContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; 77 operatorGetMinContextInfo78 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 79 { 80 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 81 return Context::operator()(env, argc, argv, self); 82 } operatorGetMinContextInfo83 napi_status operator()(napi_env env, napi_value *result) override 84 { 85 if (status != napi_ok) { 86 output_ = nullptr; 87 return status; 88 } 89 return Context::operator()(env, result); 90 } 91 }; 92 93 struct PermissionContextInfo : public AsyncCall::Context { 94 bool isChangePermitted = false; 95 bool isOperationAllowed = false; 96 napi_status status = napi_generic_failure; PermissionContextInfoPermissionContextInfo97 PermissionContextInfo() : Context(nullptr, nullptr){}; PermissionContextInfoPermissionContextInfo98 PermissionContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; 99 operatorPermissionContextInfo100 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 101 { 102 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 103 return Context::operator()(env, argc, argv, self); 104 } operatorPermissionContextInfo105 napi_status operator()(napi_env env, napi_value *result) override 106 { 107 if (status != napi_ok) { 108 output_ = nullptr; 109 return status; 110 } 111 return Context::operator()(env, result); 112 } 113 }; 114 115 struct SetContextInfo : public AsyncCall::Context { 116 int wallpaperType = 0; 117 std::string url = ""; 118 std::shared_ptr<OHOS::Media::PixelMap> pixelMap; 119 napi_status status = napi_generic_failure; 120 bool isPixelEmp = false; SetContextInfoSetContextInfo121 SetContextInfo() : Context(nullptr, nullptr){}; SetContextInfoSetContextInfo122 SetContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; 123 operatorSetContextInfo124 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 125 { 126 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 127 return Context::operator()(env, argc, argv, self); 128 } operatorSetContextInfo129 napi_status operator()(napi_env env, napi_value *result) override 130 { 131 if (status != napi_ok) { 132 output_ = nullptr; 133 return status; 134 } 135 return Context::operator()(env, result); 136 } 137 }; 138 139 struct GetFileContextInfo : public AsyncCall::Context { 140 static constexpr int32_t INVALID_FD = -1; 141 int32_t wallpaperType = 0; 142 int32_t wallpaperFd = INVALID_FD; 143 napi_status status = napi_generic_failure; GetFileContextInfoGetFileContextInfo144 GetFileContextInfo() : Context(nullptr, nullptr){}; GetFileContextInfoGetFileContextInfo145 GetFileContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; 146 operatorGetFileContextInfo147 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 148 { 149 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 150 return Context::operator()(env, argc, argv, self); 151 } operatorGetFileContextInfo152 napi_status operator()(napi_env env, napi_value *result) override 153 { 154 if (status != napi_ok) { 155 output_ = nullptr; 156 return status; 157 } 158 return Context::operator()(env, result); 159 } 160 }; 161 162 class NapiWallpaperAbility : public WallpaperMgrService::WallpaperColorChangeListener, 163 public std::enable_shared_from_this<NapiWallpaperAbility> { 164 public: 165 NapiWallpaperAbility(napi_env env, napi_value callback); 166 virtual ~NapiWallpaperAbility(); 167 void OnColorsChange(const std::vector<uint64_t> &color, int wallpaperType) override; 168 static bool IsValidArgCount(size_t argc, size_t expectationSize); 169 static bool IsValidArgType(napi_env env, napi_value argValue, napi_valuetype expectationType); 170 static bool IsValidArgRange(napi_env env, napi_value argValue); 171 static bool CheckValidArgWallpaperType(napi_env env, size_t argc, napi_value argValue, 172 std::shared_ptr<AsyncCall::Context> ctx); 173 static void GetColorsInner(std::shared_ptr<GetContextInfo> context, const ApiInfo &apiInfo); 174 static void GetIdInner(std::shared_ptr<GetContextInfo> context); 175 static void GetFileInner(std::shared_ptr<GetFileContextInfo> context, const ApiInfo &apiInfo); 176 static void GetMinHeightInner(std::shared_ptr<GetMinContextInfo> context, const ApiInfo &apiInfo); 177 static void GetMinWidthInner(std::shared_ptr<GetMinContextInfo> context, const ApiInfo &apiInfo); 178 static void IsChangeAllowedInner(std::shared_ptr<PermissionContextInfo> context); 179 static void IsUserChangeAllowedInner(std::shared_ptr<PermissionContextInfo> context); 180 static void RestoreInner(std::shared_ptr<SetContextInfo> context, const ApiInfo &apiInfo); 181 static void SetImageInput(std::shared_ptr<SetContextInfo> context); 182 static void SetImageExec(std::shared_ptr<SetContextInfo> context, const ApiInfo &apiInfo); 183 static void GetImageInner(std::shared_ptr<GetContextInfo> context, const ApiInfo &apiInfo); 184 185 private: 186 struct EventDataWorker { 187 const std::shared_ptr<NapiWallpaperAbility> listener = nullptr; 188 const std::vector<uint64_t> color; 189 const int32_t wallpaperType; EventDataWorkerEventDataWorker190 EventDataWorker(const std::shared_ptr<NapiWallpaperAbility> &listenerIn, const std::vector<uint64_t> &colorIn, 191 const int32_t wallpaperTypeIn) 192 : listener(listenerIn), color(colorIn), wallpaperType(wallpaperTypeIn) 193 { 194 } 195 }; 196 napi_ref callback_ = nullptr; 197 napi_env env_; 198 uv_loop_s *loop_ = nullptr; 199 }; 200 201 napi_value NAPI_GetColors(napi_env env, napi_callback_info info); 202 napi_value NAPI_GetColorsSync(napi_env env, napi_callback_info info); 203 napi_value NAPI_GetId(napi_env env, napi_callback_info info); 204 napi_value NAPI_GetIdSync(napi_env env, napi_callback_info info); 205 napi_value NAPI_GetFile(napi_env env, napi_callback_info info); 206 napi_value NAPI_GetFileSync(napi_env env, napi_callback_info info); 207 napi_value NAPI_GetMinHeight(napi_env env, napi_callback_info info); 208 napi_value NAPI_GetMinHeightSync(napi_env env, napi_callback_info info); 209 napi_value NAPI_GetMinWidth(napi_env env, napi_callback_info info); 210 napi_value NAPI_GetMinWidthSync(napi_env env, napi_callback_info info); 211 napi_value NAPI_IsChangePermitted(napi_env env, napi_callback_info info); 212 napi_value NAPI_IsChangeAllowed(napi_env env, napi_callback_info info); 213 napi_value NAPI_IsOperationAllowed(napi_env env, napi_callback_info info); 214 napi_value NAPI_IsUserChangeAllowed(napi_env env, napi_callback_info info); 215 napi_value NAPI_Reset(napi_env env, napi_callback_info info); 216 napi_value NAPI_Restore(napi_env env, napi_callback_info info); 217 napi_value NAPI_SetWallpaper(napi_env env, napi_callback_info info); 218 napi_value NAPI_SetImage(napi_env env, napi_callback_info info); 219 napi_value NAPI_GetPixelMap(napi_env env, napi_callback_info info); 220 napi_value NAPI_GetImage(napi_env env, napi_callback_info info); 221 napi_value NAPI_On(napi_env env, napi_callback_info info); 222 napi_value NAPI_Off(napi_env env, napi_callback_info info); 223 } // namespace WallpaperNAPI 224 } // namespace OHOS 225 #endif // NAPI_WALLPAPER_ABILITY_H