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