1 /*
2 * Copyright (c) 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_datashare_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 "system_ability_definition.h"
29
30 namespace OHOS {
31 namespace DistributedHardware {
32 using OHOS::EventFwk::MatchingSkills;
33 using OHOS::EventFwk::CommonEventManager;
34
35 #if (defined(__LITEOS_M__) || defined(LITE_DEVICE))
36 constexpr const char* DEAL_THREAD = "datashare_common_event";
37 #endif
38 constexpr int32_t MAX_TRY_TIMES = 3;
39
GetSubscriberEventNameVec() const40 std::vector<std::string> DmDataShareEventSubscriber::GetSubscriberEventNameVec() const
41 {
42 return eventNameVec_;
43 }
44
~DmDataShareCommonEventManager()45 DmDataShareCommonEventManager::~DmDataShareCommonEventManager()
46 {
47 DmDataShareCommonEventManager::UnsubscribeDataShareCommonEvent();
48 }
49
SubscribeDataShareCommonEvent(const std::vector<std::string> & eventNameVec,const DataShareEventCallback & callback)50 bool DmDataShareCommonEventManager::SubscribeDataShareCommonEvent(const std::vector<std::string> &eventNameVec,
51 const DataShareEventCallback &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 datashare 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<DmDataShareEventSubscriber>(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 int32_t counter = 0;
82 while (counter <= MAX_TRY_TIMES) {
83 if (samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_) == ERR_OK) {
84 LOGI("SubscribeAccountEvent success.");
85 break;
86 }
87 if (++counter == MAX_TRY_TIMES) {
88 LOGE("SubscribeAccountEvent failed.");
89 }
90 sleep(1);
91 }
92 eventNameVec_ = eventNameVec;
93 eventValidFlag_ = true;
94 LOGI("success to subscribe datashare commom event name size: %{public}zu", eventNameVec.size());
95 return true;
96 }
97
98 //LCOV_EXCL_START
UnsubscribeDataShareCommonEvent()99 bool DmDataShareCommonEventManager::UnsubscribeDataShareCommonEvent()
100 {
101 std::lock_guard<std::mutex> locker(evenSubscriberMutex_);
102 if (!eventValidFlag_) {
103 LOGE("failed to unsubscribe datashare 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 datashare commom event name size: %{public}zu", eventNameVec_.size());
109 if (!CommonEventManager::UnSubscribeCommonEvent(subscriber_)) {
110 LOGE("failed to unsubscribe datashare commom event name size: %{public}zu", eventNameVec_.size());
111 return false;
112 }
113 LOGI("success to unsubscribe datashare 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 datashare commom event name size: %{public}zu", eventNameVec_.size());
131 eventValidFlag_ = false;
132 return true;
133 }
134 //LCOV_EXCL_STOP
135
OnReceiveEvent(const CommonEventData & data)136 void DmDataShareEventSubscriber::OnReceiveEvent(const CommonEventData &data)
137 {
138 std::string receiveEvent = data.GetWant().GetAction();
139 int32_t eventState = data.GetCode();
140 bool validEvent = false;
141
142 if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_DATA_SHARE_READY ||
143 receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_LOCALE_CHANGED ||
144 receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE) {
145 validEvent = true;
146 }
147 LOGI("Received datashare event: %{public}s, eventState: %{public}d", receiveEvent.c_str(), eventState);
148 if (!validEvent) {
149 LOGE("Invalied datashare type event.");
150 return;
151 }
152 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
153 ffrt::submit([=]() { callback_(receiveEvent, eventState); });
154 #else
155 std::thread dealThread([=]() { callback_(receiveEvent, eventState); });
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 DmDataShareCommonEventManager::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 datashare commom event because changeSubscriber_ is nullptr.");
173 return;
174 }
175 std::vector<std::string> eventNameVec = changeSubscriber_->GetSubscriberEventNameVec();
176 LOGI("start to subscribe datashare commom eventName: %{public}zu", eventNameVec.size());
177 if (!CommonEventManager::SubscribeCommonEvent(changeSubscriber_)) {
178 LOGE("failed to subscribe datashare commom event: %{public}zu", eventNameVec.size());
179 }
180 }
181
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)182 void DmDataShareCommonEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
183 int32_t systemAbilityId, const std::string& deviceId)
184 {
185 LOGI("systemAbility is removed with said: %{public}d.", systemAbilityId);
186 }
187 } // namespace DistributedHardware
188 } // namespace OHOS
189