• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "start_options_impl.h"
17 
18 #include "hilog_tag_wrapper.h"
19 #ifdef START_WINDOW_OPTIONS_WITH_PIXELMAP
20 #include "pixelmap_native_impl.h"
21 #endif
22 #include "process_options.h"
23 #include "securec.h"
24 #include "start_window_option.h"
25 
26 constexpr int MAX_SUPPOPRT_WINDOW_MODES_SIZE = 10;
27 
AbilityRuntime_StartOptions()28 AbilityRuntime_StartOptions::AbilityRuntime_StartOptions()
29 {}
30 
GetInnerStartOptions()31 OHOS::AAFwk::StartOptions AbilityRuntime_StartOptions::GetInnerStartOptions()
32 {
33     return options;
34 }
35 
SetStartOptionsWindowMode(AbilityRuntime_WindowMode windowMode)36 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::SetStartOptionsWindowMode(AbilityRuntime_WindowMode windowMode)
37 {
38     if (windowMode < ABILITY_RUNTIME_WINDOW_MODE_UNDEFINED ||
39         windowMode > ABILITY_RUNTIME_WINDOW_MODE_FULL_SCREEN) {
40         TAG_LOGE(AAFwkTag::APPKIT, "windowMode=%{public}d is invalid", static_cast<int32_t>(windowMode));
41         return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
42     }
43     options.SetWindowMode(static_cast<int32_t>(windowMode));
44     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
45 }
46 
GetStartOptionsWindowMode(AbilityRuntime_WindowMode & windowMode)47 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::GetStartOptionsWindowMode(AbilityRuntime_WindowMode &windowMode)
48 {
49     windowMode = static_cast<AbilityRuntime_WindowMode>(options.GetWindowMode());
50     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
51 }
52 
SetStartOptionsDisplayId(int32_t displayId)53 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::SetStartOptionsDisplayId(int32_t displayId)
54 {
55     options.SetDisplayID(displayId);
56     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
57 }
58 
GetStartOptionsDisplayId(int32_t & displayId)59 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::GetStartOptionsDisplayId(int32_t &displayId)
60 {
61     displayId = options.GetDisplayID();
62     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
63 }
64 
SetStartOptionsWithAnimation(bool withAnimation)65 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::SetStartOptionsWithAnimation(bool withAnimation)
66 {
67     options.SetWithAnimation(withAnimation);
68     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
69 }
70 
GetStartOptionsWithAnimation(bool & withAnimation)71 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::GetStartOptionsWithAnimation(bool &withAnimation)
72 {
73     withAnimation = options.GetWithAnimation();
74     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
75 }
76 
SetStartOptionsWindowLeft(int32_t windowLeft)77 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::SetStartOptionsWindowLeft(int32_t windowLeft)
78 {
79     options.windowLeftUsed_ = true;
80     options.SetWindowLeft(windowLeft);
81     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
82 }
83 
GetStartOptionsWindowLeft(int32_t & windowLeft)84 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::GetStartOptionsWindowLeft(int32_t &windowLeft)
85 {
86     if (options.windowLeftUsed_) {
87         windowLeft = options.GetWindowLeft();
88     }
89     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
90 }
91 
SetStartOptionsWindowTop(int32_t windowTop)92 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::SetStartOptionsWindowTop(int32_t windowTop)
93 {
94     options.windowTopUsed_ = true;
95     options.SetWindowTop(windowTop);
96     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
97 }
98 
GetStartOptionsWindowTop(int32_t & windowTop)99 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::GetStartOptionsWindowTop(int32_t &windowTop)
100 {
101     if (options.windowTopUsed_) {
102         windowTop = options.GetWindowTop();
103     }
104     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
105 }
106 
SetStartOptionsWindowHeight(int32_t windowHeight)107 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::SetStartOptionsWindowHeight(int32_t windowHeight)
108 {
109     options.windowHeightUsed_ = true;
110     options.SetWindowHeight(windowHeight);
111     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
112 }
113 
GetStartOptionsWindowHeight(int32_t & windowHeight)114 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::GetStartOptionsWindowHeight(int32_t &windowHeight)
115 {
116     if (options.windowHeightUsed_) {
117         windowHeight = options.GetWindowHeight();
118     }
119     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
120 }
121 
SetStartOptionsWindowWidth(int32_t windowWidth)122 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::SetStartOptionsWindowWidth(int32_t windowWidth)
123 {
124     options.windowWidthUsed_ = true;
125     options.SetWindowWidth(windowWidth);
126     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
127 }
128 
GetStartOptionsWindowWidth(int32_t & windowWidth)129 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::GetStartOptionsWindowWidth(int32_t &windowWidth)
130 {
131     if (options.windowWidthUsed_) {
132         windowWidth = options.GetWindowWidth();
133     }
134     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
135 }
136 
SetStartOptionsStartVisibility(AbilityRuntime_StartVisibility startVisibility)137 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::SetStartOptionsStartVisibility(
138     AbilityRuntime_StartVisibility startVisibility)
139 {
140     if (startVisibility < ABILITY_RUNTIME_HIDE_UPON_START ||
141         startVisibility > ABILITY_RUNTIME_SHOW_UPON_START) {
142         TAG_LOGE(AAFwkTag::APPKIT, "startVisibility=%{public}d is invalid", static_cast<int32_t>(startVisibility));
143         return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
144     }
145     if (options.processOptions == nullptr) {
146         options.processOptions = std::make_shared<OHOS::AAFwk::ProcessOptions>();
147     }
148     options.processOptions->startupVisibility =
149         static_cast<OHOS::AAFwk::StartupVisibility>(startVisibility);
150     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
151 }
152 
GetStartOptionsStartVisibility(AbilityRuntime_StartVisibility & startVisibility)153 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::GetStartOptionsStartVisibility(
154     AbilityRuntime_StartVisibility &startVisibility)
155 {
156     if (options.processOptions == nullptr) {
157         TAG_LOGE(AAFwkTag::APPKIT, "null processOptions");
158         return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
159     }
160     startVisibility = static_cast<AbilityRuntime_StartVisibility>(options.processOptions->startupVisibility);
161     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
162 }
163 
SetStartOptionsStartWindowIcon(OH_PixelmapNative * startWindowIcon)164 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::SetStartOptionsStartWindowIcon(
165     OH_PixelmapNative *startWindowIcon)
166 {
167     if (startWindowIcon == nullptr) {
168         TAG_LOGE(AAFwkTag::APPKIT, "null startWindowIcon");
169         return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
170     }
171     if (options.startWindowOption == nullptr) {
172         options.startWindowOption = std::make_shared<OHOS::AAFwk::StartWindowOption>();
173     }
174 #ifdef START_WINDOW_OPTIONS_WITH_PIXELMAP
175     options.startWindowOption->startWindowIcon = startWindowIcon->GetInnerPixelmap();
176     options.startWindowOption->hasStartWindow =
177         (options.startWindowOption->startWindowIcon != nullptr);
178 #endif
179     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
180 }
181 
GetStartOptionsStartWindowIcon(OH_PixelmapNative ** startWindowIcon)182 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::GetStartOptionsStartWindowIcon(
183     OH_PixelmapNative **startWindowIcon)
184 {
185     if (options.startWindowOption == nullptr || startWindowIcon == nullptr) {
186         TAG_LOGE(AAFwkTag::APPKIT, "null startWindowOption or startWindowIcon");
187         return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
188     }
189     if (*startWindowIcon != nullptr) {
190         TAG_LOGE(AAFwkTag::APPKIT, "startWindowIcon is not null");
191         return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
192     }
193 #ifdef START_WINDOW_OPTIONS_WITH_PIXELMAP
194     if (options.startWindowOption->hasStartWindow) {
195         std::unique_ptr<OH_PixelmapNative> icon = std::make_unique<OH_PixelmapNative>(
196             options.startWindowOption->startWindowIcon);
197         *startWindowIcon = icon.release();
198     }
199 #endif
200     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
201 }
202 
SetStartOptionsStartWindowBackgroundColor(const char * startWindowBackgroundColor)203 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::SetStartOptionsStartWindowBackgroundColor(
204     const char* startWindowBackgroundColor)
205 {
206     if (startWindowBackgroundColor == nullptr) {
207         TAG_LOGE(AAFwkTag::APPKIT, "null startWindowBackgroundColor");
208         return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
209     }
210     if (options.startWindowOption == nullptr) {
211         options.startWindowOption = std::make_shared<OHOS::AAFwk::StartWindowOption>();
212     }
213     options.startWindowOption->startWindowBackgroundColor = startWindowBackgroundColor;
214     options.startWindowOption->hasStartWindow =
215         !options.startWindowOption->startWindowBackgroundColor.empty();
216     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
217 }
218 
GetStartOptionsStartWindowBackgroundColor(char ** startWindowBackgroundColor,size_t & size)219 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::GetStartOptionsStartWindowBackgroundColor(
220     char **startWindowBackgroundColor, size_t &size)
221 {
222     if (options.startWindowOption == nullptr || startWindowBackgroundColor == nullptr) {
223         TAG_LOGE(AAFwkTag::APPKIT, "null startWindowOption or startWindowBackgroundColor");
224         return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
225     }
226     if (*startWindowBackgroundColor != nullptr) {
227         TAG_LOGE(AAFwkTag::APPKIT, "startWindowBackgroundColor is not null");
228         return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
229     }
230     if (options.startWindowOption->hasStartWindow) {
231         size_t length = options.startWindowOption->startWindowBackgroundColor.size() + 1;
232         *startWindowBackgroundColor = static_cast<char*>(malloc(sizeof(char) * length));
233         if (*startWindowBackgroundColor == nullptr) {
234             TAG_LOGE(AAFwkTag::APPKIT, "startWindowBackgroundColor uninitialized");
235             return ABILITY_RUNTIME_ERROR_CODE_INTERNAL;
236         }
237         if (strcpy_s(*startWindowBackgroundColor, length,
238             options.startWindowOption->startWindowBackgroundColor.c_str()) != 0) {
239             TAG_LOGE(AAFwkTag::APPKIT, "strcpy_s failed");
240             free(*startWindowBackgroundColor);
241             return ABILITY_RUNTIME_ERROR_CODE_INTERNAL;
242         }
243         size = options.startWindowOption->startWindowBackgroundColor.size();
244     }
245     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
246 }
247 
SetStartOptionsSupportedWindowModes(AbilityRuntime_SupportedWindowMode * supportedWindowModes,size_t size)248 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::SetStartOptionsSupportedWindowModes(
249     AbilityRuntime_SupportedWindowMode* supportedWindowModes, size_t size)
250 {
251     if (supportedWindowModes == nullptr || size == 0 || size > MAX_SUPPOPRT_WINDOW_MODES_SIZE) {
252         TAG_LOGE(AAFwkTag::APPKIT, "null supportedWindowModes or size is invalid");
253         return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
254     }
255     for (size_t i = 0; i < size; ++i) {
256         if (supportedWindowModes[i] < ABILITY_RUNTIME_SUPPORTED_WINDOW_MODE_FULL_SCREEN ||
257             supportedWindowModes[i] > ABILITY_RUNTIME_SUPPORTED_WINDOW_MODE_FLOATING) {
258             TAG_LOGE(AAFwkTag::APPKIT, "invalild supportWindowMode:%{public}d", supportedWindowModes[i]);
259             return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
260         }
261         options.supportWindowModes_.push_back(
262             static_cast<OHOS::AppExecFwk::SupportWindowMode>(supportedWindowModes[i]));
263     }
264     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
265 }
266 
GetStartOptionsSupportedWindowModes(AbilityRuntime_SupportedWindowMode ** supportedWindowModes,size_t & size)267 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::GetStartOptionsSupportedWindowModes(
268     AbilityRuntime_SupportedWindowMode **supportedWindowModes, size_t &size)
269 {
270     if (supportedWindowModes == nullptr) {
271         TAG_LOGE(AAFwkTag::APPKIT, "null supportedWindowModes");
272         return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
273     }
274     if (*supportedWindowModes != nullptr) {
275         TAG_LOGE(AAFwkTag::APPKIT, "supportedWindowModes is not null");
276         return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
277     }
278     if (!options.supportWindowModes_.empty()) {
279         size = options.supportWindowModes_.size();
280         *supportedWindowModes = static_cast<AbilityRuntime_SupportedWindowMode*>(
281             malloc(sizeof(AbilityRuntime_SupportedWindowMode) * options.supportWindowModes_.size()));
282         if (*supportedWindowModes == nullptr) {
283             TAG_LOGE(AAFwkTag::APPKIT, "supportedWindowModes uninitialized");
284             return ABILITY_RUNTIME_ERROR_CODE_INTERNAL;
285         }
286         for (size_t i = 0; i < size; ++i) {
287             (*supportedWindowModes)[i] =
288                 static_cast<AbilityRuntime_SupportedWindowMode>(options.supportWindowModes_[i]);
289         }
290     }
291     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
292 }
293 
SetStartOptionsMinWindowWidth(int32_t minWindowWidth)294 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::SetStartOptionsMinWindowWidth(int32_t minWindowWidth)
295 {
296     options.minWindowWidthUsed_ = true;
297     options.SetMinWindowWidth(minWindowWidth);
298     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
299 }
300 
GetStartOptionsMinWindowWidth(int32_t & minWindowWidth)301 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::GetStartOptionsMinWindowWidth(int32_t &minWindowWidth)
302 {
303     if (options.minWindowWidthUsed_) {
304         minWindowWidth = options.GetMinWindowWidth();
305     }
306     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
307 }
308 
SetStartOptionsMaxWindowWidth(int32_t maxWindowWidth)309 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::SetStartOptionsMaxWindowWidth(int32_t maxWindowWidth)
310 {
311     options.maxWindowWidthUsed_ = true;
312     options.SetMaxWindowWidth(maxWindowWidth);
313     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
314 }
315 
GetStartOptionsMaxWindowWidth(int32_t & maxWindowWidth)316 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::GetStartOptionsMaxWindowWidth(int32_t &maxWindowWidth)
317 {
318     if (options.maxWindowWidthUsed_) {
319         maxWindowWidth = options.GetMaxWindowWidth();
320     }
321     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
322 }
323 
SetStartOptionsMinWindowHeight(int32_t minWindowHeight)324 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::SetStartOptionsMinWindowHeight(int32_t minWindowHeight)
325 {
326     options.minWindowHeightUsed_ = true;
327     options.SetMinWindowHeight(minWindowHeight);
328     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
329 }
330 
GetStartOptionsMinWindowHeight(int32_t & minWindowHeight)331 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::GetStartOptionsMinWindowHeight(int32_t &minWindowHeight)
332 {
333     if (options.minWindowHeightUsed_) {
334         minWindowHeight = options.GetMinWindowHeight();
335     }
336     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
337 }
338 
SetStartOptionsMaxWindowHeight(int32_t maxWindowHeight)339 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::SetStartOptionsMaxWindowHeight(int32_t maxWindowHeight)
340 {
341     options.maxWindowHeightUsed_ = true;
342     options.SetMaxWindowHeight(maxWindowHeight);
343     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
344 }
345 
GetStartOptionsMaxWindowHeight(int32_t & maxWindowHeight)346 AbilityRuntime_ErrorCode AbilityRuntime_StartOptions::GetStartOptionsMaxWindowHeight(int32_t &maxWindowHeight)
347 {
348     if (options.maxWindowHeightUsed_) {
349         maxWindowHeight = options.GetMaxWindowHeight();
350     }
351     return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
352 }