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