• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "bundle_status_adapter.h"
17 
18 #include "avsession_log.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21 #include "cJSON.h"
22 #include "want.h"
23 #include "want_params_wrapper.h"
24 #include "string_wrapper.h"
25 #include "array_wrapper.h"
26 
27 namespace OHOS::AVSession {
28 std::shared_ptr<BundleStatusAdapter> BundleStatusAdapter::instance_;
29 std::recursive_mutex BundleStatusAdapter::instanceLock_;
30 
BundleStatusAdapter()31 BundleStatusAdapter::BundleStatusAdapter()
32 {
33     SLOGI("construct");
34 }
35 
~BundleStatusAdapter()36 BundleStatusAdapter::~BundleStatusAdapter()
37 {
38     SLOGI("destroy");
39 }
40 
GetInstance()41 BundleStatusAdapter& BundleStatusAdapter::GetInstance()
42 {
43     std::lock_guard lockGuard(instanceLock_);
44     if (instance_ != nullptr) {
45         return *instance_;
46     }
47     SLOGI("GetInstance in");
48     instance_ = std::make_shared<BundleStatusAdapter>();
49     return *instance_;
50 }
51 
ReleaseInstance()52 void BundleStatusAdapter::ReleaseInstance()
53 {
54     std::lock_guard lockGuard(instanceLock_);
55     SLOGI("ReleaseInstance in");
56     instance_ = nullptr;
57 }
58 
Init()59 void BundleStatusAdapter::Init()
60 {
61     auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
62     if (!systemAbilityManager) {
63         SLOGI("fail to get system ability mgr");
64         return;
65     }
66 
67     auto remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
68     if (!remoteObject) {
69         SLOGI("fail to get bundle manager proxy");
70         return;
71     }
72 
73     std::lock_guard bundleMgrProxyLockGuard(bundleMgrProxyLock_);
74     CHECK_AND_RETURN_LOG(bundleMgrProxy == nullptr, "bundleMgrProxy alive return");
75     SLOGI("get bundle manager proxy success");
76     bundleMgrProxy = iface_cast<AppExecFwk::BundleMgrProxy>(remoteObject);
77     bundleResourceProxy = bundleMgrProxy->GetBundleResourceProxy();
78 }
79 
GetBundleIcon(const std::string bundleName,const std::string abilityName,std::string & icon)80 bool BundleStatusAdapter::GetBundleIcon(const std::string bundleName, const std::string abilityName, std::string& icon)
81 {
82     SLOGI("GetBundleIcon with bundleName:%{public}s", bundleName.c_str());
83 
84     if (bundleMgrProxy == nullptr || bundleResourceProxy == nullptr) {
85         SLOGE("GetBundleIcon with bundleMgrProxy:%{public}d, bundleResourceProxy:%{public}d",
86             static_cast<int>(bundleMgrProxy != nullptr), static_cast<int>(bundleResourceProxy != nullptr));
87         return false;
88     }
89     AppExecFwk::BundleInfo bundleInfo;
90     if (!bundleMgrProxy->GetBundleInfo(bundleName, getBundleInfoWithHapModule, bundleInfo, startUserId)) {
91         SLOGE("GetBundleInfo of bundleName:%{public}s fail!", bundleName.c_str());
92         return false;
93     }
94     std::vector<AppExecFwk::LauncherAbilityResourceInfo> LauncherAbilityResourceInfoList;
95     ErrCode ret = bundleResourceProxy->GetLauncherAbilityResourceInfo(bundleName, 0, LauncherAbilityResourceInfoList);
96     if (ret != ERR_OK) {
97         SLOGE("GetLauncherAbilityResourceInfo of bundleName:%{public}s fail for errCode:%{public}d",
98             bundleName.c_str(), ret);
99         return false;
100     }
101     for (const auto& resourceInfo : LauncherAbilityResourceInfoList) {
102         if (abilityName == resourceInfo.abilityName) {
103             icon = resourceInfo.icon;
104             break;
105         }
106     }
107     if (icon.empty() && LauncherAbilityResourceInfoList.size() > 0) {
108         icon = LauncherAbilityResourceInfoList[LauncherAbilityResourceInfoList.size() - 1].icon;
109     }
110 
111     return true;
112 }
113 
SubscribeBundleStatusEvent(const std::string bundleName,const std::function<void (const std::string,const int32_t userId)> & callback,int32_t userId)114 bool BundleStatusAdapter::SubscribeBundleStatusEvent(const std::string bundleName,
115     const std::function<void(const std::string, const int32_t userId)>& callback, int32_t userId)
116 {
117     SLOGI("Bundle status adapter subscribe bundle status event, bundleName=%{public}s, userId=%{public}d",
118         bundleName.c_str(), userId);
119     auto bundleStatusListener = bundleStatusListeners_.find(std::make_pair(bundleName, userId));
120     if (bundleStatusListener != bundleStatusListeners_.end()) {
121         SLOGE("bundle status has already register");
122         return false;
123     }
124     auto bundleStatusCallback = [this](std::string bundleName, int32_t userId) {
125         NotifyBundleRemoved(bundleName, userId);
126     };
127     sptr<BundleStatusCallbackImpl> bundleStatusCallbackImpl =
128             new(std::nothrow) BundleStatusCallbackImpl(bundleStatusCallback, userId);
129     if (bundleStatusCallbackImpl == nullptr) {
130         SLOGE("no memory");
131         return false;
132     }
133     bool isProxyNull = false;
134     {
135         std::lock_guard bundleMgrProxyLockGuard(bundleMgrProxyLock_);
136         isProxyNull = (bundleMgrProxy == nullptr);
137     }
138     if (isProxyNull) {
139         SLOGE("SubscribeBundleStatusEvent with proxy null!");
140         Init();
141         std::lock_guard bundleMgrProxyLockGuard(bundleMgrProxyLock_);
142         if (bundleMgrProxy == nullptr) {
143             SLOGE("SubscribeBundleStatusEvent with proxy null after init!");
144             return false;
145         }
146     }
147     bundleStatusCallbackImpl->SetBundleName(bundleName);
148     bundleStatusCallbackImpl->SetUserId(userId);
149     if (bundleMgrProxy->RegisterBundleStatusCallback(bundleStatusCallbackImpl)) {
150         bundleStatusListeners_.insert(std::make_pair(std::make_pair(bundleName, userId), callback));
151         return true;
152     } else {
153         SLOGE("Register bundle status callback failed, bundleName=%{public}s", bundleName.c_str());
154         return false;
155     }
156 }
157 
IsAudioPlayback(const std::string & bundleName,const std::string & abilityName)158 bool BundleStatusAdapter::IsAudioPlayback(const std::string& bundleName, const std::string& abilityName)
159 {
160     SLOGI("Estimate bundle audio playback status, bundleName=%{public}s", bundleName.c_str());
161     AppExecFwk::AbilityInfo abilityInfo;
162     bool flag = false;
163     if (bundleMgrProxy->GetAbilityInfo(bundleName, abilityName, abilityInfo)) {
164         flag = static_cast<int32_t>(abilityInfo.backgroundModes) == backgroundModeDemand ? true : false;
165     }
166     return flag;
167 }
168 
NotifyBundleRemoved(const std::string bundleName,const int32_t userId)169 void BundleStatusAdapter::NotifyBundleRemoved(const std::string bundleName, const int32_t userId)
170 {
171     auto bundleStatusListener = bundleStatusListeners_.find(std::make_pair(bundleName, userId));
172     if (bundleStatusListener == bundleStatusListeners_.end()) {
173         return;
174     }
175     bundleStatusListener->second(bundleName, userId);
176     // BMS will keep callbackImpl for the bundleName & userId until avsession do ClearBundleStatusCallback
177     SLOGI("notify bundle status callback without erase, bundleName=%{public}s, userId=%{public}d",
178         bundleName.c_str(), userId);
179 }
180 
GetBundleNameFromUid(const int32_t uid)181 std::string BundleStatusAdapter::GetBundleNameFromUid(const int32_t uid)
182 {
183     std::string bundleName {""};
184     if (bundleMgrProxy != nullptr) {
185         bundleMgrProxy->GetNameForUid(uid, bundleName);
186     }
187     return bundleName;
188 }
189 
GetUidFromBundleName(const std::string bundleName,const int32_t userId)190 int32_t BundleStatusAdapter::GetUidFromBundleName(const std::string bundleName, const int32_t userId)
191 {
192     AppExecFwk::BundleInfo bundleInfo;
193     {
194         std::lock_guard bundleMgrProxyLockGuard(bundleMgrProxyLock_);
195         CHECK_AND_RETURN_RET_LOG(bundleMgrProxy != nullptr, -1, "bundleMgrProxy is null");
196         auto ret = bundleMgrProxy->GetBundleInfo(bundleName,
197             static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_DEFAULT),
198             bundleInfo, userId);
199         CHECK_AND_RETURN_RET_LOG(ret, -1, "getbundleinfo fail");
200     }
201     return bundleInfo.uid;
202 }
203 
CheckBundleSupport(std::string & profile)204 bool BundleStatusAdapter::CheckBundleSupport(std::string& profile)
205 {
206     // check bundle support background mode & playmusiclist intent
207     cJSON* profileValues = cJSON_Parse(profile.c_str());
208     CHECK_AND_RETURN_RET_LOG(profileValues != nullptr, false, "parse profile fail");
209     if (cJSON_IsInvalid(profileValues)) {
210         SLOGE("CheckBundleSupport parse profile not valid json");
211         cJSON_Delete(profileValues);
212         return false;
213     }
214     cJSON* insightIntentsArray = cJSON_GetObjectItem(profileValues, "insightIntents");
215     if (insightIntentsArray == nullptr || !cJSON_IsArray(insightIntentsArray)) {
216         SLOGE("CheckBundleSupport json do not contain insightIntentsArray");
217         cJSON_Delete(profileValues);
218         return false;
219     }
220     cJSON* insightIntentsItem = nullptr;
221     cJSON_ArrayForEach(insightIntentsItem, insightIntentsArray) {
222         cJSON* intentNameItem = cJSON_GetObjectItem(insightIntentsItem, "intentName");
223         if (intentNameItem == nullptr || !cJSON_IsString(intentNameItem)) {
224             SLOGE("CheckBundleSupport json do not contain intentName");
225             continue;
226         }
227         const char* insightName = intentNameItem->valuestring;
228         if (insightName != nullptr &&
229             strcmp(insightName, PLAY_MUSICLIST.c_str()) != 0 && strcmp(insightName, PLAY_AUDIO.c_str()) != 0) {
230             continue;
231         }
232 
233         cJSON* uiAbilityItem = cJSON_GetObjectItem(insightIntentsItem, "uiAbility");
234         if (uiAbilityItem == nullptr) {
235             SLOGE("CheckBundleSupport json do not contain uiAbility");
236             cJSON_Delete(profileValues);
237             return false;
238         }
239         cJSON* executeModeArray = cJSON_GetObjectItem(uiAbilityItem, "executeMode");
240         if (executeModeArray == nullptr || !cJSON_IsArray(executeModeArray)) {
241             SLOGE("CheckBundleSupport json do not contain executeMode");
242             cJSON_Delete(profileValues);
243             return false;
244         }
245         cJSON* modeItem = nullptr;
246         cJSON_ArrayForEach(modeItem, executeModeArray) {
247             if (cJSON_IsString(modeItem) && modeItem->valuestring != nullptr &&
248                 strcmp(modeItem->valuestring, "background") == 0) {
249                 cJSON_Delete(profileValues);
250                 return true;
251             }
252         }
253     }
254     cJSON_Delete(profileValues);
255     return false;
256 }
257 
IsSupportPlayIntent(const std::string & bundleName,std::string & supportModule,std::string & profile)258 __attribute__((no_sanitize("cfi"))) bool BundleStatusAdapter::IsSupportPlayIntent(const std::string& bundleName,
259     std::string& supportModule, std::string& profile)
260 {
261     if (bundleMgrProxy == nullptr) {
262         return false;
263     }
264     AppExecFwk::BundleInfo bundleInfo;
265     if (!bundleMgrProxy->GetBundleInfo(bundleName, getBundleInfoWithHapModule, bundleInfo, startUserId)) {
266         SLOGE("GetBundleInfo=%{public}s fail", bundleName.c_str());
267         return false;
268     }
269     bool isSupportIntent = false;
270     for (std::string module : bundleInfo.moduleNames) {
271         auto ret = bundleMgrProxy->GetJsonProfile(AppExecFwk::ProfileType::INTENT_PROFILE, bundleName, module,
272             profile, startUserId);
273         if (ret == 0) {
274             SLOGI("GetJsonProfile success, profile=%{public}s", profile.c_str());
275             isSupportIntent = true;
276             supportModule = module;
277             break;
278         }
279     }
280     if (!isSupportIntent) {
281         SLOGE("Bundle=%{public}s does not support insight", bundleName.c_str());
282         return false;
283     }
284     return CheckBundleSupport(profile);
285 }
286 
BundleStatusCallbackImpl(const std::function<void (const std::string,const int32_t userId)> & callback,int32_t userId)287 BundleStatusCallbackImpl::BundleStatusCallbackImpl(
288     const std::function<void(const std::string, const int32_t userId)>& callback, int32_t userId)
289 {
290     SLOGI("Create bundle status instance with userId %{public}d", userId);
291     callback_ = callback;
292     userId_ = userId;
293 }
294 
~BundleStatusCallbackImpl()295 BundleStatusCallbackImpl::~BundleStatusCallbackImpl()
296 {
297     SLOGI("Destroy bundle status instance");
298 }
299 
OnBundleStateChanged(const uint8_t installType,const int32_t resultCode,const std::string & resultMsg,const std::string & bundleName)300 void BundleStatusCallbackImpl::OnBundleStateChanged(const uint8_t installType, const int32_t resultCode,
301     const std::string &resultMsg, const std::string &bundleName)
302 {
303     if (installType == static_cast<uint8_t>(AppExecFwk::InstallType::UNINSTALL_CALLBACK)) {
304         callback_(bundleName, userId_);
305     }
306 }
307 }
308