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 "app_mgr_event.h"
17
18 #include "accesstoken_kit.h"
19 #include "common_event_support.h"
20 #include "hilog_tag_wrapper.h"
21 #include "ipc_skeleton.h"
22
23 namespace OHOS {
24 namespace AppExecFwk {
25
AppMgrEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo,const std::function<void ()> & callback)26 AppMgrEventSubscriber::AppMgrEventSubscriber(
27 const EventFwk::CommonEventSubscribeInfo &subscribeInfo, const std::function<void()> &callback)
28 : EventFwk::CommonEventSubscriber(subscribeInfo), callback_(callback)
29 {}
30
OnReceiveEvent(const EventFwk::CommonEventData & data)31 void AppMgrEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
32 {
33 const AAFwk::Want &want = data.GetWant();
34 std::string action = want.GetAction();
35 TAG_LOGD(AAFwkTag::APPMGR, "The action: %{public}s.", action.c_str());
36 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF) {
37 if (callback_ != nullptr) {
38 callback_();
39 }
40 }
41 }
42
GetCallerPid(const std::shared_ptr<AppRunningRecord> & callerAppRecord)43 int32_t AppMgrEventUtil::GetCallerPid(const std::shared_ptr<AppRunningRecord> &callerAppRecord)
44 {
45 if (!callerAppRecord) {
46 TAG_LOGW(AAFwkTag::APPMGR, "null callerAppRecord");
47 return IPCSkeleton::GetCallingPid();
48 }
49
50 if (!callerAppRecord->GetPriorityObject()) {
51 TAG_LOGW(AAFwkTag::APPMGR, "null priorityObject");
52 return IPCSkeleton::GetCallingPid();
53 }
54
55 return callerAppRecord->GetPid();
56 }
57
UpdateStartupType(const std::shared_ptr<AbilityInfo> & abilityInfo,int32_t & abilityType,int32_t & extensionType)58 void AppMgrEventUtil::UpdateStartupType(const std::shared_ptr<AbilityInfo> &abilityInfo, int32_t &abilityType,
59 int32_t &extensionType)
60 {
61 if (abilityInfo == nullptr) {
62 return;
63 }
64 TAG_LOGD(AAFwkTag::APPMGR, "bundleName:%{public}s, abilityName:%{public}s", abilityInfo->bundleName.c_str(),
65 abilityInfo->name.c_str());
66 abilityType = static_cast<int32_t>(abilityInfo->type);
67 if (abilityInfo->type != AbilityType::EXTENSION) {
68 return;
69 }
70 extensionType = static_cast<int32_t>(abilityInfo->extensionAbilityType);
71 }
72
SendCreateAtomicServiceProcessEvent(const std::shared_ptr<AppRunningRecord> & callerAppRecord,const std::shared_ptr<AppRunningRecord> & appRecord,const std::string & moduleName,const std::string & abilityName)73 bool AppMgrEventUtil::SendCreateAtomicServiceProcessEvent(const std::shared_ptr<AppRunningRecord> &callerAppRecord,
74 const std::shared_ptr<AppRunningRecord> &appRecord, const std::string &moduleName, const std::string &abilityName)
75 {
76 if (!appRecord) {
77 TAG_LOGE(AAFwkTag::APPMGR, "null appRecord");
78 return false;
79 }
80 AAFwk::EventInfo eventInfo;
81 eventInfo.abilityName = abilityName;
82 eventInfo.moduleName = moduleName;
83 eventInfo.bundleName = appRecord->GetBundleName();
84
85 eventInfo.callerUid = appRecord->GetCallerUid() == -1 ? IPCSkeleton::GetCallingUid() : appRecord->GetCallerUid();
86 if (callerAppRecord == nullptr) {
87 Security::AccessToken::NativeTokenInfo nativeTokenInfo = {};
88 auto token = appRecord->GetCallerTokenId() == -1 ?
89 static_cast<int>(IPCSkeleton::GetCallingTokenID()) : appRecord->GetCallerTokenId();
90 Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(token, nativeTokenInfo);
91 eventInfo.callerBundleName = "";
92 eventInfo.callerProcessName = nativeTokenInfo.processName;
93 } else {
94 eventInfo.callerBundleName = callerAppRecord->GetName();
95 eventInfo.callerProcessName = callerAppRecord->GetProcessName();
96 }
97 auto eventName = AAFwk::EventName::CREATE_ATOMIC_SERVICE_PROCESS;
98 AAFwk::EventReport::SendAtomicServiceEvent(eventName, HiSysEventType::BEHAVIOR, eventInfo);
99 return true;
100 }
101
SendProcessStartEvent(const std::shared_ptr<AppRunningRecord> & callerAppRecord,const std::shared_ptr<AppRunningRecord> & appRecord,AAFwk::EventInfo & eventInfo)102 bool AppMgrEventUtil::SendProcessStartEvent(const std::shared_ptr<AppRunningRecord> &callerAppRecord,
103 const std::shared_ptr<AppRunningRecord> &appRecord, AAFwk::EventInfo &eventInfo)
104 {
105 if (!appRecord) {
106 TAG_LOGW(AAFwkTag::APPMGR, "null appRecord");
107 return false;
108 }
109 time_t currentTime = time(nullptr);
110 eventInfo.time = currentTime;
111 eventInfo.callerUid = appRecord->GetCallerUid() == -1 ? IPCSkeleton::GetCallingUid() : appRecord->GetCallerUid();
112 if (!appRecord->GetAbilities().empty()) {
113 auto abilityinfo = appRecord->GetAbilities().begin()->second->GetAbilityInfo();
114 UpdateStartupType(abilityinfo, eventInfo.abilityType, eventInfo.extensionType);
115 } else {
116 TAG_LOGI(AAFwkTag::APPMGR, "abilities failed");
117 }
118 #define START_UP_ABILITY_TYPE_PREMAKE 100
119 if (eventInfo.isPreload) {
120 eventInfo.abilityType = START_UP_ABILITY_TYPE_PREMAKE + eventInfo.preloadMode;
121 }
122 if (!callerAppRecord) {
123 Security::AccessToken::NativeTokenInfo nativeTokenInfo = {};
124 auto token = appRecord->GetCallerTokenId() == -1 ?
125 static_cast<int>(IPCSkeleton::GetCallingTokenID()) : appRecord->GetCallerTokenId();
126 Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(token, nativeTokenInfo);
127 eventInfo.callerBundleName = "";
128 eventInfo.callerProcessName = nativeTokenInfo.processName;
129 eventInfo.callerPid = IPCSkeleton::GetCallingPid();
130 } else {
131 if (callerAppRecord->GetBundleName().empty()) {
132 eventInfo.callerBundleName = callerAppRecord->GetName();
133 } else {
134 eventInfo.callerBundleName = callerAppRecord->GetBundleName();
135 }
136 eventInfo.callerProcessName = callerAppRecord->GetProcessName();
137 eventInfo.callerPid = GetCallerPid(callerAppRecord);
138 }
139 if (!appRecord->GetBundleName().empty()) {
140 eventInfo.bundleName = appRecord->GetBundleName();
141 }
142 eventInfo.processName = appRecord->GetProcessName();
143 if (!appRecord->GetPriorityObject()) {
144 TAG_LOGE(AAFwkTag::APPMGR, "null priorityObject");
145 } else {
146 eventInfo.pid = appRecord->GetPid();
147 }
148 AAFwk::EventReport::SendProcessStartEvent(AAFwk::EventName::PROCESS_START, eventInfo);
149 return true;
150 }
151
SendProcessStartFailedEvent(std::shared_ptr<AppRunningRecord> callerAppRecord,std::shared_ptr<AppRunningRecord> appRecord,AAFwk::EventInfo & eventInfo)152 bool AppMgrEventUtil::SendProcessStartFailedEvent(std::shared_ptr<AppRunningRecord> callerAppRecord,
153 std::shared_ptr<AppRunningRecord> appRecord, AAFwk::EventInfo &eventInfo)
154 {
155 if (!appRecord) {
156 TAG_LOGE(AAFwkTag::APPMGR, "null appRecord");
157 return false;
158 }
159 time_t currentTime = time(nullptr);
160 if (currentTime <= 0) {
161 TAG_LOGE(AAFwkTag::APPMGR, "currentTime <= 0");
162 return false;
163 }
164 eventInfo.time = currentTime;
165 eventInfo.callerUid = appRecord->GetCallerUid() == -1 ? IPCSkeleton::GetCallingUid() : appRecord->GetCallerUid();
166 if (!appRecord->GetAbilities().empty()) {
167 auto abilityRecord = appRecord->GetAbilities().begin()->second;
168 if (!abilityRecord) {
169 TAG_LOGE(AAFwkTag::APPMGR, "abilityRecord null");
170 return false;
171 }
172 auto abilityinfo = abilityRecord->GetAbilityInfo();
173 UpdateStartupType(abilityinfo, eventInfo.abilityType, eventInfo.extensionType);
174 } else {
175 TAG_LOGI(AAFwkTag::APPMGR, "abilities failed");
176 }
177 UpdateCallerInfo(eventInfo, callerAppRecord, appRecord);
178 if (!appRecord->GetBundleName().empty()) {
179 eventInfo.bundleName = appRecord->GetBundleName();
180 }
181 eventInfo.processName = appRecord->GetProcessName();
182 eventInfo.processType = static_cast<int32_t>(appRecord->GetProcessType());
183 if (!appRecord->GetPriorityObject()) {
184 TAG_LOGE(AAFwkTag::APPMGR, "null priorityObject");
185 } else {
186 eventInfo.pid = appRecord->GetPid();
187 }
188 AAFwk::EventReport::SendProcessStartFailedEvent(AAFwk::EventName::PROCESS_START_FAILED, eventInfo);
189 return true;
190 }
191
UpdateCallerInfo(AAFwk::EventInfo & eventInfo,std::shared_ptr<AppRunningRecord> callerAppRecord,std::shared_ptr<AppRunningRecord> appRecord)192 void AppMgrEventUtil::UpdateCallerInfo(AAFwk::EventInfo &eventInfo, std::shared_ptr<AppRunningRecord> callerAppRecord,
193 std::shared_ptr<AppRunningRecord> appRecord)
194 {
195 if (!callerAppRecord) {
196 Security::AccessToken::NativeTokenInfo nativeTokenInfo = {};
197 auto token = appRecord->GetCallerTokenId() == -1 ?
198 static_cast<int32_t>(IPCSkeleton::GetCallingTokenID()) : appRecord->GetCallerTokenId();
199 Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(token, nativeTokenInfo);
200 eventInfo.callerBundleName = "";
201 eventInfo.callerProcessName = nativeTokenInfo.processName;
202 eventInfo.callerPid = IPCSkeleton::GetCallingPid();
203 } else {
204 if (callerAppRecord->GetBundleName().empty()) {
205 eventInfo.callerBundleName = callerAppRecord->GetName();
206 } else {
207 eventInfo.callerBundleName = callerAppRecord->GetBundleName();
208 }
209 eventInfo.callerProcessName = callerAppRecord->GetProcessName();
210 eventInfo.callerPid = GetCallerPid(callerAppRecord);
211 }
212 }
213
214 #ifdef SUPPORT_CHILD_PROCESS
SendChildProcessStartFailedEvent(std::shared_ptr<ChildProcessRecord> childRecord,ProcessStartFailedReason reason,int32_t subReason)215 bool AppMgrEventUtil::SendChildProcessStartFailedEvent(std::shared_ptr<ChildProcessRecord> childRecord,
216 ProcessStartFailedReason reason, int32_t subReason)
217 {
218 if (!childRecord) {
219 TAG_LOGW(AAFwkTag::APPMGR, "null appRecord");
220 return false;
221 }
222 auto hostRecord = childRecord->GetHostRecord();
223 if (!hostRecord) {
224 TAG_LOGW(AAFwkTag::APPMGR, "null hostRecord");
225 return false;
226 }
227 AAFwk::EventInfo eventInfo;
228 time_t currentTime = time(nullptr);
229 if (currentTime <= 0) {
230 TAG_LOGE(AAFwkTag::APPMGR, "currentTime <= 0");
231 return false;
232 }
233 eventInfo.time = currentTime;
234 eventInfo.callerUid = hostRecord->GetUid();
235 eventInfo.callerPid = hostRecord->GetPriorityObject() != nullptr ? hostRecord->GetPid() : -1;
236 eventInfo.callerBundleName = hostRecord->GetBundleName();
237 eventInfo.callerProcessName = hostRecord->GetProcessName();
238 eventInfo.bundleName = hostRecord->GetBundleName();
239 eventInfo.processName = childRecord->GetProcessName();
240 eventInfo.processType = static_cast<int32_t>(childRecord->GetProcessType());
241 eventInfo.reason = static_cast<int32_t>(reason);
242 eventInfo.subReason = subReason;
243 AAFwk::EventReport::SendProcessStartFailedEvent(AAFwk::EventName::PROCESS_START_FAILED, eventInfo);
244 return true;
245 }
246 #endif // SUPPORT_CHILD_PROCESS
247
SendRenderProcessStartFailedEvent(std::shared_ptr<RenderRecord> renderRecord,ProcessStartFailedReason reason,int32_t subReason)248 bool AppMgrEventUtil::SendRenderProcessStartFailedEvent(std::shared_ptr<RenderRecord> renderRecord,
249 ProcessStartFailedReason reason, int32_t subReason)
250 {
251 if (!renderRecord) {
252 TAG_LOGW(AAFwkTag::APPMGR, "null appRecord");
253 return false;
254 }
255 auto hostRecord = renderRecord->GetHostRecord();
256 if (!hostRecord) {
257 TAG_LOGW(AAFwkTag::APPMGR, "null hostRecord");
258 return false;
259 }
260 AAFwk::EventInfo eventInfo;
261 time_t currentTime = time(nullptr);
262 if (currentTime <= 0) {
263 TAG_LOGE(AAFwkTag::APPMGR, "currentTime <= 0");
264 return false;
265 }
266 eventInfo.time = currentTime;
267 eventInfo.callerUid = hostRecord->GetUid();
268 eventInfo.callerPid = hostRecord->GetPriorityObject() != nullptr ? hostRecord->GetPid() : -1;
269 eventInfo.callerBundleName = hostRecord->GetBundleName();
270 eventInfo.callerProcessName = hostRecord->GetProcessName();
271 eventInfo.bundleName = hostRecord->GetBundleName();
272 eventInfo.processName = renderRecord->GetProcessName();
273 eventInfo.processType = static_cast<int32_t>(renderRecord->GetProcessType());
274 eventInfo.reason = static_cast<int32_t>(reason);
275 eventInfo.subReason = subReason;
276 AAFwk::EventReport::SendProcessStartFailedEvent(AAFwk::EventName::PROCESS_START_FAILED, eventInfo);
277 return true;
278 }
279
SendReStartProcessEvent(AAFwk::EventInfo & eventInfo,int32_t appUid,int64_t restartTime)280 void AppMgrEventUtil::SendReStartProcessEvent(AAFwk::EventInfo &eventInfo, int32_t appUid, int64_t restartTime)
281 {
282 // eventInfo come from SendProcessStartEvent
283 eventInfo.time = restartTime;
284 eventInfo.appUid = appUid;
285 AAFwk::EventReport::SendKeyEvent(AAFwk::EventName::RESTART_PROCESS_BY_SAME_APP,
286 HiSysEventType::BEHAVIOR, eventInfo);
287 }
288 } // namespace AppExecFwk
289 } // namespace OHOS
290