1 /* 2 * Copyright (c) 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 16 #ifndef NAPI_DATASHARE_HELPER_H 17 #define NAPI_DATASHARE_HELPER_H 18 19 #include <memory> 20 #include "async_call.h" 21 #include "datashare_helper.h" 22 #include "data_share_common.h" 23 #include "napi_datashare_observer.h" 24 25 namespace OHOS { 26 namespace DataShare { 27 class NapiDataShareHelper { 28 public: 29 static napi_value Napi_CreateDataShareHelper(napi_env env, napi_callback_info info); 30 static napi_value Napi_OpenFile(napi_env env, napi_callback_info info); 31 static napi_value Napi_On(napi_env env, napi_callback_info info); 32 static napi_value Napi_Off(napi_env env, napi_callback_info info); 33 static napi_value Napi_Insert(napi_env env, napi_callback_info info); 34 static napi_value Napi_Delete(napi_env env, napi_callback_info info); 35 static napi_value Napi_Query(napi_env env, napi_callback_info info); 36 static napi_value Napi_Update(napi_env env, napi_callback_info info); 37 static napi_value Napi_BatchInsert(napi_env env, napi_callback_info info); 38 static napi_value Napi_GetType(napi_env env, napi_callback_info info); 39 static napi_value Napi_GetFileTypes(napi_env env, napi_callback_info info); 40 static napi_value Napi_NormalizeUri(napi_env env, napi_callback_info info); 41 static napi_value Napi_DenormalizeUri(napi_env env, napi_callback_info info); 42 static napi_value Napi_NotifyChange(napi_env env, napi_callback_info info); 43 private: 44 static napi_value GetConstructor(napi_env env); 45 static napi_value Initialize(napi_env env, napi_callback_info info); 46 47 bool HasRegisteredObserver(napi_env env, std::list<sptr<NAPIDataShareObserver>> &list, napi_value callback); 48 void RegisteredObserver(napi_env env, const std::string &uri, napi_value callback); 49 void UnRegisteredObserver(napi_env env, const std::string &uri, napi_value callback); 50 void UnRegisteredObserver(napi_env env, const std::string &uri); 51 52 std::shared_ptr<DataShareHelper> datashareHelper_ = nullptr; 53 std::map<std::string, std::list<sptr<NAPIDataShareObserver>>> observerMap_; 54 std::mutex listMutex_ {}; 55 56 struct CreateContextInfo : public AsyncCall::Context { 57 napi_env env = nullptr; 58 napi_ref ref = nullptr; 59 bool isStageMode = true; 60 std::string strUri = ""; 61 std::shared_ptr<AppExecFwk::Context> contextF = nullptr; 62 std::shared_ptr<OHOS::AbilityRuntime::Context> contextS = nullptr; 63 std::shared_ptr<DataShareHelper> dataShareHelper = nullptr; CreateContextInfoCreateContextInfo64 CreateContextInfo() : Context(nullptr, nullptr) {}; CreateContextInfoCreateContextInfo65 CreateContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {}; ~CreateContextInfoCreateContextInfo66 ~CreateContextInfo() 67 { 68 if (env != nullptr && ref != nullptr) { 69 napi_delete_reference(env, ref); 70 } 71 } 72 }; 73 74 struct ContextInfo : public AsyncCall::Context { 75 NapiDataShareHelper *proxy = nullptr; 76 napi_status status = napi_generic_failure; 77 int resultNumber = 0; 78 std::shared_ptr<DataShareResultSet> resultObject = nullptr; 79 std::string resultString = ""; 80 std::vector<std::string> resultStrArr; 81 std::string uri; 82 std::string mode; 83 DataShareValuesBucket valueBucket; 84 DataSharePredicates predicates; 85 std::vector<std::string> columns; 86 std::vector<DataShareValuesBucket> values; 87 std::string mimeTypeFilter; 88 ContextInfoContextInfo89 ContextInfo() : Context(nullptr, nullptr) {}; ContextInfoContextInfo90 ContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {}; ~ContextInfoContextInfo91 virtual ~ContextInfo() {}; 92 operatorContextInfo93 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 94 { 95 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 96 NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast<void **>(&proxy)), napi_invalid_arg); 97 NAPI_ASSERT_BASE(env, proxy != nullptr, "there is no native upload task", napi_invalid_arg); 98 return Context::operator()(env, argc, argv, self); 99 } operatorContextInfo100 napi_status operator()(napi_env env, napi_value *result) override 101 { 102 if (status != napi_ok) { 103 return status; 104 } 105 return Context::operator()(env, result); 106 } 107 }; 108 }; 109 } // namespace DataShare 110 } // namespace OHOS 111 #endif /* NAPI_DATASHARE_HELPER_H */ 112