1 /* 2 * Copyright (c) 2021 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 #ifndef OHOS_ABILITY_RUNTIME_NAPI_DATA_ABILITY_HELPER_H 17 #define OHOS_ABILITY_RUNTIME_NAPI_DATA_ABILITY_HELPER_H 18 #include "data_ability_observer_stub.h" 19 #include "feature_ability_common.h" 20 21 namespace OHOS { 22 namespace AppExecFwk { 23 class NAPIDataAbilityObserver : public AAFwk::DataAbilityObserverStub { 24 public: 25 void OnChange() override; 26 void SetEnv(const napi_env &env); 27 void SetCallbackRef(const napi_ref &ref); 28 void ReleaseJSCallback(); 29 30 void CallJsMethod(); 31 32 private: 33 void SafeReleaseJSCallback(); 34 35 napi_env env_ = nullptr; 36 napi_ref ref_ = nullptr; 37 bool isCallingback_ = false; 38 bool needRelease_ = false; 39 std::mutex mutex_; 40 }; 41 42 class NAPIDataAbilityHelperWrapper { 43 public: NAPIDataAbilityHelperWrapper(std::weak_ptr<DataAbilityHelper> && dataAbilityHelper)44 explicit NAPIDataAbilityHelperWrapper(std::weak_ptr<DataAbilityHelper>&& dataAbilityHelper) 45 : dataAbilityHelper_(dataAbilityHelper) {} GetDataAbilityHelper()46 inline std::shared_ptr<DataAbilityHelper> GetDataAbilityHelper() const 47 { 48 return dataAbilityHelper_.lock(); 49 } 50 51 private: 52 std::weak_ptr<DataAbilityHelper> dataAbilityHelper_; 53 }; 54 55 /** 56 * @brief DataAbilityHelper NAPI module registration. 57 * 58 * @param env The environment that the Node-API call is invoked under. 59 * @param exports An empty object via the exports parameter as a convenience. 60 * 61 * @return The return value from Init is treated as the exports object for the module. 62 */ 63 napi_value DataAbilityHelperInit(napi_env env, napi_value exports); 64 napi_value DataAbilityHelperConstructor(napi_env env, napi_callback_info info); 65 66 /** 67 * @brief DataAbilityHelper NAPI method : insert. 68 * 69 * @param env The environment that the Node-API call is invoked under. 70 * @param info The callback info passed into the callback function. 71 * 72 * @return The return value from NAPI C++ to JS for the module. 73 */ 74 napi_value NAPI_Insert(napi_env env, napi_callback_info info); 75 76 /** 77 * @brief Insert processing function. 78 * 79 * @param env The environment that the Node-API call is invoked under. 80 * @param insertCB Process data asynchronously. 81 * 82 * @return Return JS data successfully, otherwise return nullptr. 83 */ 84 napi_value InsertWrap(napi_env env, napi_callback_info info, DAHelperInsertCB *insertCB); 85 86 /** 87 * @brief Insert Async. 88 * 89 * @param env The environment that the Node-API call is invoked under. 90 * @param args Indicates the arguments passed into the callback. 91 * @param argcPromise Asynchronous data processing. 92 * @param insertCB Process data asynchronously. 93 * 94 * @return Return JS data successfully, otherwise return nullptr. 95 */ 96 napi_value InsertAsync(napi_env env, napi_value *args, const size_t argCallback, DAHelperInsertCB *insertCB); 97 98 /** 99 * @brief Insert Promise. 100 * 101 * @param env The environment that the Node-API call is invoked under. 102 * @param insertCB Process data asynchronously. 103 * 104 * @return Return JS data successfully, otherwise return nullptr. 105 */ 106 napi_value InsertPromise(napi_env env, DAHelperInsertCB *insertCB); 107 108 /** 109 * @brief Insert asynchronous processing function. 110 * 111 * @param env The environment that the Node-API call is invoked under. 112 * @param data Point to asynchronous processing of data. 113 */ 114 void InsertExecuteCB(napi_env env, void *data); 115 116 /** 117 * @brief The callback at the end of the asynchronous callback. 118 * 119 * @param env The environment that the Node-API call is invoked under. 120 * @param data Point to asynchronous processing of data. 121 */ 122 void InsertAsyncCompleteCB(napi_env env, napi_status status, void *data); 123 124 /** 125 * @brief The callback at the end of the Promise callback. 126 * 127 * @param env The environment that the Node-API call is invoked under. 128 * @param data Point to asynchronous processing of data. 129 */ 130 void InsertPromiseCompleteCB(napi_env env, napi_status status, void *data); 131 132 /** 133 * @brief DataAbilityHelper NAPI method : notifyChange. 134 * 135 * @param env The environment that the Node-API call is invoked under. 136 * @param info The callback info passed into the callback function. 137 * 138 * @return The return value from NAPI C++ to JS for the module. 139 */ 140 napi_value NAPI_NotifyChange(napi_env env, napi_callback_info info); 141 142 /** 143 * @brief NotifyChange processing function. 144 * 145 * @param env The environment that the Node-API call is invoked under. 146 * @param notifyChangeCB Process data asynchronously. 147 * 148 * @return Return JS data successfully, otherwise return nullptr. 149 */ 150 napi_value NotifyChangeWrap(napi_env env, napi_callback_info info, DAHelperNotifyChangeCB *notifyChangeCB); 151 152 /** 153 * @brief NotifyChange Async. 154 * 155 * @param env The environment that the Node-API call is invoked under. 156 * @param args Indicates the arguments passed into the callback. 157 * @param argcPromise Asynchronous data processing. 158 * @param notifyChangeCB Process data asynchronously. 159 * 160 * @return Return JS data successfully, otherwise return nullptr. 161 */ 162 napi_value NotifyChangeAsync( 163 napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, DAHelperNotifyChangeCB *notifyChangeCB); 164 165 /** 166 * @brief NotifyChange Promise. 167 * 168 * @param env The environment that the Node-API call is invoked under. 169 * @param notifyChangeCB Process data asynchronously. 170 * 171 * @return Return JS data successfully, otherwise return nullptr. 172 */ 173 napi_value NotifyChangePromise(napi_env env, DAHelperNotifyChangeCB *notifyChangeCB); 174 175 /** 176 * @brief NotifyChange asynchronous processing function. 177 * 178 * @param env The environment that the Node-API call is invoked under. 179 * @param data Point to asynchronous processing of data. 180 */ 181 void NotifyChangeExecuteCB(napi_env env, void *data); 182 183 /** 184 * @brief The callback at the end of the asynchronous callback. 185 * 186 * @param env The environment that the Node-API call is invoked under. 187 * @param data Point to asynchronous processing of data. 188 */ 189 void NotifyChangeAsyncCompleteCB(napi_env env, napi_status status, void *data); 190 191 /** 192 * @brief The callback at the end of the Promise callback. 193 * 194 * @param env The environment that the Node-API call is invoked under. 195 * @param data Point to asynchronous processing of data. 196 */ 197 void NotifyChangePromiseCompleteCB(napi_env env, napi_status status, void *data); 198 199 /** 200 * @brief DataAbilityHelper NAPI method : on. 201 * 202 * @param env The environment that the Node-API call is invoked under. 203 * @param info The callback info passed into the callback function. 204 * 205 * @return The return value from NAPI C++ to JS for the module. 206 */ 207 napi_value NAPI_Register(napi_env env, napi_callback_info info); 208 209 /** 210 * @brief On processing function. 211 * 212 * @param env The environment that the Node-API call is invoked under. 213 * @param onCB Process data asynchronously. 214 * 215 * @return Return JS data successfully, otherwise return nullptr. 216 */ 217 napi_value RegisterWrap(napi_env env, napi_callback_info info, DAHelperOnOffCB *onCB); 218 219 /** 220 * @brief On Async. 221 * 222 * @param env The environment that the Node-API call is invoked under. 223 * @param args Indicates the arguments passed into the callback. 224 * @param argcPromise Asynchronous data processing. 225 * @param onCB Process data asynchronously. 226 * 227 * @return Return JS data successfully, otherwise return nullptr. 228 */ 229 napi_value RegisterAsync( 230 napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, DAHelperOnOffCB *onCB); 231 232 /** 233 * @brief On asynchronous processing function. 234 * 235 * @param env The environment that the Node-API call is invoked under. 236 * @param data Point to asynchronous processing of data. 237 */ 238 void RegisterExecuteCB(napi_env env, void *data); 239 void RegisterCompleteCB(napi_env env, napi_status status, void *data); 240 241 /** 242 * @brief DataAbilityHelper NAPI method : off. 243 * 244 * @param env The environment that the Node-API call is invoked under. 245 * @param info The callback info passed into the callback function. 246 * 247 * @return The return value from NAPI C++ to JS for the module. 248 */ 249 napi_value NAPI_UnRegister(napi_env env, napi_callback_info info); 250 251 /** 252 * @brief Off processing function. 253 * 254 * @param env The environment that the Node-API call is invoked under. 255 * @param offCB Process data asynchronously. 256 * 257 * @return Return JS data successfully, otherwise return nullptr. 258 */ 259 napi_value UnRegisterWrap(napi_env env, napi_callback_info info, DAHelperOnOffCB *offCB); 260 261 /** 262 * @brief Off Async. 263 * 264 * @param env The environment that the Node-API call is invoked under. 265 * @param offCB Process data asynchronously. 266 * 267 * @return Return JS data successfully, otherwise return nullptr. 268 */ 269 napi_value UnRegisterSync(napi_env env, DAHelperOnOffCB *offCB); 270 271 /** 272 * @brief Off asynchronous processing function. 273 * 274 * @param env The environment that the Node-API call is invoked under. 275 * @param data Point to asynchronous processing of data. 276 */ 277 void UnRegisterExecuteCB(napi_env env, void *data); 278 void UnRegisterCompleteCB(napi_env env, napi_status status, void *data); 279 void FindRegisterObs(napi_env env, DAHelperOnOffCB *data); 280 /** 281 * @brief Parse the ValuesBucket parameters. 282 * 283 * @param param Indicates the want parameters saved the parse result. 284 * @param env The environment that the Node-API call is invoked under. 285 * @param args Indicates the arguments passed into the callback. 286 * 287 * @return The return value from NAPI C++ to JS for the module. 288 */ 289 napi_value UnwrapValuesBucket(std::string &value, napi_env env, napi_value args); 290 291 napi_value NAPI_GetType(napi_env env, napi_callback_info info); 292 napi_value NAPI_GetType(napi_env env, napi_callback_info info); 293 napi_value GetTypeWrap(napi_env env, napi_callback_info info, DAHelperGetTypeCB *gettypeCB); 294 napi_value GetTypeAsync(napi_env env, napi_value *args, const size_t argCallback, DAHelperGetTypeCB *gettypeCB); 295 napi_value GetTypePromise(napi_env env, DAHelperGetTypeCB *gettypeCB); 296 void GetTypeExecuteCB(napi_env env, void *data); 297 void GetTypeAsyncCompleteCB(napi_env env, napi_status status, void *data); 298 void GetTypePromiseCompleteCB(napi_env env, napi_status status, void *data); 299 300 napi_value NAPI_GetFileTypes(napi_env env, napi_callback_info info); 301 napi_value NAPI_GetFileTypes(napi_env env, napi_callback_info info); 302 napi_value GetFileTypesWrap(napi_env env, napi_callback_info info, DAHelperGetFileTypesCB *getfiletypesCB); 303 napi_value GetFileTypesAsync( 304 napi_env env, napi_value *args, const size_t argCallback, DAHelperGetFileTypesCB *getfiletypesCB); 305 napi_value GetFileTypesPromise(napi_env env, DAHelperGetFileTypesCB *getfiletypesCB); 306 void GetFileTypesExecuteCB(napi_env env, void *data); 307 void GetFileTypesAsyncCompleteCB(napi_env env, napi_status status, void *data); 308 void GetFileTypesPromiseCompleteCB(napi_env env, napi_status status, void *data); 309 napi_value WrapGetFileTypesCB(napi_env env, const DAHelperGetFileTypesCB &getfiletypesCB); 310 311 napi_value NAPI_NormalizeUri(napi_env env, napi_callback_info info); 312 napi_value NAPI_NormalizeUri(napi_env env, napi_callback_info info); 313 napi_value NormalizeUriWrap(napi_env env, napi_callback_info info, DAHelperNormalizeUriCB *normalizeuriCB); 314 napi_value NormalizeUriAsync( 315 napi_env env, napi_value *args, const size_t argCallback, DAHelperNormalizeUriCB *normalizeuriCB); 316 napi_value NormalizeUriPromise(napi_env env, DAHelperNormalizeUriCB *normalizeuriCB); 317 void NormalizeUriExecuteCB(napi_env env, void *data); 318 void NormalizeUriAsyncCompleteCB(napi_env env, napi_status status, void *data); 319 void NormalizeUriPromiseCompleteCB(napi_env env, napi_status status, void *data); 320 321 napi_value NAPI_DenormalizeUri(napi_env env, napi_callback_info info); 322 napi_value NAPI_DenormalizeUri(napi_env env, napi_callback_info info); 323 napi_value DenormalizeUriWrap(napi_env env, napi_callback_info info, DAHelperDenormalizeUriCB *denormalizeuriCB); 324 napi_value DenormalizeUriAsync( 325 napi_env env, napi_value *args, const size_t argCallback, DAHelperDenormalizeUriCB *denormalizeuriCB); 326 napi_value DenormalizeUriPromise(napi_env env, DAHelperDenormalizeUriCB *denormalizeuriCB); 327 void DenormalizeUriExecuteCB(napi_env env, void *data); 328 void DenormalizeUriAsyncCompleteCB(napi_env env, napi_status status, void *data); 329 void DenormalizeUriPromiseCompleteCB(napi_env env, napi_status status, void *data); 330 331 napi_value NAPI_Delete(napi_env env, napi_callback_info info); 332 333 napi_value DeleteWrap(napi_env env, napi_callback_info info, DAHelperDeleteCB *deleteCB); 334 napi_value DeleteAsync(napi_env env, napi_value *args, const size_t argCallback, DAHelperDeleteCB *deleteCB); 335 336 napi_value DeletePromise(napi_env env, DAHelperDeleteCB *deleteCB); 337 338 void DeleteExecuteCB(napi_env env, void *data); 339 340 void DeleteAsyncCompleteCB(napi_env env, napi_status status, void *data); 341 342 void DeletePromiseCompleteCB(napi_env env, napi_status status, void *data); 343 344 napi_value NAPI_Update(napi_env env, napi_callback_info info); 345 346 napi_value UpdateWrap(napi_env env, napi_callback_info info, DAHelperUpdateCB *updateCB); 347 napi_value UpdateAsync(napi_env env, napi_value *args, const size_t argCallback, DAHelperUpdateCB *updateCB); 348 349 napi_value UpdatePromise(napi_env env, DAHelperUpdateCB *updateCB); 350 351 void UpdateExecuteCB(napi_env env, void *data); 352 353 void UpdateAsyncCompleteCB(napi_env env, napi_status status, void *data); 354 355 void UpdatePromiseCompleteCB(napi_env env, napi_status status, void *data); 356 357 napi_value NAPI_Call(napi_env env, napi_callback_info info); 358 359 napi_value NAPI_OpenFile(napi_env env, napi_callback_info info); 360 361 napi_value OpenFileWrap(napi_env env, napi_callback_info info, DAHelperOpenFileCB *openFileCB); 362 napi_value OpenFileAsync(napi_env env, napi_value *args, const size_t argCallback, DAHelperOpenFileCB *openFileCB); 363 364 napi_value OpenFilePromise(napi_env env, DAHelperOpenFileCB *openFileCB); 365 366 void OpenFileExecuteCB(napi_env env, void *data); 367 368 void OpenFileAsyncCompleteCB(napi_env env, napi_status status, void *data); 369 370 void OpenFilePromiseCompleteCB(napi_env env, napi_status status, void *data); 371 372 napi_value NAPI_BatchInsert(napi_env env, napi_callback_info info); 373 374 napi_value BatchInsertWrap(napi_env env, napi_callback_info info, DAHelperBatchInsertCB *batchInsertCB); 375 napi_value BatchInsertAsync( 376 napi_env env, napi_value *args, const size_t argCallback, DAHelperBatchInsertCB *batchInsertCB); 377 378 napi_value BatchInsertPromise(napi_env env, DAHelperBatchInsertCB *batchInsertCB); 379 380 void BatchInsertExecuteCB(napi_env env, void *data); 381 382 void BatchInsertAsyncCompleteCB(napi_env env, napi_status status, void *data); 383 384 void BatchInsertPromiseCompleteCB(napi_env env, napi_status status, void *data); 385 386 std::vector<NativeRdb::ValuesBucket> NapiValueObject(napi_env env, napi_value param); 387 388 bool UnwrapArrayObjectFromJS(napi_env env, napi_value param, std::vector<NativeRdb::ValuesBucket> &value); 389 390 napi_value NAPI_Query(napi_env env, napi_callback_info info); 391 392 napi_value QueryWrap(napi_env env, napi_callback_info info, DAHelperQueryCB *queryCB); 393 394 napi_value QuerySync(napi_env env, napi_value *args, const size_t argCallback, DAHelperQueryCB *queryCB); 395 396 napi_value QueryPromise(napi_env env, DAHelperQueryCB *queryCB); 397 398 napi_value WrapResultSet(napi_env env, const std::shared_ptr<NativeRdb::AbsSharedResultSet> &resultSet); 399 400 void AnalysisValuesBucket(NativeRdb::ValuesBucket &valuesBucket, const napi_env &env, const napi_value &arg); 401 void SetValuesBucketObject( 402 NativeRdb::ValuesBucket &valuesBucket, const napi_env &env, std::string keyStr, napi_value value); 403 404 void UnwrapDataAbilityPredicates(NativeRdb::DataAbilityPredicates &predicates, napi_env env, napi_value value); 405 406 /** 407 * @brief DataAbilityHelper NAPI method : executeBatch. 408 * 409 * @param env The environment that the Node-API call is invoked under. 410 * @param info The callback info passed into the callback function. 411 * 412 * @return The return value from NAPI C++ to JS for the module. 413 */ 414 napi_value NAPI_ExecuteBatch(napi_env env, napi_callback_info info); 415 416 /** 417 * @brief ExecuteBatch processing function. 418 * 419 * @param env The environment that the Node-API call is invoked under. 420 * @param executeBatchCB Process data asynchronously. 421 * 422 * @return Return JS data successfully, otherwise return nullptr. 423 */ 424 napi_value ExecuteBatchWrap(napi_env env, napi_callback_info info, DAHelperExecuteBatchCB *executeBatchCB); 425 426 bool UnwrapArrayOperationFromJS( 427 napi_env env, napi_value param, std::vector<std::shared_ptr<DataAbilityOperation>> &result); 428 /** 429 * @brief ExecuteBatch Async. 430 * 431 * @param env The environment that the Node-API call is invoked under. 432 * @param args Indicates the arguments passed into the callback. 433 * @param argcPromise Asynchronous data processing. 434 * @param executeBatchCB Process data asynchronously. 435 * 436 * @return Return JS data successfully, otherwise return nullptr. 437 */ 438 napi_value ExecuteBatchAsync( 439 napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, DAHelperExecuteBatchCB *executeBatchCB); 440 441 /** 442 * @brief ExecuteBatch Promise. 443 * 444 * @param env The environment that the Node-API call is invoked under. 445 * @param executeBatchCB Process data asynchronously. 446 * 447 * @return Return JS data successfully, otherwise return nullptr. 448 */ 449 napi_value ExecuteBatchPromise(napi_env env, DAHelperExecuteBatchCB *executeBatchCB); 450 451 /** 452 * @brief ExecuteBatch asynchronous processing function. 453 * 454 * @param env The environment that the Node-API call is invoked under. 455 * @param data Point to asynchronous processing of data. 456 */ 457 void ExecuteBatchExecuteCB(napi_env env, void *data); 458 459 /** 460 * @brief The callback at the end of the asynchronous callback. 461 * 462 * @param env The environment that the Node-API call is invoked under. 463 * @param data Point to asynchronous processing of data. 464 */ 465 void ExecuteBatchAsyncCompleteCB(napi_env env, napi_status status, void *data); 466 467 /** 468 * @brief The callback at the end of the Promise callback. 469 * 470 * @param env The environment that the Node-API call is invoked under. 471 * @param data Point to asynchronous processing of data. 472 */ 473 void ExecuteBatchPromiseCompleteCB(napi_env env, napi_status status, void *data); 474 475 void GetDataAbilityResultForResult( 476 napi_env env, const std::vector<std::shared_ptr<DataAbilityResult>> &dataAbilityResult, napi_value result); 477 478 void GetDataAbilityHelper(napi_env env, napi_value thisVar, std::shared_ptr<DataAbilityHelper>& dataAbilityHelper); 479 void DeleteDAHelperOnOffCB(DAHelperOnOffCB *onCB); 480 bool NeedErase(std::vector<DAHelperOnOffCB*>::iterator& iter, 481 const std::shared_ptr<DataAbilityHelper>&& dataAbilityHelper); 482 void EraseMemberProperties(DAHelperOnOffCB* onCB); 483 } // namespace AppExecFwk 484 } // namespace OHOS 485 #endif /* OHOS_ABILITY_RUNTIME_NAPI_DATA_ABILITY_HELPER_H */ 486