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 33 #define BUFFER_LENGTH_MAX (128) 34 #define DEFAULT_STACK_ID (1) 35 #define DEFAULT_LAST_MEMORY_LEVEL (-1) 36 #define DEFAULT_WEIGHT (-1) 37 38 #define MAX_MISSION_NUM (65535) 39 #define QUERY_RECENT_RUNNING_MISSION_INFO_TYPE (2) 40 #define BUSINESS_ERROR_CODE_OK 0 41 namespace OHOS { 42 namespace WallpaperNAPI { 43 struct GetContextInfo : public AsyncCall::Context { 44 int wallpaperType; 45 std::vector<RgbaColor> colors; 46 int wallpaperId; 47 std::shared_ptr<OHOS::Media::PixelMap> pixelMap; 48 napi_status status = napi_generic_failure; GetContextInfoGetContextInfo49 GetContextInfo() : Context(nullptr, nullptr) { }; GetContextInfoGetContextInfo50 GetContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) { }; 51 operatorGetContextInfo52 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 53 { 54 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 55 return Context::operator()(env, argc, argv, self); 56 } operatorGetContextInfo57 napi_status operator()(napi_env env, napi_value *result) override 58 { 59 if (status != napi_ok) { 60 output_ = nullptr; 61 return status; 62 } 63 return Context::operator()(env, result); 64 } 65 }; 66 67 struct GetMinContextInfo : public AsyncCall::Context { 68 int minHeight; 69 int minWidth; 70 napi_status status = napi_generic_failure; GetMinContextInfoGetMinContextInfo71 GetMinContextInfo() : Context(nullptr, nullptr) { }; GetMinContextInfoGetMinContextInfo72 GetMinContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) { }; 73 operatorGetMinContextInfo74 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 75 { 76 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 77 return Context::operator()(env, argc, argv, self); 78 } operatorGetMinContextInfo79 napi_status operator()(napi_env env, napi_value *result) override 80 { 81 if (status != napi_ok) { 82 output_ = nullptr; 83 return status; 84 } 85 return Context::operator()(env, result); 86 } 87 }; 88 89 struct PermissionContextInfo : public AsyncCall::Context { 90 bool isChangePermitted; 91 bool isOperationAllowed; 92 napi_status status = napi_generic_failure; PermissionContextInfoPermissionContextInfo93 PermissionContextInfo() : Context(nullptr, nullptr) { }; PermissionContextInfoPermissionContextInfo94 PermissionContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) { }; 95 operatorPermissionContextInfo96 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 97 { 98 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 99 return Context::operator()(env, argc, argv, self); 100 } operatorPermissionContextInfo101 napi_status operator()(napi_env env, napi_value *result) override 102 { 103 if (status != napi_ok) { 104 output_ = nullptr; 105 return status; 106 } 107 return Context::operator()(env, result); 108 } 109 }; 110 111 struct SetContextInfo : public AsyncCall::Context { 112 int wallpaperType; 113 std::string url = ""; 114 std::unique_ptr<OHOS::Media::PixelMap> pixelMap; 115 napi_status status = napi_generic_failure; 116 bool isPixelEmp = false; SetContextInfoSetContextInfo117 SetContextInfo() : Context(nullptr, nullptr) { }; SetContextInfoSetContextInfo118 SetContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) { }; 119 operatorSetContextInfo120 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 121 { 122 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 123 return Context::operator()(env, argc, argv, self); 124 } operatorSetContextInfo125 napi_status operator()(napi_env env, napi_value *result) override 126 { 127 if (status != napi_ok) { 128 output_ = nullptr; 129 return status; 130 } 131 return Context::operator()(env, result); 132 } 133 }; 134 135 struct GetFileContextInfo : public AsyncCall::Context { 136 napi_status status = napi_generic_failure; GetFileContextInfoGetFileContextInfo137 GetFileContextInfo() : Context(nullptr, nullptr) { }; GetFileContextInfoGetFileContextInfo138 GetFileContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) { }; 139 operatorGetFileContextInfo140 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 141 { 142 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 143 return Context::operator()(env, argc, argv, self); 144 } operatorGetFileContextInfo145 napi_status operator()(napi_env env, napi_value *result) override 146 { 147 if (status != napi_ok) { 148 output_ = nullptr; 149 return status; 150 } 151 return Context::operator()(env, result); 152 } 153 }; 154 extern std::shared_ptr<WallpaperMgrService::WallpaperColorChangeListener> colorChangeListener_; 155 156 class NapiWallpaperAbility : public WallpaperMgrService::WallpaperColorChangeListener, 157 public std::enable_shared_from_this<NapiWallpaperAbility> { 158 public: 159 NapiWallpaperAbility(napi_env env, napi_value callback); 160 virtual ~NapiWallpaperAbility(); 161 void onColorsChange(std::vector<RgbaColor> color, int wallpaperType) override; 162 163 private: 164 struct EventDataWorker { 165 const std::shared_ptr<NapiWallpaperAbility> listener = nullptr; 166 const std::vector<RgbaColor> color; 167 const int wallpaperType; EventDataWorkerEventDataWorker168 EventDataWorker(const std::shared_ptr<NapiWallpaperAbility> &listenerIn, const std::vector<RgbaColor> &colorIn, 169 const int32_t wallpaperTypeIn) 170 : listener(listenerIn), color(colorIn), wallpaperType(wallpaperTypeIn) 171 { 172 } 173 }; 174 napi_ref callback_ = nullptr; 175 napi_env env_; 176 uv_loop_s *loop_ = nullptr; 177 }; 178 179 napi_value NAPI_GetColors(napi_env env, napi_callback_info info); 180 napi_value NAPI_GetId(napi_env env, napi_callback_info info); 181 napi_value NAPI_GetMinHeight(napi_env env, napi_callback_info info); 182 napi_value NAPI_GetMinWidth(napi_env env, napi_callback_info info); 183 napi_value NAPI_IsChangePermitted(napi_env env, napi_callback_info info); 184 napi_value NAPI_IsOperationAllowed(napi_env env, napi_callback_info info); 185 napi_value NAPI_Reset(napi_env env, napi_callback_info info); 186 napi_value NAPI_SetWallpaper(napi_env env, napi_callback_info info); 187 napi_value NAPI_GetPixelMap(napi_env env, napi_callback_info info); 188 napi_value NAPI_ScreenshotLiveWallpaper(napi_env env, napi_callback_info info); 189 napi_value NAPI_On(napi_env env, napi_callback_info info); 190 napi_value NAPI_Off(napi_env env, napi_callback_info info); 191 napi_value NAPI_GetFile(napi_env env, napi_callback_info info); 192 } 193 } 194 #endif // NAPI_WALLPAPER_ABILITY_H