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