add interface: close

Signed-off-by: wangdengze <wangdengze@huawei.com>
This commit is contained in:
wangdengze 2024-03-18 15:41:36 +08:00
parent f7683c1d84
commit e4c91208ea
5 changed files with 148 additions and 109 deletions

View File

@ -28,6 +28,7 @@ public:
static const int EXCEPTION_HELPER_UNINITIALIZED = 15700010;
static const int EXCEPTION_URI_NOT_EXIST = 15700011;
static const int EXCEPTION_DATA_AREA_NOT_EXIST = 15700012;
static const int EXCEPTION_HELPER_CLOSED = 15700013;
virtual ~Error() {};
virtual std::string GetMessage() const = 0;
virtual int GetCode() const = 0;

View File

@ -78,9 +78,9 @@ public:
int GetCode() const override;
};
class HelperNotExistError : public Error {
class HelperAlreadyClosedError : public Error {
public:
HelperNotExistError() = default;
HelperAlreadyClosedError() = default;
std::string GetMessage() const override;
int GetCode() const override;
};

View File

@ -87,14 +87,14 @@ int DataAreaNotExistError::GetCode() const
return EXCEPTION_DATA_AREA_NOT_EXIST;
}
std::string HelperNotExistError::GetMessage() const
std::string HelperAlreadyClosedError::GetMessage() const
{
return "The dataShareHelper has been closed.";
return "The DataShareHelper instance is already closed.";
}
int HelperNotExistError::GetCode() const
int HelperAlreadyClosedError::GetCode() const
{
return EXCEPTION_HELPER_UNINITIALIZED;
return EXCEPTION_HELPER_CLOSED;
}
} // namespace DataShare
} // namespace OHOS

View File

