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