1 /*
2  * Copyright (c) 2021-2024 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 
UnwrapStartOptions(napi_env env,napi_value param,AAFwk::StartOptions & startOptions)165 bool UnwrapStartOptions(napi_env env, napi_value param, AAFwk::StartOptions &startOptions)
166 {
167     TAG_LOGI(AAFwkTag::JSNAPI, "called");
168 
169     if (!IsTypeForNapiValue(env, param, napi_object)) {
170         TAG_LOGI(AAFwkTag::JSNAPI, "not napi_object");
171         return false;
172     }
173 
174     int32_t windowMode = 0;
175     if (UnwrapInt32ByPropertyName(env, param, "windowMode", windowMode)) {
176         startOptions.SetWindowMode(windowMode);
177     }
178 
179     int32_t displayId = 0;
180     if (UnwrapInt32ByPropertyName(env, param, "displayId", displayId)) {
181         TAG_LOGI(AAFwkTag::JSNAPI, "get display id ok displayId %{public}d", displayId);
182         startOptions.SetDisplayID(displayId);
183     }
184 
185     bool withAnimation = true;
186     if (UnwrapBooleanByPropertyName(env, param, "withAnimation", withAnimation)) {
187         startOptions.SetWithAnimation(withAnimation);
188     }
189 
190     UnwrapStartOptionsWindowOptions(env, param, startOptions);
191 
192     bool windowFocused = true;
193     if (UnwrapBooleanByPropertyName(env, param, "windowFocused", windowFocused)) {
194         startOptions.SetWindowFocused(windowFocused);
195     }
196 
197     std::vector<int32_t> supportWindowModes;
198     if (UnwrapInt32ArrayByPropertyName(env, param, "supportWindowModes", supportWindowModes)) {
199         for (int32_t mode : supportWindowModes) {
200             startOptions.supportWindowModes_.emplace_back(AppExecFwk::SupportWindowMode(mode));
201         }
202     }
203 
204     return true;
205 }
206 
UnwrapStartOptionsAndWant(napi_env env,napi_value param,AAFwk::StartOptions & startOptions,AAFwk::Want & want)207 bool UnwrapStartOptionsAndWant(napi_env env, napi_value param, AAFwk::StartOptions &startOptions, AAFwk::Want &want)
208 {
209     if (!IsTypeForNapiValue(env, param, napi_object)) {
210         TAG_LOGI(AAFwkTag::JSNAPI, "not napi_object");
211         return false;
212     }
213     napi_value jsValue = GetPropertyValueByPropertyName(env, param, "parameters", napi_object);
214     if (jsValue != nullptr) {
215         AAFwk::WantParams wantParams;
216         if (UnwrapWantParams(env, jsValue, wantParams)) {
217             want.SetParams(wantParams);
218         }
219     }
220     int32_t flags = 0;
221     if (UnwrapInt32ByPropertyName(env, param, "flags", flags)) {
222         want.SetFlags(flags);
223     }
224     return UnwrapStartOptions(env, param, startOptions);
225 }
226 EXTERN_C_END
227 }  // namespace AppExecFwk
228 }  // namespace OHOS
229