1 /*
2 * Copyright (c) 2024-2025 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_account_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_log.h"
24 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
25 #include "ffrt.h"
26 #endif
27 #include "iservice_registry.h"
28 #include "multiple_user_connector.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 #if (defined(__LITEOS_M__) || defined(LITE_DEVICE))
37 constexpr const char* DEAL_THREAD = "account_common_event";
38 #endif
39 constexpr int32_t MAX_TRY_TIMES = 3;
40
GetSubscriberEventNameVec() const41 std::vector<std::string> DmAccountEventSubscriber::GetSubscriberEventNameVec() const
42 {
43 return eventNameVec_;
44 }
45
~DmAccountCommonEventManager()46 DmAccountCommonEventManager::~DmAccountCommonEventManager()
47 {
48 DmAccountCommonEventManager::UnsubscribeAccountCommonEvent();
49 }
50
SubscribeAccountCommonEvent(const std::vector<std::string> & eventNameVec,const AccountEventCallback & callback)51 bool DmAccountCommonEventManager::SubscribeAccountCommonEvent(const std::vector<std::string> &eventNameVec,
52 const AccountEventCallback &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 account 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<DmAccountEventSubscriber>(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 int32_t counter = 0;
83 while (counter <= MAX_TRY_TIMES) {
84 if (samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_) == ERR_OK) {
85 LOGI("SubscribeAccountEvent success.");
86 break;
87 }
88 if (++counter == MAX_TRY_TIMES) {
89 LOGE("SubscribeAccountEvent failed.");
90 }
91 sleep(1);
92 }
93 eventNameVec_ = eventNameVec;
94 eventValidFlag_ = true;
95 LOGI("success to subscribe account commom event name size: %{public}zu", eventNameVec.size());
96 return true;
97 }
98
UnsubscribeAccountCommonEvent()99 bool DmAccountCommonEventManager::UnsubscribeAccountCommonEvent()
100 {
101 std::lock_guard<std::mutex> locker(evenSubscriberMutex_);
102 if (!eventValidFlag_) {
103 LOGE("failed to unsubscribe account 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 account commom event name size: %{public}zu", eventNameVec_.size());
109 if (!CommonEventManager::UnSubscribeCommonEvent(subscriber_)) {
110 LOGE("failed to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size());
111 return false;
112 }
113 LOGI("success to unsubscribe account 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 account commom event name size: %{public}zu", eventNameVec_.size());
131 eventValidFlag_ = false;
132 return true;
133 }
134
OnReceiveEvent(const CommonEventData & data)135 void DmAccountEventSubscriber::OnReceiveEvent(const CommonEventData &data)
136 {
137 std::string receiveEvent = data.GetWant().GetAction();
138 int32_t currentUserId = -1;
139 int32_t beforeUserId = -1;
140 bool accountValidEvent = false;
141
142 if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
143 currentUserId = data.GetCode();
144 beforeUserId = std::atoi(data.GetWant().GetStringParam("oldId").c_str());
145 accountValidEvent = true;
146 }
147 if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED ||
148 receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_USER_STOPPED ||
149 receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) {
150 beforeUserId = data.GetCode();
151 accountValidEvent = true;
152 }
153 if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT ||
154 receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGIN) {
155 currentUserId = data.GetWant().GetIntParam("userId", 0);
156 beforeUserId = currentUserId;
157 accountValidEvent = true;
158 }
159 if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_USER_INFO_UPDATED) {
160 currentUserId = data.GetCode();
161 beforeUserId = currentUserId;
162 accountValidEvent = true;
163 }
164 LOGI("Received account event: %{public}s, currentUserId: %{public}d, beforeUserId: %{public}d",
165 receiveEvent.c_str(), currentUserId, beforeUserId);
166 if (!accountValidEvent) {
167 LOGE("Invalied account type event.");
168 return;
169 }
170 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
171 ffrt::submit([=]() { callback_(receiveEvent, currentUserId, beforeUserId); });
172 #else
173 std::thread dealThread([=]() { callback_(receiveEvent, currentUserId, beforeUserId); });
174 int32_t ret = pthread_setname_np(dealThread.native_handle(), DEAL_THREAD);
175 if (ret != DM_OK) {
176 LOGE("dealThread setname failed.");
177 }
178 dealThread.detach();
179 #endif
180 }
181
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)182 void DmAccountCommonEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility(
183 int32_t systemAbilityId, const std::string& deviceId)
184 {
185 LOGI("systemAbility is added with said: %{public}d.", systemAbilityId);
186 if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
187 return;
188 }
189 if (changeSubscriber_ == nullptr) {
190 LOGE("failed to subscribe account commom event because changeSubscriber_ is nullptr.");
191 return;
192 }
193 std::vector<std::string> eventNameVec = changeSubscriber_->GetSubscriberEventNameVec();
194 LOGI("start to subscribe account commom eventName: %{public}zu", eventNameVec.size());
195 if (!CommonEventManager::SubscribeCommonEvent(changeSubscriber_)) {
196 LOGE("failed to subscribe account commom event: %{public}zu", eventNameVec.size());
197 }
198 DMAccountInfo dmAccountInfo;
199 int32_t userId = MultipleUserConnector::GetCurrentAccountUserID();
200 dmAccountInfo.accountId = MultipleUserConnector::GetOhosAccountId();
201 dmAccountInfo.accountName = MultipleUserConnector::GetOhosAccountName();
202 LOGI("after subscribe account event accountId: %{public}s, userId: %{public}s, accountName: %{public}s",
203 GetAnonyString(dmAccountInfo.accountId).c_str(), GetAnonyInt32(userId).c_str(),
204 GetAnonyString(dmAccountInfo.accountName).c_str());
205 if (userId > 0) {
206 MultipleUserConnector::SetAccountInfo(userId, dmAccountInfo);
207 }
208 }
209
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)210 void DmAccountCommonEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
211 int32_t systemAbilityId, const std::string& deviceId)
212 {
213 LOGI("systemAbility is removed with said: %{public}d.", systemAbilityId);
214 }
215 } // namespace DistributedHardware
216 } // namespace OHOS
217