1eace7efcSopenharmony_ci/*
2eace7efcSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License.
5eace7efcSopenharmony_ci * You may obtain a copy of the License at
6eace7efcSopenharmony_ci *
7eace7efcSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8eace7efcSopenharmony_ci *
9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and
13eace7efcSopenharmony_ci * limitations under the License.
14eace7efcSopenharmony_ci */
15eace7efcSopenharmony_ci
16eace7efcSopenharmony_ci#include "app_exception_handler.h"
17eace7efcSopenharmony_ci
18eace7efcSopenharmony_ci#include "ability_record.h"
19eace7efcSopenharmony_ci#include "app_exception_callback_stub.h"
20eace7efcSopenharmony_ci#include "app_mgr_util.h"
21eace7efcSopenharmony_ci#include "freeze_util.h"
22eace7efcSopenharmony_ci#include "hilog_tag_wrapper.h"
23eace7efcSopenharmony_ci
24eace7efcSopenharmony_cinamespace OHOS {
25eace7efcSopenharmony_ciusing AppExecFwk::LifecycleException;
26eace7efcSopenharmony_ciusing AbilityRuntime::FreezeUtil;
27eace7efcSopenharmony_cinamespace AAFwk {
28eace7efcSopenharmony_cinamespace {
29eace7efcSopenharmony_ciclass AppExceptionCallback : public AppExecFwk::AppExceptionCallbackStub {
30eace7efcSopenharmony_ci    /**
31eace7efcSopenharmony_ci     * Notify abilityManager lifecycle exception.
32eace7efcSopenharmony_ci     *
33eace7efcSopenharmony_ci     * @param type lifecycle failed type
34eace7efcSopenharmony_ci     * @param token associated ability
35eace7efcSopenharmony_ci     */
36eace7efcSopenharmony_ci    void OnLifecycleException(LifecycleException type, sptr<IRemoteObject> token) override
37eace7efcSopenharmony_ci    {
38eace7efcSopenharmony_ci        auto abilityRecord = Token::GetAbilityRecordByToken(token);
39eace7efcSopenharmony_ci        if (abilityRecord == nullptr) {
40eace7efcSopenharmony_ci            TAG_LOGW(AAFwkTag::ABILITYMGR, "abilityRecord null");
41eace7efcSopenharmony_ci            return;
42eace7efcSopenharmony_ci        }
43eace7efcSopenharmony_ci
44eace7efcSopenharmony_ci        TAG_LOGI(AAFwkTag::ABILITYMGR, "lifecycle exception: %{public}s, %{public}d",
45eace7efcSopenharmony_ci            abilityRecord->GetURI().c_str(), type);
46eace7efcSopenharmony_ci        abilityRecord->SetFreezeStrategy(FreezeStrategy::NOTIFY_FREEZE_MGR);
47eace7efcSopenharmony_ci    }
48eace7efcSopenharmony_ci};
49eace7efcSopenharmony_ci}
50eace7efcSopenharmony_ci
51eace7efcSopenharmony_ciAppExceptionHandler &AppExceptionHandler::GetInstance()
52eace7efcSopenharmony_ci{
53eace7efcSopenharmony_ci    static AppExceptionHandler appExceptionHandler;
54eace7efcSopenharmony_ci    return appExceptionHandler;
55eace7efcSopenharmony_ci}
56eace7efcSopenharmony_ci
57eace7efcSopenharmony_civoid AppExceptionHandler::RegisterAppExceptionCallback()
58eace7efcSopenharmony_ci{
59eace7efcSopenharmony_ci    auto appMgr = AppMgrUtil::GetAppMgr();
60eace7efcSopenharmony_ci    if (appMgr == nullptr) {
61eace7efcSopenharmony_ci        TAG_LOGW(AAFwkTag::ABILITYMGR, "AppMgrUtil::GetAppMgr failed");
62eace7efcSopenharmony_ci        return;
63eace7efcSopenharmony_ci    }
64eace7efcSopenharmony_ci
65eace7efcSopenharmony_ci    auto service = appMgr->GetAmsMgr();
66eace7efcSopenharmony_ci    if (service == nullptr) {
67eace7efcSopenharmony_ci        TAG_LOGW(AAFwkTag::ABILITYMGR, "GetAmsMgr failed");
68eace7efcSopenharmony_ci        return;
69eace7efcSopenharmony_ci    }
70eace7efcSopenharmony_ci    auto callback = sptr<AppExceptionCallback>(new AppExceptionCallback());
71eace7efcSopenharmony_ci    service->SetAppExceptionCallback(callback->AsObject());
72eace7efcSopenharmony_ci}
73eace7efcSopenharmony_ci
74eace7efcSopenharmony_civoid AppExceptionHandler::AbilityForegroundFailed(sptr<IRemoteObject> token, const std::string &msg)
75eace7efcSopenharmony_ci{
76eace7efcSopenharmony_ci    auto abilityRecord = Token::GetAbilityRecordByToken(token);
77eace7efcSopenharmony_ci    if (abilityRecord == nullptr) {
78eace7efcSopenharmony_ci        TAG_LOGW(AAFwkTag::ABILITYMGR, "abilityRecord null");
79eace7efcSopenharmony_ci        return;
80eace7efcSopenharmony_ci    }
81eace7efcSopenharmony_ci    FreezeUtil::LifecycleFlow flow{token, FreezeUtil::TimeoutState::FOREGROUND};
82eace7efcSopenharmony_ci    FreezeUtil::GetInstance().AppendLifecycleEvent(flow, std::string("AbilityForegroundFailed: " + msg));
83eace7efcSopenharmony_ci
84eace7efcSopenharmony_ci    TAG_LOGI(AAFwkTag::ABILITYMGR, "AbilityForegroundFailed: %{public}s", abilityRecord->GetURI().c_str());
85eace7efcSopenharmony_ci    abilityRecord->SetFreezeStrategy(FreezeStrategy::NOTIFY_FREEZE_MGR);
86eace7efcSopenharmony_ci}
87eace7efcSopenharmony_ci}  // namespace AAFwk
88eace7efcSopenharmony_ci}  // namespace OHOS
89