1 /*
2 * Copyright (c) 2022 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 "resident_process_manager.h"
17
18 #include "ability_manager_service.h"
19 #include "user_controller.h"
20
21 namespace OHOS {
22 namespace AAFwk {
ResidentProcessManager()23 ResidentProcessManager::ResidentProcessManager()
24 {}
25
~ResidentProcessManager()26 ResidentProcessManager::~ResidentProcessManager()
27 {}
28
StartResidentProcess(const std::vector<AppExecFwk::BundleInfo> & bundleInfos)29 void ResidentProcessManager::StartResidentProcess(const std::vector<AppExecFwk::BundleInfo> &bundleInfos)
30 {
31 DelayedSingleton<AppScheduler>::GetInstance()->StartupResidentProcess(bundleInfos);
32 }
33
StartResidentProcessWithMainElement(std::vector<AppExecFwk::BundleInfo> & bundleInfos)34 void ResidentProcessManager::StartResidentProcessWithMainElement(std::vector<AppExecFwk::BundleInfo> &bundleInfos)
35 {
36 std::set<uint32_t> needEraseIndexSet;
37
38 for (size_t i = 0; i < bundleInfos.size(); i++) {
39 std::string processName = bundleInfos[i].applicationInfo.process;
40 if (!bundleInfos[i].isKeepAlive || processName.empty()) {
41 needEraseIndexSet.insert(i);
42 continue;
43 }
44 for (auto hapModuleInfo : bundleInfos[i].hapModuleInfos) {
45 std::string mainElement;
46 if (!CheckMainElement(hapModuleInfo, processName, mainElement, needEraseIndexSet, i)) {
47 continue;
48 }
49
50 needEraseIndexSet.insert(i);
51 // startAbility
52 Want want;
53 want.SetElementName(hapModuleInfo.bundleName, mainElement);
54 HILOG_INFO("Start resident ability, mainElement: %{public}s", mainElement.c_str());
55 DelayedSingleton<AbilityManagerService>::GetInstance()->StartAbility(want, USER_ID_NO_HEAD,
56 DEFAULT_INVAL_VALUE);
57 }
58 }
59
60 // delete item which process has been started.
61 for (auto iter = needEraseIndexSet.rbegin(); iter != needEraseIndexSet.rend(); iter++) {
62 bundleInfos.erase(bundleInfos.begin() + *iter);
63 }
64 }
65
CheckMainElement(const AppExecFwk::HapModuleInfo & hapModuleInfo,const std::string & processName,std::string & mainElement,std::set<uint32_t> & needEraseIndexSet,size_t bundleInfoIndex)66 bool ResidentProcessManager::CheckMainElement(const AppExecFwk::HapModuleInfo &hapModuleInfo,
67 const std::string &processName, std::string &mainElement,
68 std::set<uint32_t> &needEraseIndexSet, size_t bundleInfoIndex)
69 {
70 if (!hapModuleInfo.isModuleJson) {
71 // old application model
72 mainElement = hapModuleInfo.mainAbility;
73 if (mainElement.empty()) {
74 return false;
75 }
76
77 // old application model, use ability 'process'
78 bool isAbilityKeepAlive = false;
79 for (auto abilityInfo : hapModuleInfo.abilityInfos) {
80 if (abilityInfo.process != processName || abilityInfo.name != mainElement) {
81 continue;
82 }
83 isAbilityKeepAlive = true;
84 }
85 if (!isAbilityKeepAlive) {
86 return false;
87 }
88
89 std::string uriStr;
90 bool getDataAbilityUri = DelayedSingleton<AbilityManagerService>::GetInstance()->GetDataAbilityUri(
91 hapModuleInfo.abilityInfos, mainElement, uriStr);
92 if (getDataAbilityUri) {
93 // dataability, need use AcquireDataAbility
94 HILOG_INFO("Start resident dataability, mainElement: %{public}s, uri: %{public}s",
95 mainElement.c_str(), uriStr.c_str());
96 Uri uri(uriStr);
97 DelayedSingleton<AbilityManagerService>::GetInstance()->AcquireDataAbility(uri, true, nullptr);
98 needEraseIndexSet.insert(bundleInfoIndex);
99 return false;
100 }
101 } else {
102 // new application model
103 mainElement = hapModuleInfo.mainElementName;
104 if (mainElement.empty()) {
105 return false;
106 }
107
108 // new application model, user model 'process'
109 if (hapModuleInfo.process != processName) {
110 return false;
111 }
112 }
113
114 // ability need to start, but need to filt page ability
115 bool mainElementIsPageAbility = false;
116 for (auto abilityInfo : hapModuleInfo.abilityInfos) {
117 if (abilityInfo.name == mainElement && abilityInfo.type == AppExecFwk::AbilityType::PAGE) {
118 mainElementIsPageAbility = true;
119 break;
120 }
121 }
122 if (mainElementIsPageAbility) {
123 HILOG_DEBUG("%{public}s is page ability", mainElement.c_str());
124 return false;
125 }
126
127 return true;
128 }
129 } // namespace AAFwk
130 } // namespace OHOS
131