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 #include "js_form_info_util.h"
17
18 #include "hilog_wrapper.h"
19 #include "js_runtime_utils.h"
20 #include "napi_form_util.h"
21
22 namespace OHOS {
23 namespace AbilityRuntime {
24 EXTERN_C_START
25
ConvertFormInfoFilter(NativeEngine & engine,NativeValue * jsValue,AppExecFwk::FormInfoFilter & formInfoFilter)26 bool ConvertFormInfoFilter(NativeEngine &engine, NativeValue* jsValue, AppExecFwk::FormInfoFilter &formInfoFilter)
27 {
28 if (jsValue->TypeOf() != NATIVE_OBJECT) {
29 HILOG_ERROR("%{public}s, an object is expected, but an argument of different type is passed in.", __func__);
30 return false;
31 }
32
33 NativeObject* nativeObject = ConvertNativeValueTo<NativeObject>(jsValue);
34 if (nativeObject == nullptr) {
35 HILOG_ERROR("%{public}s called, nativeObject is nullptr.", __func__);
36 return false;
37 }
38 NativeValue* nativeDataValue = nativeObject->GetProperty("moduleName");
39 if (nativeDataValue == nullptr || (nativeDataValue->TypeOf() != NATIVE_UNDEFINED &&
40 !ConvertFromJsValue(engine, nativeDataValue, formInfoFilter.moduleName))) {
41 HILOG_ERROR("%{public}s called, convert nativeDataValue failed.", __func__);
42 return false;
43 }
44 HILOG_INFO("%{public}s called, module name is %{public}s.", __func__, formInfoFilter.moduleName.c_str());
45
46 return true;
47 }
48
CreateJsFormInfo(NativeEngine & engine,const AppExecFwk::FormInfo & formInfo)49 NativeValue* CreateJsFormInfo(NativeEngine &engine, const AppExecFwk::FormInfo &formInfo)
50 {
51 NativeValue* objValue = engine.CreateObject();
52 napi_value napiValue = reinterpret_cast<napi_value>(objValue);
53 ParseFormInfoIntoNapi(reinterpret_cast<napi_env>(&engine), formInfo, napiValue);
54 return objValue;
55 }
56
CreateJsFormInfoArray(NativeEngine & engine,const std::vector<AppExecFwk::FormInfo> & formInfos)57 NativeValue* CreateJsFormInfoArray(NativeEngine &engine, const std::vector<AppExecFwk::FormInfo> &formInfos)
58 {
59 NativeValue* arrayValue = engine.CreateArray(formInfos.size());
60 NativeArray* array = ConvertNativeValueTo<NativeArray>(arrayValue);
61 uint32_t index = 0;
62 for (const auto &formInfo : formInfos) {
63 array->SetElement(index++, CreateJsFormInfo(engine, formInfo));
64 }
65 return arrayValue;
66 }
67 } // namespace AppExecFwk
68 } // namespace OHOS
69 EXTERN_C_END