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_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_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 "multiple_user_connector.h"
30 #include "system_ability_definition.h"
31
32 namespace OHOS {
33 namespace DistributedHardware {
34 using OHOS::EventFwk::MatchingSkills;
35 using OHOS::EventFwk::CommonEventManager;
36
37 #if (defined(__LITEOS_M__) || defined(LITE_DEVICE))
38 constexpr const char* DEAL_THREAD = "account_common_event";
39 #endif
40 constexpr int32_t MAX_TRY_TIMES = 3;
41
GetSubscriberEventNameVec() const42 std::vector<std::string> DmAccountEventSubscriber::GetSubscriberEventNameVec() const
43 {
44 return eventNameVec_;
45 }
46
~DmAccountCommonEventManager()47 DmAccountCommonEventManager::~DmAccountCommonEventManager()
48 {
49 DmAccountCommonEventManager::UnsubscribeAccountCommonEvent();
50 }
51
SubscribeAccountCommonEvent(const std::vector<std::string> & eventNameVec,const AccountEventCallback & callback)52 bool DmAccountCommonEventManager::SubscribeAccountCommonEvent(const std::vector<std::string> &eventNameVec,
53 const AccountEventCallback &callback)
54 {
55 if (eventNameVec.empty() || callback == nullptr) {
56 LOGE("eventNameVec is empty or callback is nullptr.");
57 return false;
58 }
59 std::lock_guard<std::mutex> locker(evenSubscriberMutex_);
60 if (eventValidFlag_) {
61 LOGE("failed to subscribe account commom eventName size: %{public}zu", eventNameVec.size());
62 return false;
63 }
64
65 MatchingSkills matchingSkills;
66 for (auto &item : eventNameVec) {
67 matchingSkills.AddEvent(item);
68 }
69 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
70 subscriber_ = std::make_shared<DmAccountEventSubscriber>(subscriberInfo, callback, eventNameVec);
71 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
72 if (samgrProxy == nullptr) {
73 LOGE("samgrProxy is nullptr");
74 subscriber_ = nullptr;
75 return false;
76 }
77 statusChangeListener_ = new (std::nothrow) SystemAbilityStatusChangeListener(subscriber_);
78 if (statusChangeListener_ == nullptr) {
79 LOGE("statusChangeListener_ is nullptr");
80 subscriber_ = nullptr;
81 return false;
82 }
83 while (counter_ != MAX_TRY_TIMES) {
84 if (samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_) == ERR_OK) {
85 LOGI("SubscribeAccountEvent success.");
86 counter_ = 0;
87 break;
88 }
89 if (++counter_ == MAX_TRY_TIMES) {
90 LOGI("SubscribeAccountEvent failed.");
91 }
92 sleep(1);
93 }
94 eventNameVec_ = eventNameVec;
95 eventValidFlag_ = true;
96 LOGI("success to subscribe account commom event name size: %{public}zu", eventNameVec.size());
97 return true;
98 }
99
UnsubscribeAccountCommonEvent()100 bool DmAccountCommonEventManager::UnsubscribeAccountCommonEvent()
101 {
102 std::lock_guard<std::mutex> locker(evenSubscriberMutex_);
103 if (!eventValidFlag_) {
104 LOGE("failed to unsubscribe account commom event name size: %{public}zu because event is invalid.",
105 eventNameVec_.size());
106 return false;
107 }
108 if (subscriber_ != nullptr) {
109 LOGI("start to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size());
110 if (!CommonEventManager::UnSubscribeCommonEvent(subscriber_)) {
111 LOGE("failed to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size());
112 return false;
113 }
114 LOGI("success to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size());
115 subscriber_ = nullptr;
116 }
117 if (statusChangeListener_ != nullptr) {
118 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
119 if (samgrProxy == nullptr) {
120 LOGE("samgrProxy is nullptr");
121 return false;
122 }
123 int32_t ret = samgrProxy->UnSubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_);
124 if (ret != ERR_OK) {
125 LOGE("failed to unsubscribe system ability COMMON_EVENT_SERVICE_ID ret:%{public}d", ret);
126 return false;
127 }
128 statusChangeListener_ = nullptr;
129 }
130
131 LOGI("success to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size());
132 eventValidFlag_ = false;
133 return true;
134 }
135
OnReceiveEvent(const CommonEventData & data)136 void DmAccountEventSubscriber::OnReceiveEvent(const CommonEventData &data)
137 {
138 std::string receiveEvent = data.GetWant().GetAction();
139 LOGI("Received account event: %{public}s", receiveEvent.c_str());
140 int32_t userId = data.GetCode();
141 if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
142 userId = data.GetCode();
143 } else if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT) {
144 userId = data.GetWant().GetIntParam("userId", 0);
145 } else if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGIN) {
146 userId = data.GetWant().GetIntParam("userId", 0);
147 }
148 if (userId <= 0) {
149 LOGE("userId is less zero");
150 return;
151 }
152 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
153 ffrt::submit([=]() { callback_(userId, receiveEvent); });
154 #else
155 std::thread dealThread([=]() { callback_(userId, receiveEvent); });
156 int32_t ret = pthread_setname_np(dealThread.native_handle(), DEAL_THREAD);
157 if (ret != DM_OK) {
158 LOGE("dealThread setname failed.");
159 }
160 dealThread.detach();
161 #endif
162 }
163
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)164 void DmAccountCommonEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility(
165 int32_t systemAbilityId, const std::string& deviceId)
166 {
167 LOGI("systemAbility is added with said: %{public}d.", systemAbilityId);
168 if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
169 return;
170 }
171 if (changeSubscriber_ == nullptr) {
172 LOGE("failed to subscribe account commom event because changeSubscriber_ is nullptr.");
173 return;
174 }
175 std::vector<std::string> eventNameVec = changeSubscriber_->GetSubscriberEventNameVec();
176 LOGI("start to subscribe account commom eventName: %{public}zu", eventNameVec.size());
177 if (!CommonEventManager::SubscribeCommonEvent(changeSubscriber_)) {
178 LOGE("failed to subscribe account commom event: %{public}zu", eventNameVec.size());
179 }
180 int32_t userId = MultipleUserConnector::GetCurrentAccountUserID();
181 std::string accountId = MultipleUserConnector::GetOhosAccountId();
182 LOGI("after subscribe account event accountId: %{public}s, userId: %{public}s",
183 GetAnonyString(accountId).c_str(), GetAnonyInt32(userId).c_str());
184 if (userId > 0) {
185 MultipleUserConnector::SetSwitchOldUserId(userId);
186 MultipleUserConnector::SetSwitchOldAccountId(accountId);
187 }
188 }
189
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)190 void DmAccountCommonEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
191 int32_t systemAbilityId, const std::string& deviceId)
192 {
193 LOGI("systemAbility is removed with said: %{public}d.", systemAbilityId);
194 }
195 } // namespace DistributedHardware
196 } // namespace OHOS
197