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_exception_handler.h"
17
18 #include "ability_record.h"
19 #include "app_exception_callback_stub.h"
20 #include "app_mgr_util.h"
21 #include "freeze_util.h"
22 #include "hilog_tag_wrapper.h"
23
24 namespace OHOS {
25 using AppExecFwk::LifecycleException;
26 using AbilityRuntime::FreezeUtil;
27 namespace AAFwk {
28 namespace {
29 class AppExceptionCallback : public AppExecFwk::AppExceptionCallbackStub {
30 /**
31 * Notify abilityManager lifecycle exception.
32 *
33 * @param type lifecycle failed type
34 * @param token associated ability
35 */
36 void OnLifecycleException(LifecycleException type, sptr<IRemoteObject> token) override
37 {
38 auto abilityRecord = Token::GetAbilityRecordByToken(token);
39 if (abilityRecord == nullptr) {
40 TAG_LOGW(AAFwkTag::ABILITYMGR, "abilityRecord null");
41 return;
42 }
43
44 TAG_LOGI(AAFwkTag::ABILITYMGR, "lifecycle exception: %{public}s, %{public}d",
45 abilityRecord->GetURI().c_str(), type);
46 abilityRecord->SetFreezeStrategy(FreezeStrategy::NOTIFY_FREEZE_MGR);
47 }
48 };
49 }
50
GetInstance()51 AppExceptionHandler &AppExceptionHandler::GetInstance()
52 {
53 static AppExceptionHandler appExceptionHandler;
54 return appExceptionHandler;
55 }
56
RegisterAppExceptionCallback()57 void AppExceptionHandler::RegisterAppExceptionCallback()
58 {
59 auto appMgr = AppMgrUtil::GetAppMgr();
60 if (appMgr == nullptr) {
61 TAG_LOGW(AAFwkTag::ABILITYMGR, "AppMgrUtil::GetAppMgr failed");
62 return;
63 }
64
65 auto service = appMgr->GetAmsMgr();
66 if (service == nullptr) {
67 TAG_LOGW(AAFwkTag::ABILITYMGR, "GetAmsMgr failed");
68 return;
69 }
70 auto callback = sptr<AppExceptionCallback>(new AppExceptionCallback());
71 service->SetAppExceptionCallback(callback->AsObject());
72 }
73
AbilityForegroundFailed(sptr<IRemoteObject> token, const std::string &msg)74 void AppExceptionHandler::AbilityForegroundFailed(sptr<IRemoteObject> token, const std::string &msg)
75 {
76 auto abilityRecord = Token::GetAbilityRecordByToken(token);
77 if (abilityRecord == nullptr) {
78 TAG_LOGW(AAFwkTag::ABILITYMGR, "abilityRecord null");
79 return;
80 }
81 FreezeUtil::LifecycleFlow flow{token, FreezeUtil::TimeoutState::FOREGROUND};
82 FreezeUtil::GetInstance().AppendLifecycleEvent(flow, std::string("AbilityForegroundFailed: " + msg));
83
84 TAG_LOGI(AAFwkTag::ABILITYMGR, "AbilityForegroundFailed: %{public}s", abilityRecord->GetURI().c_str());
85 abilityRecord->SetFreezeStrategy(FreezeStrategy::NOTIFY_FREEZE_MGR);
86 }
87 } // namespace AAFwk
88 } // namespace OHOS
89