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,bool & isDataAbility,std::string & uriStr,int32_t userId)67 bool MainElementUtils::CheckMainElement(const AppExecFwk::HapModuleInfo &hapModuleInfo,
68 const std::string &processName, std::string &mainElement, bool &isDataAbility,
69 std::string &uriStr, 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 isDataAbility = DelayedSingleton<AbilityManagerService>::GetInstance()->GetDataAbilityUri(
91 hapModuleInfo.abilityInfos, mainElement, uriStr);
92 if (isDataAbility) {
93 return false;
94 }
95 } else {
96 TAG_LOGI(AAFwkTag::ABILITYMGR, "new mode: %{public}s", hapModuleInfo.bundleName.c_str());
97 // new application model
98 mainElement = hapModuleInfo.mainElementName;
99 if (mainElement.empty()) {
100 TAG_LOGI(AAFwkTag::ABILITYMGR, "mainElement empty");
101 return false;
102 }
103
104 // new application model, user model 'process'
105 if (hapModuleInfo.process != processName) {
106 TAG_LOGI(AAFwkTag::ABILITYMGR, "processName err: %{public}s", processName.c_str());
107 return false;
108 }
109 }
110 return IsMainElementTypeOk(hapModuleInfo, mainElement, userId);
111 }
112
CheckMainUIAbility(const AppExecFwk::BundleInfo & bundleInfo,std::string & mainElementName)113 bool MainElementUtils::CheckMainUIAbility(const AppExecFwk::BundleInfo &bundleInfo, std::string& mainElementName)
114 {
115 for (const auto& hapModuleInfo : bundleInfo.hapModuleInfos) {
116 if (hapModuleInfo.moduleType != AppExecFwk::ModuleType::ENTRY) {
117 continue;
118 }
119
120 mainElementName = hapModuleInfo.mainElementName;
121 if (mainElementName.empty()) {
122 return false;
123 }
124 for (const auto &abilityInfo: hapModuleInfo.abilityInfos) {
125 if (abilityInfo.type != AppExecFwk::AbilityType::PAGE) {
126 continue;
127 }
128 if (abilityInfo.name == mainElementName) {
129 return true;
130 }
131 }
132 break;
133 }
134 return false;
135 }
136
CheckStatusBarAbility(const AppExecFwk::BundleInfo & bundleInfo)137 bool MainElementUtils::CheckStatusBarAbility(const AppExecFwk::BundleInfo &bundleInfo)
138 {
139 for (const auto& hapModuleInfo : bundleInfo.hapModuleInfos) {
140 for (const auto &extensionInfo: hapModuleInfo.extensionInfos) {
141 if (extensionInfo.type == AppExecFwk::ExtensionAbilityType::STATUS_BAR_VIEW) {
142 return true;
143 }
144 }
145 }
146 return false;
147 }
148 } // namespace AAFwk
149 } // namespace OHOS
150