@ -23,6 +23,7 @@
#include "data_share_common.h"
#include "datashare_helper.h"
#include "napi_datashare_observer.h"
#include <shared_mutex>
namespace OHOS {
namespace DataShare {
@ -61,13 +62,18 @@ private:
static napi_value SetSilentSwitch(napi_env env, napi_callback_info info, bool enable);
bool HasRegisteredObserver(napi_env env, std::list<sptr<NAPIDataShareObserver>> &list, napi_value callback);
void RegisteredObserver(napi_env env, const std::string &uri, napi_value callback);
void UnRegisteredObserver(napi_env env, const std::string &uri, napi_value callback);
void UnRegisteredObserver(napi_env env, const std::string &uri);
void RegisteredObserver(napi_env env, const std::string &uri, napi_value callback,
std::shared_ptr<DataShareHelper> helper);
void UnRegisteredObserver(napi_env env, const std::string &uri, napi_value callback,
std::shared_ptr<DataShareHelper> helper);
void UnRegisteredObserver(napi_env env, const std::string &uri, std::shared_ptr<DataShareHelper> helper);
static bool GetOptions(napi_env env, napi_value jsValue, CreateOptions &options);
void SetHelper(std::shared_ptr<DataShareHelper> dataShareHelper);
std::shared_ptr<DataShareHelper> GetHelper();
std::shared_ptr<DataShareHelper> datashareHelper_ = nullptr;
std::map<std::string, std::list<sptr<NAPIDataShareObserver>>> observerMap_;
std::mutex listMutex_{};
std::shared_mutex mutex_;
std::shared_ptr<NapiRdbSubscriberManager> jsRdbObsManager_ = nullptr;
std::shared_ptr<NapiPublishedSubscriberManager> jsPublishedObsManager_ = nullptr;
@ -111,7 +117,6 @@ private:
std::vector<OperationResult> results;
UpdateOperations updateOperations;
std::vector<BatchUpdateResult> batchUpdateResult;
static std::mutex lockHelper_;
ContextInfo() : Context(nullptr, nullptr) {};
ContextInfo(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};

View File

@ -157,7 +157,7 @@ napi_value NapiDataShareHelper::Napi_CreateDataShareHelper(napi_env env, napi_ca
status);
proxy->jsRdbObsManager_ = std::make_shared<NapiRdbSubscriberManager>(ctxInfo->dataShareHelper);
proxy->jsPublishedObsManager_ = std::make_shared<NapiPublishedSubscriberManager>(ctxInfo->dataShareHelper);
proxy->datashareHelper_ = std::move(ctxInfo->dataShareHelper);
proxy->SetHelper(std::move(ctxInfo->dataShareHelper));
return status;
};
auto exec = [ctxInfo](AsyncCall::Context *ctx) {
@ -248,15 +248,15 @@ napi_value NapiDataShareHelper::Napi_OpenFile(napi_env env, napi_callback_info i
return napi_ok;
};
auto exec = [context](AsyncCall::Context *ctx) {
std::lock_guard<std::mutex> lock(lockHelper_);
if (context->proxy->datashareHelper_ != nullptr && !context->uri.empty()) {
auto helper = context->proxy->GetHelper();
if (helper != nullptr && !context->uri.empty()) {
OHOS::Uri uri(context->uri);
context->resultNumber = context->proxy->datashareHelper_->OpenFile(uri, context->mode);
context->resultNumber = helper->OpenFile(uri, context->mode);
context->status = napi_ok;
} else {
LOG_ERROR("dataShareHelper_ is nullptr : %{public}d, context->uri is empty : %{public}d",
context->proxy->datashareHelper_ == nullptr, context->uri.empty());
context->error = std::make_shared<HelperNotExistError>();
helper == nullptr, context->uri.empty());
context->error = std::make_shared<HelperAlreadyClosedError>();
}
};
context->SetAction(std::move(input), std::move(output));
@ -296,15 +296,15 @@ napi_value NapiDataShareHelper::Napi_Insert(napi_env env, napi_callback_info inf
return napi_ok;
};
auto exec = [context](AsyncCall::Context *ctx) {
std::lock_guard<std::mutex> lock(lockHelper_);
if (context->proxy->datashareHelper_ != nullptr && !context->uri.empty()) {
auto helper = context->proxy->GetHelper();
if (helper != nullptr && !context->uri.empty()) {
OHOS::Uri uri(context->uri);
context->resultNumber = context->proxy->datashareHelper_->Insert(uri, context->valueBucket);
context->resultNumber = helper->Insert(uri, context->valueBucket);
context->status = napi_ok;
} else {
LOG_ERROR("dataShareHelper_ is nullptr : %{public}d, context->uri is empty : %{public}d",
context->proxy->datashareHelper_ == nullptr, context->uri.empty());
context->error = std::make_shared<HelperNotExistError>();
helper == nullptr, context->uri.empty());
context->error = std::make_shared<HelperAlreadyClosedError>();
}
};
context->SetAction(std::move(input), std::move(output));
@ -341,15 +341,15 @@ napi_value NapiDataShareHelper::Napi_Delete(napi_env env, napi_callback_info inf
return napi_ok;
};
auto exec = [context](AsyncCall::Context *ctx) {
std::lock_guard<std::mutex> lock(lockHelper_);
if (context->proxy->datashareHelper_ != nullptr && !context->uri.empty()) {
auto helper = context->proxy->GetHelper();
if (helper != nullptr && !context->uri.empty()) {
OHOS::Uri uri(context->uri);
context->resultNumber = context->proxy->datashareHelper_->Delete(uri, context->predicates);
context->resultNumber = helper->Delete(uri, context->predicates);
context->status = napi_ok;
} else {
LOG_ERROR("dataShareHelper_ is nullptr : %{public}d, context->uri is empty : %{public}d",
context->proxy->datashareHelper_ == nullptr, context->uri.empty());
context->error = std::make_shared<HelperNotExistError>();
helper == nullptr, context->uri.empty());
context->error = std::make_shared<HelperAlreadyClosedError>();
}
};
context->SetAction(std::move(input), std::move(output));
@ -396,16 +396,16 @@ napi_value NapiDataShareHelper::Napi_Query(napi_env env, napi_callback_info info
return napi_ok;
};
auto exec = [context](AsyncCall::Context *ctx) {
std::lock_guard<std::mutex> lock(lockHelper_);
if (context->proxy->datashareHelper_ != nullptr && !context->uri.empty()) {
auto helper = context->proxy->GetHelper();
if (helper != nullptr && !context->uri.empty()) {
OHOS::Uri uri(context->uri);
context->resultObject = context->proxy->datashareHelper_->Query(uri,
context->predicates, context->columns, &(context->businessError));
context->resultObject = helper->Query(uri, context->predicates, context->columns,
&(context->businessError));
context->status = napi_ok;
} else {
LOG_ERROR("dataShareHelper_ is nullptr : %{public}d, context->uri is empty : %{public}d",
context->proxy->datashareHelper_ == nullptr, context->uri.empty());
context->error = std::make_shared<HelperNotExistError>();
helper == nullptr, context->uri.empty());
context->error = std::make_shared<HelperAlreadyClosedError>();
}
};
context->SetAction(std::move(input), std::move(output));
@ -449,16 +449,15 @@ napi_value NapiDataShareHelper::Napi_Update(napi_env env, napi_callback_info inf
return napi_ok;
};
auto exec = [context](AsyncCall::Context *ctx) {
std::lock_guard<std::mutex> lock(lockHelper_);
if (context->proxy->datashareHelper_ != nullptr && !context->uri.empty()) {
auto helper = context->proxy->GetHelper();
if (helper != nullptr && !context->uri.empty()) {
OHOS::Uri uri(context->uri);
context->resultNumber =
context->proxy->datashareHelper_->Update(uri, context->predicates, context->valueBucket);
context->resultNumber = helper->Update(uri, context->predicates, context->valueBucket);
context->status = napi_ok;
} else {
LOG_ERROR("dataShareHelper_ is nullptr : %{public}d, context->uri is empty : %{public}d",
context->proxy->datashareHelper_ == nullptr, context->uri.empty());
context->error = std::make_shared<HelperNotExistError>();
helper == nullptr, context->uri.empty());
context->error = std::make_shared<HelperAlreadyClosedError>();
}
};
context->SetAction(std::move(input), std::move(output));
@ -490,15 +489,14 @@ napi_value NapiDataShareHelper::Napi_BatchUpdate(napi_env env, napi_callback_inf
return napi_ok;
};
auto exec = [context](AsyncCall::Context *ctx) {
std::lock_guard<std::mutex> lock(lockHelper_);
if (context->proxy->datashareHelper_ != nullptr && !context->updateOperations.empty()) {
context->resultNumber = context->proxy->datashareHelper_->BatchUpdate(context->updateOperations,
context->batchUpdateResult);
auto helper = context->proxy->GetHelper();
if (helper != nullptr && !context->updateOperations.empty()) {
context->resultNumber = helper->BatchUpdate(context->updateOperations, context->batchUpdateResult);
context->status = napi_ok;
} else {
LOG_ERROR("dataShareHelper_ is nullptr : %{public}d, context->updateOperations is empty : %{public}d",
context->proxy->datashareHelper_ == nullptr, context->updateOperations.empty());
context->error = std::make_shared<HelperNotExistError>();
helper == nullptr, context->updateOperations.empty());
context->error = std::make_shared<HelperAlreadyClosedError>();
}
};
context->SetAction(std::move(input), std::move(output));
@ -537,15 +535,15 @@ napi_value NapiDataShareHelper::Napi_BatchInsert(napi_env env, napi_callback_inf
return napi_ok;
};
auto exec = [context](AsyncCall::Context *ctx) {
std::lock_guard<std::mutex> lock(lockHelper_);
if (context->proxy->datashareHelper_ != nullptr && !context->uri.empty()) {
auto helper = context->proxy->GetHelper();
if (helper != nullptr && !context->uri.empty()) {
OHOS::Uri uri(context->uri);
context->resultNumber = context->proxy->datashareHelper_->BatchInsert(uri, context->values);
context->resultNumber = helper->BatchInsert(uri, context->values);
context->status = napi_ok;
} else {
LOG_ERROR("dataShareHelper_ is nullptr : %{public}d, context->uri is empty : %{public}d",
context->proxy->datashareHelper_ == nullptr, context->uri.empty());
context->error = std::make_shared<HelperNotExistError>();
helper == nullptr, context->uri.empty());
context->error = std::make_shared<HelperAlreadyClosedError>();
}
};
context->SetAction(std::move(input), std::move(output));
@ -567,15 +565,15 @@ napi_value NapiDataShareHelper::Napi_GetType(napi_env env, napi_callback_info in
return napi_ok;
};
auto exec = [context](AsyncCall::Context *ctx) {
std::lock_guard<std::mutex> lock(lockHelper_);
if (context->proxy->datashareHelper_ != nullptr && !context->uri.empty()) {
auto helper = context->proxy->GetHelper();
if (helper != nullptr && !context->uri.empty()) {
OHOS::Uri uri(context->uri);
context->resultString = context->proxy->datashareHelper_->GetType(uri);
context->resultString = helper->GetType(uri);
context->status = napi_ok;
} else {
LOG_ERROR("dataShareHelper_ is nullptr : %{public}d, context->uri is empty : %{public}d",
context->proxy->datashareHelper_ == nullptr, context->uri.empty());
context->error = std::make_shared<HelperNotExistError>();
helper == nullptr, context->uri.empty());
context->error = std::make_shared<HelperAlreadyClosedError>();
}
};
context->SetAction(std::move(input), std::move(output));
@ -605,15 +603,15 @@ napi_value NapiDataShareHelper::Napi_GetFileTypes(napi_env env, napi_callback_in
return napi_ok;
};
auto exec = [context](AsyncCall::Context *ctx) {
std::lock_guard<std::mutex> lock(lockHelper_);
if (context->proxy->datashareHelper_ != nullptr && !context->uri.empty()) {
auto helper = context->proxy->GetHelper();
if (helper != nullptr && !context->uri.empty()) {
OHOS::Uri uri(context->uri);
context->resultStrArr = context->proxy->datashareHelper_->GetFileTypes(uri, context->mimeTypeFilter);
context->resultStrArr = helper->GetFileTypes(uri, context->mimeTypeFilter);
context->status = napi_ok;
} else {
LOG_ERROR("dataShareHelper_ is nullptr : %{public}d, context->uri is empty : %{public}d",
context->proxy->datashareHelper_ == nullptr, context->uri.empty());
context->error = std::make_shared<HelperNotExistError>();
helper == nullptr, context->uri.empty());
context->error = std::make_shared<HelperAlreadyClosedError>();
}
};
context->SetAction(std::move(input), std::move(output));
@ -635,16 +633,16 @@ napi_value NapiDataShareHelper::Napi_NormalizeUri(napi_env env, napi_callback_in
return napi_ok;
};
auto exec = [context](AsyncCall::Context *ctx) {
std::lock_guard<std::mutex> lock(lockHelper_);
if (context->proxy->datashareHelper_ != nullptr && !context->uri.empty()) {
auto helper = context->proxy->GetHelper();
if (helper != nullptr && !context->uri.empty()) {
OHOS::Uri uri(context->uri);
Uri uriValue = context->proxy->datashareHelper_->NormalizeUri(uri);
Uri uriValue = helper->NormalizeUri(uri);
context->resultString = uriValue.ToString();
context->status = napi_ok;
} else {
LOG_ERROR("dataShareHelper_ is nullptr : %{public}d, context->uri is empty : %{public}d",
context->proxy->datashareHelper_ == nullptr, context->uri.empty());
context->error = std::make_shared<HelperNotExistError>();
helper == nullptr, context->uri.empty());
context->error = std::make_shared<HelperAlreadyClosedError>();
}
};
context->SetAction(std::move(input), std::move(output));
@ -666,16 +664,16 @@ napi_value NapiDataShareHelper::Napi_DenormalizeUri(napi_env env, napi_callback_
return napi_ok;
};
auto exec = [context](AsyncCall::Context *ctx) {
std::lock_guard<std::mutex> lock(lockHelper_);
if (context->proxy->datashareHelper_ != nullptr && !context->uri.empty()) {
auto helper = context->proxy->GetHelper();
if (helper != nullptr && !context->uri.empty()) {
OHOS::Uri uri(context->uri);
Uri uriValue = context->proxy->datashareHelper_->DenormalizeUri(uri);
Uri uriValue = helper->DenormalizeUri(uri);
context->resultString = uriValue.ToString();
context->status = napi_ok;
} else {
LOG_ERROR("dataShareHelper_ is nullptr : %{public}d, context->uri is empty : %{public}d",
context->proxy->datashareHelper_ == nullptr, context->uri.empty());
context->error = std::make_shared<HelperNotExistError>();
helper == nullptr, context->uri.empty());
context->error = std::make_shared<HelperAlreadyClosedError>();
}
};
context->SetAction(std::move(input), std::move(output));
@ -697,15 +695,15 @@ napi_value NapiDataShareHelper::Napi_NotifyChange(napi_env env, napi_callback_in
return napi_ok;
};
auto exec = [context](AsyncCall::Context *ctx) {
std::lock_guard<std::mutex> lock(lockHelper_);
if (context->proxy->datashareHelper_ != nullptr && !context->uri.empty()) {
auto helper = context->proxy->GetHelper();
if (helper != nullptr && !context->uri.empty()) {
OHOS::Uri uri(context->uri);
context->proxy->datashareHelper_->NotifyChange(uri);
helper->NotifyChange(uri);
context->status = napi_ok;
} else {
LOG_ERROR("dataShareHelper_ is nullptr : %{public}d, context->uri is empty : %{public}d",
context->proxy->datashareHelper_ == nullptr, context->uri.empty());
context->error = std::make_shared<HelperNotExistError>();
helper == nullptr, context->uri.empty());
context->error = std::make_shared<HelperAlreadyClosedError>();
}
};
context->SetAction(std::move(input), std::move(output));
@ -726,7 +724,9 @@ napi_value NapiDataShareHelper::Napi_AddTemplate(napi_env env, napi_callback_inf
NapiDataShareHelper *proxy = nullptr;
NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast<void **>(&proxy)), nullptr);
NAPI_ASSERT_BASE(env, proxy != nullptr, "there is no NapiDataShareHelper instance", nullptr);
NAPI_ASSERT_BASE(env, proxy->datashareHelper_ != nullptr, "there is no DataShareHelper instance", nullptr);
auto helper = proxy->GetHelper();
NAPI_ASSERT_CALL_ERRCODE_SYNC(env, helper != nullptr, error = std::make_shared<HelperAlreadyClosedError>(), error,
nullptr);
napi_valuetype valueType;
NAPI_CALL(env, napi_typeof(env, argv[0], &valueType));
@ -745,7 +745,7 @@ napi_value NapiDataShareHelper::Napi_AddTemplate(napi_env env, napi_callback_inf
error = std::make_shared<ParametersTypeError>("template", "Template"), error, nullptr);
Template tpl = DataShareJSUtils::Convert2Template(env, argv[PARAM2]);
auto res = proxy->datashareHelper_->AddQueryTemplate(uri, atoll(subscriberId.c_str()), tpl);
auto res = helper->AddQueryTemplate(uri, atoll(subscriberId.c_str()), tpl);
NAPI_ASSERT_CALL_ERRCODE_SYNC(env, res != E_URI_NOT_EXIST && res != E_BUNDLE_NAME_NOT_EXIST,
error = std::make_shared<UriNotExistError>(), error, nullptr);
return DataShareJSUtils::Convert2JSValue(env, res);
@ -764,7 +764,9 @@ napi_value NapiDataShareHelper::Napi_DelTemplate(napi_env env, napi_callback_inf
NapiDataShareHelper *proxy = nullptr;
NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast<void **>(&proxy)), nullptr);
NAPI_ASSERT_BASE(env, proxy != nullptr, "there is no NapiDataShareHelper instance", nullptr);
NAPI_ASSERT_BASE(env, proxy->datashareHelper_ != nullptr, "there is no DataShareHelper instance", nullptr);
auto helper = proxy->GetHelper();
NAPI_ASSERT_CALL_ERRCODE_SYNC(env, helper != nullptr, error = std::make_shared<HelperAlreadyClosedError>(), error,
nullptr);
napi_valuetype valueType;
NAPI_CALL(env, napi_typeof(env, argv[0], &valueType));
@ -778,7 +780,7 @@ napi_value NapiDataShareHelper::Napi_DelTemplate(napi_env env, napi_callback_inf
error = std::make_shared<ParametersTypeError>("subscriberId", "string"), error, nullptr);
std::string subscriberId = DataShareJSUtils::Convert2String(env, argv[1]);
auto res = proxy->datashareHelper_->DelQueryTemplate(uri, atoll(subscriberId.c_str()));
auto res = helper->DelQueryTemplate(uri, atoll(subscriberId.c_str()));
NAPI_ASSERT_CALL_ERRCODE_SYNC(env, res != E_URI_NOT_EXIST && res != E_BUNDLE_NAME_NOT_EXIST,
error = std::make_shared<UriNotExistError>(), error, nullptr);
return DataShareJSUtils::Convert2JSValue(env, res);
@ -822,12 +824,13 @@ napi_value NapiDataShareHelper::Napi_Publish(napi_env env, napi_callback_info in
return napi_ok;
};
auto exec = [context](AsyncCall::Context *ctx) {
if (context->proxy->datashareHelper_ == nullptr) {
auto helper = context->proxy->GetHelper();
if (helper == nullptr) {
LOG_ERROR("dataShareHelper_ is nullptr");
context->error = std::make_shared<HelperAlreadyClosedError>();
return;
}
context->results =
context->proxy->datashareHelper_->Publish(context->publishData, context->bundleName);
context->results = helper->Publish(context->publishData, context->bundleName);
context->status = napi_ok;
};
context->SetAction(std::move(input), std::move(output));
@ -860,11 +863,13 @@ napi_value NapiDataShareHelper::Napi_GetPublishedData(napi_env env, napi_callbac
return napi_ok;
};
auto exec = [context](AsyncCall::Context *ctx) {
if (context->proxy->datashareHelper_ == nullptr) {
auto helper = context->proxy->GetHelper();
if (helper == nullptr) {
LOG_ERROR("dataShareHelper_ is nullptr");
context->error = std::make_shared<HelperAlreadyClosedError>();
return;
}
context->publishData = context->proxy->datashareHelper_->GetPublishedData(context->bundleName,
context->publishData = helper->GetPublishedData(context->bundleName,
context->resultNumber);
context->status = napi_ok;
};
@ -902,8 +907,10 @@ napi_value NapiDataShareHelper::Napi_On(napi_env env, napi_callback_info info)
NapiDataShareHelper *proxy = nullptr;
NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast<void **>(&proxy)), nullptr);
NAPI_ASSERT_BASE(env, proxy != nullptr, "there is no NapiDataShareHelper instance", nullptr);
NAPI_ASSERT_BASE(env, proxy->datashareHelper_ != nullptr, "there is no DataShareHelper instance", nullptr);
auto helper = proxy->GetHelper();
std::shared_ptr<Error> error = nullptr;
NAPI_ASSERT_CALL_ERRCODE_SYNC(env, helper != nullptr, error = std::make_shared<HelperAlreadyClosedError>(), error,
nullptr);
NAPI_CALL(env, napi_typeof(env, argv[1], &valueType));
NAPI_ASSERT_BASE(env, valueType == napi_string, "uri is not string", nullptr);
std::string uri = DataShareJSUtils::Convert2String(env, argv[1]);
@ -911,7 +918,7 @@ napi_value NapiDataShareHelper::Napi_On(napi_env env, napi_callback_info info)
NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valueType));
NAPI_ASSERT_BASE(env, valueType == napi_function, "callback is not a function", nullptr);
proxy->RegisteredObserver(env, uri, argv[PARAM2]);
proxy->RegisteredObserver(env, uri, argv[PARAM2], std::move(helper));
return nullptr;
}
@ -944,7 +951,10 @@ napi_value NapiDataShareHelper::Napi_Off(napi_env env, napi_callback_info info)
NapiDataShareHelper *proxy = nullptr;
NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast<void **>(&proxy)), nullptr);
NAPI_ASSERT_BASE(env, proxy != nullptr, "there is no NapiDataShareHelper instance", nullptr);
NAPI_ASSERT_BASE(env, proxy->datashareHelper_ != nullptr, "there is no DataShareHelper instance", nullptr);
auto helper = proxy->GetHelper();
std::shared_ptr<Error> error = nullptr;
NAPI_ASSERT_CALL_ERRCODE_SYNC(env, helper != nullptr, error = std::make_shared<HelperAlreadyClosedError>(), error,
nullptr);
NAPI_CALL(env, napi_typeof(env, argv[1], &valueType));
NAPI_ASSERT_BASE(env, valueType == napi_string, "uri is not string", nullptr);
@ -953,10 +963,10 @@ napi_value NapiDataShareHelper::Napi_Off(napi_env env, napi_callback_info info)
if (argc == ARGS_THREE) {
NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valueType));
NAPI_ASSERT_BASE(env, valueType == napi_function, "callback is not a function", nullptr);
proxy->UnRegisteredObserver(env, uri, argv[PARAM2]);
proxy->UnRegisteredObserver(env, uri, argv[PARAM2], std::move(helper));
return nullptr;
}
proxy->UnRegisteredObserver(env, uri);
proxy->UnRegisteredObserver(env, uri, std::move(helper));
return nullptr;
}
@ -971,17 +981,18 @@ napi_value NapiDataShareHelper::Napi_Close(napi_env env, napi_callback_info info
return napi_ok;
};
auto exec = [context](AsyncCall::Context *ctx) {
std::lock_guard<std::mutex> lock(lockHelper_);
if (context->proxy->datashareHelper_ != nullptr) {
if (context->proxy->datashareHelper_->Release()) {
context->proxy->datashareHelper_ = nullptr;
context->status = napi_ok;
} else {
context->error = std::make_shared<InnerError>();
}
} else {
LOG_WARN("The dataShareHelper has been closed.");
auto helper = context->proxy->GetHelper();
if (helper == nullptr) {
context->status = napi_ok;
return;
}
if (!helper->Release()) {
context->error = std::make_shared<InnerError>();
return;
}
context->proxy->SetHelper(nullptr);
LOG_INFO("Close dataShareHelper succeed.");
context->status = napi_ok;
};
context->SetAction(std::move(input), std::move(output));
AsyncCall asyncCall(env, info, context);
@ -1000,7 +1011,8 @@ bool NapiDataShareHelper::HasRegisteredObserver(napi_env env, std::list<sptr<NAP
return false;
}
void NapiDataShareHelper::RegisteredObserver(napi_env env, const std::string &uri, napi_value callback)
void NapiDataShareHelper::RegisteredObserver(napi_env env, const std::string &uri, napi_value callback,
std::shared_ptr<DataShareHelper> helper)
{
std::lock_guard<std::mutex> lck(listMutex_);
observerMap_.try_emplace(uri);
@ -1016,11 +1028,12 @@ void NapiDataShareHelper::RegisteredObserver(napi_env env, const std::string &ur
LOG_ERROR("observer is nullptr");
return;
}
datashareHelper_->RegisterObserver(Uri(uri), observer);
helper->RegisterObserver(Uri(uri), observer);
list.push_back(observer);
}
void NapiDataShareHelper::UnRegisteredObserver(napi_env env, const std::string &uri, napi_value callback)
void NapiDataShareHelper::UnRegisteredObserver(napi_env env, const std::string &uri, napi_value callback,
std::shared_ptr<DataShareHelper> helper)
{
std::lock_guard<std::mutex> lck(listMutex_);
auto obs = observerMap_.find(uri);
@ -1035,7 +1048,7 @@ void NapiDataShareHelper::UnRegisteredObserver(napi_env env, const std::string &
++it;
continue;
}
datashareHelper_->UnregisterObserver(Uri(uri), *it);
helper->UnregisterObserver(Uri(uri), *it);
(*it)->observer_->DeleteReference();
it = list.erase(it);
break;
@ -1045,7 +1058,8 @@ void NapiDataShareHelper::UnRegisteredObserver(napi_env env, const std::string &
}
}
void NapiDataShareHelper::UnRegisteredObserver(napi_env env, const std::string &uri)
void NapiDataShareHelper::UnRegisteredObserver(napi_env env, const std::string &uri,
std::shared_ptr<DataShareHelper> helper)
{
std::lock_guard<std::mutex> lck(listMutex_);
auto obs = observerMap_.find(uri);
@ -1056,7 +1070,7 @@ void NapiDataShareHelper::UnRegisteredObserver(napi_env env, const std::string &
auto &list = obs->second;
auto it = list.begin();
while (it != list.end()) {
datashareHelper_->UnregisterObserver(Uri(uri), *it);
helper->UnregisterObserver(Uri(uri), *it);
(*it)->observer_->DeleteReference();
it = list.erase(it);
}
@ -1074,7 +1088,9 @@ napi_value NapiDataShareHelper::Napi_SubscribeRdbObserver(napi_env env, size_t a
NapiDataShareHelper *proxy = nullptr;
NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast<void **>(&proxy)), jsResults);
NAPI_ASSERT_BASE(env, proxy != nullptr, "there is no NapiDataShareHelper instance", jsResults);
NAPI_ASSERT_BASE(env, proxy->datashareHelper_ != nullptr, "there is no DataShareHelper instance", jsResults);
auto helper = proxy->GetHelper();
NAPI_ASSERT_CALL_ERRCODE_SYNC(env, helper != nullptr, error = std::make_shared<HelperAlreadyClosedError>(), error,
jsResults);
napi_valuetype valueType;
NAPI_CALL(env, napi_typeof(env, argv[1], &valueType));
@ -1112,7 +1128,9 @@ napi_value NapiDataShareHelper::Napi_UnsubscribeRdbObserver(napi_env env, size_t
NapiDataShareHelper *proxy = nullptr;
NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast<void **>(&proxy)), jsResults);
NAPI_ASSERT_BASE(env, proxy != nullptr, "there is no NapiDataShareHelper instance", jsResults);
NAPI_ASSERT_BASE(env, proxy->datashareHelper_ != nullptr, "there is no DataShareHelper instance", jsResults);
auto helper = proxy->GetHelper();
NAPI_ASSERT_CALL_ERRCODE_SYNC(env, helper != nullptr, error = std::make_shared<HelperAlreadyClosedError>(), error,
jsResults);
napi_valuetype valueType;
NAPI_CALL(env, napi_typeof(env, argv[1], &valueType));
@ -1154,7 +1172,9 @@ napi_value NapiDataShareHelper::Napi_SubscribePublishedObserver(napi_env env, si
NapiDataShareHelper *proxy = nullptr;
NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast<void **>(&proxy)), jsResults);
NAPI_ASSERT_BASE(env, proxy != nullptr, "there is no NapiDataShareHelper instance", jsResults);
NAPI_ASSERT_BASE(env, proxy->datashareHelper_ != nullptr, "there is no DataShareHelper instance", jsResults);
auto helper = proxy->GetHelper();
NAPI_ASSERT_CALL_ERRCODE_SYNC(env, helper != nullptr, error = std::make_shared<HelperAlreadyClosedError>(), error,
jsResults);
napi_valuetype valueType;
NAPI_CALL(env, napi_typeof(env, argv[1], &valueType));
@ -1192,7 +1212,9 @@ napi_value NapiDataShareHelper::Napi_UnsubscribePublishedObserver(napi_env env,
NapiDataShareHelper *proxy = nullptr;
NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast<void **>(&proxy)), jsResults);
NAPI_ASSERT_BASE(env, proxy != nullptr, "there is no NapiDataShareHelper instance", jsResults);
NAPI_ASSERT_BASE(env, proxy->datashareHelper_ != nullptr, "there is no DataShareHelper instance", jsResults);
auto helper = proxy->GetHelper();
NAPI_ASSERT_CALL_ERRCODE_SYNC(env, helper != nullptr, error = std::make_shared<HelperAlreadyClosedError>(), error,
jsResults);
napi_valuetype valueType;
NAPI_CALL(env, napi_typeof(env, argv[1], &valueType));
@ -1260,5 +1282,16 @@ napi_value NapiDataShareHelper::SetSilentSwitch(napi_env env, napi_callback_info
AsyncCall asyncCall(env, info, context);
return asyncCall.Call(env, exec);
}
void NapiDataShareHelper::NapiDataShareHelper::SetHelper(std::shared_ptr<DataShareHelper> dataShareHelper)
{
std::unique_lock<std::shared_mutex> lock(mutex_);
datashareHelper_ = std::move(dataShareHelper);
}
std::shared_ptr<DataShareHelper> NapiDataShareHelper::GetHelper()
{
std::shared_lock<std::shared_mutex> lock(mutex_);
return datashareHelper_;
}
} // namespace DataShare
} // namespace OHOS