1 /*
2 * Copyright (c) 2021-2025 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 #include "napi_common_start_options.h"
17
18 #include "ability_info.h"
19 #include "hilog_tag_wrapper.h"
20 #include "napi_common_util.h"
21 #include "napi_common_want.h"
22 #include "int_wrapper.h"
23 #include "js_window_animation_utils.h"
24 #include "process_options.h"
25 #include "start_window_option.h"
26 #ifdef START_WINDOW_OPTIONS_WITH_PIXELMAP
27 #include "pixel_map_napi.h"
28 #endif
29 #include "wm_animation_common.h"
30
31 namespace OHOS {
32 namespace AppExecFwk {
33 EXTERN_C_START
34
UnwrapProcessOptions(napi_env env,napi_value param,std::shared_ptr<AAFwk::ProcessOptions> & processOptions)35 bool UnwrapProcessOptions(napi_env env, napi_value param, std::shared_ptr<AAFwk::ProcessOptions> &processOptions)
36 {
37 if (!IsExistsByPropertyName(env, param, "processMode") &&
38 !IsExistsByPropertyName(env, param, "startupVisibility")) {
39 return true;
40 }
41 auto option = std::make_shared<AAFwk::ProcessOptions>();
42 int32_t processMode = 0;
43 if (!UnwrapInt32ByPropertyName(env, param, "processMode", processMode)) {
44 TAG_LOGE(AAFwkTag::JSNAPI, "Unwrap processMode failed");
45 return false;
46 }
47 option->processMode = AAFwk::ProcessOptions::ConvertInt32ToProcessMode(processMode);
48 if (option->processMode == AAFwk::ProcessMode::UNSPECIFIED) {
49 TAG_LOGE(AAFwkTag::JSNAPI, "Convert processMode failed");
50 return false;
51 }
52 int32_t startupVisibility = 0;
53 if (!UnwrapInt32ByPropertyName(env, param, "startupVisibility", startupVisibility)) {
54 TAG_LOGE(AAFwkTag::JSNAPI, "Unwrap startupVisibility failed");
55 return false;
56 }
57 option->startupVisibility = AAFwk::ProcessOptions::ConvertInt32ToStartupVisibility(startupVisibility);
58 if (option->startupVisibility == AAFwk::StartupVisibility::UNSPECIFIED) {
59 TAG_LOGE(AAFwkTag::JSNAPI, "Convert startupVisibility failed");
60 return false;
61 }
62 processOptions = option;
63 TAG_LOGI(AAFwkTag::JSNAPI, "processMode:%{public}d, startupVisibility:%{public}d",
64 static_cast<int32_t>(processOptions->processMode),
65 static_cast<int32_t>(processOptions->startupVisibility));
66 return true;
67 }
68
69 #ifdef START_WINDOW_OPTIONS_WITH_PIXELMAP
UnwrapPixelMapFromJS(napi_env env,napi_value param,std::shared_ptr<Media::PixelMap> & value)70 bool UnwrapPixelMapFromJS(napi_env env, napi_value param, std::shared_ptr<Media::PixelMap> &value)
71 {
72 auto pixelMap = OHOS::Media::PixelMapNapi::GetPixelMap(env, param);
73 if (!pixelMap) {
74 TAG_LOGE(AAFwkTag::JSNAPI, "Unwrap pixelMap failed");
75 return false;
76 }
77 value = pixelMap;
78 return true;
79 }
80
UnwrapPixelMapByPropertyName(napi_env env,napi_value jsObject,const char * propertyName,std::shared_ptr<Media::PixelMap> & value)81 bool UnwrapPixelMapByPropertyName(
82 napi_env env, napi_value jsObject, const char *propertyName, std::shared_ptr<Media::PixelMap> &value)
83 {
84 napi_value jsValue = GetPropertyValueByPropertyName(env, jsObject, propertyName, napi_object);
85 if (jsValue == nullptr) {
86 return false;
87 }
88
89 return UnwrapPixelMapFromJS(env, jsValue, value);
90 }
91 #endif
92
UnwrapStartWindowOption(napi_env env,napi_value param,std::shared_ptr<AAFwk::StartWindowOption> & startWindowOption)93 bool UnwrapStartWindowOption(napi_env env, napi_value param,
94 std::shared_ptr<AAFwk::StartWindowOption> &startWindowOption)
95 {
96 auto option = std::make_shared<AAFwk::StartWindowOption>();
97 std::string startWindowBackgroundColor;
98 if (IsExistsByPropertyName(env, param, "startWindowBackgroundColor")) {
99 if (!UnwrapStringByPropertyName(env, param, "startWindowBackgroundColor", startWindowBackgroundColor)) {
100 TAG_LOGE(AAFwkTag::JSNAPI, "Unwrap startWindowBackgroundColor failed");
101 return false;
102 }
103 option->startWindowBackgroundColor = startWindowBackgroundColor;
104 }
105 if (!startWindowBackgroundColor.empty()) {
106 option->hasStartWindow = true;
107 }
108
109 #ifdef START_WINDOW_OPTIONS_WITH_PIXELMAP
110 std::shared_ptr<Media::PixelMap> startWindowIcon = nullptr;
111 if (IsExistsByPropertyName(env, param, "startWindowIcon")) {
112 if (!UnwrapPixelMapByPropertyName(env, param, "startWindowIcon", startWindowIcon)) {
113 TAG_LOGE(AAFwkTag::JSNAPI, "Unwrap startWindowIcon failed");
114 return false;
115 }
116 option->startWindowIcon = startWindowIcon;
117 }
118 if (startWindowIcon != nullptr) {
119 option->hasStartWindow = true;
120 }
121 #endif
122 startWindowOption = option;
123 return true;
124 }
125
UnwrapStartOptionsWithProcessOption(napi_env env,napi_value param,AAFwk::StartOptions & startOptions)126 bool UnwrapStartOptionsWithProcessOption(napi_env env, napi_value param, AAFwk::StartOptions &startOptions)
127 {
128 UnwrapStartOptions(env, param, startOptions);
129 if (!UnwrapProcessOptions(env, param, startOptions.processOptions)) {
130 TAG_LOGE(AAFwkTag::JSNAPI, "Unwrap processOptions failed");
131 return false;
132 }
133 if (!UnwrapStartWindowOption(env, param, startOptions.startWindowOption)) {
134 TAG_LOGE(AAFwkTag::JSNAPI, "Unwrap startWindowOption failed");
135 return false;
136 }
137 return true;
138 }
139
UnwrapStartOptionsWindowOptions(napi_env env,napi_value param,AAFwk::StartOptions & startOptions)140 void UnwrapStartOptionsWindowOptions(napi_env env, napi_value param, AAFwk::StartOptions &startOptions)
141 {
142 int32_t windowLeft = 0;
143 if (UnwrapInt32ByPropertyName(env, param, "windowLeft", windowLeft)) {
144 startOptions.SetWindowLeft(windowLeft);
145 startOptions.windowLeftUsed_ = true;
146 }
147
148 int32_t windowTop = 0;
149 if (UnwrapInt32ByPropertyName(env, param, "windowTop", windowTop)) {
150 startOptions.SetWindowTop(windowTop);
151 startOptions.windowTopUsed_ = true;
152 }
153
154 int32_t windowWidth = 0;
155 if (UnwrapInt32ByPropertyName(env, param, "windowWidth", windowWidth)) {
156 startOptions.SetWindowWidth(windowWidth);
157 startOptions.windowWidthUsed_ = true;
158 }
159
160 int32_t windowHeight = 0;
161 if (UnwrapInt32ByPropertyName(env, param, "windowHeight", windowHeight)) {
162 startOptions.SetWindowHeight(windowHeight);
163 startOptions.windowHeightUsed_ = true;
164 }
165
166 int32_t minWindowWidth = 0;
167 if (UnwrapInt32ByPropertyName(env, param, "minWindowWidth", minWindowWidth)) {
168 startOptions.SetMinWindowWidth(minWindowWidth);
169 startOptions.minWindowWidthUsed_ = true;
170 }
171
172 int32_t minWindowHeight = 0;
173 if (UnwrapInt32ByPropertyName(env, param, "minWindowHeight", minWindowHeight)) {
174 startOptions.SetMinWindowHeight(minWindowHeight);
175 startOptions.minWindowHeightUsed_ = true;
176 }
177
178 int32_t maxWindowWidth = 0;
179 if (UnwrapInt32ByPropertyName(env, param, "maxWindowWidth", maxWindowWidth)) {
180 startOptions.SetMaxWindowWidth(maxWindowWidth);
181 startOptions.maxWindowWidthUsed_ = true;
182 }
183
184 int32_t maxWindowHeight = 0;
185 if (UnwrapInt32ByPropertyName(env, param, "maxWindowHeight", maxWindowHeight)) {
186 startOptions.SetMaxWindowHeight(maxWindowHeight);
187 startOptions.maxWindowHeightUsed_ = true;
188 }
189 }
190
UnwrapStartOptions(napi_env env,napi_value param,AAFwk::StartOptions & startOptions)191 bool UnwrapStartOptions(napi_env env, napi_value param, AAFwk::StartOptions &startOptions)
192 {
193 TAG_LOGI(AAFwkTag::JSNAPI, "called");
194
195 if (!IsTypeForNapiValue(env, param, napi_object)) {
196 TAG_LOGI(AAFwkTag::JSNAPI, "not napi_object");
197 return false;
198 }
199
200 int32_t windowMode = 0;
201 if (UnwrapInt32ByPropertyName(env, param, "windowMode", windowMode)) {
202 startOptions.SetWindowMode(windowMode);
203 }
204
205 int32_t displayId = 0;
206 if (UnwrapInt32ByPropertyName(env, param, "displayId", displayId)) {
207 TAG_LOGI(AAFwkTag::JSNAPI, "get displayId %{public}d ok", displayId);
208 startOptions.SetDisplayID(displayId);
209 }
210
211 bool withAnimation = true;
212 if (UnwrapBooleanByPropertyName(env, param, "withAnimation", withAnimation)) {
213 startOptions.SetWithAnimation(withAnimation);
214 }
215
216 UnwrapStartOptionsWindowOptions(env, param, startOptions);
217
218 bool windowFocused = true;
219 if (UnwrapBooleanByPropertyName(env, param, "windowFocused", windowFocused)) {
220 startOptions.SetWindowFocused(windowFocused);
221 }
222
223 bool hideStartWindow = false;
224 if (UnwrapBooleanByPropertyName(env, param, "hideStartWindow", hideStartWindow)) {
225 startOptions.SetHideStartWindow(hideStartWindow);
226 }
227
228 std::vector<int32_t> supportWindowModes;
229 if (UnwrapInt32ArrayByPropertyName(env, param, "supportWindowModes", supportWindowModes)) {
230 for (int32_t mode : supportWindowModes) {
231 startOptions.supportWindowModes_.emplace_back(AppExecFwk::SupportWindowMode(mode));
232 }
233 }
234
235 bool hasWindowCreateParams = false;
236 napi_has_named_property(env, param, "windowCreateParams", &hasWindowCreateParams);
237 if (hasWindowCreateParams) {
238 napi_value windowJsObject = nullptr;
239 napi_get_named_property(env, param, "windowCreateParams", &windowJsObject);
240 Rosen::WindowCreateParams windowCreateParams;
241 if (Rosen::ConvertWindowCreateParamsFromJsValue(env, windowJsObject, windowCreateParams)) {
242 startOptions.windowCreateParams_ = std::make_shared<Rosen::WindowCreateParams>(windowCreateParams);
243 }
244 }
245
246 return true;
247 }
248
UnwrapStartOptionsAndWant(napi_env env,napi_value param,AAFwk::StartOptions & startOptions,AAFwk::Want & want)249 bool UnwrapStartOptionsAndWant(napi_env env, napi_value param, AAFwk::StartOptions &startOptions, AAFwk::Want &want)
250 {
251 if (!IsTypeForNapiValue(env, param, napi_object)) {
252 TAG_LOGI(AAFwkTag::JSNAPI, "not napi_object");
253 return false;
254 }
255 napi_value jsValue = GetPropertyValueByPropertyName(env, param, "parameters", napi_object);
256 if (jsValue != nullptr) {
257 AAFwk::WantParams wantParams;
258 if (UnwrapWantParams(env, jsValue, wantParams)) {
259 want.SetParams(wantParams);
260 }
261 }
262 int32_t flags = 0;
263 if (UnwrapInt32ByPropertyName(env, param, "flags", flags)) {
264 want.SetFlags(flags);
265 }
266 return UnwrapStartOptions(env, param, startOptions);
267 }
268 EXTERN_C_END
269 } // namespace AppExecFwk
270 } // namespace OHOS
271