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