1 /*
2  * Copyright (c) 2024 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 "main_element_utils.h"
17 
18 #include "ability_manager_service.h"
19 #include "ability_util.h"
20 
21 namespace OHOS {
22 namespace AAFwk {
23 namespace {
24 /**
25 * IsMainElementTypeOk, check if it is a valid main element type.
26 *
27 * @param hapModuleInfo The hap module info.
28 * @param mainElement The returned main element.
29 * @param userId User id.
30 * @return Whether it is a valid main element type.
31 */
IsMainElementTypeOk(const AppExecFwk::HapModuleInfo &hapModuleInfo, const std::string &mainElement, int32_t userId)32 bool IsMainElementTypeOk(const AppExecFwk::HapModuleInfo &hapModuleInfo, const std::string &mainElement,
33     int32_t userId)
34 {
35     if (userId == 0) {
36         for (const auto &abilityInfo: hapModuleInfo.abilityInfos) {
37             TAG_LOGD(AAFwkTag::ABILITYMGR, "compare ability: %{public}s", abilityInfo.name.c_str());
38             if (abilityInfo.name == mainElement) {
39                 return abilityInfo.type != AppExecFwk::AbilityType::PAGE;
40             }
41         }
42         return true;
43     }
44     for (const auto &extensionInfo: hapModuleInfo.extensionInfos) {
45         TAG_LOGD(AAFwkTag::ABILITYMGR, "compare extension: %{public}s", extensionInfo.name.c_str());
46         if (extensionInfo.name == mainElement) {
47             return extensionInfo.type == AppExecFwk::ExtensionAbilityType::SERVICE;
48         }
49     }
50     return false;
51 }
52 } // namespace
53 
UpdateMainElement(const std::string &bundleName, const std::string &moduleName, const std::string &mainElement, bool updateEnable, int32_t userId)54 void MainElementUtils::UpdateMainElement(const std::string &bundleName, const std::string &moduleName,
55     const std::string &mainElement, bool updateEnable, int32_t userId)
56 {
57     auto abilityMs = DelayedSingleton<AbilityManagerService>::GetInstance();
58     CHECK_POINTER(abilityMs);
59     auto ret = abilityMs->UpdateKeepAliveEnableState(bundleName, moduleName, mainElement, updateEnable, userId);
60     if (ret != ERR_OK) {
61         TAG_LOGE(AAFwkTag::ABILITYMGR,
62             "update keepAlive fail,bundle:%{public}s,mainElement:%{public}s,enable:%{public}d,userId:%{public}d",
63             bundleName.c_str(), mainElement.c_str(), updateEnable, userId);
64     }
65 }
66 
CheckMainElement(const AppExecFwk::HapModuleInfo &hapModuleInfo, const std::string &processName, std::string &mainElement, std::set<uint32_t> &needEraseIndexSet, size_t bundleInfoIndex, int32_t userId)67 bool MainElementUtils::CheckMainElement(const AppExecFwk::HapModuleInfo &hapModuleInfo,
68     const std::string &processName, std::string &mainElement,
69     std::set<uint32_t> &needEraseIndexSet, size_t bundleInfoIndex, int32_t userId)
70 {
71     if (!hapModuleInfo.isModuleJson) {
72         // old application model
73         mainElement = hapModuleInfo.mainAbility;
74         if (mainElement.empty()) {
75             return false;
76         }
77 
78         // old application model, use ability 'process'
79         bool isAbilityKeepAlive = false;
80         for (auto abilityInfo : hapModuleInfo.abilityInfos) {
81             if (abilityInfo.process != processName || abilityInfo.name != mainElement) {
82                 continue;
83             }
84             isAbilityKeepAlive = true;
85         }
86         if (!isAbilityKeepAlive) {
87             return false;
88         }
89 
90         std::string uriStr;
91         bool getDataAbilityUri = DelayedSingleton<AbilityManagerService>::GetInstance()->GetDataAbilityUri(
92             hapModuleInfo.abilityInfos, mainElement, uriStr);
93         if (getDataAbilityUri) {
94             // dataability, need use AcquireDataAbility
95             TAG_LOGI(AAFwkTag::ABILITYMGR, "call, mainElement: %{public}s, uri: %{public}s",
96                 mainElement.c_str(), uriStr.c_str());
97             Uri uri(uriStr);
98             DelayedSingleton<AbilityManagerService>::GetInstance()->AcquireDataAbility(uri, true, nullptr);
99             needEraseIndexSet.insert(bundleInfoIndex);
100             return false;
101         }
102     } else {
103         TAG_LOGI(AAFwkTag::ABILITYMGR, "new mode: %{public}s", hapModuleInfo.bundleName.c_str());
104         // new application model
105         mainElement = hapModuleInfo.mainElementName;
106         if (mainElement.empty()) {
107             TAG_LOGI(AAFwkTag::ABILITYMGR, "mainElement empty");
108             return false;
109         }
110 
111         // new application model, user model 'process'
112         if (hapModuleInfo.process != processName) {
113             TAG_LOGI(AAFwkTag::ABILITYMGR, "processName err: %{public}s", processName.c_str());
114             return false;
115         }
116     }
117     return IsMainElementTypeOk(hapModuleInfo, mainElement, userId);
118 }
119 }  // namespace AAFwk
120 }  // namespace OHOS
121