1 /*
2 * Copyright (c) 2022 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 "iservice_registry.h"
21 #include "static_subscriber_connection.h"
22 #include "system_ability_definition.h"
23
24 namespace OHOS {
25 namespace EventFwk {
ConnectAbility(const Want & want,const CommonEventData & event,const sptr<IRemoteObject> & callerToken,const int32_t & userId)26 int AbilityManagerHelper::ConnectAbility(
27 const Want &want, const CommonEventData &event, const sptr<IRemoteObject> &callerToken, const int32_t &userId)
28 {
29 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
30 EVENT_LOGI("enter, target bundle = %{public}s", want.GetBundle().c_str());
31 std::lock_guard<std::mutex> lock(mutex_);
32
33 if (!GetAbilityMgrProxy()) {
34 EVENT_LOGE("failed to get ability manager proxy!");
35 return -1;
36 }
37
38 sptr<StaticSubscriberConnection> connection = new (std::nothrow) StaticSubscriberConnection(event);
39 if (connection == nullptr) {
40 EVENT_LOGE("failed to create obj!");
41 return -1;
42 }
43 return abilityMgr_->ConnectAbility(want, connection, callerToken, userId);
44 }
45
GetAbilityMgrProxy()46 bool AbilityManagerHelper::GetAbilityMgrProxy()
47 {
48 EVENT_LOGI("GetAbilityMgrProxy enter");
49 if (abilityMgr_ == nullptr) {
50 sptr<ISystemAbilityManager> systemAbilityManager =
51 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
52 if (systemAbilityManager == nullptr) {
53 EVENT_LOGE("Failed to get system ability mgr.");
54 return false;
55 }
56
57 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
58 if (remoteObject == nullptr) {
59 EVENT_LOGE("Failed to get ability manager service.");
60 return false;
61 }
62
63 abilityMgr_ = iface_cast<AAFwk::IAbilityManager>(remoteObject);
64 if ((abilityMgr_ == nullptr) || (abilityMgr_->AsObject() == nullptr)) {
65 EVENT_LOGE("Failed to get system ability manager services ability");
66 return false;
67 }
68
69 deathRecipient_ = new (std::nothrow) AbilityManagerDeathRecipient();
70 if (deathRecipient_ == nullptr) {
71 EVENT_LOGE("Failed to create AbilityManagerDeathRecipient");
72 return false;
73 }
74 if (!abilityMgr_->AsObject()->AddDeathRecipient(deathRecipient_)) {
75 EVENT_LOGW("Failed to add AbilityManagerDeathRecipient");
76 }
77 }
78
79 return true;
80 }
81
Clear()82 void AbilityManagerHelper::Clear()
83 {
84 EVENT_LOGI("enter");
85 std::lock_guard<std::mutex> lock(mutex_);
86
87 if ((abilityMgr_ != nullptr) && (abilityMgr_->AsObject() != nullptr)) {
88 abilityMgr_->AsObject()->RemoveDeathRecipient(deathRecipient_);
89 }
90 abilityMgr_ = nullptr;
91 }
92 } // namespace EventFwk
93 } // namespace OHOS
94