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 "ability_manager_helper.h"
17
18 #include "event_log_wrapper.h"
19 #include "event_trace_wrapper.h"
20 #include "hitrace_meter_adapter.h"
21 #include "in_process_call_wrapper.h"
22 #include "iservice_registry.h"
23 #include "static_subscriber_connection.h"
24 #include "system_ability_definition.h"
25
26 namespace OHOS {
27 namespace EventFwk {
28 namespace {
29 constexpr int32_t DISCONNECT_DELAY_TIME = 15000; // ms
30 constexpr int32_t TIME_UNIT_SIZE = 1000;
31 }
32
AbilityManagerHelper()33 AbilityManagerHelper::AbilityManagerHelper()
34 {
35 ffrt_ = std::make_shared<ffrt::queue>("AbilityManagerHelper");
36 }
37
ConnectAbility(const Want & want,const CommonEventData & event,const sptr<IRemoteObject> & callerToken,const int32_t & userId)38 int AbilityManagerHelper::ConnectAbility(
39 const Want &want, const CommonEventData &event, const sptr<IRemoteObject> &callerToken, const int32_t &userId)
40 {
41 NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
42 EVENT_LOGD("enter, target bundle = %{public}s", want.GetBundle().c_str());
43 int result = -1;
44 ffrt::task_handle handle = ffrt_->submit_h([&]() {
45 std::string connectionKey =
46 want.GetBundle() + "_" + want.GetElement().GetAbilityName() + "_" + std::to_string(userId);
47
48 if (!GetAbilityMgrProxy()) {
49 result = -1;
50 return;
51 }
52 auto it = subscriberConnection_.find(connectionKey);
53 if (it != subscriberConnection_.end()) {
54 it->second->NotifyEvent(event);
55 EVENT_LOGD("Static cache sends events!");
56 result = ERR_OK;
57 return;
58 }
59
60 sptr<StaticSubscriberConnection> connection =
61 new (std::nothrow) StaticSubscriberConnection(event, connectionKey);
62 if (connection == nullptr) {
63 EVENT_LOGE("failed to create obj!");
64 result = -1;
65 return;
66 }
67 result = abilityMgr_->ConnectAbility(want, connection, callerToken, userId);
68 if (result != ERR_OK) {
69 EVENT_LOGE("Connect failed, result=%{public}d", result);
70 return;
71 }
72 subscriberConnection_[connectionKey] = connection;
73 DisconnectServiceAbilityDelay(connection, "");
74 EVENT_LOGD("Connect success");
75 });
76 ffrt_->wait(handle);
77 return result;
78 }
79
GetAbilityMgrProxy()80 bool AbilityManagerHelper::GetAbilityMgrProxy()
81 {
82 if (abilityMgr_ == nullptr) {
83 sptr<ISystemAbilityManager> systemAbilityManager =
84 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
85 if (systemAbilityManager == nullptr) {
86 EVENT_LOGE("Failed to get system ability mgr.");
87 return false;
88 }
89
90 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
91 if (remoteObject == nullptr) {
92 EVENT_LOGE("Failed to get ability manager service.");
93 return false;
94 }
95
96 abilityMgr_ = iface_cast<AAFwk::IAbilityManager>(remoteObject);
97 if ((abilityMgr_ == nullptr) || (abilityMgr_->AsObject() == nullptr)) {
98 EVENT_LOGE("Failed to get system ability manager services ability");
99 return false;
100 }
101
102 deathRecipient_ = new (std::nothrow) AbilityManagerDeathRecipient();
103 if (deathRecipient_ == nullptr) {
104 EVENT_LOGE("Failed to create AbilityManagerDeathRecipient");
105 return false;
106 }
107 if (!abilityMgr_->AsObject()->AddDeathRecipient(deathRecipient_)) {
108 EVENT_LOGW("Failed to add AbilityManagerDeathRecipient");
109 }
110 }
111
112 return true;
113 }
114
Clear()115 void AbilityManagerHelper::Clear()
116 {
117 EVENT_LOGI("enter");
118 std::lock_guard<ffrt::mutex> lock(mutex_);
119
120 if ((abilityMgr_ != nullptr) && (abilityMgr_->AsObject() != nullptr)) {
121 abilityMgr_->AsObject()->RemoveDeathRecipient(deathRecipient_);
122 }
123 abilityMgr_ = nullptr;
124 }
125
DisconnectServiceAbilityDelay(const sptr<StaticSubscriberConnection> & connection,const std::string & action)126 void AbilityManagerHelper::DisconnectServiceAbilityDelay(
127 const sptr<StaticSubscriberConnection> &connection, const std::string &action)
128 {
129 EVENT_LOGD("enter");
130 if (connection == nullptr) {
131 EVENT_LOGW("connection is nullptr");
132 return;
133 }
134 std::function<void()> task = [connection, action, this]() {
135 this->DisconnectAbility(connection, action);
136 };
137 ffrt_->submit(task, ffrt::task_attr().delay(DISCONNECT_DELAY_TIME * TIME_UNIT_SIZE));
138 }
139
DisconnectAbility(const sptr<StaticSubscriberConnection> & connection,const std::string & action)140 void AbilityManagerHelper::DisconnectAbility(
141 const sptr<StaticSubscriberConnection> &connection, const std::string &action)
142 {
143 EVENT_LOGD("enter");
144 if (connection == nullptr) {
145 EVENT_LOGW("connection is nullptr");
146 return;
147 }
148
149 auto it = std::find_if(subscriberConnection_.begin(), subscriberConnection_.end(),
150 [&connection](const auto &pair) { return pair.second == connection; });
151 if (it == subscriberConnection_.end()) {
152 EVENT_LOGW("failed to find connection!");
153 return;
154 }
155 if (connection) {
156 connection->RemoveEvent(action);
157 if (!connection->IsEmptyAction()) {
158 EVENT_LOGD("Event within 15 seconds!");
159 return;
160 }
161 }
162 IN_PROCESS_CALL_WITHOUT_RET(abilityMgr_->DisconnectAbility(connection));
163 subscriberConnection_.erase(it);
164 EVENT_LOGD("erase connection!");
165 }
166 } // namespace EventFwk
167 } // namespace OHOS
168