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 <shared_mutex> 21 22 #include "async_call.h" 23 #include "data_share_common.h" 24 #include "datashare_helper.h" 25 #include "napi_datashare_observer.h" 26 #include "napi_subscriber_manager.h" 27 28 namespace OHOS { 29 namespace DataShare { 30 class NapiDataShareHelper { 31 public: 32 static napi_value Napi_CreateDataShareHelper(napi_env env, napi_callback_info info); 33 static napi_value Napi_On(napi_env env, napi_callback_info info); 34 static napi_value Napi_Off(napi_env env, napi_callback_info info); 35 static napi_value Napi_Insert(napi_env env, napi_callback_info info); 36 static napi_value Napi_Delete(napi_env env, napi_callback_info info); 37 static napi_value Napi_Query(napi_env env, napi_callback_info info); 38 static napi_value Napi_Update(napi_env env, napi_callback_info info); 39 static napi_value Napi_BatchUpdate(napi_env env, napi_callback_info info); 40 static napi_value Napi_BatchInsert(napi_env env, napi_callback_info info); 41 static napi_value Napi_NormalizeUri(napi_env env, napi_callback_info info); 42 static napi_value Napi_DenormalizeUri(napi_env env, napi_callback_info info); 43 static napi_value Napi_NotifyChange(napi_env env, napi_callback_info info); 44 static napi_value Napi_AddTemplate(napi_env env, napi_callback_info info); 45 static napi_value Napi_DelTemplate(napi_env env, napi_callback_info info); 46 static napi_value Napi_Publish(napi_env env, napi_callback_info info); 47 static napi_value Napi_GetPublishedData(napi_env env, napi_callback_info info); 48 static napi_value EnableSilentProxy(napi_env env, napi_callback_info info); 49 static napi_value DisableSilentProxy(napi_env env, napi_callback_info info); 50 static napi_value Napi_Close(napi_env env, napi_callback_info info); 51 52 private: 53 static napi_value GetConstructor(napi_env env); 54 static napi_value Initialize(napi_env env, napi_callback_info info); 55 static napi_value Napi_SubscribeRdbObserver(napi_env env, size_t argc, napi_value *argv, napi_value self); 56 static napi_value Napi_UnsubscribeRdbObserver(napi_env env, size_t argc, napi_value *argv, napi_value self); 57 static napi_value Napi_RegisterObserver(napi_env env, size_t argc, napi_value *argv, napi_value self); 58 static napi_value Napi_UnregisterObserver(napi_env env, size_t argc, napi_value *argv, napi_value self); 59 static napi_value Napi_SubscribePublishedObserver(napi_env env, size_t argc, napi_value *argv, napi_value self); 60 static napi_value Napi_UnsubscribePublishedObserver(napi_env env, size_t argc, napi_value *argv, napi_value self); 61 static napi_value SetSilentSwitch(napi_env env, napi_callback_info info, bool enable); 62 63 bool HasRegisteredObserver(napi_env env, std::list<sptr<NAPIDataShareObserver>> &list, napi_value callback); 64 void RegisteredObserver(napi_env env, const std::string &uri, napi_value callback, 65 std::shared_ptr<DataShareHelper> helper, bool isNotifyDetails = false); 66 void UnRegisteredObserver(napi_env env, const std::string &uri, napi_value callback, 67 std::shared_ptr<DataShareHelper> helper, bool isNotifyDetails = false); 68 void UnRegisteredObserver(napi_env env, const std::string& uri, std::shared_ptr<DataShareHelper> helper, 69 bool isNotifyDetails = false); 70 static bool GetOptions(napi_env env, napi_value jsValue, CreateOptions &options); 71 void SetHelper(std::shared_ptr<DataShareHelper> dataShareHelper); 72 std::shared_ptr<DataShareHelper> GetHelper(); 73 std::shared_ptr<DataShareHelper> datashareHelper_ = nullptr; 74 std::map<std::string, std::list<sptr<NAPIDataShareObserver>>> observerMap_; 75 std::mutex listMutex_{}; 76 std::shared_mutex mutex_; 77 std::shared_ptr<NapiRdbSubscriberManager> jsRdbObsManager_ = nullptr; 78 std::shared_ptr<NapiPublishedSubscriberManager> jsPublishedObsManager_ = nullptr; 79 80 struct CreateContextInfo : public AsyncCall::Context { 81 napi_env env = nullptr; 82 napi_ref ref = nullptr; 83 bool isStageMode = true; 84 CreateOptions options; 85 std::string strUri; 86 std::shared_ptr<AppExecFwk::Context> contextF = nullptr; 87 std::shared_ptr<OHOS::AbilityRuntime::Context> contextS = nullptr; 88 std::shared_ptr<DataShareHelper> dataShareHelper = nullptr; 89 bool silentSwitch = false; CreateContextInfoCreateContextInfo90 CreateContextInfo() : Context(nullptr, nullptr) {}; CreateContextInfoCreateContextInfo91 CreateContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {}; ~CreateContextInfoCreateContextInfo92 ~CreateContextInfo() 93 { 94 if (env != nullptr && ref != nullptr) { 95 napi_delete_reference(env, ref); 96 } 97 } 98 }; 99 100 struct ContextInfo : public AsyncCall::Context { 101 NapiDataShareHelper *proxy = nullptr; 102 napi_status status = napi_generic_failure; 103 int resultNumber = 0; 104 std::shared_ptr<DataShareResultSet> resultObject = nullptr; 105 std::string resultString = ""; 106 std::vector<std::string> resultStrArr; 107 std::string uri; 108 std::string mode; 109 DataShareValuesBucket valueBucket; 110 DataSharePredicates predicates; 111 std::vector<std::string> columns; 112 std::vector<DataShareValuesBucket> values; 113 std::string mimeTypeFilter; 114 DatashareBusinessError businessError; 115 Data publishData; 116 std::string bundleName; 117 std::vector<OperationResult> results; 118 UpdateOperations updateOperations; 119 std::vector<BatchUpdateResult> batchUpdateResult; 120 DataShareObserver::ChangeInfo changeInfo; 121 bool isNotifyDetails = false; 122 ContextInfoContextInfo123 ContextInfo() : Context(nullptr, nullptr) {}; ContextInfoContextInfo124 ContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {}; ~ContextInfoContextInfo125 virtual ~ContextInfo() {}; 126 operatorContextInfo127 napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override 128 { 129 NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg); 130 NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast<void **>(&proxy)), napi_invalid_arg); 131 NAPI_ASSERT_BASE(env, proxy != nullptr, "there is no native upload task", napi_invalid_arg); 132 return Context::operator()(env, argc, argv, self); 133 } operatorContextInfo134 napi_status operator()(napi_env env, napi_value *result) override 135 { 136 if (status != napi_ok) { 137 return status; 138 } 139 return Context::operator()(env, result); 140 } 141 }; 142 static void Notify(const std::shared_ptr<NapiDataShareHelper::ContextInfo> context, 143 std::shared_ptr<DataShareHelper> helper); 144 }; 145 } // namespace DataShare 146 } // namespace OHOS 147 #endif /* NAPI_DATASHARE_HELPER_H */ 148