diff --git a/services/BUILD.gn b/services/BUILD.gn index 5a4c55fe388943f92abf5d138abc2d11b7ae973e..2734c198de0fae55124c643a56d90d5db9621791 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -36,12 +36,16 @@ config("ces_services_public_config") { include_dirs = [ "${ces_services_path}/include", + "//base/account/os_account/interfaces/innerkits/osaccount/native/include", + "//foundation/aafwk/standard/frameworks/kits/ability/native/include", "//utils/system/safwk/native/include", ] } ohos_shared_library("cesfwk_services") { sources = [ + "${ces_services_path}/src/ability_manager_death_recipient.cpp", + "${ces_services_path}/src/ability_manager_helper.cpp", "${ces_services_path}/src/bms_death_recipient.cpp", "${ces_services_path}/src/bundle_manager_helper.cpp", "${ces_services_path}/src/common_event_control_manager.cpp", @@ -53,6 +57,8 @@ ohos_shared_library("cesfwk_services") { "${ces_services_path}/src/inner_common_event_manager.cpp", "${ces_services_path}/src/ordered_event_handler.cpp", "${ces_services_path}/src/publish_manager.cpp", + "${ces_services_path}/src/static_subscriber_connection.cpp", + "${ces_services_path}/src/static_subscriber_manager.cpp", "${ces_services_path}/src/subscriber_death_recipient.cpp", "${ces_services_path}/src/system_time.cpp", ] @@ -62,19 +68,23 @@ ohos_shared_library("cesfwk_services") { public_configs = [ ":ces_services_public_config" ] deps = [ + ":static_subscriber_config.json", "${ces_common_path}:libevent_common", "${ces_core_path}:cesfwk_core", "${ces_native_path}:cesfwk_innerkits", + "//foundation/aafwk/standard/frameworks/kits/ability/native:static_subscriber_ipc", "//utils/native/base:utils", ] external_deps = [ + "aafwk_standard:ability_manager", "aafwk_standard:want", "appexecfwk_standard:appexecfwk_base", "appexecfwk_standard:appexecfwk_core", "appexecfwk_standard:libeventhandler", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", + "os_account_standard:os_account_innerkits", "safwk:system_ability_fwk", "samgr_standard:samgr_proxy", ] @@ -82,3 +92,9 @@ ohos_shared_library("cesfwk_services") { subsystem_name = "notification" part_name = "ces_standard" } + +ohos_prebuilt_etc("static_subscriber_config.json") { + source = "//base/notification/ces_standard/services/ces/resource/static_subscriber_config.json" + subsystem_name = "notification" + part_name = "ces_standard" +} diff --git a/services/ces/include/ability_manager_death_recipient.h b/services/ces/include/ability_manager_death_recipient.h new file mode 100644 index 0000000000000000000000000000000000000000..b976fd691792bbc5de9f1770adffa88ae60610ee --- /dev/null +++ b/services/ces/include/ability_manager_death_recipient.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_ABILITY_MANAGER_DEATH_RECIPIENT_H +#define FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_ABILITY_MANAGER_DEATH_RECIPIENT_H + +#include "iremote_object.h" + +namespace OHOS { +namespace EventFwk { +class AbilityManagerDeathRecipient : public IRemoteObject::DeathRecipient { +public: + void OnRemoteDied(const wptr &wptrDeath) override; +}; +} // namespace EventFwk +} // namespace OHOS + +#endif // FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_ABILITY_MANAGER_DEATH_RECIPIENT_H \ No newline at end of file diff --git a/services/ces/include/ability_manager_helper.h b/services/ces/include/ability_manager_helper.h new file mode 100644 index 0000000000000000000000000000000000000000..d2625481ccb67957e596b1cfb6d69365fdd898ce --- /dev/null +++ b/services/ces/include/ability_manager_helper.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_ABILITY_MANAGER_HELPER_H +#define FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_ABILITY_MANAGER_HELPER_H + +#include "ability_connect_callback_interface.h" +#include "ability_manager_interface.h" +#include "ability_manager_death_recipient.h" +#include "common_event_data.h" +#include "singleton.h" + +namespace OHOS { +namespace EventFwk { +class AbilityManagerHelper : public DelayedSingleton { +public: + AbilityManagerHelper() {} + + virtual ~AbilityManagerHelper() {} + + int ConnectAbility(const AAFwk::Want &want, const CommonEventData &event, const sptr &callerToken); + + void Clear(); +private: + bool GetAbilityMgrProxy(); + + std::mutex mutex_; + sptr abilityMgr_; + sptr deathRecipient_; +}; +} // namespace EventFwk +} // namespace OHOS + +#endif // FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_ABILITY_MANAGER_HELPER_H diff --git a/services/ces/include/bundle_manager_helper.h b/services/ces/include/bundle_manager_helper.h index 02b57a5216a6e55177a4ae91d4e3ec90009364f0..5a8d9fc60f62cec318eaee3b872821c31b15dd65 100644 --- a/services/ces/include/bundle_manager_helper.h +++ b/services/ces/include/bundle_manager_helper.h @@ -17,11 +17,15 @@ #define FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_BUNDLE_MANAGER_HELPER_H #include +#include + #include "bms_death_recipient.h" #include "bundle_mgr_interface.h" +#include "extension_ability_info.h" #include "iremote_object.h" #include "refbase.h" #include "singleton.h" +#include "want.h" namespace OHOS { namespace EventFwk { @@ -37,6 +41,10 @@ public: std::string GetBundleName(int uid); + bool QueryExtensionInfos(const AAFwk::Want &want, std::vector &extensionInfo); + + bool GetResConfigFile(const AppExecFwk::ExtensionAbilityInfo &extension, std::vector &profileInfos); + bool CheckPermission(const std::string &bundleName, const std::string &permission); void ClearBundleManagerHelper(); diff --git a/services/ces/include/inner_common_event_manager.h b/services/ces/include/inner_common_event_manager.h index 49b8c57ef131cc61eb7186e4c248d3097fdc91f7..63382946a639fc205ffdfb06c0ecaf0397b330e1 100644 --- a/services/ces/include/inner_common_event_manager.h +++ b/services/ces/include/inner_common_event_manager.h @@ -18,6 +18,7 @@ #include "common_event_control_manager.h" #include "icommon_event.h" +#include "static_subscriber_manager.h" namespace OHOS { namespace EventFwk { @@ -29,7 +30,7 @@ public: bool PublishCommonEvent(const CommonEventData &data, const CommonEventPublishInfo &publishinfo, const sptr &commonEventListener, const struct tm &recordTime, const pid_t &pid, const uid_t &uid, - const std::string &bundleName); + const std::string &bundleName, const sptr &service = nullptr); bool SubscribeCommonEvent(const CommonEventSubscribeInfo &subscribeInfo, const sptr &commonEventListener, const struct tm &recordTime, const pid_t &pid, const uid_t &uid, @@ -50,9 +51,11 @@ public: private: bool ProcessStickyEvent(const CommonEventRecord &record); + void PublishEventToStaticSubscribers(const CommonEventData &data, const sptr &service); private: std::shared_ptr controlPtr_; + std::shared_ptr staticSubscriberManager_; DISALLOW_COPY_AND_MOVE(InnerCommonEventManager); }; } // namespace EventFwk diff --git a/services/ces/include/static_subscriber_connection.h b/services/ces/include/static_subscriber_connection.h new file mode 100644 index 0000000000000000000000000000000000000000..14258986a55fcdf808de31c23c1f256217e13155 --- /dev/null +++ b/services/ces/include/static_subscriber_connection.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_STATIC_SUBSCRIBER_CONNECTION_H +#define FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_STATIC_SUBSCRIBER_CONNECTION_H + +#include "ability_connect_callback_stub.h" +#include "common_event_data.h" +#include "static_subscriber_proxy.h" + +namespace OHOS { +namespace EventFwk { +class StaticSubscriberConnection : public AAFwk::AbilityConnectionStub { +public: + explicit StaticSubscriberConnection(const CommonEventData& event) : event_(event) {} + void OnAbilityConnectDone( + const AppExecFwk::ElementName &element, const sptr &remoteObject, int resultCode) override; + + void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override; +private: + sptr proxy_ = nullptr; + CommonEventData event_; +}; +} // namespace EventFwk +} // namespace OHOS +#endif // FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_STATIC_SUBSCRIBER_CONNECTION_H diff --git a/services/ces/include/static_subscriber_manager.h b/services/ces/include/static_subscriber_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..a104a784cab1026ba9341152fed20e3d9e0ad8d9 --- /dev/null +++ b/services/ces/include/static_subscriber_manager.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_STATIC_SUBSCRIBER_MANAGER_H +#define FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_STATIC_SUBSCRIBER_MANAGER_H + +#include +#include + +#include "singleton.h" + +namespace OHOS { +namespace EventFwk { +class StaticSubscriberManager : public DelayedSingleton { +public: + StaticSubscriberManager(); + + virtual ~StaticSubscriberManager() override; + + /** + * Check whether the app is static subscriber. + * @param bundleName the bundleName of a app. + * @return whether the app is static subscriber or not. + */ + bool IsStaticSubscriber(const std::string &bundleName); + +private: + bool Init(); + + std::vector subscriberList_; + bool isInit_ = false; +}; +} // namespace EventFwk +} // namespace OHOS + +#endif // FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_STATIC_SUBSCRIBER_MANAGER_H diff --git a/services/ces/include/subscriber_death_recipient.h b/services/ces/include/subscriber_death_recipient.h index b62fcb2672263b91876df85bfc0b91fe3e9f8971..70d49730a6e95699ff3fc8884840f409035cb6a7 100644 --- a/services/ces/include/subscriber_death_recipient.h +++ b/services/ces/include/subscriber_death_recipient.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/services/ces/resource/static_subscriber_config.json b/services/ces/resource/static_subscriber_config.json new file mode 100644 index 0000000000000000000000000000000000000000..f151ea5a19009dc2cd7e646e510071c222e6ba98 --- /dev/null +++ b/services/ces/resource/static_subscriber_config.json @@ -0,0 +1,7 @@ +{ + "apps": [ + "com.ohos.launcher", + "com.ohos.systemui", + "com.example.staticsubscriberapplication" + ] +} \ No newline at end of file diff --git a/services/ces/src/ability_manager_death_recipient.cpp b/services/ces/src/ability_manager_death_recipient.cpp new file mode 100644 index 0000000000000000000000000000000000000000..563c23c08186b026ba9fcd21d66887f108964353 --- /dev/null +++ b/services/ces/src/ability_manager_death_recipient.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ability_manager_death_recipient.h" +#include "ability_manager_helper.h" +#include "event_log_wrapper.h" +#include "singleton.h" + +namespace OHOS { +namespace EventFwk { +void AbilityManagerDeathRecipient::OnRemoteDied(const wptr &wptrDeath) +{ + EVENT_LOGI("ability manager service died, remove the proxy object"); + + if (wptrDeath == nullptr) { + EVENT_LOGE("wptrDeath is null"); + return; + } + + sptr object = wptrDeath.promote(); + if (object == nullptr) { + EVENT_LOGE("object is null"); + return; + } + + DelayedSingleton::GetInstance()->Clear(); + + return; +} +} // namespace EventFwk +} // namespace OHOS \ No newline at end of file diff --git a/services/ces/src/ability_manager_helper.cpp b/services/ces/src/ability_manager_helper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c62f1059a36a487cf76cc5227d0a28bc557f806 --- /dev/null +++ b/services/ces/src/ability_manager_helper.cpp @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ability_manager_helper.h" + +#include "event_log_wrapper.h" +#include "iservice_registry.h" +#include "static_subscriber_connection.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace EventFwk { +int AbilityManagerHelper::ConnectAbility( + const Want &want, const CommonEventData &event, const sptr &callerToken) +{ + EVENT_LOGI("enter, target bundle = %{public}s", want.GetBundle().c_str()); + std::lock_guard lock(mutex_); + + if (!GetAbilityMgrProxy()) { + EVENT_LOGE("failed to get ability manager proxy!"); + return -1; + } + + sptr connection(new (std::nothrow) StaticSubscriberConnection(event)); + return abilityMgr_->ConnectAbility(want, connection, callerToken); +} + +bool AbilityManagerHelper::GetAbilityMgrProxy() +{ + EVENT_LOGI("enter"); + if (abilityMgr_ == nullptr) { + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityManager == nullptr) { + EVENT_LOGE("Failed to get system ability mgr."); + return false; + } + + sptr remoteObject = systemAbilityManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID); + if (remoteObject == nullptr) { + EVENT_LOGE("Failed to get ability manager service."); + return false; + } + + abilityMgr_ = iface_cast(remoteObject); + if ((abilityMgr_ == nullptr) || (abilityMgr_->AsObject() == nullptr)) { + EVENT_LOGE("Failed to get system ability manager services ability"); + return false; + } + + deathRecipient_ = (new (std::nothrow) AbilityManagerDeathRecipient()); + if (deathRecipient_ == nullptr) { + EVENT_LOGE("Failed to create AbilityManagerDeathRecipient"); + } + if (!abilityMgr_->AsObject()->AddDeathRecipient(deathRecipient_)) { + EVENT_LOGW("Failed to add AbilityManagerDeathRecipient"); + } + } + + return true; +} + +void AbilityManagerHelper::Clear() +{ + EVENT_LOGI("enter"); + std::lock_guard lock(mutex_); + + if ((abilityMgr_ != nullptr) && (abilityMgr_->AsObject() != nullptr)) { + abilityMgr_->AsObject()->RemoveDeathRecipient(deathRecipient_); + } + abilityMgr_ = nullptr; +} +} // namespace EventFwk +} // namespace OHOS diff --git a/services/ces/src/bundle_manager_helper.cpp b/services/ces/src/bundle_manager_helper.cpp index d2d4d4bb45e7693d453bfa3c3e51f80a5b67e9b6..c21591b3bb9e115929d1391b43df0521a0136a13 100644 --- a/services/ces/src/bundle_manager_helper.cpp +++ b/services/ces/src/bundle_manager_helper.cpp @@ -14,13 +14,19 @@ */ #include "bundle_manager_helper.h" + #include "bundle_constants.h" +#include "bundle_mgr_client.h" #include "event_log_wrapper.h" #include "iservice_registry.h" +#include "nlohmann/json.hpp" +#include "os_account_manager.h" #include "system_ability_definition.h" namespace OHOS { namespace EventFwk { +const std::string META_NAME_STATIC_SUBSCRIBER = "ohos.extension.staticSubscriber"; + using namespace OHOS::AppExecFwk::Constants; BundleManagerHelper::BundleManagerHelper() : sptrBundleMgr_(nullptr), bmsDeath_(nullptr) @@ -45,6 +51,54 @@ std::string BundleManagerHelper::GetBundleName(int uid) return bundleName; } +bool BundleManagerHelper::QueryExtensionInfos(const AAFwk::Want &want, + std::vector &extensionInfos) +{ + EVENT_LOGI("enter"); + + std::lock_guard lock(mutex_); + + if (!GetBundleMgrProxy()) { + return false; + } + // get first active user account, this might be modified later. + std::vector osAccountInfos; + ErrCode ret = AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos); + if (ret != ERR_OK) { + EVENT_LOGE("failed to QueryAllCreatedOsAccounts!"); + return false; + } + if (osAccountInfos.size() == 0) { + EVENT_LOGE("no os account acquired!"); + return false; + } + int userId = 100; // 100 : default user id + for (auto info : osAccountInfos) { + if (info.GetIsActived()) { + userId = info.GetLocalId(); + EVENT_LOGE("active userId = %{public}d", userId); + break; + } + } + return sptrBundleMgr_->QueryExtensionAbilityInfos(want, AppExecFwk::ExtensionAbilityType::STATICSUBSCRIBER, + 0, userId, extensionInfos); +} + +bool BundleManagerHelper::GetResConfigFile(const AppExecFwk::ExtensionAbilityInfo &extension, + std::vector &profileInfos) +{ + EVENT_LOGI("enter"); + + std::lock_guard lock(mutex_); + + if (!GetBundleMgrProxy()) { + return false; + } + + AppExecFwk::BundleMgrClient client; + return client.GetResConfigFile(extension, META_NAME_STATIC_SUBSCRIBER, profileInfos); +} + bool BundleManagerHelper::CheckIsSystemAppByUid(uid_t uid) { EVENT_LOGI("enter"); @@ -110,8 +164,7 @@ bool BundleManagerHelper::GetBundleMgrProxy() return false; } if (!sptrBundleMgr_->AsObject()->AddDeathRecipient(bmsDeath_)) { - EVENT_LOGE("Failed to add death recipient"); - return false; + EVENT_LOGW("Failed to add death recipient"); } } diff --git a/services/ces/src/common_event_manager_service.cpp b/services/ces/src/common_event_manager_service.cpp index 45f02d8d4c4509628563f3de59532c1b9bc08b34..e04e1ab7829815b4975b73f1398664c63c15e25d 100644 --- a/services/ces/src/common_event_manager_service.cpp +++ b/services/ces/src/common_event_manager_service.cpp @@ -14,6 +14,7 @@ */ #include "common_event_manager_service.h" + #include "bundle_manager_helper.h" #include "datetime_ex.h" #include "event_log_wrapper.h" @@ -181,7 +182,8 @@ bool CommonEventManagerService::PublishCommonEventDetailed(const CommonEventData recordTime, pid, uid, - bundleName); + bundleName, + this); return handler_->PostTask(PublishCommonEventFunc); } diff --git a/services/ces/src/inner_common_event_manager.cpp b/services/ces/src/inner_common_event_manager.cpp index 12d36b797387312c7c6e8559cba2884a9d4dbedc..51541e688a83f94dc3b1aed634ce4fd44e94e200 100644 --- a/services/ces/src/inner_common_event_manager.cpp +++ b/services/ces/src/inner_common_event_manager.cpp @@ -15,23 +15,29 @@ #include "inner_common_event_manager.h" +#include "ability_manager_helper.h" #include "bundle_manager_helper.h" #include "common_event_sticky_manager.h" #include "common_event_subscriber_manager.h" #include "common_event_support.h" #include "common_event_support_mapper.h" #include "event_log_wrapper.h" +#include "nlohmann/json.hpp" #include "system_time.h" #include "want.h" namespace OHOS { namespace EventFwk { -InnerCommonEventManager::InnerCommonEventManager() : controlPtr_(std::make_shared()) +constexpr static char JSON_KEY_COMMON_EVENTS[] = "commonEvents"; +constexpr static char JSON_KEY_EVENTS[] = "events"; + +InnerCommonEventManager::InnerCommonEventManager() : controlPtr_(std::make_shared()), + staticSubscriberManager_(std::make_shared()) {} bool InnerCommonEventManager::PublishCommonEvent(const CommonEventData &data, const CommonEventPublishInfo &publishInfo, const sptr &commonEventListener, const struct tm &recordTime, const pid_t &pid, const uid_t &uid, - const std::string &bundleName) + const std::string &bundleName, const sptr &service) { EVENT_LOGI("enter %{public}s(pid = %{public}d, uid = %{public}d), event = %{public}s", bundleName.c_str(), pid, uid, data.GetWant().GetAction().c_str()); @@ -41,6 +47,8 @@ bool InnerCommonEventManager::PublishCommonEvent(const CommonEventData &data, co return false; } + PublishEventToStaticSubscribers(data, service); + if ((!publishInfo.IsOrdered()) && (commonEventListener != nullptr)) { EVENT_LOGE("When publishing unordered events, the subscriber object is not required."); return false; @@ -93,6 +101,52 @@ bool InnerCommonEventManager::PublishCommonEvent(const CommonEventData &data, co return true; } +void InnerCommonEventManager::PublishEventToStaticSubscribers(const CommonEventData &data, + const sptr &service) +{ + EVENT_LOGI("enter"); + + if (staticSubscriberManager_ == nullptr) { + EVENT_LOGE("staticSubscriberManager_ == nullptr!"); + return; + } + + Want want; + std::vector extensions; + // get all static subscriber type extensions + if (!DelayedSingleton::GetInstance()->QueryExtensionInfos(want, extensions)) { + EVENT_LOGE("QueryExtensionByWant failed"); + return; + } + // filter legel extensions and connect them + for (auto extension : extensions) { + if (!staticSubscriberManager_->IsStaticSubscriber(extension.bundleName)) { + continue; + } + EVENT_LOGI("find legel extension,bundlename = %{public}s", extension.bundleName.c_str()); + std::vector profileInfos; + if (!DelayedSingleton::GetInstance()->GetResConfigFile(extension, profileInfos)) { + EVENT_LOGE("GetProfile failed"); + return; + } + + for (auto profile : profileInfos) { + nlohmann::json jsonObj = nlohmann::json::parse(profile, nullptr, false); + int size = jsonObj[JSON_KEY_COMMON_EVENTS][JSON_KEY_EVENTS].size(); + for (int i = 0; i < size; i++) { + if (jsonObj[JSON_KEY_COMMON_EVENTS][JSON_KEY_EVENTS][i].get() == + data.GetWant().GetAction()) { + want.SetElementName(extension.bundleName, extension.moduleName); + EVENT_LOGI("Ready to connect to extension %{public}s in bundle %{public}s", + extension.moduleName.c_str(), extension.bundleName.c_str()); + DelayedSingleton::GetInstance()->ConnectAbility(want, data, service); + break; + } + } + } + } +} + bool InnerCommonEventManager::SubscribeCommonEvent(const CommonEventSubscribeInfo &subscribeInfo, const sptr &commonEventListener, const struct tm &recordTime, const pid_t &pid, const uid_t &uid, const std::string &bundleName) diff --git a/services/ces/src/static_subscriber_connection.cpp b/services/ces/src/static_subscriber_connection.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34b332fdcfc72c7eccae59f0bad51c20e04b162c --- /dev/null +++ b/services/ces/src/static_subscriber_connection.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "static_subscriber_connection.h" + +#include "event_log_wrapper.h" + +namespace OHOS { +namespace EventFwk { +void StaticSubscriberConnection::OnAbilityConnectDone( + const AppExecFwk::ElementName &element, const sptr &remoteObject, int resultCode) +{ + EVENT_LOGI("enter"); + proxy_ = (new (std::nothrow) AppExecFwk::StaticSubscriberProxy(remoteObject)); + ErrCode ec = proxy_->OnCommonEventTriggered(&event_); + EVENT_LOGI("end, errorCode = %d", ec); +} + +void StaticSubscriberConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) +{ + EVENT_LOGI("enter"); + EVENT_LOGI("end"); +} +} // namespace EventFwk +} // namespace OHOS diff --git a/services/ces/src/static_subscriber_manager.cpp b/services/ces/src/static_subscriber_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..16f1737fc9f7172141033325ad3eaadfba145aeb --- /dev/null +++ b/services/ces/src/static_subscriber_manager.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "static_subscriber_manager.h" + +#include +#include +#include "nlohmann/json.hpp" + +#include "event_log_wrapper.h" + +namespace OHOS { +namespace EventFwk { +const std::string STATIC_SUBSCRIBER_CONFIG_FILE = "/system/etc/static_subscriber_config.json"; + +const std::string CONFIG_APPS = "apps"; + +StaticSubscriberManager::StaticSubscriberManager() {} + +StaticSubscriberManager::~StaticSubscriberManager() {} + +bool StaticSubscriberManager::Init() +{ + EVENT_LOGI("enter"); + + nlohmann::json jsonObj; + std::ifstream jfile(STATIC_SUBSCRIBER_CONFIG_FILE); + if (!jfile.is_open()) { + EVENT_LOGI("json file can not open"); + isInit_ = false; + return false; + } + jfile >> jsonObj; + jfile.close(); + + int size = jsonObj[CONFIG_APPS].size(); + for (int i = 0; i < size; i++) { + subscriberList_.emplace_back(jsonObj[CONFIG_APPS][i].get()); + } + isInit_ = true; + return true; +} + +bool StaticSubscriberManager::IsStaticSubscriber(const std::string &bundleName) +{ + EVENT_LOGI("enter"); + if (!isInit_ && !Init()) { + EVENT_LOGE("failed to init subscriber list"); + return false; + } + std::vector::iterator iter = subscriberList_.begin(); + iter = find(subscriberList_.begin(), subscriberList_.end(), bundleName); + if (iter != subscriberList_.end()) { + return true; + } + return false; +} +} // namespace EventFwk +} // namespace OHOS diff --git a/services/test/unittest/common_event_dump_test.cpp b/services/test/unittest/common_event_dump_test.cpp index 5f62875e8bac92d5bd444b2fefb2979877bfc499..ab66cd12b364ea446155e0d25ed0d32f4ee2d9de 100644 --- a/services/test/unittest/common_event_dump_test.cpp +++ b/services/test/unittest/common_event_dump_test.cpp @@ -377,7 +377,8 @@ bool CommonEventDumpTest::PublishCommonEvent(const CommonEventData &data, const recordTime, callingPid, callingUid, - bundleName); + bundleName, + nullptr); return handler_->PostTask(PublishCommonEventFunc); } diff --git a/services/test/unittest/common_event_freeze_test.cpp b/services/test/unittest/common_event_freeze_test.cpp index e4e4d3326a3998aefc442767813c0b5ff6436e1b..f39c0645c614dc4a881e8a3412a1ad93491c8274 100755 --- a/services/test/unittest/common_event_freeze_test.cpp +++ b/services/test/unittest/common_event_freeze_test.cpp @@ -336,7 +336,8 @@ bool CommonEventFreezeTest::PublishCommonEvent(const CommonEventData &data, cons recordTime, callingPid, callingUid, - bundleName); + bundleName, + nullptr); return handler_->PostTask(PublishCommonEventFunc); }