• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "form_binding_data.h"
16 
17 #include "fms_log_wrapper.h"
18 #include "form_provider_data.h"
19 #include "js_runtime_utils.h"
20 
21 namespace OHOS {
22 namespace AbilityRuntime {
23 #define ARGS_MAX_COUNT 10
24 namespace {
25 class FormBindingData {
26 public:
FormBindingData(const std::shared_ptr<AppExecFwk::FormProviderData> & formProviderData)27     explicit FormBindingData(const std::shared_ptr<AppExecFwk::FormProviderData>& formProviderData)
28         : formProviderData_(formProviderData) {}
29     ~FormBindingData() = 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<FormBindingData>(static_cast<FormBindingData*>(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, FormBindingData, OnCreateFormBindingData);
40     }
41 private:
42     napi_value OnCreateFormBindingData(napi_env env, size_t argc, napi_value* argv);
43     std::shared_ptr<AppExecFwk::FormProviderData> formProviderData_;
44 };
45 
OnCreateFormBindingData(napi_env env,size_t argc,napi_value * argv)46 napi_value FormBindingData::OnCreateFormBindingData(napi_env env, size_t argc, napi_value* argv)
47 {
48     HILOG_INFO("call");
49     std::string formDataStr;
50     if (argc > 0) {
51         napi_value nativeValue = nullptr;
52         napi_valuetype type = napi_undefined;
53         napi_typeof(env, argv[0], &type);
54         if (type == napi_string) {
55             HILOG_DEBUG("param type is string");
56             nativeValue = argv[0];
57         } else if (type == napi_object) {
58             HILOG_DEBUG("param type is object");
59             napi_value globalValue = nullptr;
60             napi_get_global(env, &globalValue);
61             napi_value jsonValue;
62             napi_get_named_property(env, globalValue, "JSON", &jsonValue);
63 
64             napi_value stringifyValue = nullptr;
65             napi_get_named_property(env, jsonValue, "stringify", &stringifyValue);
66             napi_value funcArgv[1] = { argv[0] };
67             napi_value transValue = nullptr;
68             napi_call_function(env, jsonValue, stringifyValue, 1, funcArgv, &transValue);
69             nativeValue = transValue;
70         } else {
71             HILOG_ERROR("param type not string or object");
72             return CreateJsUndefined(env);
73         }
74 
75         if (!ConvertFromJsValue(env, nativeValue, formDataStr)) {
76             HILOG_ERROR("Parse formDataStr failed");
77             return CreateJsUndefined(env);
78         }
79     }
80     napi_value objValue = nullptr;
81     napi_create_object(env, &objValue);
82     formProviderData_->SetDataString(formDataStr);
83     napi_set_named_property(env, objValue, "data", CreateJsValue(env, formDataStr));
84     HILOG_DEBUG("call:%{private}s", formDataStr.c_str());
85     return objValue;
86 }
87 }
88 
FormBindingDataInit(napi_env env,napi_value exportObj)89 napi_value FormBindingDataInit(napi_env env, napi_value exportObj)
90 {
91     HILOG_INFO("call");
92 
93     auto formProviderData = std::make_shared<AppExecFwk::FormProviderData>();
94     auto formBindingData = std::make_unique<FormBindingData>(formProviderData);
95     napi_wrap(env, exportObj, formBindingData.release(), FormBindingData::Finalizer, nullptr, nullptr);
96 
97     const char *moduleName = "FormBindingData";
98     BindNativeFunction(env, exportObj, "createFormBindingData", moduleName, FormBindingData::CreateFormBindingData);
99 
100     HILOG_INFO("end");
101     return exportObj;
102 }
103 } // namespace AbilityRuntime
104 } // namespace OHOS
105