1 /*
2 * Copyright (c) 2021-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 "reminder_bundle_manager_helper.h"
17
18 #include "if_system_ability_manager.h"
19 #include "iservice_registry.h"
20 #include "os_account_manager.h"
21 #include "system_ability_definition.h"
22
23 #include "ans_const_define.h"
24 #include "ans_log_wrapper.h"
25
26 namespace OHOS {
27 namespace Notification {
ReminderBundleManagerHelper()28 ReminderBundleManagerHelper::ReminderBundleManagerHelper()
29 {
30 deathRecipient_ = new (std::nothrow)
31 RemoteDeathRecipient(std::bind(&ReminderBundleManagerHelper::OnRemoteDied, this, std::placeholders::_1));
32 if (deathRecipient_ == nullptr) {
33 ANS_LOGE("Failed to create RemoteDeathRecipient instance");
34 }
35 }
36
~ReminderBundleManagerHelper()37 ReminderBundleManagerHelper::~ReminderBundleManagerHelper()
38 {
39 std::lock_guard<std::mutex> lock(connectionMutex_);
40 Disconnect();
41 }
42
OnRemoteDied(const wptr<IRemoteObject> & object)43 void ReminderBundleManagerHelper::OnRemoteDied(const wptr<IRemoteObject> &object)
44 {
45 std::lock_guard<std::mutex> lock(connectionMutex_);
46 Disconnect();
47 }
48
Connect()49 void ReminderBundleManagerHelper::Connect()
50 {
51 if (bundleMgr_ != nullptr) {
52 return;
53 }
54
55 sptr<ISystemAbilityManager> systemAbilityManager =
56 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
57 if (systemAbilityManager == nullptr) {
58 return;
59 }
60
61 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
62 if (remoteObject == nullptr) {
63 return;
64 }
65
66 bundleMgr_ = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
67 if (bundleMgr_ != nullptr) {
68 bundleMgr_->AsObject()->AddDeathRecipient(deathRecipient_);
69 }
70 }
71
Disconnect()72 void ReminderBundleManagerHelper::Disconnect()
73 {
74 if (bundleMgr_ != nullptr) {
75 bundleMgr_->AsObject()->RemoveDeathRecipient(deathRecipient_);
76 bundleMgr_ = nullptr;
77 }
78 }
79
GetBundleNameByUid(int32_t uid)80 std::string ReminderBundleManagerHelper::GetBundleNameByUid(int32_t uid)
81 {
82 std::string bundle;
83
84 std::lock_guard<std::mutex> lock(connectionMutex_);
85
86 Connect();
87
88 if (bundleMgr_ != nullptr) {
89 std::string identity = IPCSkeleton::ResetCallingIdentity();
90 bundleMgr_->GetNameForUid(uid, bundle);
91 IPCSkeleton::SetCallingIdentity(identity);
92 }
93
94 return bundle;
95 }
96
GetDefaultUidByBundleName(const std::string & bundle,const int32_t userId)97 int32_t ReminderBundleManagerHelper::GetDefaultUidByBundleName(const std::string &bundle, const int32_t userId)
98 {
99 int32_t uid = -1;
100
101 std::lock_guard<std::mutex> lock(connectionMutex_);
102
103 Connect();
104
105 if (bundleMgr_ != nullptr) {
106 std::string identity = IPCSkeleton::ResetCallingIdentity();
107 uid = bundleMgr_->GetUidByBundleName(bundle, userId);
108 if (uid < 0) {
109 ANS_LOGW("get invalid uid of bundle %{public}s in userId %{public}d", bundle.c_str(), userId);
110 }
111 IPCSkeleton::SetCallingIdentity(identity);
112 }
113
114 return uid;
115 }
116
GetBundleInfo(const std::string & bundleName,const AppExecFwk::BundleFlag flag,int32_t userId,AppExecFwk::BundleInfo & bundleInfo)117 bool ReminderBundleManagerHelper::GetBundleInfo(const std::string &bundleName, const AppExecFwk::BundleFlag flag,
118 int32_t userId, AppExecFwk::BundleInfo &bundleInfo)
119 {
120 std::lock_guard<std::mutex> lock(connectionMutex_);
121
122 Connect();
123
124 if (bundleMgr_ == nullptr) {
125 return false;
126 }
127 int32_t callingUserId;
128 AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(userId, callingUserId);
129 std::string identity = IPCSkeleton::ResetCallingIdentity();
130 bool ret = bundleMgr_->GetBundleInfo(bundleName, flag, bundleInfo, callingUserId);
131 IPCSkeleton::SetCallingIdentity(identity);
132 return ret;
133 }
134
GetAppIndexByUid(const int32_t uid)135 int32_t ReminderBundleManagerHelper::GetAppIndexByUid(const int32_t uid)
136 {
137 int32_t appIndex = 0;
138 std::lock_guard<std::mutex> lock(connectionMutex_);
139 Connect();
140 if (nullptr == bundleMgr_) {
141 return appIndex;
142 }
143 std::string bundleName;
144 std::string identity = IPCSkeleton::ResetCallingIdentity();
145 bundleMgr_->GetNameAndIndexForUid(uid, bundleName, appIndex);
146 IPCSkeleton::SetCallingIdentity(identity);
147 return appIndex;
148 }
149
150 } // namespace Notification
151 } // namespace OHOS
152