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 "recovery_info_timer.h"
17eace7efcSopenharmony_ci#include "app_exit_reason_data_manager.h"
18eace7efcSopenharmony_ci
19eace7efcSopenharmony_cinamespace OHOS {
20eace7efcSopenharmony_cinamespace AAFwk {
21eace7efcSopenharmony_ciconstexpr int32_t HOURS_TO_SECOND = 60 * 60;
22eace7efcSopenharmony_ciconstexpr int32_t TIMEOUT_DELETE_TIME = 168;
23eace7efcSopenharmony_ciconstexpr int32_t RESERVE_NUM = 5;
24eace7efcSopenharmony_ci
25eace7efcSopenharmony_ciRecoveryInfoTimer& RecoveryInfoTimer::GetInstance()
26eace7efcSopenharmony_ci{
27eace7efcSopenharmony_ci    static RecoveryInfoTimer instance;
28eace7efcSopenharmony_ci    return instance;
29eace7efcSopenharmony_ci}
30eace7efcSopenharmony_civoid RecoveryInfoTimer::SubmitSaveRecoveryInfo(RecoveryInfo recoveryInfo)
31eace7efcSopenharmony_ci{
32eace7efcSopenharmony_ci    std::lock_guard<std::mutex> lock(recoveryInfoQueueLock_);
33eace7efcSopenharmony_ci    auto findByInfo = [&recoveryInfo](RecoveryInfo& item) {
34eace7efcSopenharmony_ci        return item.abilityName == recoveryInfo.abilityName && item.bundleName == recoveryInfo.bundleName &&
35eace7efcSopenharmony_ci            item.moduleName == recoveryInfo.moduleName;
36eace7efcSopenharmony_ci    };
37eace7efcSopenharmony_ci    auto i = find_if(recoveryInfoQueue_.begin(), recoveryInfoQueue_.end(), findByInfo);
38eace7efcSopenharmony_ci    if (i != recoveryInfoQueue_.end()) {
39eace7efcSopenharmony_ci        recoveryInfoQueue_.erase(i);
40eace7efcSopenharmony_ci    }
41eace7efcSopenharmony_ci    recoveryInfoQueue_.push_back(recoveryInfo);
42eace7efcSopenharmony_ci
43eace7efcSopenharmony_ci    int64_t now = recoveryInfo.time;
44eace7efcSopenharmony_ci    auto timeoutDeleteTime = TIMEOUT_DELETE_TIME * HOURS_TO_SECOND;
45eace7efcSopenharmony_ci    auto reserveNumber = RESERVE_NUM;
46eace7efcSopenharmony_ci    int timeoutCount = 0;
47eace7efcSopenharmony_ci    for (auto p = recoveryInfoQueue_.begin(); p != recoveryInfoQueue_.end(); p++) {
48eace7efcSopenharmony_ci        if (now - p->time >= timeoutDeleteTime) {
49eace7efcSopenharmony_ci            timeoutCount++;
50eace7efcSopenharmony_ci        }
51eace7efcSopenharmony_ci    }
52eace7efcSopenharmony_ci
53eace7efcSopenharmony_ci    timeoutCount -= reserveNumber;
54eace7efcSopenharmony_ci    for (; timeoutCount > 0; timeoutCount--) {
55eace7efcSopenharmony_ci        auto recoveryInfo = recoveryInfoQueue_.begin();
56eace7efcSopenharmony_ci        TAG_LOGI(AAFwkTag::ABILITYMGR, "clearRecoveryInfo bundleName: %{public}s, abilityName: %{public}s",
57eace7efcSopenharmony_ci            recoveryInfo->bundleName.c_str(), recoveryInfo->abilityName.c_str());
58eace7efcSopenharmony_ci        (void)DelayedSingleton<AbilityRuntime::AppExitReasonDataManager>::GetInstance()->
59eace7efcSopenharmony_ci            DeleteAbilityRecoverInfo(recoveryInfo->tokenId, recoveryInfo->moduleName, recoveryInfo->abilityName);
60eace7efcSopenharmony_ci        recoveryInfoQueue_.pop_front();
61eace7efcSopenharmony_ci    }
62eace7efcSopenharmony_ci}
63eace7efcSopenharmony_ci}
64eace7efcSopenharmony_ci}