1 /*
2  * Copyright (c) 2022-2023 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 #include "js_form_binding_data.h"
16 
17 #include "fms_log_wrapper.h"
18 #include "form_provider_data.h"
19 #include "js_runtime_utils.h"
20 #include "napi_form_util.h"
21 
22 namespace OHOS {
23 namespace AbilityRuntime {
24 #define ARGS_MAX_COUNT 10
25 namespace {
26 class JsFormBindingData {
27 public:
28     JsFormBindingData() = default;
29     ~JsFormBindingData() = default;
30 
Finalizer(napi_env env,void * data,void * hint)31     static void Finalizer(napi_env env, void* data, void* hint)
32     {
33         HILOG_INFO("call");
34         std::unique_ptr<JsFormBindingData>(static_cast<JsFormBindingData*>(data));
35     }
36 
CreateFormBindingData(napi_env env,napi_callback_info info)37     static napi_value CreateFormBindingData(napi_env env, napi_callback_info info)
38     {
39         GET_CB_INFO_AND_CALL(env, info, JsFormBindingData, OnCreateFormBindingData);
40     }
41 private:
42     napi_value OnCreateFormBindingData(napi_env env, size_t argc, napi_value* argv);
43 };
44 
OnCreateFormBindingData(napi_env env,size_t argc,napi_value * argv)45 napi_value JsFormBindingData::OnCreateFormBindingData(napi_env env, size_t argc, napi_value* argv)
46 {
47     std::string formDataStr;
48     if (argc > 0) {
49         napi_value nativeValue = nullptr;
50         napi_valuetype type = napi_undefined;
51         napi_typeof(env, argv[0], &type);
52         if (type == napi_string) {
53             nativeValue = argv[0];
54         } else if (type == napi_object) {
55             napi_value globalValue = nullptr;
56             napi_get_global(env, &globalValue);
57             napi_value jsonValue;
58             napi_get_named_property(env, globalValue, "JSON", &jsonValue);
59 
60             napi_value stringifyValue = nullptr;
61             napi_get_named_property(env, jsonValue, "stringify", &stringifyValue);
62             napi_value funcArgv[1] = { argv[0] };
63             napi_value transValue = nullptr;
64             napi_call_function(env, jsonValue, stringifyValue, 1, funcArgv, &transValue);
65             nativeValue = transValue;
66         }
67         ConvertFromJsValue(env, nativeValue, formDataStr);
68     }
69     napi_value objValue = nullptr;
70     napi_create_object(env, &objValue);
71     napi_set_named_property(env, objValue, "data", CreateJsValue(env, formDataStr));
72     return objValue;
73 }
74 }
75 
JsFormBindingDataInit(napi_env env,napi_value exportObj)76 napi_value JsFormBindingDataInit(napi_env env, napi_value exportObj)
77 {
78     HILOG_INFO("call");
79 
80     auto formBindingData = std::make_unique<JsFormBindingData>();
81     napi_wrap(env, exportObj, formBindingData.release(), JsFormBindingData::Finalizer, nullptr, nullptr);
82 
83     const char *moduleName = "JsFormBindingData";
84     BindNativeFunction(env, exportObj, "createFormBindingData", moduleName, JsFormBindingData::CreateFormBindingData);
85 
86     HILOG_INFO("call");
87     return exportObj;
88 }
89 } // namespace AbilityRuntime
90 } // namespace OHOS
91