1 /*
2 * Copyright (c) 2022-2023 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 "hitrace_meter_adapter.h"
20 #include "in_process_call_wrapper.h"
21 #include "iservice_registry.h"
22 #include "static_subscriber_connection.h"
23 #include "system_ability_definition.h"
24
25 namespace OHOS {
26 namespace EventFwk {
27 namespace {
28 constexpr int32_t DISCONNECT_DELAY_TIME = 15000; // ms
29 constexpr int32_t TIME_UNIT_SIZE = 1000;
30 }
31
AbilityManagerHelper()32 AbilityManagerHelper::AbilityManagerHelper()
33 {
34 ffrt_ = std::make_shared<ffrt::queue>("AbilityManagerHelper");
35 }
36
ConnectAbility(const Want & want,const CommonEventData & event,const sptr<IRemoteObject> & callerToken,const int32_t & userId)37 int AbilityManagerHelper::ConnectAbility(
38 const Want &want, const CommonEventData &event, const sptr<IRemoteObject> &callerToken, const int32_t &userId)
39 {
40 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
41 EVENT_LOGI("enter, target bundle = %{public}s", want.GetBundle().c_str());
42 std::lock_guard<std::mutex> lock(mutex_);
43
44 if (!GetAbilityMgrProxy()) {
45 return -1;
46 }
47
48 sptr<StaticSubscriberConnection> connection = new (std::nothrow) StaticSubscriberConnection(event);
49 if (connection == nullptr) {
50 EVENT_LOGE("failed to create obj!");
51 return -1;
52 }
53 int32_t result = abilityMgr_->ConnectAbility(want, connection, callerToken, userId);
54 if (result == ERR_OK) {
55 subscriberConnection_.emplace(connection);
56 }
57 return result;
58 }
59
GetAbilityMgrProxy()60 bool AbilityManagerHelper::GetAbilityMgrProxy()
61 {
62 if (abilityMgr_ == nullptr) {
63 sptr<ISystemAbilityManager> systemAbilityManager =
64 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
65 if (systemAbilityManager == nullptr) {
66 EVENT_LOGE("Failed to get system ability mgr.");
67 return false;
68 }
69
70 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
71 if (remoteObject == nullptr) {
72 EVENT_LOGE("Failed to get ability manager service.");
73 return false;
74 }
75
76 abilityMgr_ = iface_cast<AAFwk::IAbilityManager>(remoteObject);
77 if ((abilityMgr_ == nullptr) || (abilityMgr_->AsObject() == nullptr)) {
78 EVENT_LOGE("Failed to get system ability manager services ability");
79 return false;
80 }
81
82 deathRecipient_ = new (std::nothrow) AbilityManagerDeathRecipient();
83 if (deathRecipient_ == nullptr) {
84 EVENT_LOGE("Failed to create AbilityManagerDeathRecipient");
85 return false;
86 }
87 if (!abilityMgr_->AsObject()->AddDeathRecipient(deathRecipient_)) {
88 EVENT_LOGW("Failed to add AbilityManagerDeathRecipient");
89 }
90 }
91
92 return true;
93 }
94
Clear()95 void AbilityManagerHelper::Clear()
96 {
97 EVENT_LOGI("enter");
98 std::lock_guard<std::mutex> lock(mutex_);
99
100 if ((abilityMgr_ != nullptr) && (abilityMgr_->AsObject() != nullptr)) {
101 abilityMgr_->AsObject()->RemoveDeathRecipient(deathRecipient_);
102 }
103 abilityMgr_ = nullptr;
104 }
105
DisconnectServiceAbilityDelay(const sptr<StaticSubscriberConnection> & connection)106 void AbilityManagerHelper::DisconnectServiceAbilityDelay(const sptr<StaticSubscriberConnection> &connection)
107 {
108 EVENT_LOGD("enter");
109 if (connection == nullptr) {
110 EVENT_LOGE("connection is nullptr");
111 return;
112 }
113
114 std::function<void()> task = [connection]() {
115 AbilityManagerHelper::GetInstance()->DisconnectAbility(connection);
116 };
117 ffrt_->submit(task, ffrt::task_attr().delay(DISCONNECT_DELAY_TIME * TIME_UNIT_SIZE));
118 }
119
DisconnectAbility(const sptr<StaticSubscriberConnection> & connection)120 void AbilityManagerHelper::DisconnectAbility(const sptr<StaticSubscriberConnection> &connection)
121 {
122 EVENT_LOGD("enter");
123 std::lock_guard<std::mutex> lock(mutex_);
124 if (connection == nullptr) {
125 EVENT_LOGE("connection is nullptr");
126 return;
127 }
128
129 if (subscriberConnection_.find(connection) == subscriberConnection_.end()) {
130 EVENT_LOGE("failed to find connection!");
131 return;
132 }
133
134 if (!GetAbilityMgrProxy()) {
135 return;
136 }
137 IN_PROCESS_CALL_WITHOUT_RET(abilityMgr_->DisconnectAbility(connection));
138 subscriberConnection_.erase(connection);
139 }
140 } // namespace EventFwk
141 } // namespace OHOS
142