From c33cd2701c71e706709c53606971171821046207 Mon Sep 17 00:00:00 2001 From: wangkun Date: Tue, 7 Sep 2021 15:22:35 +0800 Subject: [PATCH 1/5] Fix Js Interface Callback crash. Signed-off-by: wangkun --- .../distributeddata/include/single_kv_store.h | 14 +++ .../distributeddata/src/single_kv_store.cpp | 89 +++++++++++++------ interfaces/jskits/distributeddata/BUILD.gn | 16 ++-- 3 files changed, 84 insertions(+), 35 deletions(-) diff --git a/frameworks/jskitsimpl/distributeddata/include/single_kv_store.h b/frameworks/jskitsimpl/distributeddata/include/single_kv_store.h index 47c0b990d..dd9942fdf 100644 --- a/frameworks/jskitsimpl/distributeddata/include/single_kv_store.h +++ b/frameworks/jskitsimpl/distributeddata/include/single_kv_store.h @@ -88,8 +88,17 @@ public: std::unique_ptr snapshot) override; void OnChange(const DistributedKv::ChangeNotification ¬ification) override; private: + struct EventDataWorker { + const DataObserver *observer = nullptr; + const DistributedKv::ChangeNotification data; + EventDataWorker(const DataObserver * const & observerIn, const DistributedKv::ChangeNotification &dataIn) + : observer(observerIn), + data(dataIn.GetInsertEntries(), dataIn.GetUpdateEntries(), + dataIn.GetDeleteEntries(), dataIn.GetDeviceId(), false) {} + }; napi_ref callback_ = nullptr; napi_env env_; + uv_loop_s *loop_ = nullptr; }; class SyncObserver : public DistributedKv::KvStoreSyncCallback { @@ -98,8 +107,13 @@ public: virtual ~SyncObserver(); void SyncCompleted(const std::map &results) override; private: + struct EventDataWorker { + const SyncObserver *observer = nullptr; + std::map data; + }; napi_ref callback_ = nullptr; napi_env env_; + uv_loop_s *loop_ = nullptr; }; } #endif // OHOS_SINGLE_KV_STORE_H diff --git a/frameworks/jskitsimpl/distributeddata/src/single_kv_store.cpp b/frameworks/jskitsimpl/distributeddata/src/single_kv_store.cpp index 0150561fa..c91d54153 100644 --- a/frameworks/jskitsimpl/distributeddata/src/single_kv_store.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/single_kv_store.cpp @@ -15,10 +15,13 @@ #define LOG_TAG "SingleKVStore" #include "single_kv_store.h" -#include "js_util.h" -#include "single_kvstore.h" + +#include + #include "async_call.h" +#include "js_util.h" #include "log_print.h" +#include "single_kvstore.h" using namespace OHOS::DistributedKv; namespace OHOS::DistributedData { @@ -72,6 +75,7 @@ napi_value SingleKVStore::Put(napi_env env, napi_callback_info info) { auto context = std::make_shared(); auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { + // 2 is the max number of parameters NAPI_ASSERT_BASE(env, argc >= 2, " should 2 or more parameters!", napi_invalid_arg); context->key = JSUtil::Convert2String(env, argv[0]); context->value = JSUtil::Convert2Vector(env, argv[1]); @@ -84,6 +88,7 @@ napi_value SingleKVStore::Put(napi_env env, napi_callback_info info) context->status = (status != Status::SUCCESS) ? napi_generic_failure : napi_ok; }; context->SetAction(std::move(input)); + // 2 is the callback position AsyncCall asyncCall(env, info, std::dynamic_pointer_cast(context), 2); return asyncCall.Call(env, exec); } @@ -160,6 +165,7 @@ napi_value SingleKVStore::Sync(napi_env env, napi_callback_info info) size_t argc = JSUtil::MAX_ARGC; napi_value argv[JSUtil::MAX_ARGC] = {nullptr}; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &self, nullptr)); + // the number of parameters is greater than 2 is error NAPI_ASSERT_BASE(env, argc >= 2, "args is out of range", nullptr); NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", nullptr); SingleKVStore *proxy = nullptr; @@ -170,8 +176,9 @@ napi_value SingleKVStore::Sync(napi_env env, napi_callback_info info) int32_t mode = int32_t(SyncMode::PUSH_PULL); napi_get_value_int32(env, argv[1], &mode); uint32_t delay = 0; + // 3 is the max number of parameters if (argc >= 3) { - napi_get_value_uint32(env, argv[2], &delay); + napi_get_value_uint32(env, argv[2], &delay); // argv[2] is the third parameter } ZLOGD("sync data %{public}d, mode:%{public}d, devices:%{public}zu", static_cast(argc), mode, devices.size()); @@ -270,6 +277,7 @@ DataObserver::DataObserver(napi_env env, napi_value callback) : env_(env) { napi_create_reference(env, callback, 1, &callback_); + napi_get_uv_event_loop(env, &loop_); } DataObserver::~DataObserver() @@ -290,23 +298,38 @@ void DataObserver::OnChange(const ChangeNotification ¬ification) notification.GetInsertEntries().size(), notification.GetUpdateEntries().size(), notification.GetDeleteEntries().size()); KvStoreObserver::OnChange(notification); - napi_value jsNotification = JSUtil::Convert2JSNotification(env_, notification); - napi_value callback = nullptr; - napi_value args[1] = {jsNotification}; - napi_get_reference_value(env_, callback_, &callback); - napi_value global = nullptr; - napi_get_global(env_, &global); - napi_value result; - napi_status status = napi_call_function(env_, global, callback, 1, args, &result); - if (status != napi_ok) { - ZLOGE("notify data change failed status:%{public}d callback:%{public}p", status, callback); - } + EventDataWorker *eventDataWorker = new EventDataWorker(this, notification); + uv_work_t *work = new uv_work_t; + work->data = eventDataWorker; + uv_queue_work(loop_, work, + [](uv_work_t *work) {}, + [](uv_work_t *work, int status) { + EventDataWorker *eventDataInner = reinterpret_cast(work->data); + napi_value jsNotification = JSUtil::Convert2JSNotification(eventDataInner->observer->env_, + eventDataInner->data); + napi_value callback = nullptr; + napi_value args[1] = {jsNotification}; + napi_get_reference_value(eventDataInner->observer->env_, eventDataInner->observer->callback_, &callback); + napi_value global = nullptr; + napi_get_global(eventDataInner->observer->env_, &global); + napi_value result; + napi_status callStatus = napi_call_function(eventDataInner->observer->env_, global, callback, + 1, args, &result); + if (callStatus != napi_ok) { + ZLOGE("notify data change failed callStatus:%{public}d callback:%{public}p", callStatus, callback); + } + delete eventDataInner; + eventDataInner = nullptr; + delete work; + work = nullptr; + }); } SyncObserver::SyncObserver(napi_env env, napi_value callback) : env_(env) { napi_create_reference(env, callback, 1, &callback_); + napi_get_uv_event_loop(env, &loop_); } SyncObserver::~SyncObserver() @@ -316,17 +339,31 @@ SyncObserver::~SyncObserver() void SyncObserver::SyncCompleted(const std::map &results) { - std::map dataMap; - for (const auto &[key, value] : results) { - dataMap.emplace(key, int32_t(value)); - } - napi_value notification = JSUtil::Convert2JSTupleArray(env_, dataMap); - napi_value callback = nullptr; - napi_value args[1] = {notification}; - napi_get_reference_value(env_, callback_, &callback); - napi_value global = nullptr; - napi_value result = nullptr; - napi_get_global(env_, &global); - napi_call_function(env_, global, callback, 1, args, &result); + EventDataWorker *eventDataWorker = new EventDataWorker(); + eventDataWorker->observer = this; + eventDataWorker->data = results; + uv_work_t *work = new uv_work_t; + work->data = eventDataWorker; + uv_queue_work(loop_, work, + [](uv_work_t *work) {}, + [](uv_work_t *work, int status) { + EventDataWorker *eventDataInner = reinterpret_cast(work->data); + std::map dataMap; + for (const auto &[key, value] : eventDataInner->data) { + dataMap.emplace(key, int32_t(value)); + } + napi_value notification = JSUtil::Convert2JSTupleArray(eventDataInner->observer->env_, dataMap); + napi_value callback = nullptr; + napi_value args[1] = {notification}; + napi_get_reference_value(eventDataInner->observer->env_, eventDataInner->observer->callback_, &callback); + napi_value global = nullptr; + napi_value result = nullptr; + napi_get_global(eventDataInner->observer->env_, &global); + napi_call_function(eventDataInner->observer->env_, global, callback, 1, args, &result); + delete eventDataInner; + eventDataInner = nullptr; + delete work; + work = nullptr; + }); } } diff --git a/interfaces/jskits/distributeddata/BUILD.gn b/interfaces/jskits/distributeddata/BUILD.gn index b5bcec0c7..8e35282e6 100644 --- a/interfaces/jskits/distributeddata/BUILD.gn +++ b/interfaces/jskits/distributeddata/BUILD.gn @@ -21,8 +21,8 @@ group("build_module") { js_declaration("distributeddatamgr_js") { part_name = "distributeddatamgr" sources = [ - "./api/@ohos.data.distributeddata.d.ts", "./api/@ohos.data", + "./api/@ohos.data.distributeddata.d.ts", "./api/data/distributeddata/kvmanager_config.d.ts", "./api/data/distributeddata/kvstore.d.ts", "./api/data/distributeddata/plain_ordinary_js_objects.d.ts", @@ -31,9 +31,7 @@ js_declaration("distributeddatamgr_js") { } ohos_copy("distributeddatamgr_declaration") { - sources = [ - "./api" - ] + sources = [ "./api" ] outputs = [ target_out_dir + "/$target_name/" ] module_source_dir = target_out_dir + "/$target_name" module_install_name = "" @@ -42,6 +40,7 @@ ohos_copy("distributeddatamgr_declaration") { ohos_shared_library("distributeddata") { include_dirs = [ "//third_party/node/src", + "//third_party/libuv/include", "//utils/native/base/include", "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata/include", "//foundation/distributeddatamgr/distributeddatamgr/frameworks/jskitsimpl/distributeddata/include", @@ -60,13 +59,12 @@ ohos_shared_library("distributeddata") { deps = [ "//foundation/ace/napi:ace_napi", "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata:distributeddata_inner", - "//utils/native/base:utils" + "//third_party/libuv:uv_static", + "//utils/native/base:utils", ] - external_deps = [ - "hiviewdfx_hilog_native:libhilog" - ] + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] subsystem_name = "distributeddatamgr" relative_install_dir = "module/data" -} \ No newline at end of file +} -- Gitee From 0a25ecbba3d43bb3da1b492b84d8fa0045fe2cea Mon Sep 17 00:00:00 2001 From: wangkun Date: Wed, 8 Sep 2021 10:00:21 +0800 Subject: [PATCH 2/5] mutex usage fix. Signed-off-by: wangkun --- .../distributeddataservice/app/src/kvstore_resultset_impl.cpp | 2 +- services/distributeddataservice/app/src/single_kvstore_impl.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/services/distributeddataservice/app/src/kvstore_resultset_impl.cpp b/services/distributeddataservice/app/src/kvstore_resultset_impl.cpp index dac050779..36e0bb459 100755 --- a/services/distributeddataservice/app/src/kvstore_resultset_impl.cpp +++ b/services/distributeddataservice/app/src/kvstore_resultset_impl.cpp @@ -188,7 +188,7 @@ Status KvStoreResultSetImpl::CloseResultSet(DistributedDB::KvStoreNbDelegate *kv return Status::INVALID_ARGUMENT; } DdsTrace trace(std::string(LOG_TAG "::") + std::string(__FUNCTION__)); - std::shared_lock lock(this->mutex_); + std::unique_lock lock(this->mutex_); DistributedDB::DBStatus status = kvStoreNbDelegate->CloseResultSet(kvStoreResultSet_); if (status != DistributedDB::DBStatus::OK) { return Status::DB_ERROR; diff --git a/services/distributeddataservice/app/src/single_kvstore_impl.cpp b/services/distributeddataservice/app/src/single_kvstore_impl.cpp index affe51662..c67beddd1 100755 --- a/services/distributeddataservice/app/src/single_kvstore_impl.cpp +++ b/services/distributeddataservice/app/src/single_kvstore_impl.cpp @@ -1448,6 +1448,7 @@ Status SingleKvStoreImpl::SetCapabilityRange(const std::vector &loc Status SingleKvStoreImpl::GetSecurityLevel(SecurityLevel &securityLevel) { + std::shared_lock lock(storeNbDelegateMutex_); if (kvStoreNbDelegate_ == nullptr) { return Status::STORE_NOT_OPEN; } -- Gitee From 2284298419ca7a1337cf4583ac314e692b88e667 Mon Sep 17 00:00:00 2001 From: wangkun Date: Tue, 14 Sep 2021 15:46:42 +0800 Subject: [PATCH 3/5] Delete the unused code Signed-off-by: wangkun --- .../adapter/communicator/src/softbus_adapter_standard.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp b/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp index e79d42649..21c564e14 100755 --- a/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp +++ b/services/distributeddataservice/adapter/communicator/src/softbus_adapter_standard.cpp @@ -500,7 +500,6 @@ Status SoftBusAdapter::SendData(const PipeInfo &pipeInfo, const DeviceId &device { SessionAttribute attr; attr.dataType = TYPE_BYTES; - attr.unique = true; ZLOGD("[SendData] to %{public}s ,session:%{public}s, size:%{public}d", ToBeAnonymous(deviceId.deviceId).c_str(), pipeInfo.pipeId.c_str(), size); int sessionId = OpenSession(pipeInfo.pipeId.c_str(), pipeInfo.pipeId.c_str(), @@ -545,7 +544,6 @@ bool SoftBusAdapter::IsSameStartedOnPeer(const struct PipeInfo &pipeInfo, } SessionAttribute attr; attr.dataType = TYPE_BYTES; - attr.unique = true; int sessionId = OpenSession(pipeInfo.pipeId.c_str(), pipeInfo.pipeId.c_str(), ToNodeID("", peer.deviceId).c_str(), "GROUP_ID", &attr); ZLOGI("[IsSameStartedOnPeer] sessionId=%{public}d", sessionId); @@ -765,7 +763,7 @@ void AppDataListenerWrap::OnBytesReceived(int sessionId, const void *data, unsig NotifyDataListeners(reinterpret_cast(data), dataLen, peerUuid, {std::string(peerSessionName), ""}); } -void AppDataListenerWrap::NotifyDataListeners(const uint8_t *ptr, const int size, const string &deviceId, +void AppDataListenerWrap::NotifyDataListeners(const uint8_t *ptr, const int size, const std::string &deviceId, const PipeInfo &pipeInfo) { return softBusAdapter_->NotifyDataListeners(ptr, size, deviceId, pipeInfo); -- Gitee From 7d8a950ef1996d07b97ca69c6a27d306f4c45f97 Mon Sep 17 00:00:00 2001 From: wangkun Date: Wed, 22 Sep 2021 11:16:33 +0800 Subject: [PATCH 4/5] Delete unused code Signed-off-by: wangkun --- interfaces/jskits/distributeddata/BUILD.gn | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/interfaces/jskits/distributeddata/BUILD.gn b/interfaces/jskits/distributeddata/BUILD.gn index 8e35282e6..24362422d 100644 --- a/interfaces/jskits/distributeddata/BUILD.gn +++ b/interfaces/jskits/distributeddata/BUILD.gn @@ -18,18 +18,6 @@ group("build_module") { deps = [ ":distributeddata" ] } -js_declaration("distributeddatamgr_js") { - part_name = "distributeddatamgr" - sources = [ - "./api/@ohos.data", - "./api/@ohos.data.distributeddata.d.ts", - "./api/data/distributeddata/kvmanager_config.d.ts", - "./api/data/distributeddata/kvstore.d.ts", - "./api/data/distributeddata/plain_ordinary_js_objects.d.ts", - "./api/data/distributeddata/single_kvstore.d.ts", - ] -} - ohos_copy("distributeddatamgr_declaration") { sources = [ "./api" ] outputs = [ target_out_dir + "/$target_name/" ] -- Gitee From 3b6b82eb93fdd64f43cd48c600a3000fe9f7160d Mon Sep 17 00:00:00 2001 From: wbq_sky Date: Mon, 27 Sep 2021 17:39:42 +0800 Subject: [PATCH 5/5] alther the wrong spelling Signed-off-by: wbq_sky --- .../libs/distributeddb/syncer/src/meta_data.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/distributeddataservice/libs/distributeddb/syncer/src/meta_data.cpp b/services/distributeddataservice/libs/distributeddb/syncer/src/meta_data.cpp index 753d41274..7d1da4efd 100755 --- a/services/distributeddataservice/libs/distributeddb/syncer/src/meta_data.cpp +++ b/services/distributeddataservice/libs/distributeddb/syncer/src/meta_data.cpp @@ -25,7 +25,7 @@ namespace DistributedDB { namespace { - const int STR_TO_LL_BY_DEX = 10; + const int STR_TO_LL_BY_DEC = 10; // store local timeoffset;this is a special key; const std::string LOCALTIMEOFFSET_KEY = "localTimeOffset"; const std::string DEVICEID_PREFIX_KEY = "deviceId"; @@ -240,7 +240,7 @@ void Metadata::GetMetadataFromMap(const DeviceID &deviceId, MetaDataValue &outVa int64_t Metadata::StringToLong(const std::vector &value) { std::string valueString(value.begin(), value.end()); - int64_t longData = std::strtoll(valueString.c_str(), nullptr, STR_TO_LL_BY_DEX); + int64_t longData = std::strtoll(valueString.c_str(), nullptr, STR_TO_LL_BY_DEC); LOGD("Metadata::StringToLong longData = %lld\n", longData); return longData; } @@ -309,4 +309,4 @@ void Metadata::GetHashDeviceId(const DeviceID &deviceId, DeviceID &hashDeviceId, hashDeviceId = deviceIdToHashDeviceIdMap_[deviceId]; } } -} // namespace DistributedDB \ No newline at end of file +} // namespace DistributedDB -- Gitee