• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "dp_account_common_event.h"
17 
18 #include <pthread.h>
19 #include <thread>
20 
21 #include "common_event_support.h"
22 #include "distributed_device_profile_errors.h"
23 #include "distributed_device_profile_log.h"
24 #include "ffrt.h"
25 #include "iservice_registry.h"
26 #include "multi_user_manager.h"
27 #include "system_ability_definition.h"
28 
29 namespace OHOS {
30 namespace DistributedDeviceProfile {
31 using OHOS::EventFwk::MatchingSkills;
32 using OHOS::EventFwk::CommonEventManager;
33 
34 constexpr int32_t MAX_TRY_TIMES = 3;
35 
36 namespace {
37     const std::string TAG = "AccountCommonEvent";
38 }
39 
GetSubscriberEventNameVec() const40 std::vector<std::string> DpAccountEventSubscriber::GetSubscriberEventNameVec() const
41 {
42     return eventNameVec_;
43 }
44 
~DpAccountCommonEventManager()45 DpAccountCommonEventManager::~DpAccountCommonEventManager()
46 {
47     DpAccountCommonEventManager::UnsubscribeAccountCommonEvent();
48 }
49 
SubscribeAccountCommonEvent(const std::vector<std::string> & eventNameVec,const AccountEventCallback & callback)50 bool DpAccountCommonEventManager::SubscribeAccountCommonEvent(const std::vector<std::string> &eventNameVec,
51     const AccountEventCallback &callback)
52 {
53     if (eventNameVec.empty() || callback == nullptr) {
54         HILOGE("eventNameVec is empty or callback is nullptr.");
55         return false;
56     }
57     std::lock_guard<std::mutex> locker(evenSubscriberMutex_);
58     if (eventValidFlag_) {
59         HILOGE("failed to subscribe account 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<DpAccountEventSubscriber>(subscriberInfo, callback, eventNameVec);
69     auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
70     if (samgrProxy == nullptr) {
71         HILOGE("samgrProxy is nullptr");
72         subscriber_ = nullptr;
73         return false;
74     }
75     statusChangeListener_ = new (std::nothrow) SystemAbilityStatusChangeListener(subscriber_);
76     if (statusChangeListener_ == nullptr) {
77         HILOGE("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             HILOGI("SubscribeAccountEvent success.");
84             counter_ = 0;
85             break;
86         }
87         if (++counter_ == MAX_TRY_TIMES) {
88             HILOGE("SubscribeAccountEvent failed.");
89         }
90         sleep(1);
91     }
92     eventNameVec_ = eventNameVec;
93     eventValidFlag_ = true;
94     HILOGI("success to subscribe account commom event name size: %{public}zu", eventNameVec.size());
95     return true;
96 }
97 
UnsubscribeAccountCommonEvent()98 bool DpAccountCommonEventManager::UnsubscribeAccountCommonEvent()
99 {
100     std::lock_guard<std::mutex> locker(evenSubscriberMutex_);
101     if (!eventValidFlag_) {
102         HILOGE("failed to unsubscribe account commom event name size: %{public}zu because event is invalid.",
103             eventNameVec_.size());
104         return false;
105     }
106     if (subscriber_ != nullptr) {
107         HILOGI("start to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size());
108         if (!CommonEventManager::UnSubscribeCommonEvent(subscriber_)) {
109             HILOGE("failed to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size());
110             return false;
111         }
112         HILOGI("success to unsubscribe account 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             HILOGE("samgrProxy is nullptr");
119             return false;
120         }
121         int32_t ret = samgrProxy->UnSubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_);
122         if (ret != ERR_OK) {
123             HILOGE("failed to unsubscribe system ability COMMON_EVENT_SERVICE_ID ret:%{public}d", ret);
124             return false;
125         }
126         statusChangeListener_ = nullptr;
127     }
128 
129     HILOGI("success to unsubscribe account commom event name size: %{public}zu", eventNameVec_.size());
130     eventValidFlag_ = false;
131     return true;
132 }
133 
OnReceiveEvent(const CommonEventData & data)134 void DpAccountEventSubscriber::OnReceiveEvent(const CommonEventData &data)
135 {
136     std::string receiveEvent = data.GetWant().GetAction();
137     HILOGI("Received account event: %{public}s", receiveEvent.c_str());
138     int32_t userId = data.GetCode();
139     bool isValidEvent = false;
140     if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED ||
141         receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
142         userId = data.GetCode();
143         isValidEvent = true;
144     }
145     if (userId <= 0 || !isValidEvent) {
146         HILOGE("Invalid account type event.");
147         return;
148     }
149     ffrt::submit([=]() { callback_(userId, receiveEvent); });
150 }
151 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)152 void DpAccountCommonEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility(
153     int32_t systemAbilityId, const std::string& deviceId)
154 {
155     HILOGI("systemAbility is added with said: %{public}d.", systemAbilityId);
156     if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
157         return;
158     }
159     if (changeSubscriber_ == nullptr) {
160         HILOGE("failed to subscribe account commom event because changeSubscriber_ is nullptr.");
161         return;
162     }
163     std::vector<std::string> eventNameVec = changeSubscriber_->GetSubscriberEventNameVec();
164     HILOGI("start to subscribe account commom eventName: %{public}zu", eventNameVec.size());
165     if (!CommonEventManager::SubscribeCommonEvent(changeSubscriber_)) {
166         HILOGE("failed to subscribe account commom event: %{public}zu", eventNameVec.size());
167     }
168 }
169 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)170 void DpAccountCommonEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
171     int32_t systemAbilityId, const std::string& deviceId)
172 {
173     HILOGI("systemAbility is removed with said: %{public}d.", systemAbilityId);
174 }
175 } // namespace DistributedHardware
176 } // namespace OHOS
177