• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     while (counter_ != MAX_TRY_TIMES) {
82         if (samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_) == ERR_OK) {
83             LOGI("SubscribeAccountEvent success.");
84             counter_ = 0;
85             break;
86         }
87         if (++counter_ == MAX_TRY_TIMES) {
88             LOGI("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 
UnsubscribeDataShareCommonEvent()98 bool DmDataShareCommonEventManager::UnsubscribeDataShareCommonEvent()
99 {
100     std::lock_guard<std::mutex> locker(evenSubscriberMutex_);
101     if (!eventValidFlag_) {
102         LOGE("failed to unsubscribe datashare 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 datashare commom event name size: %{public}zu", eventNameVec_.size());
108         if (!CommonEventManager::UnSubscribeCommonEvent(subscriber_)) {
109             LOGE("failed to unsubscribe datashare commom event name size: %{public}zu", eventNameVec_.size());
110             return false;
111         }
112         LOGI("success to unsubscribe datashare 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 datashare commom event name size: %{public}zu", eventNameVec_.size());
130     eventValidFlag_ = false;
131     return true;
132 }
133 
OnReceiveEvent(const CommonEventData & data)134 void DmDataShareEventSubscriber::OnReceiveEvent(const CommonEventData &data)
135 {
136     std::string receiveEvent = data.GetWant().GetAction();
137     bool validEvent = false;
138 
139     if (receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_DATA_SHARE_READY ||
140         receiveEvent == EventFwk::CommonEventSupport::COMMON_EVENT_LOCALE_CHANGED) {
141         validEvent = true;
142     }
143     LOGI("Received datashare event: %{public}s", receiveEvent.c_str());
144     if (!validEvent) {
145         LOGE("Invalied datashare type event.");
146         return;
147     }
148 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
149     ffrt::submit([=]() { callback_(receiveEvent); });
150 #else
151     std::thread dealThread([=]() { callback_(receiveEvent); });
152     int32_t ret = pthread_setname_np(dealThread.native_handle(), DEAL_THREAD);
153     if (ret != DM_OK) {
154         LOGE("dealThread setname failed.");
155     }
156     dealThread.detach();
157 #endif
158 }
159 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)160 void DmDataShareCommonEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility(
161     int32_t systemAbilityId, const std::string& deviceId)
162 {
163     LOGI("systemAbility is added with said: %{public}d.", systemAbilityId);
164     if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
165         return;
166     }
167     if (changeSubscriber_ == nullptr) {
168         LOGE("failed to subscribe datashare commom event because changeSubscriber_ is nullptr.");
169         return;
170     }
171     std::vector<std::string> eventNameVec = changeSubscriber_->GetSubscriberEventNameVec();
172     LOGI("start to subscribe datashare commom eventName: %{public}zu", eventNameVec.size());
173     if (!CommonEventManager::SubscribeCommonEvent(changeSubscriber_)) {
174         LOGE("failed to subscribe datashare commom event: %{public}zu", eventNameVec.size());
175     }
176 }
177 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)178 void DmDataShareCommonEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
179     int32_t systemAbilityId, const std::string& deviceId)
180 {
181     LOGI("systemAbility is removed with said: %{public}d.", systemAbilityId);
182 }
183 } // namespace DistributedHardware
184 } // namespace OHOS
185