1 /*
2 * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "sr_common_event_subscriber.h"
17
18 #include "common_event_support.h"
19 #include "hilog_tag_wrapper.h"
20 #include "service_router_data_mgr.h"
21 #include "want.h"
22
23 namespace OHOS {
24 namespace AbilityRuntime {
SrCommonEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)25 SrCommonEventSubscriber::SrCommonEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo)
26 : EventFwk::CommonEventSubscriber(subscribeInfo)
27 {
28 TAG_LOGD(AAFwkTag::SER_ROUTER, "created");
29 }
30
~SrCommonEventSubscriber()31 SrCommonEventSubscriber::~SrCommonEventSubscriber()
32 {
33 TAG_LOGD(AAFwkTag::SER_ROUTER, "destroyed");
34 }
35
OnReceiveEvent(const EventFwk::CommonEventData & eventData)36 void SrCommonEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
37 {
38 const AAFwk::Want& want = eventData.GetWant();
39 std::string action = want.GetAction();
40 std::string bundleName = want.GetElement().GetBundleName();
41 TAG_LOGI(AAFwkTag::SER_ROUTER, "action:%{public}s.", action.c_str());
42 if (action.empty() || eventHandler_ == nullptr) {
43 TAG_LOGE(AAFwkTag::SER_ROUTER, "Invalid action or event handler");
44 return;
45 }
46 if (bundleName.empty() && action != EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
47 TAG_LOGE(AAFwkTag::SER_ROUTER, "invalid param, bundleName: %{public}s",
48 bundleName.c_str());
49 return;
50 }
51 std::weak_ptr<SrCommonEventSubscriber> weakThis = shared_from_this();
52 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
53 int32_t userId = eventData.GetCode();
54 auto task = [weakThis, userId]() {
55 std::shared_ptr<SrCommonEventSubscriber> sharedThis = weakThis.lock();
56 if (sharedThis) {
57 TAG_LOGI(AAFwkTag::SER_ROUTER, "%{public}d COMMON_EVENT_USER_SWITCHED", userId);
58 ServiceRouterDataMgr::GetInstance().LoadAllBundleInfos();
59 }
60 };
61 eventHandler_->PostTask(task, "SrCommonEventSubscriber:LoadAllBundleInfos");
62 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED ||
63 action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) {
64 auto task = [weakThis, bundleName]() {
65 TAG_LOGI(AAFwkTag::SER_ROUTER, "bundle changed, bundleName: %{public}s", bundleName.c_str());
66 std::shared_ptr<SrCommonEventSubscriber> sharedThis = weakThis.lock();
67 if (sharedThis) {
68 ServiceRouterDataMgr::GetInstance().LoadBundleInfo(bundleName);
69 }
70 };
71 eventHandler_->PostTask(task, "SrCommonEventSubscriber:LoadBundleInfo");
72 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
73 auto task = [weakThis, bundleName]() {
74 TAG_LOGI(AAFwkTag::SER_ROUTER, "bundle remove, bundleName: %{public}s", bundleName.c_str());
75 std::shared_ptr<SrCommonEventSubscriber> sharedThis = weakThis.lock();
76 if (sharedThis) {
77 ServiceRouterDataMgr::GetInstance().DeleteBundleInfo(bundleName);
78 }
79 };
80 eventHandler_->PostTask(task, "SrCommonEventSubscriber:DeleteBundleInfo");
81 } else {
82 TAG_LOGW(AAFwkTag::SER_ROUTER, "Invalid action");
83 }
84 }
85 } // AbilityRuntime
86 } // OHOS
87