1 /*
2 * Copyright (c) 2022-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 "static_subscriber_connection.h"
17
18 #include "ability_manager_helper.h"
19 #include "event_log_wrapper.h"
20 #include "event_report.h"
21
22 namespace OHOS {
23 namespace EventFwk {
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)24 void StaticSubscriberConnection::OnAbilityConnectDone(
25 const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode)
26 {
27 std::lock_guard<ffrt::recursive_mutex> lock(mutex_);
28 sptr<StaticSubscriberConnection> sThis = this;
29 auto proxy = sThis->GetProxy(remoteObject);
30 std::string bundleName = element.GetURI();
31 EVENT_LOGI("called, %{public}s", bundleName.c_str());
32 for (auto &event : events_) {
33 NotifyEvent(event);
34 }
35 events_.clear();
36 }
37
NotifyEvent(const CommonEventData & event)38 void StaticSubscriberConnection::NotifyEvent(const CommonEventData &event)
39 {
40 std::lock_guard<ffrt::recursive_mutex> lock(mutex_);
41 if (proxy_ == nullptr) {
42 events_.push_back(event);
43 EVENT_LOGW_LIMIT("Cache events");
44 return;
45 }
46 action_.push_back(event.GetWant().GetAction());
47 wptr<StaticSubscriberConnection> wThis = this;
48 staticNotifyQueue_->submit([event, wThis]() {
49 sptr<StaticSubscriberConnection> sThis = wThis.promote();
50 if (!sThis) {
51 EVENT_LOGE("Connection expired, skip Notify");
52 return;
53 }
54 int32_t funcResult = -1;
55 ErrCode ec = sThis->proxy_->OnReceiveEvent(event, funcResult);
56 EVENT_LOGI("Notify %{public}s to %{public}s end, code %{public}d",
57 event.GetWant().GetAction().c_str(), sThis->connectionKey_.c_str(), ec);
58 AbilityManagerHelper::GetInstance()->DisconnectServiceAbilityDelay(sThis, event.GetWant().GetAction());
59 });
60 }
61
RemoveEvent(const std::string & action)62 void StaticSubscriberConnection::RemoveEvent(const std::string &action)
63 {
64 std::lock_guard<ffrt::recursive_mutex> lock(mutex_);
65 auto it = std::find(action_.begin(), action_.end(), action);
66 if (it != action_.end()) {
67 action_.erase(it);
68 }
69 }
70
GetProxy(const sptr<IRemoteObject> & remoteObject)71 sptr<StaticSubscriberProxy> StaticSubscriberConnection::GetProxy(const sptr<IRemoteObject> &remoteObject)
72 {
73 if (proxy_ == nullptr) {
74 proxy_ = new (std::nothrow) StaticSubscriberProxy(remoteObject);
75 if (proxy_ == nullptr) {
76 EVENT_LOGE("failed to create StaticSubscriberProxy!");
77 }
78 }
79 return proxy_;
80 }
81
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)82 void StaticSubscriberConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode)
83 {}
84 } // namespace EventFwk
85 } // namespace OHOS
86