1 /*
2 * Copyright (c) 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 "dm_package_common_event.h"
17
18 #include <pthread.h>
19 #include <thread>
20
21 #include "common_event_support.h"
22 #include "dm_anonymous.h"
23 #include "dm_constants.h"
24 #include "dm_log.h"
25 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
26 #include "ffrt.h"
27 #endif
28 #include "iservice_registry.h"
29 #include "system_ability_definition.h"
30
31 namespace OHOS {
32 namespace DistributedHardware {
33 using OHOS::EventFwk::MatchingSkills;
34 using OHOS::EventFwk::CommonEventManager;
35 #if (defined(__LITEOS_M__) || defined(LITE_DEVICE))
36 constexpr const char* DEAL_THREAD = "package_common_event";
37 #endif
38 const std::string APP_ID = "appId";
39 constexpr int32_t MAX_TRY_TIMES = 3;
40
GetSubscriberEventNameVec() const41 std::vector<std::string> DmPackageEventSubscriber::GetSubscriberEventNameVec() const
42 {
43 return eventNameVec_;
44 }
45
~DmPackageCommonEventManager()46 DmPackageCommonEventManager::~DmPackageCommonEventManager()
47 {
48 DmPackageCommonEventManager::UnsubscribePackageCommonEvent();
49 }
50
SubscribePackageCommonEvent(const std::vector<std::string> & eventNameVec,const PackageEventCallback & callback)51 bool DmPackageCommonEventManager::SubscribePackageCommonEvent(const std::vector<std::string> &eventNameVec,
52 const PackageEventCallback &callback)
53 {
54 if (eventNameVec.empty() || callback == nullptr) {
55 LOGE("eventNameVec is empty or callback is nullptr.");
56 return false;
57 }
58 std::lock_guard<std::mutex> locker(evenSubscriberMutex_);
59 if (eventValidFlag_) {
60 LOGE("failed to subscribe package commom eventName size: %{public}zu", eventNameVec.size());
61 return false;
62 }
63
64 MatchingSkills matchingSkills;
65 for (auto &item : eventNameVec) {
66 matchingSkills.AddEvent(item);
67 }
68 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
69 subscriber_ = std::make_shared<DmPackageEventSubscriber>(subscriberInfo, callback, eventNameVec);
70 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
71 if (samgrProxy == nullptr) {
72 LOGE("samgrProxy is nullptr");
73 subscriber_ = nullptr;
74 return false;
75 }
76 statusChangeListener_ = new (std::nothrow) SystemAbilityStatusChangeListener(subscriber_);
77 if (statusChangeListener_ == nullptr) {
78 LOGE("statusChangeListener_ is nullptr");
79 subscriber_ = nullptr;
80 return false;
81 }
82 while (counter_ != MAX_TRY_TIMES) {
83 if (samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_) == ERR_OK) {
84 LOGI("SubscribePackageEvent success.");
85 counter_ = 0;
86 break;
87 }
88 if (++counter_ == MAX_TRY_TIMES) {
89 LOGI("SubscribePackageEvent failed.");
90 }
91 sleep(1);
92 }
93 eventNameVec_ = eventNameVec;
94 eventValidFlag_ = true;
95 LOGI("success to subscribe package commom event name size: %{public}zu", eventNameVec.size());
96 return true;
97 }
98
UnsubscribePackageCommonEvent()99 bool DmPackageCommonEventManager::UnsubscribePackageCommonEvent()
100 {
101 std::lock_guard<std::mutex> locker(evenSubscriberMutex_);
102 if (!eventValidFlag_) {
103 LOGE("failed to unsubscribe package commom event name size: %{public}zu because event is invalid.",
104 eventNameVec_.size());
105 return false;
106 }
107 if (subscriber_ != nullptr) {
108 LOGI("start to unsubscribe package commom event name size: %{public}zu", eventNameVec_.size());
109 if (!CommonEventManager::UnSubscribeCommonEvent(subscriber_)) {
110 LOGE("failed to unsubscribe package commom event name size: %{public}zu", eventNameVec_.size());
111 return false;
112 }
113 LOGI("success to unsubscribe package commom event name size: %{public}zu", eventNameVec_.size());
114 subscriber_ = nullptr;
115 }
116 if (statusChangeListener_ != nullptr) {
117 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
118 if (samgrProxy == nullptr) {
119 LOGE("samgrProxy is nullptr");
120 return false;
121 }
122 int32_t ret = samgrProxy->UnSubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_);
123 if (ret != ERR_OK) {
124 LOGE("failed to unsubscribe system ability COMMON_EVENT_SERVICE_ID ret:%{public}d", ret);
125 return false;
126 }
127 statusChangeListener_ = nullptr;
128 }
129
130 LOGI("success to unsubscribe package commom event name size: %{public}zu", eventNameVec_.size());
131 eventValidFlag_ = false;
132 return true;
133 }
134
OnReceiveEvent(const CommonEventData & data)135 void DmPackageEventSubscriber::OnReceiveEvent(const CommonEventData &data)
136 {
137 std::string receiveEvent = data.GetWant().GetAction();
138 std::string appId = data.GetWant().GetStringParam(APP_ID);
139 LOGI("Received package event: %{public}s", receiveEvent.c_str());
140
141 if (receiveEvent != EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_FULLY_REMOVED &&
142 receiveEvent != EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
143 LOGE("Invalied package type event.");
144 return;
145 }
146 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
147 ffrt::submit([=]() { callback_(appId, receiveEvent); });
148 #else
149 std::thread dealThread([=]() { callback_(appId, receiveEvent); });
150 int32_t ret = pthread_setname_np(dealThread.native_handle(), DEAL_THREAD);
151 if (ret != DM_OK) {
152 LOGE("dealThread setname failed.");
153 }
154 dealThread.detach();
155 #endif
156 }
157
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)158 void DmPackageCommonEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility(
159 int32_t systemAbilityId, const std::string& deviceId)
160 {
161 LOGI("systemAbility is added with said: %{public}d.", systemAbilityId);
162 if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
163 return;
164 }
165 if (changeSubscriber_ == nullptr) {
166 LOGE("failed to subscribe package commom event because changeSubscriber_ is nullptr.");
167 return;
168 }
169 std::vector<std::string> eventNameVec = changeSubscriber_->GetSubscriberEventNameVec();
170 LOGI("start to subscribe package commom eventName: %{public}zu", eventNameVec.size());
171 if (!CommonEventManager::SubscribeCommonEvent(changeSubscriber_)) {
172 LOGE("failed to subscribe package commom event: %{public}zu", eventNameVec.size());
173 }
174 }
175
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)176 void DmPackageCommonEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
177 int32_t systemAbilityId, const std::string& deviceId)
178 {
179 LOGI("systemAbility is removed with said: %{public}d.", systemAbilityId);
180 }
181 } // namespace DistributedHardware
182 } // namespace OHOS