!514 【DataShare】 crash问题修改

Merge pull request !514 from lott14/master
This commit is contained in:
openharmony_ci 2022-07-28 09:17:16 +00:00 committed by Gitee
commit 73cd586e66
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 32 additions and 23 deletions

View File

@ -44,22 +44,8 @@ private:
static napi_value GetConstructor(napi_env env);
static napi_value Initialize(napi_env env, napi_callback_info info);
static napi_value RegisterWrap(napi_env env, napi_callback_info info, DSHelperOnOffCB *onCB);
static napi_value RegisterAsync(
napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, DSHelperOnOffCB *onCB);
static void RegisterExecuteCB(napi_env env, void *data);
static void RegisterCompleteCB(napi_env env, napi_status status, void *data);
static napi_value UnRegisterWrap(napi_env env, napi_callback_info info, DSHelperOnOffCB *offCB);
static napi_value UnRegisterAsync(
napi_env env, napi_value *args, size_t argc, const size_t argcPromise, DSHelperOnOffCB *offCB);
static void UnRegisterExecuteCB(napi_env env, void *data);
static void UnRegisterCompleteCB(napi_env env, napi_status status, void *data);
static void FindRegisterObs(napi_env env, DSHelperOnOffCB *data);
static void FindRegisterObsByCallBack(napi_env env, DSHelperOnOffCB *data);
std::shared_ptr<DataShareHelper> datashareHelper_ = nullptr;
std::map<std::string, sptr<AAFwk::IDataAbilityObserver>> observerMap_;
std::map<std::string, sptr<NAPIDataShareObserver>> observerMap_;
struct ContextInfo : public AsyncCall::Context {
NapiDataShareHelper *proxy = nullptr;

View File

@ -29,10 +29,10 @@ public:
NAPIDataShareObserver(napi_env env, napi_value callback);
virtual ~NAPIDataShareObserver();
void OnChange() override;
void DeleteReference();
private:
struct ObserverWorker {
const NAPIDataShareObserver *observer_ = nullptr;
napi_env env_ = nullptr;
ObserverWorker(const NAPIDataShareObserver *observerIn) : observer_(observerIn) {}
};

View File

@ -162,7 +162,17 @@ napi_value NapiDataShareHelper::Initialize(napi_env env, napi_callback_info info
g_dataShareHelperList.emplace_back(proxy->datashareHelper_);
auto finalize = [](napi_env env, void * data, void * hint) {
NapiDataShareHelper *proxy = reinterpret_cast<NapiDataShareHelper *>(data);
delete proxy;
if (proxy != nullptr) {
auto it = proxy->observerMap_.begin();
while (it != proxy->observerMap_.end()) {
if (proxy->datashareHelper_ != nullptr) {
proxy->datashareHelper_->UnregisterObserver(Uri(it->first), it->second);
}
it->second->DeleteReference();
}
proxy->observerMap_.clear();
delete proxy;
}
};
if (napi_wrap(env, self, proxy, finalize, nullptr, nullptr) != napi_ok) {
finalize(env, proxy, nullptr);
@ -684,6 +694,7 @@ napi_value NapiDataShareHelper::Napi_On(napi_env env, napi_callback_info info)
auto obs = proxy->observerMap_.find(uri);
if (obs != proxy->observerMap_.end()) {
proxy->datashareHelper_->UnregisterObserver(Uri(uri), obs->second);
obs->second->DeleteReference();
proxy->observerMap_.erase(uri);
}
proxy->datashareHelper_->RegisterObserver(Uri(uri), observer);
@ -730,6 +741,7 @@ napi_value NapiDataShareHelper::Napi_Off(napi_env env, napi_callback_info info)
auto obs = proxy->observerMap_.find(uri);
if (obs != proxy->observerMap_.end()) {
proxy->datashareHelper_->UnregisterObserver(Uri(uri), obs->second);
obs->second->DeleteReference();
proxy->observerMap_.erase(uri);
} else {
LOG_DEBUG("this uri hasn't been registered");

View File

@ -25,16 +25,15 @@ NAPIDataShareObserver::NAPIDataShareObserver(napi_env env, napi_value callback)
napi_get_uv_event_loop(env, &loop_);
}
NAPIDataShareObserver::~NAPIDataShareObserver()
{
if (ref_ != nullptr) {
napi_delete_reference(env_, ref_);
}
}
NAPIDataShareObserver::~NAPIDataShareObserver() {}
void NAPIDataShareObserver::OnChange()
{
LOG_DEBUG("OnChange called");
if (ref_ == nullptr) {
LOG_ERROR("ref_ == nullptr");
return;
}
ObserverWorker *observerWorker = new (std::nothrow)ObserverWorker(this);
if (observerWorker == nullptr) {
LOG_ERROR("Failed to create observerWorker");
@ -51,6 +50,10 @@ void NAPIDataShareObserver::OnChange()
[](uv_work_t *work, int status) {
LOG_DEBUG("uv_queue_work start");
std::shared_ptr<ObserverWorker> innerWorker(reinterpret_cast<ObserverWorker *>(work->data));
if (innerWorker->observer_->ref_ == nullptr) {
LOG_ERROR("innerWorker->observer_->ref_ is nullptr");
return;
}
napi_value callback = nullptr;
napi_value args[2] = {0};
napi_value global = nullptr;
@ -70,5 +73,13 @@ void NAPIDataShareObserver::OnChange()
delete work;
}
}
void NAPIDataShareObserver::DeleteReference()
{
if (ref_ != nullptr) {
napi_delete_reference(env_, ref_);
ref_ = nullptr;
}
}
} // namespace DataShare
} // namespace OHOS