1 /*
2 * Copyright (c) 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 #define LOG_TAG "UnifiedDataPropertiesNapi"
16 #include "unified_data_properties_napi.h"
17
18 #include "napi_data_utils.h"
19 #include "unified_data.h"
20
21 namespace OHOS {
22 namespace UDMF {
Constructor(napi_env env)23 napi_value UnifiedDataPropertiesNapi::Constructor(napi_env env)
24 {
25 LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedDataPropertiesNapi");
26 napi_property_descriptor properties[] = {
27 DECLARE_NAPI_GETTER_SETTER("extras", GetExtras, SetExtras),
28 DECLARE_NAPI_GETTER_SETTER("tag", GetTag, SetTag),
29 DECLARE_NAPI_GETTER_SETTER("shareOptions", GetShareOptions, SetShareOptions),
30 DECLARE_NAPI_GETTER("timestamp", GetTimestamp),
31 DECLARE_NAPI_GETTER_SETTER("getDelayData", GetDelayData, SetDelayData),
32 };
33 size_t count = sizeof(properties) / sizeof(properties[0]);
34 return NapiDataUtils::DefineClass(env, "UnifiedDataProperties", properties, count, UnifiedDataPropertiesNapi::New);
35 }
36
New(napi_env env,napi_callback_info info)37 napi_value UnifiedDataPropertiesNapi::New(napi_env env, napi_callback_info info)
38 {
39 LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedDataPropertiesNapi");
40 auto ctxt = std::make_shared<ContextBase>();
41 ctxt->GetCbInfoSync(env, info);
42 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
43
44 auto* properties = new (std::nothrow) UnifiedDataPropertiesNapi();
45 ASSERT_ERR(ctxt->env, properties != nullptr, Status::E_ERROR, "no memory for properties!");
46
47 properties->value_ = std::make_shared<UnifiedDataProperties>();
48 ASSERT_CALL(env, napi_wrap(env, ctxt->self, properties, Destructor, nullptr, nullptr), properties);
49 return ctxt->self;
50 }
51
Destructor(napi_env env,void * data,void * hint)52 void UnifiedDataPropertiesNapi::Destructor(napi_env env, void *data, void *hint)
53 {
54 LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedDataPropertiesNapi finalize.");
55 auto *properties = static_cast<UnifiedDataPropertiesNapi *>(data);
56 ASSERT_VOID(properties != nullptr, "finalize null!");
57 if (properties->delayDataRef_ != nullptr) {
58 napi_delete_reference(env, properties->delayDataRef_);
59 }
60 delete properties;
61 }
62
GetPropertiesNapi(napi_env env,napi_callback_info info,std::shared_ptr<ContextBase> ctxt)63 UnifiedDataPropertiesNapi* UnifiedDataPropertiesNapi::GetPropertiesNapi(
64 napi_env env, napi_callback_info info, std::shared_ptr<ContextBase> ctxt)
65 {
66 LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedDataPropertiesNapi");
67 ctxt->GetCbInfoSync(env, info);
68 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
69 return reinterpret_cast<UnifiedDataPropertiesNapi*>(ctxt->native);
70 }
71
GetExtras(napi_env env,napi_callback_info info)72 napi_value UnifiedDataPropertiesNapi::GetExtras(napi_env env, napi_callback_info info)
73 {
74 LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedDataPropertiesNapi");
75 auto ctxt = std::make_shared<ContextBase>();
76 auto properties = GetPropertiesNapi(env, info, ctxt);
77 ASSERT_ERR(ctxt->env, (properties != nullptr && properties->value_ != nullptr),
78 Status::E_ERROR, "invalid object!");
79 ctxt->output = OHOS::AppExecFwk::WrapWantParams(env, properties->value_->extras);
80 return ctxt->output;
81 }
82
SetExtras(napi_env env,napi_callback_info info)83 napi_value UnifiedDataPropertiesNapi::SetExtras(napi_env env, napi_callback_info info)
84 {
85 LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedDataPropertiesNapi");
86 auto ctxt = std::make_shared<ContextBase>();
87 AAFwk::WantParams value;
88 auto input = [env, ctxt, &value](size_t argc, napi_value* argv) {
89 ASSERT_BUSINESS_ERR(ctxt, argc >= 1, Status::E_ERROR, "Mandatory parameters are left unspecified");
90 bool ret = OHOS::AppExecFwk::UnwrapWantParams(env, argv[0], value);
91 ASSERT_BUSINESS_ERR(ctxt, ret, Status::E_ERROR, "parameter extras type must be Record<string, object>");
92 };
93 ctxt->GetCbInfoSync(env, info, input);
94 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
95 auto properties = static_cast<UnifiedDataPropertiesNapi*>(ctxt->native);
96 ASSERT_ERR(ctxt->env, (properties != nullptr && properties->value_ != nullptr),
97 Status::E_ERROR, "invalid object!");
98 properties->value_->extras = value;
99 return nullptr;
100 }
101
GetTag(napi_env env,napi_callback_info info)102 napi_value UnifiedDataPropertiesNapi::GetTag(napi_env env, napi_callback_info info)
103 {
104 LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedDataPropertiesNapi");
105 auto ctxt = std::make_shared<ContextBase>();
106 auto properties = GetPropertiesNapi(env, info, ctxt);
107 ASSERT_ERR(ctxt->env, (properties != nullptr && properties->value_ != nullptr),
108 Status::E_ERROR, "invalid object!");
109 ctxt->status = NapiDataUtils::SetValue(env, properties->value_->tag, ctxt->output);
110 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set tag failed");
111 return ctxt->output;
112 }
113
SetTag(napi_env env,napi_callback_info info)114 napi_value UnifiedDataPropertiesNapi::SetTag(napi_env env, napi_callback_info info)
115 {
116 LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedDataPropertiesNapi");
117 auto ctxt = std::make_shared<ContextBase>();
118 std::string tag;
119 auto input = [env, ctxt, &tag](size_t argc, napi_value* argv) {
120 ASSERT_BUSINESS_ERR(ctxt, argc >= 1, Status::E_ERROR, "Mandatory parameters are left unspecified");
121 ctxt->status = NapiDataUtils::GetValue(env, argv[0], tag);
122 ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok, Status::E_ERROR, "parameter tag type must be string");
123 };
124 ctxt->GetCbInfoSync(env, info, input);
125 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
126 auto properties = static_cast<UnifiedDataPropertiesNapi*>(ctxt->native);
127 ASSERT_ERR(ctxt->env, (properties != nullptr && properties->value_ != nullptr),
128 Status::E_ERROR, "invalid object!");
129 properties->value_->tag = tag;
130 return nullptr;
131 }
132
GetShareOptions(napi_env env,napi_callback_info info)133 napi_value UnifiedDataPropertiesNapi::GetShareOptions(napi_env env, napi_callback_info info)
134 {
135 LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedDataPropertiesNapi");
136 auto ctxt = std::make_shared<ContextBase>();
137 auto properties = GetPropertiesNapi(env, info, ctxt);
138 ASSERT_ERR(ctxt->env, (properties != nullptr && properties->value_ != nullptr),
139 Status::E_ERROR, "invalid object!");
140 ctxt->status = NapiDataUtils::SetValue(env,
141 properties->value_->shareOptions == CROSS_DEVICE ? CROSS_APP : properties->value_->shareOptions, ctxt->output);
142 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set ShareOptions failed");
143 return ctxt->output;
144 }
145
SetShareOptions(napi_env env,napi_callback_info info)146 napi_value UnifiedDataPropertiesNapi::SetShareOptions(napi_env env, napi_callback_info info)
147 {
148 LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedDataPropertiesNapi");
149 auto ctxt = std::make_shared<ContextBase>();
150 int32_t shareOptionsValue = ShareOptions::CROSS_APP;
151 auto input = [env, ctxt, &shareOptionsValue](size_t argc, napi_value *argv) {
152 ASSERT_BUSINESS_ERR(ctxt, argc >= 1, Status::E_ERROR, "Mandatory parameters are left unspecified");
153 ctxt->status = NapiDataUtils::GetValue(env, argv[0], shareOptionsValue);
154 ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok,
155 Status::E_ERROR, "parameter shareOptions type must be ShareOptions");
156 };
157 ctxt->GetCbInfoSync(env, info, input);
158 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
159 auto properties = static_cast<UnifiedDataPropertiesNapi *>(ctxt->native);
160 ASSERT_ERR(ctxt->env, (properties != nullptr && properties->value_ != nullptr),
161 Status::E_ERROR, "invalid object!");
162 properties->value_->shareOptions = static_cast<ShareOptions>(shareOptionsValue);
163 return nullptr;
164 }
165
GetTimestamp(napi_env env,napi_callback_info info)166 napi_value UnifiedDataPropertiesNapi::GetTimestamp(napi_env env, napi_callback_info info)
167 {
168 LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedDataPropertiesNapi");
169 auto ctxt = std::make_shared<ContextBase>();
170 auto properties = GetPropertiesNapi(env, info, ctxt);
171 ASSERT_ERR(ctxt->env, (properties != nullptr && properties->value_ != nullptr),
172 Status::E_ERROR, "invalid object!");
173 ctxt->status = napi_create_date(env, properties->value_->timestamp, &ctxt->output);
174 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set timestamp failed");
175 return ctxt->output;
176 }
177
GetDelayData(napi_env env,napi_callback_info info)178 napi_value UnifiedDataPropertiesNapi::GetDelayData(napi_env env, napi_callback_info info)
179 {
180 LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedDataPropertiesNapi");
181 auto ctxt = std::make_shared<ContextBase>();
182 auto properties = GetPropertiesNapi(env, info, ctxt);
183 ASSERT_ERR(ctxt->env, (properties != nullptr && properties->value_ != nullptr),
184 Status::E_ERROR, "invalid object!");
185 if (properties->delayDataRef_ == nullptr) {
186 LOG_INFO(UDMF_KITS_NAPI, "get GetDelayData undefined");
187 napi_value undefinedValue;
188 napi_get_undefined(env, &undefinedValue);
189 return undefinedValue;
190 }
191 ctxt->status = napi_get_reference_value(env, properties->delayDataRef_, &ctxt->output);
192 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set delayData failed");
193 return ctxt->output;
194 }
195
SetDelayData(napi_env env,napi_callback_info info)196 napi_value UnifiedDataPropertiesNapi::SetDelayData(napi_env env, napi_callback_info info)
197 {
198 LOG_DEBUG(UDMF_KITS_NAPI, "UnifiedDataPropertiesNapi");
199 auto ctxt = std::make_shared<ContextBase>();
200 napi_value handler;
201 napi_ref ref;
202 auto input = [env, ctxt, &handler](size_t argc, napi_value* argv) {
203 ASSERT_BUSINESS_ERR(ctxt, argc >= 1, Status::E_ERROR, "Mandatory parameters are left unspecified");
204 napi_valuetype valueType = napi_undefined;
205 ctxt->status = napi_typeof(env, argv[0], &valueType);
206 ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok && valueType == napi_function,
207 Status::E_ERROR, "parameter getDelayData type must be GetDelayData");
208 handler = argv[0];
209 };
210 ctxt->GetCbInfoSync(env, info, input);
211 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
212 auto properties = static_cast<UnifiedDataPropertiesNapi *>(ctxt->native);
213 ASSERT_ERR(ctxt->env, (properties != nullptr && properties->value_ != nullptr),
214 Status::E_ERROR, "invalid object!");
215 if (properties->delayDataRef_ != nullptr) {
216 napi_delete_reference(env, properties->delayDataRef_);
217 }
218 ctxt->status = napi_create_reference(env, handler, 1, &ref);
219 ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "napi_create_reference failed");
220 properties->delayDataRef_ = ref;
221 return nullptr;
222 }
223 } // namespace UDMF
224 } // namespace OHOS