• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #include "form_binding_data.h"
16 
17 #include "form_provider_data.h"
18 #include "hilog_wrapper.h"
19 #include "js_runtime_utils.h"
20 
21 namespace OHOS {
22 namespace AbilityRuntime {
23 namespace {
24 class FormBindingData {
25 public:
FormBindingData(const std::shared_ptr<AppExecFwk::FormProviderData> & formProviderData)26     explicit FormBindingData(const std::shared_ptr<AppExecFwk::FormProviderData>& formProviderData)
27         : formProviderData_(formProviderData) {}
28     ~FormBindingData() = default;
29 
Finalizer(NativeEngine * engine,void * data,void * hint)30     static void Finalizer(NativeEngine* engine, void* data, void* hint)
31     {
32         HILOG_INFO("FormBindingData::Finalizer is called");
33         std::unique_ptr<FormBindingData>(static_cast<FormBindingData*>(data));
34     }
35 
CreateFormBindingData(NativeEngine * engine,NativeCallbackInfo * info)36     static NativeValue* CreateFormBindingData(NativeEngine* engine, NativeCallbackInfo* info)
37     {
38         FormBindingData* me = CheckParamsAndGetThis<FormBindingData>(engine, info);
39         return (me != nullptr) ? me->OnCreateFormBindingData(*engine, *info) : nullptr;
40     }
41 private:
42     NativeValue* OnCreateFormBindingData(NativeEngine& engine, NativeCallbackInfo& info);
43     std::shared_ptr<AppExecFwk::FormProviderData> formProviderData_;
44 };
45 
OnCreateFormBindingData(NativeEngine & engine,NativeCallbackInfo & info)46 NativeValue* FormBindingData::OnCreateFormBindingData(NativeEngine& engine, NativeCallbackInfo& info)
47 {
48     HILOG_INFO("%{public}s called.", __func__);
49     std::string formDataStr;
50     if (info.argc > 0) {
51         NativeValue* nativeValue = nullptr;
52         if ((info.argv[0])->TypeOf() == NATIVE_STRING) {
53             HILOG_DEBUG("%{public}s called, param type is string.", __func__);
54             nativeValue = info.argv[0];
55         } else if ((info.argv[0])->TypeOf() == NATIVE_OBJECT) {
56             HILOG_DEBUG("%{public}s called, param type is object.", __func__);
57             napi_env napiEnv = reinterpret_cast<napi_env>(&engine);
58             napi_value globalValue = nullptr;
59             napi_get_global(napiEnv, &globalValue);
60             napi_value jsonValue;
61             napi_get_named_property(napiEnv, globalValue, "JSON", &jsonValue);
62 
63             napi_value stringifyValue = nullptr;
64             napi_get_named_property(napiEnv, jsonValue, "stringify", &stringifyValue);
65             napi_value funcArgv[1] = { reinterpret_cast<napi_value>(info.argv[0]) };
66             napi_value transValue = nullptr;
67             napi_call_function(napiEnv, jsonValue, stringifyValue, 1, funcArgv, &transValue);
68             nativeValue = reinterpret_cast<NativeValue*>(transValue);
69         } else {
70             HILOG_ERROR("%{public}s, param type not string or object", __func__);
71             return engine.CreateUndefined();
72         }
73 
74         if (!ConvertFromJsValue(engine, nativeValue, formDataStr)) {
75             HILOG_ERROR("%{public}s, Parse formDataStr failed", __func__);
76             return engine.CreateUndefined();
77         }
78     }
79     NativeValue* objValue = engine.CreateObject();
80     NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
81     formProviderData_->SetDataString(formDataStr);
82     object->SetProperty("data", CreateJsValue(engine, formDataStr));
83     HILOG_DEBUG("%{public}s called:%{private}s", __func__, formDataStr.c_str());
84     return objValue;
85 }
86 }
87 
FormBindingDataInit(NativeEngine * engine,NativeValue * exportObj)88 NativeValue* FormBindingDataInit(NativeEngine* engine, NativeValue* exportObj)
89 {
90     HILOG_INFO("%{public}s called.", __func__);
91     if (engine == nullptr || exportObj == nullptr) {
92         HILOG_ERROR("%{public}s engine or exportObj nullptr.", __func__);
93         return nullptr;
94     }
95 
96     NativeObject* object = ConvertNativeValueTo<NativeObject>(exportObj);
97     if (object == nullptr) {
98         HILOG_ERROR("%{public}s convertNativeValueTo result is nullptr.", __func__);
99         return nullptr;
100     }
101 
102     auto formProviderData = std::make_shared<AppExecFwk::FormProviderData>();
103     auto formBindingData = std::make_unique<FormBindingData>(formProviderData);
104     object->SetNativePointer(formBindingData.release(), FormBindingData::Finalizer, nullptr);
105 
106     const char *moduleName = "FormBindingData";
107     BindNativeFunction(*engine, *object, "createFormBindingData", moduleName, FormBindingData::CreateFormBindingData);
108 
109     HILOG_INFO("%{public}s called end.", __func__);
110     return exportObj;
111 }
112 } // namespace AbilityRuntime
113 } // namespace OHOS
114