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