mirror of
https://gitee.com/openharmony/notification_common_event_service
synced 2024-11-23 07:59:56 +00:00
!1155 为disableEvent加锁以及解决lambda捕获this指针
Merge pull request !1155 from CheerfulRicky/refactor
This commit is contained in:
commit
7d19fcb458
@ -22,6 +22,7 @@
|
||||
#include "native_engine/native_value.h"
|
||||
#include "runtime.h"
|
||||
#include "static_subscriber_extension.h"
|
||||
#include <memory>
|
||||
|
||||
namespace OHOS {
|
||||
namespace EventFwk {
|
||||
@ -55,6 +56,8 @@ public:
|
||||
|
||||
void ExecNapiWrap(napi_env env, napi_value obj);
|
||||
|
||||
std::weak_ptr<JsStaticSubscriberExtension> GetWeakPtr();
|
||||
|
||||
private:
|
||||
AbilityRuntime::JsRuntime& jsRuntime_;
|
||||
std::unique_ptr<NativeReference> jsObj_;
|
||||
|
@ -26,6 +26,7 @@
|
||||
#include "napi/native_node_api.h"
|
||||
#include "napi_remote_object.h"
|
||||
#include "static_subscriber_stub_impl.h"
|
||||
#include <memory>
|
||||
|
||||
namespace OHOS {
|
||||
namespace EventFwk {
|
||||
@ -194,6 +195,11 @@ void JsStaticSubscriberExtension::OnDisconnect(const AAFwk::Want& want)
|
||||
Extension::OnDisconnect(want);
|
||||
}
|
||||
|
||||
std::weak_ptr<JsStaticSubscriberExtension> JsStaticSubscriberExtension::GetWeakPtr()
|
||||
{
|
||||
return std::static_pointer_cast<JsStaticSubscriberExtension>(shared_from_this());
|
||||
}
|
||||
|
||||
void JsStaticSubscriberExtension::OnReceiveEvent(std::shared_ptr<CommonEventData> data)
|
||||
{
|
||||
EVENT_LOGD("%{public}s called.", __func__);
|
||||
@ -201,20 +207,24 @@ void JsStaticSubscriberExtension::OnReceiveEvent(std::shared_ptr<CommonEventData
|
||||
EVENT_LOGE("handler is invalid");
|
||||
return;
|
||||
}
|
||||
std::weak_ptr<JsStaticSubscriberExtension> wThis = GetWeakPtr();
|
||||
|
||||
auto task = [this, data]() {
|
||||
auto task = [wThis, data]() {
|
||||
std::shared_ptr<JsStaticSubscriberExtension> sThis = wThis.lock();
|
||||
if (sThis == nullptr) {
|
||||
return;
|
||||
}
|
||||
if (data == nullptr) {
|
||||
EVENT_LOGE("OnReceiveEvent common event data is invalid");
|
||||
return;
|
||||
}
|
||||
StaticSubscriberExtension::OnReceiveEvent(data);
|
||||
if (!jsObj_) {
|
||||
if (!sThis->jsObj_) {
|
||||
EVENT_LOGE("Not found StaticSubscriberExtension.js");
|
||||
return;
|
||||
}
|
||||
|
||||
AbilityRuntime::HandleScope handleScope(jsRuntime_);
|
||||
napi_env env = jsRuntime_.GetNapiEnv();
|
||||
AbilityRuntime::HandleScope handleScope(sThis->jsRuntime_);
|
||||
napi_env env = sThis->jsRuntime_.GetNapiEnv();
|
||||
napi_value commonEventData = nullptr;
|
||||
napi_create_object(env, &commonEventData);
|
||||
Want want = data->GetWant();
|
||||
@ -236,7 +246,7 @@ void JsStaticSubscriberExtension::OnReceiveEvent(std::shared_ptr<CommonEventData
|
||||
napi_set_named_property(env, commonEventData, "parameters", napiParams);
|
||||
|
||||
napi_value argv[] = {commonEventData};
|
||||
napi_value obj = jsObj_->GetNapiValue();
|
||||
napi_value obj = sThis->jsObj_->GetNapiValue();
|
||||
if (obj == nullptr) {
|
||||
EVENT_LOGE("Failed to get StaticSubscriberExtension object");
|
||||
return;
|
||||
|
@ -116,6 +116,7 @@ private:
|
||||
bool hasInitAllowList_ = false;
|
||||
bool hasInitValidSubscribers_ = false;
|
||||
std::mutex subscriberMutex_;
|
||||
std::mutex disableEventsMutex_;
|
||||
};
|
||||
} // namespace EventFwk
|
||||
} // namespace OHOS
|
||||
|
@ -16,6 +16,7 @@
|
||||
#include "static_subscriber_manager.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <mutex>
|
||||
#include <set>
|
||||
|
||||
#include "ability_manager_helper.h"
|
||||
@ -82,6 +83,7 @@ bool StaticSubscriberManager::InitValidSubscribers()
|
||||
if (!validSubscribers_.empty()) {
|
||||
validSubscribers_.clear();
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(disableEventsMutex_);
|
||||
if (!disableEvents_.empty()) {
|
||||
disableEvents_.clear();
|
||||
}
|
||||
@ -129,6 +131,7 @@ bool StaticSubscriberManager::InitValidSubscribers()
|
||||
bool StaticSubscriberManager::IsDisableEvent(const std::string &bundleName, const std::string &event)
|
||||
{
|
||||
EVENT_LOGD("Called.");
|
||||
std::lock_guard<std::mutex> lock(disableEventsMutex_);
|
||||
auto bundleIt = disableEvents_.find(bundleName);
|
||||
if (bundleIt == disableEvents_.end()) {
|
||||
return false;
|
||||
@ -387,7 +390,7 @@ void StaticSubscriberManager::AddSubscriberWithBundleName(const std::string &bun
|
||||
void StaticSubscriberManager::RemoveSubscriberWithBundleName(const std::string &bundleName, const int32_t &userId)
|
||||
{
|
||||
EVENT_LOGD("enter, bundleName = %{public}s, userId = %{public}d", bundleName.c_str(), userId);
|
||||
|
||||
std::lock_guard<std::mutex> lock(disableEventsMutex_);
|
||||
for (auto it = validSubscribers_.begin(); it != validSubscribers_.end();) {
|
||||
auto subIt = it->second.begin();
|
||||
while (subIt != it->second.end()) {
|
||||
@ -487,6 +490,7 @@ int32_t StaticSubscriberManager::UpdateDisableEvents(
|
||||
const std::string &bundleName, const std::vector<std::string> &events, bool enable)
|
||||
{
|
||||
EVENT_LOGD("Called.");
|
||||
std::lock_guard<std::mutex> lock(disableEventsMutex_);
|
||||
auto finder = disableEvents_.find(bundleName);
|
||||
if (finder == disableEvents_.end()) {
|
||||
if (!enable) {
|
||||
|
Loading…
Reference in New Issue
Block a user